From 9f0a18ecce6a7a8488e6402f1facd0ee3ef18cee Mon Sep 17 00:00:00 2001 From: pompurin404 Date: Sat, 28 Sep 2024 20:10:51 +0800 Subject: [PATCH] cleanup on shutdown --- changelog.md | 1 + src/main/index.ts | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/changelog.md b/changelog.md index e0bfe76..de16231 100644 --- a/changelog.md +++ b/changelog.md @@ -2,6 +2,7 @@ - Windows 允许添加 `noadmin` 参数以普通权限启动程序 - 支持在 Windows 任务栏显示网速信息 +- 支持在关机前自动关闭系统代理 ### Bug Fixes diff --git a/src/main/index.ts b/src/main/index.ts index bdfefa5..d12d0a4 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -1,7 +1,7 @@ import { electronApp, optimizer, is } from '@electron-toolkit/utils' import { registerIpcMainHandlers } from './utils/ipc' import windowStateKeeper from 'electron-window-state' -import { app, shell, BrowserWindow, Menu, dialog, Notification } from 'electron' +import { app, shell, BrowserWindow, Menu, dialog, Notification, powerMonitor } from 'electron' import { addProfileItem, getAppConfig } from './config' import { quitWithoutCore, startCore, stopCore } from './core/manager' import { triggerSysProxy } from './sys/sysproxy' @@ -96,9 +96,17 @@ app.on('window-all-closed', (e) => { // } }) -app.on('before-quit', async () => { - await stopCore() +app.on('before-quit', async (e) => { + e.preventDefault() triggerSysProxy(false) + await stopCore() + app.exit() +}) + +powerMonitor.on('shutdown', async (e) => { + e.preventDefault() + triggerSysProxy(false) + await stopCore() app.exit() }) @@ -239,6 +247,11 @@ export async function createWindow(): Promise { } }) + mainWindow.on('session-end', async () => { + triggerSysProxy(false) + await stopCore() + }) + mainWindow.webContents.setWindowOpenHandler((details) => { shell.openExternal(details.url) return { action: 'deny' }