restore emain

This commit is contained in:
sawka 2024-08-01 02:05:48 -07:00
parent de28e7e00c
commit 0a9418be10

View File

@ -1,8 +1,8 @@
// Copyright 2024, Command Line Inc. // Copyright 2024, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
import { initGlobal } from "../frontend/app/store/global"; import { initGlobal } from "@/app/store/global";
import { WaveDevVarName, WaveDevViteVarName } from "../frontend/util/isdev"; import { WaveDevVarName, WaveDevViteVarName } from "@/util/isdev";
import * as electron from "electron"; import * as electron from "electron";
import { autoUpdater } from "electron-updater"; import { autoUpdater } from "electron-updater";
import fs from "fs"; import fs from "fs";
@ -34,6 +34,7 @@ const waveSrvReady: Promise<boolean> = new Promise((resolve, _) => {
}); });
let globalIsQuitting = false; let globalIsQuitting = false;
let globalIsStarting = true; let globalIsStarting = true;
let globalIsRelaunching = false;
const isDev = !electronApp.isPackaged; const isDev = !electronApp.isPackaged;
const isDevVite = isDev && process.env.ELECTRON_RENDERER_URL; const isDevVite = isDev && process.env.ELECTRON_RENDERER_URL;
@ -214,7 +215,8 @@ async function handleWSEvent(evtMsg: WSEventType) {
return; return;
} }
const clientData = await services.ClientService.GetClientData(); const clientData = await services.ClientService.GetClientData();
const newWin = createBrowserWindow(clientData.oid, windowData); const settings = await services.FileService.GetSettingsConfig();
const newWin = createBrowserWindow(clientData.oid, windowData, settings);
await newWin.readyPromise; await newWin.readyPromise;
newWin.show(); newWin.show();
} else if (evtMsg.eventtype == "electron:closewindow") { } else if (evtMsg.eventtype == "electron:closewindow") {
@ -290,15 +292,39 @@ function shFrameNavHandler(event: Electron.Event<Electron.WebContentsWillFrameNa
// note, this does not *show* the window. // note, this does not *show* the window.
// to show, await win.readyPromise and then win.show() // to show, await win.readyPromise and then win.show()
function createBrowserWindow(clientId: string, waveWindow: WaveWindow): WaveBrowserWindow { function createBrowserWindow(
clientId: string,
waveWindow: WaveWindow,
settings: SettingsConfigType
): WaveBrowserWindow {
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) {
const primaryDisplay = electron.screen.getPrimaryDisplay();
const { width } = primaryDisplay.workAreaSize;
winWidth = width - winPosX - 100;
if (winWidth > 2000) {
winWidth = 2000;
}
}
if (winHeight == null || winHeight == 0) {
const primaryDisplay = electron.screen.getPrimaryDisplay();
const { height } = primaryDisplay.workAreaSize;
winHeight = height - winPosY - 100;
if (winHeight > 1200) {
winHeight = 1200;
}
}
let winBounds = { let winBounds = {
x: waveWindow.pos.x, x: winPosX,
y: waveWindow.pos.y, y: winPosY,
width: waveWindow.winsize.width, width: winWidth,
height: waveWindow.winsize.height, height: winHeight,
}; };
winBounds = ensureBoundsAreVisible(winBounds); winBounds = ensureBoundsAreVisible(winBounds);
const bwin = new electron.BrowserWindow({ const winOpts: Electron.BrowserWindowConstructorOptions = {
titleBarStyle: "hiddenInset", titleBarStyle: "hiddenInset",
x: winBounds.x, x: winBounds.x,
y: winBounds.y, y: winBounds.y,
@ -316,8 +342,26 @@ function createBrowserWindow(clientId: string, waveWindow: WaveWindow): WaveBrow
}, },
show: false, show: false,
autoHideMenuBar: true, autoHideMenuBar: true,
backgroundColor: "#000000", };
}); const isTransparent = settings?.window?.transparent ?? false;
const isBlur = !isTransparent && (settings?.window?.blur ?? false);
if (isTransparent) {
winOpts.transparent = true;
} else if (isBlur) {
switch (unamePlatform) {
case "win32": {
winOpts.backgroundMaterial = "acrylic";
break;
}
case "darwin": {
winOpts.vibrancy = "fullscreen-ui";
break;
}
}
} else {
winOpts.backgroundColor = "#222222";
}
const bwin = new electron.BrowserWindow(winOpts);
(bwin as any).waveWindowId = waveWindow.oid; (bwin as any).waveWindowId = waveWindow.oid;
let readyResolve: (value: void) => void; let readyResolve: (value: void) => void;
(bwin as any).readyPromise = new Promise((resolve, _) => { (bwin as any).readyPromise = new Promise((resolve, _) => {
@ -519,7 +563,8 @@ electron.ipcMain.on("getEnv", (event, varName) => {
async function createNewWaveWindow() { async function createNewWaveWindow() {
const clientData = await services.ClientService.GetClientData(); const clientData = await services.ClientService.GetClientData();
const newWindow = await services.ClientService.MakeWindow(); const newWindow = await services.ClientService.MakeWindow();
const newBrowserWindow = createBrowserWindow(clientData.oid, newWindow); const settings = await services.FileService.GetSettingsConfig();
const newBrowserWindow = createBrowserWindow(clientData.oid, newWindow, settings);
newBrowserWindow.show(); newBrowserWindow.show();
} }
@ -563,6 +608,7 @@ function makeAppMenu() {
}, },
{ {
role: "close", role: "close",
accelerator: "", // clear the accelerator
click: () => { click: () => {
electron.BrowserWindow.getFocusedWindow()?.close(); electron.BrowserWindow.getFocusedWindow()?.close();
}, },
@ -616,6 +662,12 @@ function makeAppMenu() {
{ {
role: "forceReload", role: "forceReload",
}, },
{
label: "Relaunch All Windows",
click: () => {
relaunchBrowserWindows();
},
},
{ {
role: "toggleDevTools", role: "toggleDevTools",
}, },
@ -638,6 +690,14 @@ function makeAppMenu() {
role: "togglefullscreen", role: "togglefullscreen",
}, },
]; ];
const windowMenu: Electron.MenuItemConstructorOptions[] = [
{ role: "minimize", accelerator: "" },
{ role: "zoom" },
{ type: "separator" },
{ role: "front" },
{ type: "separator" },
{ role: "window" },
];
const menuTemplate: Electron.MenuItemConstructorOptions[] = [ const menuTemplate: Electron.MenuItemConstructorOptions[] = [
{ {
role: "appMenu", role: "appMenu",
@ -656,6 +716,7 @@ function makeAppMenu() {
}, },
{ {
role: "windowMenu", role: "windowMenu",
submenu: windowMenu,
}, },
]; ];
const menu = electron.Menu.buildFromTemplate(menuTemplate); const menu = electron.Menu.buildFromTemplate(menuTemplate);
@ -663,6 +724,9 @@ function makeAppMenu() {
} }
electronApp.on("window-all-closed", () => { electronApp.on("window-all-closed", () => {
if (globalIsRelaunching) {
return;
}
if (unamePlatform !== "darwin") { if (unamePlatform !== "darwin") {
electronApp.quit(); electronApp.quit();
} }
@ -857,6 +921,36 @@ async function configureAutoUpdater() {
} }
// ====== AUTO-UPDATER ====== // // ====== AUTO-UPDATER ====== //
async function relaunchBrowserWindows() {
globalIsRelaunching = true;
const windows = electron.BrowserWindow.getAllWindows();
for (const window of windows) {
window.removeAllListeners();
window.close();
}
globalIsRelaunching = false;
const clientData = await services.ClientService.GetClientData();
const settings = await services.FileService.GetSettingsConfig();
const wins: WaveBrowserWindow[] = [];
for (const windowId of clientData.windowids.slice().reverse()) {
const windowData: WaveWindow = (await services.ObjectService.GetObject("window:" + windowId)) as WaveWindow;
if (windowData == null) {
services.WindowService.CloseWindow(windowId).catch((e) => {
/* ignore */
});
continue;
}
const win = createBrowserWindow(clientData.oid, windowData, settings);
wins.push(win);
}
for (const win of wins) {
await win.readyPromise;
console.log("show", win.waveWindowId);
win.show();
}
}
async function appMain() { async function appMain() {
const startTs = Date.now(); const startTs = Date.now();
const instanceLock = electronApp.requestSingleInstanceLock(); const instanceLock = electronApp.requestSingleInstanceLock();
@ -877,27 +971,8 @@ async function appMain() {
} }
const ready = await waveSrvReady; const ready = await waveSrvReady;
console.log("wavesrv ready signal received", ready, Date.now() - startTs, "ms"); console.log("wavesrv ready signal received", ready, Date.now() - startTs, "ms");
console.log("get client data");
const clientData = await services.ClientService.GetClientData();
console.log("client data ready");
await electronApp.whenReady(); await electronApp.whenReady();
const wins: WaveBrowserWindow[] = []; relaunchBrowserWindows();
for (const windowId of clientData.windowids.slice().reverse()) {
const windowData: WaveWindow = (await services.ObjectService.GetObject("window:" + windowId)) as WaveWindow;
if (windowData == null) {
services.WindowService.CloseWindow(windowId).catch((e) => {
/* ignore */
});
continue;
}
const win = createBrowserWindow(clientData.oid, windowData);
wins.push(win);
}
for (const win of wins) {
await win.readyPromise;
console.log("show", win.waveWindowId);
win.show();
}
configureAutoUpdater(); configureAutoUpdater();
globalIsStarting = false; globalIsStarting = false;