diff --git a/packages/gui/package.json b/packages/gui/package.json index 31c78a6..c647088 100644 --- a/packages/gui/package.json +++ b/packages/gui/package.json @@ -44,7 +44,7 @@ "vue": "^2.6.11", "vue-json-editor-fix-cn": "^1.4.3", "vue-router": "^3.4.8", - "@natmri/platform-napi": "0.0.8" + "@natmri/platform-napi": "0.0.7" }, "devDependencies": { "@vue/cli-plugin-babel": "^4.5.0", diff --git a/packages/gui/src/background.js b/packages/gui/src/background.js index b9ef662..1e9d5fb 100644 --- a/packages/gui/src/background.js +++ b/packages/gui/src/background.js @@ -187,6 +187,10 @@ function createWindow (startHideWindow) { Menu.setApplicationMenu(null) win.setMenu(null) + // !!IMPORTANT + if(process.platform === 'win32') { + powerMonitor.setupMainWindow(win) + } if (process.env.WEBPACK_DEV_SERVER_URL) { // Load the url of the dev server if in development mode @@ -444,7 +448,8 @@ if (!isFirstInstance) { } powerMonitor.on('shutdown', async (e) => { - e.preventDefault() + if(process.platform !== 'win32') + e.preventDefault() log.info('系统关机,恢复代理设置') await quit() }) diff --git a/packages/gui/src/background/powerMonitor.js b/packages/gui/src/background/powerMonitor.js index e4e304c..edf85e0 100644 --- a/packages/gui/src/background/powerMonitor.js +++ b/packages/gui/src/background/powerMonitor.js @@ -1,9 +1,23 @@ -import { powerMonitor as _powerMonitor } from 'electron/main' -import { createShutdownBlocker, destroyShutdownBlocker } from '@natmri/platform-napi' +import { powerMonitor as _powerMonitor, BrowserWindow } from 'electron' +import { setMainWindowHandle, insertWndProcHook, removeWndProcHook, acquireShutdownBlock, releaseShutdownBlock } from '@natmri/platform-napi' class PowerMonitor { - _listeners = [] - _shutdownCallback = null + constructor() { + this.setup = false + this._listeners = [] + this._shutdownCallback = null + } + + /** + * + * @param {BrowserWindow} window + */ + setupMainWindow(window) { + if(!this.setup) { + setMainWindowHandle(window.getNativeWindowHandle().readBigInt64LE()) + this.setup = true + } + } addListener(event, listener) { return this.on(event, listener) @@ -17,7 +31,8 @@ class PowerMonitor { if(event === 'shutdown' && process.platform === 'win32') { this._listeners = [] if(this._shutdownCallback) { - destroyShutdownBlocker() + removeWndProcHook() + releaseShutdownBlock() this._shutdownCallback = null } } else { @@ -30,9 +45,10 @@ class PowerMonitor { if(!this._shutdownCallback) { this._shutdownCallback = async () => { await Promise.all(this._listeners.map((fn) => fn())) - destroyShutdownBlocker() + releaseShutdownBlock() } - createShutdownBlocker("正在停止 DevSidecar 代理", this._shutdownCallback) + insertWndProcHook(this._shutdownCallback) + acquireShutdownBlock("正在停止 DevSidecar 代理") } this._listeners.push(listener) } else { @@ -50,7 +66,7 @@ class PowerMonitor { once(event, listener) { if(event === 'shutdown' && process.platform === 'win32') { - + return this.on(event, listener) } else { return _powerMonitor.once(event, listener) } diff --git a/packages/gui/vue.config.js b/packages/gui/vue.config.js index 8ab804b..775996b 100644 --- a/packages/gui/vue.config.js +++ b/packages/gui/vue.config.js @@ -32,6 +32,19 @@ module.exports = { }, pluginOptions: { electronBuilder: { + externals: [ + '@natmri/platform-napi', + "@natmri/platform-napi-win32-x64-msvc", + "@natmri/platform-napi-darwin-x64", + "@natmri/platform-napi-linux-x64-gnu", + "@natmri/platform-napi-darwin-arm64", + "@natmri/platform-napi-linux-arm64-gnu", + "@natmri/platform-napi-linux-arm64-musl", + "@natmri/platform-napi-win32-arm64-msvc", + "@natmri/platform-napi-linux-arm-gnueabihf", + "@natmri/platform-napi-linux-x64-musl", + "@natmri/platform-napi-win32-ia32-msvc" + ], nodeIntegration: true, // Provide an array of files that, when changed, will recompile the main process and restart Electron // Your main process file will be added by default