diff --git a/packages/gui/public/index.html b/packages/gui/public/index.html index 41cb07a2..6dc21839 100644 --- a/packages/gui/public/index.html +++ b/packages/gui/public/index.html @@ -6,6 +6,7 @@ <meta name="viewport" content="width=device-width,initial-scale=1.0"> <link rel="icon" href="<%= BASE_URL %>favicon.ico"> <title><%= htmlWebpackPlugin.options.title %></title> + <script type="application/javascript">window.config = {}</script> </head> <body style="height:100%"> <div id="app" style="height:100%"> diff --git a/packages/gui/src/background.js b/packages/gui/src/background.js index 69c5675d..9cf76015 100644 --- a/packages/gui/src/background.js +++ b/packages/gui/src/background.js @@ -118,7 +118,7 @@ function setTray () { showWin() }) - appTray.on('right-click', function (event, bounds) { + appTray.on('right-click', function () { setTimeout(function () { appTray.popUpContextMenu(contextMenu) }, 200) @@ -203,7 +203,7 @@ function createWindow (startHideWindow) { hideWin() } - win.on('closed', async (e) => { + win.on('closed', async () => { win = null tray = null }) @@ -244,12 +244,39 @@ function createWindow (startHideWindow) { await quit() }) + const shortcut = (event, input) => { + // 按 F12,打开/关闭 开发者工具 + if (input.key === 'F12') { + // 阻止默认的按键事件行为 + event.preventDefault() + // 切换开发者工具显示状态 + switchDevTools() + // eslint-disable-next-line brace-style + } + // 按 F5,刷新页面 + else if (input.key === 'F5') { + // 阻止默认的按键事件行为 + event.preventDefault() + // 刷新页面 + win.webContents.reload() + } + } + // 监听键盘事件 win.webContents.on('before-input-event', (event, input) => { - // 按 F12,打开/关闭 开发者工具 - if (input.key === 'F12' && input.type === 'keyUp' && !input.control && !input.alt && !input.shift && !input.meta) { - switchDevTools() + if (input.type !== 'keyUp' || input.control || input.alt || input.shift || input.meta) { + return } + win.webContents.executeJavaScript('config') + .then((value) => { + console.info('window.config:', value) + if (!value || (value.disableBeforeInputEvent !== true && value.disableBeforeInputEvent !== 'true')) { + shortcut(event, input) + } + }) + .catch(() => { + shortcut(event, input) + }) }) } @@ -333,17 +360,17 @@ if (!isFirstInstance) { app.quit() }, 1000) } else { - app.on('before-quit', async (event) => { + app.on('before-quit', async () => { log.info('before-quit') if (process.platform === 'darwin') { quit() } }) app.on('will-quit', () => { - globalShortcut.unregisterAll(); + globalShortcut.unregisterAll() log.info('应用关闭,注销所有快捷键') - }); - app.on('second-instance', (event, commandLine, workingDirectory) => { + }) + app.on('second-instance', (event, commandLine) => { log.info('new app started, command:', commandLine) if (win) { showWin() diff --git a/packages/gui/src/view/pages/server.vue b/packages/gui/src/view/pages/server.vue index 04f78d1b..a9eea4ab 100644 --- a/packages/gui/src/view/pages/server.vue +++ b/packages/gui/src/view/pages/server.vue @@ -91,7 +91,7 @@ <a-tab-pane tab="域名白名单" key="4"> <a-row style="margin-top:10px"> <a-col span="19"> - <div>这里配置哪些域名不需要通过代理</div> + <div>这里配置的域名不会通过代理</div> </a-col> <a-col span="3"> <a-button style="margin-left:8px" type="primary" icon="plus" @click="addWhiteList()"/> diff --git a/packages/gui/src/view/pages/setting.vue b/packages/gui/src/view/pages/setting.vue index 9beb507e..dbaa1f7b 100644 --- a/packages/gui/src/view/pages/setting.vue +++ b/packages/gui/src/view/pages/setting.vue @@ -90,8 +90,9 @@ </a-form-item> <hr/> <a-form-item label="打开窗口快捷键" :label-col="labelCol" :wrapper-col="wrapperCol"> - <a-input v-model="config.app.showHideShortcut" @change="shortcutChange" @keydown="shortcutKeyDown"></a-input> + <a-input v-model="config.app.showHideShortcut" @change="shortcutChange" @keydown="shortcutKeyDown" @keyup="shortcutKeyUp"></a-input> <div class="form-help"> + 部分快捷键已被占用:F5=刷新页面,F12=开发者工具(DevTools)<br/> 当前版本,修改快捷键后,需重启 ds 才会生效 </div> </a-form-item> @@ -278,10 +279,24 @@ export default { console.error(`未能识别的按键:key=${event.key}, code=${event.code}, keyCode=${event.keyCode}`) return '' }, + async disableBeforeInputEvent () { + clearTimeout(window.enableBeforeInputEventTimeout) + window.config.disableBeforeInputEvent = true + window.enableBeforeInputEventTimeout = setTimeout(function () { + window.config.disableBeforeInputEvent = false + }, 2000) + }, shortcutChange () { this.config.app.showHideShortcut = '无' }, + shortcutKeyUp (event) { + event.preventDefault() + this.disableBeforeInputEvent() + }, shortcutKeyDown (event) { + event.preventDefault() + this.disableBeforeInputEvent() + // console.info(`code=${event.code}, key=${event.key}, keyCode=${event.keyCode}`) if (event.type !== 'keydown') { return @@ -299,8 +314,8 @@ export default { if (event.shiftKey) shortcut += 'Shift + ' if (event.metaKey) shortcut += 'Meta + ' - // 如果以上按钮都没有按下,并且当前键不是F1~F11,则直接返回(注:F12已经是打开DevTools的快捷键了) - if (shortcut === '' && !key.match(/^F([1-9]|1[01])$/g)) { + // 如果以上按钮都没有按下,并且当前键不是F1~F4、F6~F11时,则直接返回(注:F5已经是刷新页面快捷键、F12已经是打开DevTools的快捷键了) + if (shortcut === '' && !key.match(/^F([12346789]|1[01])$/g)) { this.config.app.showHideShortcut = '无' return }