-
Notifications
You must be signed in to change notification settings - Fork 337
/
Copy pathemain-window.ts
818 lines (762 loc) · 30.2 KB
/
emain-window.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
// Copyright 2025, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0
import { ClientService, ObjectService, WindowService, WorkspaceService } from "@/app/store/services";
import { RpcApi } from "@/app/store/wshclientapi";
import { fireAndForget } from "@/util/util";
import { BaseWindow, BaseWindowConstructorOptions, dialog, globalShortcut, ipcMain, screen } from "electron";
import { globalEvents } from "emain/emain-events";
import path from "path";
import { debounce } from "throttle-debounce";
import {
getGlobalIsQuitting,
getGlobalIsRelaunching,
setGlobalIsRelaunching,
setWasActive,
setWasInFg,
} from "./emain-activity";
import { getOrCreateWebViewForTab, getWaveTabViewByWebContentsId, WaveTabView } from "./emain-tabview";
import { delay, ensureBoundsAreVisible, waveKeyToElectronKey } from "./emain-util";
import { ElectronWshClient } from "./emain-wsh";
import { log } from "./log";
import { getElectronAppBasePath, unamePlatform } from "./platform";
import { updater } from "./updater";
export type WindowOpts = {
unamePlatform: string;
};
export const waveWindowMap = new Map<string, WaveBrowserWindow>(); // waveWindowId -> WaveBrowserWindow
// on blur we do not set this to null (but on destroy we do), so this tracks the *last* focused window
// e.g. it persists when the app itself is not focused
export let focusedWaveWindow: WaveBrowserWindow = null;
let cachedClientId: string = null;
async function getClientId() {
if (cachedClientId != null) {
return cachedClientId;
}
const clientData = await ClientService.GetClientData();
cachedClientId = clientData?.oid;
return cachedClientId;
}
type WindowActionQueueEntry =
| {
op: "switchtab";
tabId: string;
setInBackend: boolean;
}
| {
op: "createtab";
pinned: boolean;
}
| {
op: "closetab";
tabId: string;
}
| {
op: "switchworkspace";
workspaceId: string;
};
function isNonEmptyUnsavedWorkspace(workspace: Workspace): boolean {
return !workspace.name && !workspace.icon && (workspace.tabids?.length > 1 || workspace.pinnedtabids?.length > 1);
}
export class WaveBrowserWindow extends BaseWindow {
waveWindowId: string;
workspaceId: string;
allLoadedTabViews: Map<string, WaveTabView>;
activeTabView: WaveTabView;
private canClose: boolean;
private deleteAllowed: boolean;
private actionQueue: WindowActionQueueEntry[];
constructor(waveWindow: WaveWindow, fullConfig: FullConfigType, opts: WindowOpts) {
const settings = fullConfig?.settings;
console.log("create win", waveWindow.oid);
let winWidth = waveWindow?.winsize?.width;
let winHeight = waveWindow?.winsize?.height;
let winPosX = waveWindow.pos.x;
let winPosY = waveWindow.pos.y;
if (
(winWidth == null || winWidth === 0 || winHeight == null || winHeight === 0) &&
settings?.["window:dimensions"]
) {
const dimensions = settings["window:dimensions"];
const match = dimensions.match(/^(\d+)[xX](\d+)$/);
if (match) {
const [, dimensionWidth, dimensionHeight] = match;
const parsedWidth = parseInt(dimensionWidth, 10);
const parsedHeight = parseInt(dimensionHeight, 10);
if ((!winWidth || winWidth === 0) && Number.isFinite(parsedWidth) && parsedWidth > 0) {
winWidth = parsedWidth;
}
if ((!winHeight || winHeight === 0) && Number.isFinite(parsedHeight) && parsedHeight > 0) {
winHeight = parsedHeight;
}
} else {
console.warn('Invalid window:dimensions format. Expected "widthxheight".');
}
}
if (winWidth == null || winWidth == 0) {
const primaryDisplay = screen.getPrimaryDisplay();
const { width } = primaryDisplay.workAreaSize;
winWidth = width - winPosX - 100;
if (winWidth > 2000) {
winWidth = 2000;
}
}
if (winHeight == null || winHeight == 0) {
const primaryDisplay = screen.getPrimaryDisplay();
const { height } = primaryDisplay.workAreaSize;
winHeight = height - winPosY - 100;
if (winHeight > 1200) {
winHeight = 1200;
}
}
let winBounds = {
x: winPosX,
y: winPosY,
width: winWidth,
height: winHeight,
};
winBounds = ensureBoundsAreVisible(winBounds);
const winOpts: BaseWindowConstructorOptions = {
titleBarStyle:
opts.unamePlatform === "darwin"
? "hiddenInset"
: settings["window:nativetitlebar"]
? "default"
: "hidden",
titleBarOverlay:
opts.unamePlatform !== "darwin"
? {
symbolColor: "white",
color: "#00000000",
}
: false,
x: winBounds.x,
y: winBounds.y,
width: winBounds.width,
height: winBounds.height,
minWidth: 400,
minHeight: 300,
icon:
opts.unamePlatform == "linux"
? path.join(getElectronAppBasePath(), "public/logos/wave-logo-dark.png")
: undefined,
show: false,
autoHideMenuBar: !settings?.["window:showmenubar"],
};
const isTransparent = settings?.["window:transparent"] ?? false;
const isBlur = !isTransparent && (settings?.["window:blur"] ?? false);
if (isTransparent) {
winOpts.transparent = true;
} else if (isBlur) {
switch (opts.unamePlatform) {
case "win32": {
winOpts.backgroundMaterial = "acrylic";
break;
}
case "darwin": {
winOpts.vibrancy = "fullscreen-ui";
break;
}
}
} else {
winOpts.backgroundColor = "#222222";
}
super(winOpts);
this.actionQueue = [];
this.waveWindowId = waveWindow.oid;
this.workspaceId = waveWindow.workspaceid;
this.allLoadedTabViews = new Map<string, WaveTabView>();
const winBoundsPoller = setInterval(() => {
if (this.isDestroyed()) {
clearInterval(winBoundsPoller);
return;
}
if (this.actionQueue.length > 0) {
return;
}
this.finalizePositioning();
}, 1000);
this.on(
// @ts-expect-error
"resize",
debounce(400, (e) => this.mainResizeHandler(e))
);
this.on("resize", () => {
if (this.isDestroyed()) {
return;
}
this.activeTabView?.positionTabOnScreen(this.getContentBounds());
});
this.on(
// @ts-expect-error
"move",
debounce(400, (e) => this.mainResizeHandler(e))
);
this.on("enter-full-screen", async () => {
if (this.isDestroyed()) {
return;
}
console.log("enter-full-screen event", this.getContentBounds());
const tabView = this.activeTabView;
if (tabView) {
tabView.webContents.send("fullscreen-change", true);
}
this.activeTabView?.positionTabOnScreen(this.getContentBounds());
});
this.on("leave-full-screen", async () => {
if (this.isDestroyed()) {
return;
}
const tabView = this.activeTabView;
if (tabView) {
tabView.webContents.send("fullscreen-change", false);
}
this.activeTabView?.positionTabOnScreen(this.getContentBounds());
});
this.on("focus", () => {
if (this.isDestroyed()) {
return;
}
if (getGlobalIsRelaunching()) {
return;
}
focusedWaveWindow = this;
console.log("focus win", this.waveWindowId);
fireAndForget(() => ClientService.FocusWindow(this.waveWindowId));
setWasInFg(true);
setWasActive(true);
});
this.on("blur", () => {
// nothing for now
});
this.on("close", (e) => {
if (this.canClose) {
return;
}
if (this.isDestroyed()) {
return;
}
console.log("win 'close' handler fired", this.waveWindowId);
if (getGlobalIsQuitting() || updater?.status == "installing" || getGlobalIsRelaunching()) {
return;
}
e.preventDefault();
fireAndForget(async () => {
const numWindows = waveWindowMap.size;
const fullConfig = await RpcApi.GetFullConfigCommand(ElectronWshClient);
if (numWindows > 1 || !fullConfig.settings["window:savelastwindow"]) {
if (fullConfig.settings["window:confirmclose"]) {
const workspace = await WorkspaceService.GetWorkspace(this.workspaceId);
if (isNonEmptyUnsavedWorkspace(workspace)) {
const choice = dialog.showMessageBoxSync(this, {
type: "question",
buttons: ["Cancel", "Close Window"],
title: "Confirm",
message:
"Window has unsaved tabs, closing window will delete existing tabs.\n\nContinue?",
});
if (choice === 0) {
return;
}
}
}
this.deleteAllowed = true;
}
this.canClose = true;
this.close();
});
});
this.on("closed", () => {
console.log("win 'closed' handler fired", this.waveWindowId);
if (getGlobalIsQuitting() || updater?.status == "installing") {
console.log("win quitting or updating", this.waveWindowId);
return;
}
setTimeout(() => globalEvents.emit("windows-updated"), 50);
waveWindowMap.delete(this.waveWindowId);
if (focusedWaveWindow == this) {
focusedWaveWindow = null;
}
this.removeAllChildViews();
if (getGlobalIsRelaunching()) {
console.log("win relaunching", this.waveWindowId);
this.destroy();
return;
}
if (this.deleteAllowed) {
console.log("win removing window from backend DB", this.waveWindowId);
fireAndForget(() => WindowService.CloseWindow(this.waveWindowId, true));
}
});
waveWindowMap.set(waveWindow.oid, this);
setTimeout(() => globalEvents.emit("windows-updated"), 50);
}
private removeAllChildViews() {
for (const tabView of this.allLoadedTabViews.values()) {
if (!this.isDestroyed()) {
this.contentView.removeChildView(tabView);
}
tabView?.destroy();
}
}
async switchWorkspace(workspaceId: string) {
console.log("switchWorkspace", workspaceId, this.waveWindowId);
if (workspaceId == this.workspaceId) {
console.log("switchWorkspace already on this workspace", this.waveWindowId);
return;
}
// If the workspace is already owned by a window, then we can just call SwitchWorkspace without first prompting the user, since it'll just focus to the other window.
const workspaceList = await WorkspaceService.ListWorkspaces();
if (!workspaceList?.find((wse) => wse.workspaceid === workspaceId)?.windowid) {
const curWorkspace = await WorkspaceService.GetWorkspace(this.workspaceId);
if (curWorkspace && isNonEmptyUnsavedWorkspace(curWorkspace)) {
console.log(
`existing unsaved workspace ${this.workspaceId} has content, opening workspace ${workspaceId} in new window`
);
await createWindowForWorkspace(workspaceId);
return;
}
}
await this._queueActionInternal({ op: "switchworkspace", workspaceId });
}
async setActiveTab(tabId: string, setInBackend: boolean) {
console.log("setActiveTab", tabId, this.waveWindowId, this.workspaceId, setInBackend);
await this._queueActionInternal({ op: "switchtab", tabId, setInBackend });
}
private async initializeTab(tabView: WaveTabView) {
const clientId = await getClientId();
await tabView.initPromise;
this.contentView.addChildView(tabView);
const initOpts = {
tabId: tabView.waveTabId,
clientId: clientId,
windowId: this.waveWindowId,
activate: true,
};
tabView.savedInitOpts = { ...initOpts };
tabView.savedInitOpts.activate = false;
let startTime = Date.now();
console.log("before wave ready, init tab, sending wave-init", tabView.waveTabId);
tabView.webContents.send("wave-init", initOpts);
await tabView.waveReadyPromise;
console.log("wave-ready init time", Date.now() - startTime + "ms");
}
private async setTabViewIntoWindow(tabView: WaveTabView, tabInitialized: boolean) {
if (this.activeTabView == tabView) {
return;
}
const oldActiveView = this.activeTabView;
tabView.isActiveTab = true;
if (oldActiveView != null) {
oldActiveView.isActiveTab = false;
}
this.activeTabView = tabView;
this.allLoadedTabViews.set(tabView.waveTabId, tabView);
if (!tabInitialized) {
console.log("initializing a new tab");
const p1 = this.initializeTab(tabView);
const p2 = this.repositionTabsSlowly(100);
await Promise.all([p1, p2]);
} else {
console.log("reusing an existing tab, calling wave-init", tabView.waveTabId);
const p1 = this.repositionTabsSlowly(35);
const p2 = tabView.webContents.send("wave-init", tabView.savedInitOpts); // reinit
await Promise.all([p1, p2]);
}
// something is causing the new tab to lose focus so it requires manual refocusing
tabView.webContents.focus();
setTimeout(() => {
if (tabView.webContents && this.activeTabView == tabView && !tabView.webContents.isFocused()) {
tabView.webContents.focus();
}
}, 10);
setTimeout(() => {
if (tabView.webContents && this.activeTabView == tabView && !tabView.webContents.isFocused()) {
tabView.webContents.focus();
}
}, 30);
}
private async repositionTabsSlowly(delayMs: number) {
const activeTabView = this.activeTabView;
const winBounds = this.getContentBounds();
if (activeTabView == null) {
return;
}
if (activeTabView.isOnScreen()) {
activeTabView.setBounds({
x: 0,
y: 0,
width: winBounds.width,
height: winBounds.height,
});
} else {
activeTabView.setBounds({
x: winBounds.width - 10,
y: winBounds.height - 10,
width: winBounds.width,
height: winBounds.height,
});
}
await delay(delayMs);
if (this.activeTabView != activeTabView) {
// another tab view has been set, do not finalize this layout
return;
}
this.finalizePositioning();
}
private finalizePositioning() {
if (this.isDestroyed()) {
return;
}
const curBounds = this.getContentBounds();
this.activeTabView?.positionTabOnScreen(curBounds);
for (const tabView of this.allLoadedTabViews.values()) {
if (tabView == this.activeTabView) {
continue;
}
tabView?.positionTabOffScreen(curBounds);
}
}
async queueCreateTab(pinned = false) {
await this._queueActionInternal({ op: "createtab", pinned });
}
async queueCloseTab(tabId: string) {
await this._queueActionInternal({ op: "closetab", tabId });
}
private async _queueActionInternal(entry: WindowActionQueueEntry) {
if (this.actionQueue.length >= 2) {
this.actionQueue[1] = entry;
return;
}
const wasEmpty = this.actionQueue.length === 0;
this.actionQueue.push(entry);
if (wasEmpty) {
await this.processActionQueue();
}
}
private removeTabViewLater(tabId: string, delayMs: number) {
setTimeout(() => {
this.removeTabView(tabId, false);
}, 1000);
}
// the queue and this function are used to serialize operations that update the window contents view
// processActionQueue will replace [1] if it is already set
// we don't mess with [0] because it is "in process"
// we replace [1] because there is no point to run an action that is going to be overwritten
private async processActionQueue() {
while (this.actionQueue.length > 0) {
try {
if (this.isDestroyed()) {
break;
}
const entry = this.actionQueue[0];
let tabId: string = null;
// have to use "===" here to get the typechecker to work :/
switch (entry.op) {
case "createtab":
tabId = await WorkspaceService.CreateTab(this.workspaceId, null, true, entry.pinned);
break;
case "switchtab":
tabId = entry.tabId;
if (this.activeTabView?.waveTabId == tabId) {
continue;
}
if (entry.setInBackend) {
await WorkspaceService.SetActiveTab(this.workspaceId, tabId);
}
break;
case "closetab":
tabId = entry.tabId;
const rtn = await WorkspaceService.CloseTab(this.workspaceId, tabId, true);
if (rtn == null) {
console.log(
"[error] closeTab: no return value",
tabId,
this.workspaceId,
this.waveWindowId
);
return;
}
this.removeTabViewLater(tabId, 1000);
if (rtn.closewindow) {
this.close();
return;
}
if (!rtn.newactivetabid) {
return;
}
tabId = rtn.newactivetabid;
break;
case "switchworkspace":
const newWs = await WindowService.SwitchWorkspace(this.waveWindowId, entry.workspaceId);
if (!newWs) {
return;
}
console.log("processActionQueue switchworkspace newWs", newWs);
this.removeAllChildViews();
console.log("destroyed all tabs", this.waveWindowId);
this.workspaceId = entry.workspaceId;
this.allLoadedTabViews = new Map();
tabId = newWs.activetabid;
break;
}
if (tabId == null) {
return;
}
const [tabView, tabInitialized] = await getOrCreateWebViewForTab(this.waveWindowId, tabId);
await this.setTabViewIntoWindow(tabView, tabInitialized);
} catch (e) {
console.log("error caught in processActionQueue", e);
} finally {
this.actionQueue.shift();
}
}
}
private async mainResizeHandler(_: any) {
if (this == null || this.isDestroyed() || this.fullScreen) {
return;
}
const bounds = this.getBounds();
try {
await WindowService.SetWindowPosAndSize(
this.waveWindowId,
{ x: bounds.x, y: bounds.y },
{ width: bounds.width, height: bounds.height }
);
} catch (e) {
console.log("error sending new window bounds to backend", e);
}
}
removeTabView(tabId: string, force: boolean) {
if (!force && this.activeTabView?.waveTabId == tabId) {
console.log("cannot remove active tab", tabId, this.waveWindowId);
return;
}
const tabView = this.allLoadedTabViews.get(tabId);
if (tabView == null) {
console.log("removeTabView -- tabView not found", tabId, this.waveWindowId);
// the tab was never loaded, so just return
return;
}
this.contentView.removeChildView(tabView);
this.allLoadedTabViews.delete(tabId);
tabView.destroy();
}
destroy() {
console.log("destroy win", this.waveWindowId);
this.deleteAllowed = true;
super.destroy();
}
}
export function getWaveWindowByTabId(tabId: string): WaveBrowserWindow {
for (const ww of waveWindowMap.values()) {
if (ww.allLoadedTabViews.has(tabId)) {
return ww;
}
}
}
export function getWaveWindowByWebContentsId(webContentsId: number): WaveBrowserWindow {
const tabView = getWaveTabViewByWebContentsId(webContentsId);
if (tabView == null) {
return null;
}
return getWaveWindowByTabId(tabView.waveTabId);
}
export function getWaveWindowById(windowId: string): WaveBrowserWindow {
return waveWindowMap.get(windowId);
}
export function getWaveWindowByWorkspaceId(workspaceId: string): WaveBrowserWindow {
for (const waveWindow of waveWindowMap.values()) {
if (waveWindow.workspaceId === workspaceId) {
return waveWindow;
}
}
}
export function getAllWaveWindows(): WaveBrowserWindow[] {
return Array.from(waveWindowMap.values());
}
export async function createWindowForWorkspace(workspaceId: string) {
const newWin = await WindowService.CreateWindow(null, workspaceId);
if (!newWin) {
console.log("error creating new window", this.waveWindowId);
}
const newBwin = await createBrowserWindow(newWin, await RpcApi.GetFullConfigCommand(ElectronWshClient), {
unamePlatform,
});
newBwin.show();
}
// note, this does not *show* the window.
// to show, await win.readyPromise and then win.show()
export async function createBrowserWindow(
waveWindow: WaveWindow,
fullConfig: FullConfigType,
opts: WindowOpts
): Promise<WaveBrowserWindow> {
if (!waveWindow) {
console.log("createBrowserWindow: no waveWindow");
waveWindow = await WindowService.CreateWindow(null, "");
}
let workspace = await WorkspaceService.GetWorkspace(waveWindow.workspaceid);
if (!workspace) {
console.log("createBrowserWindow: no workspace, creating new window");
await WindowService.CloseWindow(waveWindow.oid, true);
waveWindow = await WindowService.CreateWindow(null, "");
workspace = await WorkspaceService.GetWorkspace(waveWindow.workspaceid);
}
console.log("createBrowserWindow", waveWindow.oid, workspace.oid, workspace);
const bwin = new WaveBrowserWindow(waveWindow, fullConfig, opts);
if (workspace.activetabid) {
await bwin.setActiveTab(workspace.activetabid, false);
}
return bwin;
}
ipcMain.on("set-active-tab", async (event, tabId) => {
const ww = getWaveWindowByWebContentsId(event.sender.id);
console.log("set-active-tab", tabId, ww?.waveWindowId);
await ww?.setActiveTab(tabId, true);
});
ipcMain.on("create-tab", async (event, opts) => {
const senderWc = event.sender;
const ww = getWaveWindowByWebContentsId(senderWc.id);
if (ww != null) {
await ww.queueCreateTab();
}
event.returnValue = true;
return null;
});
ipcMain.on("close-tab", async (event, workspaceId, tabId) => {
const ww = getWaveWindowByWorkspaceId(workspaceId);
if (ww == null) {
console.log(`close-tab: no window found for workspace ws=${workspaceId} tab=${tabId}`);
return;
}
await ww.queueCloseTab(tabId);
event.returnValue = true;
return null;
});
ipcMain.on("switch-workspace", (event, workspaceId) => {
fireAndForget(async () => {
const ww = getWaveWindowByWebContentsId(event.sender.id);
console.log("switch-workspace", workspaceId, ww?.waveWindowId);
await ww?.switchWorkspace(workspaceId);
});
});
export async function createWorkspace(window: WaveBrowserWindow) {
const newWsId = await WorkspaceService.CreateWorkspace("", "", "", true);
if (newWsId) {
if (window) {
await window.switchWorkspace(newWsId);
} else {
await createWindowForWorkspace(newWsId);
}
}
}
ipcMain.on("create-workspace", (event) => {
fireAndForget(async () => {
const ww = getWaveWindowByWebContentsId(event.sender.id);
console.log("create-workspace", ww?.waveWindowId);
await createWorkspace(ww);
});
});
ipcMain.on("delete-workspace", (event, workspaceId) => {
fireAndForget(async () => {
const ww = getWaveWindowByWebContentsId(event.sender.id);
console.log("delete-workspace", workspaceId, ww?.waveWindowId);
const workspaceList = await WorkspaceService.ListWorkspaces();
const workspaceHasWindow = !!workspaceList.find((wse) => wse.workspaceid === workspaceId)?.windowid;
const choice = dialog.showMessageBoxSync(this, {
type: "question",
buttons: ["Cancel", "Delete Workspace"],
title: "Confirm",
message: `Deleting workspace will also delete its contents.\n\nContinue?`,
});
if (choice === 0) {
console.log("user cancelled workspace delete", workspaceId, ww?.waveWindowId);
return;
}
const newWorkspaceId = await WorkspaceService.DeleteWorkspace(workspaceId);
console.log("delete-workspace done", workspaceId, ww?.waveWindowId);
if (ww?.workspaceId == workspaceId) {
if (newWorkspaceId) {
await ww.switchWorkspace(newWorkspaceId);
} else {
console.log("delete-workspace closing window", workspaceId, ww?.waveWindowId);
ww.destroy();
}
}
});
});
export async function createNewWaveWindow() {
log("createNewWaveWindow");
const clientData = await ClientService.GetClientData();
const fullConfig = await RpcApi.GetFullConfigCommand(ElectronWshClient);
let recreatedWindow = false;
const allWindows = getAllWaveWindows();
if (allWindows.length === 0 && clientData?.windowids?.length >= 1) {
console.log("no windows, but clientData has windowids, recreating first window");
// reopen the first window
const existingWindowId = clientData.windowids[0];
const existingWindowData = (await ObjectService.GetObject("window:" + existingWindowId)) as WaveWindow;
if (existingWindowData != null) {
const win = await createBrowserWindow(existingWindowData, fullConfig, { unamePlatform });
win.show();
recreatedWindow = true;
}
}
if (recreatedWindow) {
console.log("recreated window, returning");
return;
}
console.log("creating new window");
const newBrowserWindow = await createBrowserWindow(null, fullConfig, { unamePlatform });
newBrowserWindow.show();
}
export async function relaunchBrowserWindows() {
console.log("relaunchBrowserWindows");
setGlobalIsRelaunching(true);
const windows = getAllWaveWindows();
if (windows.length > 0) {
for (const window of windows) {
console.log("relaunch -- closing window", window.waveWindowId);
window.close();
}
await delay(1200);
}
setGlobalIsRelaunching(false);
const clientData = await ClientService.GetClientData();
const fullConfig = await RpcApi.GetFullConfigCommand(ElectronWshClient);
const wins: WaveBrowserWindow[] = [];
for (const windowId of clientData.windowids.slice().reverse()) {
const windowData: WaveWindow = await WindowService.GetWindow(windowId);
if (windowData == null) {
console.log("relaunch -- window data not found, closing window", windowId);
await WindowService.CloseWindow(windowId, true);
continue;
}
console.log("relaunch -- creating window", windowId, windowData);
const win = await createBrowserWindow(windowData, fullConfig, { unamePlatform });
wins.push(win);
}
for (const win of wins) {
console.log("show window", win.waveWindowId);
win.show();
}
}
export function registerGlobalHotkey(rawGlobalHotKey: string) {
try {
const electronHotKey = waveKeyToElectronKey(rawGlobalHotKey);
console.log("registering globalhotkey of ", electronHotKey);
globalShortcut.register(electronHotKey, () => {
const selectedWindow = focusedWaveWindow;
const firstWaveWindow = getAllWaveWindows()[0];
if (focusedWaveWindow) {
selectedWindow.focus();
} else if (firstWaveWindow) {
firstWaveWindow.focus();
} else {
fireAndForget(createNewWaveWindow);
}
});
} catch (e) {
console.log("error registering global hotkey: ", e);
}
}