From ca863da878314f61676e34588b086a975ee099f0 Mon Sep 17 00:00:00 2001 From: xiaojunnuo Date: Thu, 2 Dec 2021 23:45:52 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E5=85=B3=E9=97=AD=E7=AD=96=E7=95=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/config/index.js | 3 +- packages/gui/src/background.js | 24 ++++++++++- packages/gui/src/bridge/front.js | 5 ++- packages/gui/src/bridge/on-close/front.js | 49 +++++++++++++++++++++++ packages/gui/src/view/pages/index.vue | 1 + packages/gui/src/view/pages/setting.vue | 17 ++++++++ 6 files changed, 94 insertions(+), 5 deletions(-) create mode 100644 packages/gui/src/bridge/on-close/front.js diff --git a/packages/core/src/config/index.js b/packages/core/src/config/index.js index add18b1..65bfb90 100644 --- a/packages/core/src/config/index.js +++ b/packages/core/src/config/index.js @@ -21,7 +21,8 @@ module.exports = { }, dock: { hideWhenWinClose: false - } + }, + closeStrategy: 0 }, server: { enabled: true, diff --git a/packages/gui/src/background.js b/packages/gui/src/background.js index 95b4f3b..93481b7 100644 --- a/packages/gui/src/background.js +++ b/packages/gui/src/background.js @@ -173,9 +173,29 @@ function createWindow (startHideWindow) { tray = null }) + ipcMain.on('close', async (event, message) => { + if (message.value === 1) { + quit() + } else { + hideWin() + } + }) + win.on('close', (e) => { - if (!forceClose) { - e.preventDefault() + if (forceClose) { + return + } + e.preventDefault() + const config = DevSidecar.api.config.get() + const closeStrategy = config.app.closeStrategy + if (closeStrategy === 0) { + // 提醒 + win.webContents.send('close.showTip') + } else if (closeStrategy === 1) { + // 直接退出 + quit() + } else if (closeStrategy === 2) { + // 隐藏窗口 hideWin() } }) diff --git a/packages/gui/src/bridge/front.js b/packages/gui/src/bridge/front.js index 1df8e86..7224a30 100644 --- a/packages/gui/src/bridge/front.js +++ b/packages/gui/src/bridge/front.js @@ -4,14 +4,15 @@ import tongji from './tongji/front' import update from './update/front' import fileSelector from './file-selector/front' import autoStart from './auto-start/front' - +import onClose from './on-close/front' const modules = { // api, // 核心接口模块 error, fileSelector, // 文件选择模块 tongji, // 统计模块 update, // 自动更新 - autoStart + autoStart, + onClose } export default { install (app, api, router) { diff --git a/packages/gui/src/bridge/on-close/front.js b/packages/gui/src/bridge/on-close/front.js new file mode 100644 index 0000000..28bddfc --- /dev/null +++ b/packages/gui/src/bridge/on-close/front.js @@ -0,0 +1,49 @@ +function install (app, api) { + api.ipc.on('close.showTip', (event, message) => { + console.error('error', event, message) + const result = { + closeType: 1, + save: false + } + function onRadioChange (event) { + result.closeType = event.target.value + } + function onCheckChange (event) { + result.save = event.target.checked + } + app.$confirm({ + title: '关闭策略', + content: h =>
+
+ + + 直接关闭 + + + 最小化到系统托盘 + + +
+
+ + 记住本次选择,不再提示 + < /a-checkbox> +
+
, + async onOk () { + console.log('OK') + if (result.save) { + await api.config.save({ app: { closeStrategy: result.closeType } }) + } + api.ipc.send('close', { key: 'selected', value: result.closeType }) + }, + onCancel () { + console.log('Cancel') + } + }) + }) +} + +export default { + install +} diff --git a/packages/gui/src/view/pages/index.vue b/packages/gui/src/view/pages/index.vue index f92a3fb..a44d494 100644 --- a/packages/gui/src/view/pages/index.vue +++ b/packages/gui/src/view/pages/index.vue @@ -19,6 +19,7 @@
+
diff --git a/packages/gui/src/view/pages/setting.vue b/packages/gui/src/view/pages/setting.vue index 39a5754..6ece805 100644 --- a/packages/gui/src/view/pages/setting.vue +++ b/packages/gui/src/view/pages/setting.vue @@ -35,6 +35,23 @@ + + + + 弹出提示 + + + 直接退出 + + + 最小化到系统托盘 + + +
+ 点击窗口右上角关闭按钮的效果 +
+