From 28e7d2aa460413a62b34853333501c1af3398ab7 Mon Sep 17 00:00:00 2001 From: lumozx Date: Sun, 22 Oct 2023 10:58:33 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A7=A3=E5=86=B3win=E7=9B=B8=E4=BA=92?= =?UTF-8?q?=E8=A6=86=E7=9B=96=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/browsers/detach.ts | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/main/browsers/detach.ts b/src/main/browsers/detach.ts index 4b01e92..c90a187 100644 --- a/src/main/browsers/detach.ts +++ b/src/main/browsers/detach.ts @@ -5,18 +5,18 @@ import { WINDOW_MIN_HEIGHT } from '@/common/constans/common'; export default () => { let win: any; - const init = (pluginInfo, viewInfo, view) => { + const init = async (pluginInfo, viewInfo, view) => { ipcMain.on('detach:service', async (event, arg: { type: string }) => { const data = await operation[arg.type](); event.returnValue = data; }); - createWindow(pluginInfo, viewInfo, view); + const createWin = await createWindow(pluginInfo, viewInfo, view); // eslint-disable-next-line @typescript-eslint/no-var-requires - require('@electron/remote/main').enable(win.webContents); + require('@electron/remote/main').enable(createWin.webContents); }; const createWindow = async (pluginInfo, viewInfo, view) => { - win = new BrowserWindow({ + const createWin = new BrowserWindow({ height: viewInfo.height, minHeight: WINDOW_MIN_HEIGHT, width: viewInfo.width, @@ -43,29 +43,32 @@ export default () => { }); if (process.env.WEBPACK_DEV_SERVER_URL) { // Load the url of the dev server if in development mode - win.loadURL('http://localhost:8082'); + createWin.loadURL('http://localhost:8082'); } else { - win.loadURL(`file://${path.join(__static, './detach/index.html')}`); + createWin.loadURL(`file://${path.join(__static, './detach/index.html')}`); } - win.on('close', () => { + createWin.on('close', () => { executeHooks('PluginOut', null); }); - win.on('closed', () => { + createWin.on('closed', () => { win = undefined; }); + createWin.on('focus', () => { + win = createWin; + }); - win.once('ready-to-show', async () => { + createWin.once('ready-to-show', async () => { const config = await localConfig.getConfig(); const darkMode = config.perf.common.darkMode; darkMode && - win.webContents.executeJavaScript( + createWin.webContents.executeJavaScript( `document.body.classList.add("dark");window.rubick.theme="dark"` ); - win.setBrowserView(view); - win.webContents.executeJavaScript( + createWin.setBrowserView(view); + createWin.webContents.executeJavaScript( `window.initDetach(${JSON.stringify(pluginInfo)})` ); - win.show(); + createWin.show(); }); const executeHooks = (hook, data) => { if (!view) return; @@ -77,6 +80,7 @@ export default () => { `; view.webContents.executeJavaScript(evalJs); }; + return createWin; }; const getWindow = () => win;