Remove "main" window concept from emain.ts (#651)

This continues the work to unblock multiple window support. `emain.ts`
now contains no more references to a "main" window, instead all event
handlers will dispatch to either the window that triggered the event,
the currently focused window, or the first window to be spun up.
This commit is contained in:
Evan Simkowitz 2024-05-06 13:43:27 -07:00 committed by GitHub
parent d6b82e3792
commit 4384525ca6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -34,9 +34,6 @@ let wasActive = true;
let wasInFg = true; let wasInFg = true;
let currentGlobalShortcut: string | null = null; let currentGlobalShortcut: string | null = null;
let initialClientData: ClientDataType = null; let initialClientData: ClientDataType = null;
let windows: Windows = {};
interface Windows extends Record<string, Electron.BrowserWindow> {}
checkPromptMigrate(); checkPromptMigrate();
ensureDir(waveHome); ensureDir(waveHome);
@ -325,10 +322,7 @@ function shFrameNavHandler(event: Electron.Event<Electron.WebContentsWillFrameNa
console.log("frame navigation canceled"); console.log("frame navigation canceled");
} }
function createWindow(id: string, clientData: ClientDataType | null): Electron.BrowserWindow { function createWindow(clientData: ClientDataType | null): Electron.BrowserWindow {
if (windows[id]) {
console.error(`createWindow called for existing window ${id}`);
}
const bounds = calcBounds(clientData); const bounds = calcBounds(clientData);
setKeyUtilPlatform(platform()); setKeyUtilPlatform(platform());
const win = new electron.BrowserWindow({ const win = new electron.BrowserWindow({
@ -376,18 +370,9 @@ function createWindow(id: string, clientData: ClientDataType | null): Electron.B
wasInFg = true; wasInFg = true;
wasActive = true; wasActive = true;
}); });
win.on("close", () => {
delete windows[id];
});
win.webContents.on("zoom-changed", (e) => { win.webContents.on("zoom-changed", (e) => {
win.webContents.send("zoom-changed"); win.webContents.send("zoom-changed");
}); });
windows[id] = win;
return win;
}
function createMainWindow(clientData: ClientDataType | null) {
const win = createWindow("main", clientData);
win.webContents.setWindowOpenHandler(({ url, frameName }) => { win.webContents.setWindowOpenHandler(({ url, frameName }) => {
if (url.startsWith("https://docs.waveterm.dev/")) { if (url.startsWith("https://docs.waveterm.dev/")) {
console.log("openExternal docs", url); console.log("openExternal docs", url);
@ -408,6 +393,7 @@ function createMainWindow(clientData: ClientDataType | null) {
console.log("window-open denied", url); console.log("window-open denied", url);
return { action: "deny" }; return { action: "deny" };
}); });
return win;
} }
function mainResizeHandler(_: any, win: Electron.BrowserWindow) { function mainResizeHandler(_: any, win: Electron.BrowserWindow) {
@ -673,13 +659,13 @@ async function getClientData(willRetry: boolean, retryNum: number): Promise<Clie
} }
function sendWSSC() { function sendWSSC() {
if (windows["main"] != null) { electron.BrowserWindow.getAllWindows().forEach((win) => {
if (waveSrvProc == null) { if (waveSrvProc == null) {
windows["main"].webContents.send("wavesrv-status-change", false); win.webContents.send("wavesrv-status-change", false);
return; } else {
} win.webContents.send("wavesrv-status-change", true, waveSrvProc.pid);
windows["main"].webContents.send("wavesrv-status-change", true, waveSrvProc.pid);
} }
});
} }
function runWaveSrv() { function runWaveSrv() {
@ -747,7 +733,7 @@ electron.ipcMain.on("context-editmenu", (_, { x, y }, opts) => {
menu.popup({ x, y }); menu.popup({ x, y });
}); });
async function createMainWindowWrap() { async function createWindowWrap() {
let clientData: ClientDataType | null = null; let clientData: ClientDataType | null = null;
try { try {
clientData = await getClientDataPoll(1); clientData = await getClientDataPoll(1);
@ -755,9 +741,9 @@ async function createMainWindowWrap() {
} catch (e) { } catch (e) {
console.log("error getting wavesrv clientdata", e.toString()); console.log("error getting wavesrv clientdata", e.toString());
} }
createMainWindow(clientData); const win = createWindow(clientData);
if (clientData?.winsize.fullscreen) { if (clientData?.winsize.fullscreen) {
windows["main"].setFullScreen(true); win.setFullScreen(true);
} }
configureAutoUpdaterStartup(clientData); configureAutoUpdaterStartup(clientData);
} }
@ -776,7 +762,7 @@ function logActiveState() {
console.log("error logging active state", err); console.log("error logging active state", err);
}); });
// for next iteration // for next iteration
wasInFg = windows["main"]?.isFocused(); wasInFg = electron.BrowserWindow.getFocusedWindow()?.isFocused() ?? false;
wasActive = false; wasActive = false;
} }
@ -802,9 +788,13 @@ function reregisterGlobalShortcut(shortcut: string) {
currentGlobalShortcut = null; currentGlobalShortcut = null;
return; return;
} }
const ok = electron.globalShortcut.register(shortcut, () => { const ok = electron.globalShortcut.register(shortcut, async () => {
console.log("global shortcut triggered, showing window"); console.log("global shortcut triggered, showing window");
windows["main"]?.show(); if (electron.BrowserWindow.getAllWindows().length == 0) {
await createWindowWrap();
}
const winToShow = electron.BrowserWindow.getFocusedWindow() ?? electron.BrowserWindow.getAllWindows()[0];
winToShow?.show();
}); });
console.log("registered global shortcut", shortcut, ok ? "ok" : "failed"); console.log("registered global shortcut", shortcut, ok ? "ok" : "failed");
if (!ok) { if (!ok) {
@ -829,9 +819,9 @@ let lastUpdateCheck: Date = null;
*/ */
function setAppUpdateStatus(status: string) { function setAppUpdateStatus(status: string) {
appUpdateStatus = status; appUpdateStatus = status;
if (windows["main"] != null) { electron.BrowserWindow.getAllWindows().forEach((window) => {
windows["main"].webContents.send("app-update-status", appUpdateStatus); window.webContents.send("app-update-status", appUpdateStatus);
} });
} }
/** /**
@ -915,9 +905,14 @@ async function installAppUpdate() {
detail: "A new version has been downloaded. Restart the application to apply the updates.", detail: "A new version has been downloaded. Restart the application to apply the updates.",
}; };
await electron.dialog.showMessageBox(windows["main"], dialogOpts).then(({ response }) => { const allWindows = electron.BrowserWindow.getAllWindows();
if (allWindows.length > 0) {
await electron.dialog
.showMessageBox(electron.BrowserWindow.getFocusedWindow() ?? allWindows[0], dialogOpts)
.then(({ response }) => {
if (response === 0) autoUpdater.quitAndInstall(); if (response === 0) autoUpdater.quitAndInstall();
}); });
}
} }
electron.ipcMain.on("install-app-update", () => fireAndForget(() => installAppUpdate())); electron.ipcMain.on("install-app-update", () => fireAndForget(() => installAppUpdate()));
@ -989,10 +984,11 @@ function configureAutoUpdater(enabled: boolean) {
} }
setTimeout(runActiveTimer, 5000); // start active timer, wait 5s just to be safe setTimeout(runActiveTimer, 5000); // start active timer, wait 5s just to be safe
await app.whenReady(); await app.whenReady();
await createMainWindowWrap(); await createWindowWrap();
app.on("activate", () => { app.on("activate", () => {
if (electron.BrowserWindow.getAllWindows().length === 0) { if (electron.BrowserWindow.getAllWindows().length === 0) {
createMainWindowWrap().then(); createWindowWrap().then();
} }
checkForUpdates(); checkForUpdates();
}); });