Browse Source

设置快捷键功能优化。

pull/366/head
王良 2 months ago
parent
commit
1fce38ae43
  1. 1
      packages/gui/public/index.html
  2. 45
      packages/gui/src/background.js
  3. 2
      packages/gui/src/view/pages/server.vue
  4. 21
      packages/gui/src/view/pages/setting.vue

1
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%">

45
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()
})
// 监听键盘事件
win.webContents.on('before-input-event', (event, input) => {
const shortcut = (event, input) => {
// 按 F12,打开/关闭 开发者工具
if (input.key === 'F12' && input.type === 'keyUp' && !input.control && !input.alt && !input.shift && !input.meta) {
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) => {
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()

2
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()"/>

21
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~F11F12DevTools
if (shortcut === '' && !key.match(/^F([1-9]|1[01])$/g)) {
// F1~F4F6~F11F5F12DevTools
if (shortcut === '' && !key.match(/^F([12346789]|1[01])$/g)) {
this.config.app.showHideShortcut = '无'
return
}

Loading…
Cancel
Save