fix(electron): ensure app quits after main window close in dev

This commit is contained in:
aits2026
2026-03-05 19:28:28 +08:00
parent 7ead55d801
commit 2140a220e2
2 changed files with 42 additions and 1 deletions

View File

@@ -24,7 +24,7 @@ import { windowsHelloService } from './services/windowsHelloService'
import { exportCardDiagnosticsService } from './services/exportCardDiagnosticsService' import { exportCardDiagnosticsService } from './services/exportCardDiagnosticsService'
import { cloudControlService } from './services/cloudControlService' import { cloudControlService } from './services/cloudControlService'
import { registerNotificationHandlers, showNotification } from './windows/notificationWindow' import { destroyNotificationWindow, registerNotificationHandlers, showNotification } from './windows/notificationWindow'
import { httpService } from './services/httpService' import { httpService } from './services/httpService'
@@ -92,6 +92,7 @@ const keyService = new KeyService()
let mainWindowReady = false let mainWindowReady = false
let shouldShowMain = true let shouldShowMain = true
let isAppQuitting = false
// 更新下载状态管理Issue #294 修复) // 更新下载状态管理Issue #294 修复)
let isDownloadInProgress = false let isDownloadInProgress = false
@@ -332,6 +333,21 @@ function createWindow(options: { autoShow?: boolean } = {}) {
callback(false) callback(false)
}) })
win.on('closed', () => {
if (mainWindow !== win) return
mainWindow = null
mainWindowReady = false
if (process.platform !== 'darwin' && !isAppQuitting) {
// 隐藏通知窗也是 BrowserWindow必须销毁否则会阻止应用退出。
destroyNotificationWindow()
if (BrowserWindow.getAllWindows().length === 0) {
app.quit()
}
}
})
return win return win
} }
@@ -2427,6 +2443,9 @@ app.whenReady().then(async () => {
}) })
app.on('before-quit', async () => { app.on('before-quit', async () => {
isAppQuitting = true
// 通知窗使用 hide 而非 close退出时主动销毁避免残留窗口阻塞进程退出。
destroyNotificationWindow()
// 停止 HTTP 服务器,释放 TCP 端口占用,避免进程无法退出 // 停止 HTTP 服务器,释放 TCP 端口占用,避免进程无法退出
try { await httpService.stop() } catch {} try { await httpService.stop() } catch {}
// 终止 wcdb Worker 线程,避免线程阻止进程退出 // 终止 wcdb Worker 线程,避免线程阻止进程退出

View File

@@ -5,6 +5,28 @@ import { ConfigService } from '../services/config'
let notificationWindow: BrowserWindow | null = null let notificationWindow: BrowserWindow | null = null
let closeTimer: NodeJS.Timeout | null = null let closeTimer: NodeJS.Timeout | null = null
export function destroyNotificationWindow() {
if (closeTimer) {
clearTimeout(closeTimer)
closeTimer = null
}
lastNotificationData = null
if (!notificationWindow || notificationWindow.isDestroyed()) {
notificationWindow = null
return
}
const win = notificationWindow
notificationWindow = null
try {
win.destroy()
} catch (error) {
console.warn('[NotificationWindow] Failed to destroy window:', error)
}
}
export function createNotificationWindow() { export function createNotificationWindow() {
if (notificationWindow && !notificationWindow.isDestroyed()) { if (notificationWindow && !notificationWindow.isDestroyed()) {
return notificationWindow return notificationWindow