From 95de18a60f4da4ba000bea14e3a65193dc5950c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=89=AF?= <841369634@qq.com> Date: Thu, 23 Jan 2025 10:59:19 +0800 Subject: [PATCH] =?UTF-8?q?optimize:=20=E5=88=9B=E5=BB=BA=E7=AA=97?= =?UTF-8?q?=E5=8F=A3=E5=A4=B1=E8=B4=A5=E6=97=B6=EF=BC=8C=E8=AE=B0=E5=BD=95?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E5=B9=B6=E9=80=80=E5=87=BA=E8=BD=AF=E4=BB=B6?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/gui/src/background.js | 52 +++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/packages/gui/src/background.js b/packages/gui/src/background.js index fedf269c..af23ecf0 100644 --- a/packages/gui/src/background.js +++ b/packages/gui/src/background.js @@ -179,25 +179,35 @@ function changeAppConfig (config) { } } -function createWindow (startHideWindow) { +function createWindow (startHideWindow, autoQuitIfError = true) { // Create the browser window. const windowSize = DevSidecar.api.config.get().app.windowSize || {} - win = new BrowserWindow({ - width: windowSize.width || 900, - height: windowSize.height || 750, - title: 'DevSidecar', - webPreferences: { - enableRemoteModule: true, - contextIsolation: false, - nativeWindowOpen: true, // ADD THIS - // preload: path.join(__dirname, 'preload.js'), - // Use pluginOptions.nodeIntegration, leave this alone - // See nklayman.github.io/vue-cli-plugin-electron-builder/guide/security.html#node-integration for more info - nodeIntegration: true, // process.env.ELECTRON_NODE_INTEGRATION - }, - show: !startHideWindow, - icon: path.join(__static, 'icon.png'), - }) + + try { + win = new BrowserWindow({ + width: windowSize.width || 900, + height: windowSize.height || 750, + title: 'DevSidecar', + webPreferences: { + enableRemoteModule: true, + contextIsolation: false, + nativeWindowOpen: true, // ADD THIS + // preload: path.join(__dirname, 'preload.js'), + // Use pluginOptions.nodeIntegration, leave this alone + // See nklayman.github.io/vue-cli-plugin-electron-builder/guide/security.html#node-integration for more info + nodeIntegration: true, // process.env.ELECTRON_NODE_INTEGRATION + }, + show: !startHideWindow, + icon: path.join(__static, 'icon.png'), + }) + } catch (e) { + log.error('创建窗口失败:', e) + dialog.showErrorBox('错误', `创建窗口失败: ${e.message}`) + if (autoQuitIfError) { + quit() + } + return false + } winIsHidden = !!startHideWindow Menu.setApplicationMenu(null) @@ -309,6 +319,8 @@ function createWindow (startHideWindow) { registerShowHideShortcut(message) } }) + + return true } async function beforeQuit () { @@ -429,7 +441,7 @@ try { // On macOS it's common to re-create a window in the app when the // dock icon is clicked and there are no other windows open. if (win == null) { - createWindow(false) + createWindow(false, false) } else { showWin() } @@ -451,7 +463,9 @@ try { } try { - createWindow(startHideWindow) + if (!createWindow(startHideWindow)) { + return // 创建窗口失败,应用将关闭 + } } catch (err) { log.error('createWindow error:', err) }