-
Notifications
You must be signed in to change notification settings - Fork 337
/
Copy pathemain-tabview.ts
302 lines (277 loc) · 9.86 KB
/
emain-tabview.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Copyright 2025, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0
import { RpcApi } from "@/app/store/wshclientapi";
import { adaptFromElectronKeyEvent } from "@/util/keyutil";
import { CHORD_TIMEOUT } from "@/util/sharedconst";
import { Rectangle, shell, WebContentsView } from "electron";
import { getWaveWindowById } from "emain/emain-window";
import path from "path";
import { configureAuthKeyRequestInjection } from "./authkey";
import { setWasActive } from "./emain-activity";
import { handleCtrlShiftFocus, handleCtrlShiftState, shFrameNavHandler, shNavHandler } from "./emain-util";
import { ElectronWshClient } from "./emain-wsh";
import { getElectronAppBasePath, isDevVite } from "./platform";
function computeBgColor(fullConfig: FullConfigType): string {
const settings = fullConfig?.settings;
const isTransparent = settings?.["window:transparent"] ?? false;
const isBlur = !isTransparent && (settings?.["window:blur"] ?? false);
if (isTransparent) {
return "#00000000";
} else if (isBlur) {
return "#00000000";
} else {
return "#222222";
}
}
const wcIdToWaveTabMap = new Map<number, WaveTabView>();
export function getWaveTabViewByWebContentsId(webContentsId: number): WaveTabView {
return wcIdToWaveTabMap.get(webContentsId);
}
export class WaveTabView extends WebContentsView {
waveWindowId: string; // this will be set for any tabviews that are initialized. (unset for the hot spare)
isActiveTab: boolean;
private _waveTabId: string; // always set, WaveTabViews are unique per tab
lastUsedTs: number; // ts milliseconds
createdTs: number; // ts milliseconds
initPromise: Promise<void>;
initResolve: () => void;
savedInitOpts: WaveInitOpts;
waveReadyPromise: Promise<void>;
waveReadyResolve: () => void;
isInitialized: boolean = false;
isWaveReady: boolean = false;
isDestroyed: boolean = false;
keyboardChordMode: boolean = false;
resetChordModeTimeout: NodeJS.Timeout = null;
constructor(fullConfig: FullConfigType) {
console.log("createBareTabView");
super({
webPreferences: {
preload: path.join(getElectronAppBasePath(), "preload", "index.cjs"),
webviewTag: true,
},
});
this.createdTs = Date.now();
this.savedInitOpts = null;
this.initPromise = new Promise((resolve, _) => {
this.initResolve = resolve;
});
this.initPromise.then(() => {
this.isInitialized = true;
console.log("tabview init", Date.now() - this.createdTs + "ms");
});
this.waveReadyPromise = new Promise((resolve, _) => {
this.waveReadyResolve = resolve;
});
this.waveReadyPromise.then(() => {
this.isWaveReady = true;
});
wcIdToWaveTabMap.set(this.webContents.id, this);
if (isDevVite) {
this.webContents.loadURL(`${process.env.ELECTRON_RENDERER_URL}/index.html}`);
} else {
this.webContents.loadFile(path.join(getElectronAppBasePath(), "frontend", "index.html"));
}
this.webContents.on("destroyed", () => {
wcIdToWaveTabMap.delete(this.webContents.id);
removeWaveTabView(this.waveTabId);
this.isDestroyed = true;
});
this.setBackgroundColor(computeBgColor(fullConfig));
}
get waveTabId(): string {
return this._waveTabId;
}
set waveTabId(waveTabId: string) {
this._waveTabId = waveTabId;
}
setKeyboardChordMode(mode: boolean) {
this.keyboardChordMode = mode;
if (mode) {
if (this.resetChordModeTimeout) {
clearTimeout(this.resetChordModeTimeout);
}
this.resetChordModeTimeout = setTimeout(() => {
this.keyboardChordMode = false;
}, CHORD_TIMEOUT);
} else {
if (this.resetChordModeTimeout) {
clearTimeout(this.resetChordModeTimeout);
this.resetChordModeTimeout = null;
}
}
}
positionTabOnScreen(winBounds: Rectangle) {
const curBounds = this.getBounds();
if (
curBounds.width == winBounds.width &&
curBounds.height == winBounds.height &&
curBounds.x == 0 &&
curBounds.y == 0
) {
return;
}
this.setBounds({ x: 0, y: 0, width: winBounds.width, height: winBounds.height });
}
positionTabOffScreen(winBounds: Rectangle) {
this.setBounds({
x: -15000,
y: -15000,
width: winBounds.width,
height: winBounds.height,
});
}
isOnScreen() {
const bounds = this.getBounds();
return bounds.x == 0 && bounds.y == 0;
}
destroy() {
console.log("destroy tab", this.waveTabId);
removeWaveTabView(this.waveTabId);
if (!this.isDestroyed) {
this.webContents?.close();
}
this.isDestroyed = true;
}
}
let MaxCacheSize = 10;
const wcvCache = new Map<string, WaveTabView>();
export function setMaxTabCacheSize(size: number) {
console.log("setMaxTabCacheSize", size);
MaxCacheSize = size;
}
export function getWaveTabView(waveTabId: string): WaveTabView | undefined {
const rtn = wcvCache.get(waveTabId);
if (rtn) {
rtn.lastUsedTs = Date.now();
}
return rtn;
}
function tryEvictEntry(waveTabId: string): boolean {
const tabView = wcvCache.get(waveTabId);
if (!tabView) {
return false;
}
if (tabView.isActiveTab) {
return false;
}
const lastUsedDiff = Date.now() - tabView.lastUsedTs;
if (lastUsedDiff < 1000) {
return false;
}
const ww = getWaveWindowById(tabView.waveWindowId);
if (!ww) {
// this shouldn't happen, but if it does, just destroy the tabview
console.log("[error] WaveWindow not found for WaveTabView", tabView.waveTabId);
tabView.destroy();
return true;
} else {
// will trigger a destroy on the tabview
ww.removeTabView(tabView.waveTabId, false);
return true;
}
}
function checkAndEvictCache(): void {
if (wcvCache.size <= MaxCacheSize) {
return;
}
const sorted = Array.from(wcvCache.values()).sort((a, b) => {
// Prioritize entries which are active
if (a.isActiveTab && !b.isActiveTab) {
return -1;
}
// Otherwise, sort by lastUsedTs
return a.lastUsedTs - b.lastUsedTs;
});
const now = Date.now();
for (let i = 0; i < sorted.length - MaxCacheSize; i++) {
tryEvictEntry(sorted[i].waveTabId);
}
}
export function clearTabCache() {
const wcVals = Array.from(wcvCache.values());
for (let i = 0; i < wcVals.length; i++) {
const tabView = wcVals[i];
tryEvictEntry(tabView.waveTabId);
}
}
// returns [tabview, initialized]
export async function getOrCreateWebViewForTab(waveWindowId: string, tabId: string): Promise<[WaveTabView, boolean]> {
let tabView = getWaveTabView(tabId);
if (tabView) {
return [tabView, true];
}
const fullConfig = await RpcApi.GetFullConfigCommand(ElectronWshClient);
tabView = getSpareTab(fullConfig);
tabView.waveWindowId = waveWindowId;
tabView.lastUsedTs = Date.now();
setWaveTabView(tabId, tabView);
tabView.waveTabId = tabId;
tabView.webContents.on("will-navigate", shNavHandler);
tabView.webContents.on("will-frame-navigate", shFrameNavHandler);
tabView.webContents.on("did-attach-webview", (event, wc) => {
wc.setWindowOpenHandler((details) => {
tabView.webContents.send("webview-new-window", wc.id, details);
return { action: "deny" };
});
});
tabView.webContents.on("before-input-event", (e, input) => {
const waveEvent = adaptFromElectronKeyEvent(input);
// console.log("WIN bie", tabView.waveTabId.substring(0, 8), waveEvent.type, waveEvent.code);
handleCtrlShiftState(tabView.webContents, waveEvent);
setWasActive(true);
if (input.type == "keyDown" && tabView.keyboardChordMode) {
e.preventDefault();
tabView.setKeyboardChordMode(false);
tabView.webContents.send("reinject-key", waveEvent);
}
});
tabView.webContents.on("zoom-changed", (e) => {
tabView.webContents.send("zoom-changed");
});
tabView.webContents.setWindowOpenHandler(({ url, frameName }) => {
if (url.startsWith("http://") || url.startsWith("https://") || url.startsWith("file://")) {
console.log("openExternal fallback", url);
shell.openExternal(url);
}
console.log("window-open denied", url);
return { action: "deny" };
});
tabView.webContents.on("blur", () => {
handleCtrlShiftFocus(tabView.webContents, false);
});
configureAuthKeyRequestInjection(tabView.webContents.session);
return [tabView, false];
}
export function setWaveTabView(waveTabId: string, wcv: WaveTabView): void {
if (waveTabId == null) {
return;
}
wcvCache.set(waveTabId, wcv);
checkAndEvictCache();
}
function removeWaveTabView(waveTabId: string): void {
if (waveTabId == null) {
return;
}
wcvCache.delete(waveTabId);
}
let HotSpareTab: WaveTabView = null;
export function ensureHotSpareTab(fullConfig: FullConfigType) {
console.log("ensureHotSpareTab");
if (HotSpareTab == null) {
HotSpareTab = new WaveTabView(fullConfig);
}
}
export function getSpareTab(fullConfig: FullConfigType): WaveTabView {
setTimeout(() => ensureHotSpareTab(fullConfig), 500);
if (HotSpareTab != null) {
const rtn = HotSpareTab;
HotSpareTab = null;
console.log("getSpareTab: returning hotspare");
return rtn;
} else {
console.log("getSpareTab: creating new tab");
return new WaveTabView(fullConfig);
}
}