From 86e85138960753b8595907bd784f6fc41d982526 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=89=AF?= <841369634@qq.com> Date: Sun, 22 Sep 2024 02:51:55 +0800 Subject: [PATCH] =?UTF-8?q?optimize:=20=E4=BC=98=E5=8C=96=E5=BF=AB?= =?UTF-8?q?=E6=8D=B7=E9=94=AE=E8=AE=BE=E7=BD=AE=E5=8A=9F=E8=83=BD=EF=BC=9A?= =?UTF-8?q?1=EF=BC=89=E5=85=81=E8=AE=B8=E8=AE=BE=E7=BD=AE=E5=8D=95?= =?UTF-8?q?=E7=8B=AC=E6=8C=89=E4=B8=8BF1~F11=E5=BF=AB=E6=8D=B7=E9=94=AE?= =?UTF-8?q?=EF=BC=9B2=EF=BC=89=E4=B8=BA=E7=A9=BA=E6=98=AF=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=E4=B8=BA=20`=E6=97=A0`=EF=BC=9B3=EF=BC=89=E9=80=80?= =?UTF-8?q?=E5=87=BA=E8=BD=AF=E4=BB=B6=E6=97=B6=E6=B3=A8=E9=94=80=E5=BF=AB?= =?UTF-8?q?=E6=8D=B7=E9=94=AE=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/gui/src/background.js | 33 ++++++++++++++----------- packages/gui/src/view/pages/setting.vue | 13 ++++++---- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/packages/gui/src/background.js b/packages/gui/src/background.js index a2c7fb6d..93f8088b 100644 --- a/packages/gui/src/background.js +++ b/packages/gui/src/background.js @@ -135,7 +135,7 @@ function isLinux () { function hideWin () { if (win) { if (isLinux()) { - quit(app) + quit() return } win.hide() @@ -156,14 +156,6 @@ function showWin () { winIsHidden = false } -function switchWin () { - if (winIsHidden) { - showWin() - } else { - hideWin() - } -} - function changeAppConfig (config) { if (config.hideDockWhenWinClose != null) { hideDockWhenWinClose = config.hideDockWhenWinClose @@ -230,7 +222,7 @@ function createWindow (startHideWindow) { } e.preventDefault() if (isLinux()) { - quit(app) + quit() return } const config = DevSidecar.api.config.get() @@ -265,6 +257,9 @@ async function beforeQuit () { return DevSidecar.api.shutdown() } async function quit () { + globalShortcut.unregisterAll() + log.info('注销所有快捷键成功') + if (tray) { tray.displayBalloon({ title: '正在关闭', content: '关闭中,请稍候。。。' }) } @@ -283,7 +278,9 @@ function initApp () { // 全局监听快捷键,用于 显示/隐藏 窗口 app.whenReady().then(async () => { globalShortcut.unregisterAll() - if (DevSidecar.api.config.get().app.showHideShortcut) { + const showHideShortcut = DevSidecar.api.config.get().app.showHideShortcut + log.info('先注销所有快捷键,再根据配置设置一个全局快捷键:', showHideShortcut) + if (showHideShortcut && showHideShortcut !== '无' && showHideShortcut.length > 1) { try { const registerSuccess = globalShortcut.register(DevSidecar.api.config.get().app.showHideShortcut, () => { if (winIsHidden || !win.isFocused()) { @@ -344,7 +341,10 @@ if (!isFirstInstance) { app.on('before-quit', async (event) => { log.info('before-quit') if (process.platform === 'darwin') { - quit(app) + quit() + } else { + globalShortcut.unregisterAll() + log.info('注销所有快捷键成功') } }) app.on('second-instance', (event, commandLine, workingDirectory) => { @@ -361,7 +361,10 @@ if (!isFirstInstance) { // On macOS it is common for applications and their menu bar // to stay active until the user quits explicitly with Cmd + Q if (process.platform !== 'darwin') { - quit(app) + quit() + } else { + globalShortcut.unregisterAll() + log.info('注销所有快捷键成功') } }) @@ -419,12 +422,12 @@ if (isDevelopment) { if (process.platform === 'win32') { process.on('message', (data) => { if (data === 'graceful-exit') { - quit(app) + quit() } }) } else { process.on('SIGINT', () => { - quit(app) + quit() }) } } diff --git a/packages/gui/src/view/pages/setting.vue b/packages/gui/src/view/pages/setting.vue index 273358d4..e1309a20 100644 --- a/packages/gui/src/view/pages/setting.vue +++ b/packages/gui/src/view/pages/setting.vue @@ -279,7 +279,7 @@ export default { return '' }, shortcutChange () { - this.config.app.showHideShortcut = '' + this.config.app.showHideShortcut = '无' }, shortcutKeyDown (event) { // console.info(`code=${event.code}, key=${event.key}, keyCode=${event.keyCode}`) @@ -289,16 +289,19 @@ export default { const key = this.getEventKey(event) if (!key) { - this.config.app.showHideShortcut = '' + this.config.app.showHideShortcut = '无' return } - // 判断 Ctrl、Alt、Shift按钮是否已按下 + // 判断 Ctrl、Alt、Shift、Window 按钮是否已按下,如果已按下,则拼接键值 let shortcut = event.ctrlKey ? 'Ctrl + ' : '' if (event.altKey) shortcut += 'Alt + ' if (event.shiftKey) shortcut += 'Shift + ' - if (shortcut === '') { - this.config.app.showHideShortcut = '' + if (event.metaKey) shortcut += 'Meta + ' + + // 如果以上按钮都没有按下,并且当前键不是F1~F11,则直接返回(注:F12已经是打开DevTools的快捷键了) + if (shortcut === '' && !key.match(/^F([1-9]|1[01])$/g)) { + this.config.app.showHideShortcut = '无' return }