optimize: 优化快捷键设置功能:1)允许设置单独按下F1~F11快捷键;2)为空是显示为 `无`;3)退出软件时注销快捷键。

pull/366/head
王良 2024-09-22 02:51:55 +08:00
parent 79f6715d97
commit 86e8513896
2 changed files with 26 additions and 20 deletions

View File

@ -135,7 +135,7 @@ function isLinux () {
function hideWin () { function hideWin () {
if (win) { if (win) {
if (isLinux()) { if (isLinux()) {
quit(app) quit()
return return
} }
win.hide() win.hide()
@ -156,14 +156,6 @@ function showWin () {
winIsHidden = false winIsHidden = false
} }
function switchWin () {
if (winIsHidden) {
showWin()
} else {
hideWin()
}
}
function changeAppConfig (config) { function changeAppConfig (config) {
if (config.hideDockWhenWinClose != null) { if (config.hideDockWhenWinClose != null) {
hideDockWhenWinClose = config.hideDockWhenWinClose hideDockWhenWinClose = config.hideDockWhenWinClose
@ -230,7 +222,7 @@ function createWindow (startHideWindow) {
} }
e.preventDefault() e.preventDefault()
if (isLinux()) { if (isLinux()) {
quit(app) quit()
return return
} }
const config = DevSidecar.api.config.get() const config = DevSidecar.api.config.get()
@ -265,6 +257,9 @@ async function beforeQuit () {
return DevSidecar.api.shutdown() return DevSidecar.api.shutdown()
} }
async function quit () { async function quit () {
globalShortcut.unregisterAll()
log.info('注销所有快捷键成功')
if (tray) { if (tray) {
tray.displayBalloon({ title: '正在关闭', content: '关闭中,请稍候。。。' }) tray.displayBalloon({ title: '正在关闭', content: '关闭中,请稍候。。。' })
} }
@ -283,7 +278,9 @@ function initApp () {
// 全局监听快捷键,用于 显示/隐藏 窗口 // 全局监听快捷键,用于 显示/隐藏 窗口
app.whenReady().then(async () => { app.whenReady().then(async () => {
globalShortcut.unregisterAll() 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 { try {
const registerSuccess = globalShortcut.register(DevSidecar.api.config.get().app.showHideShortcut, () => { const registerSuccess = globalShortcut.register(DevSidecar.api.config.get().app.showHideShortcut, () => {
if (winIsHidden || !win.isFocused()) { if (winIsHidden || !win.isFocused()) {
@ -344,7 +341,10 @@ if (!isFirstInstance) {
app.on('before-quit', async (event) => { app.on('before-quit', async (event) => {
log.info('before-quit') log.info('before-quit')
if (process.platform === 'darwin') { if (process.platform === 'darwin') {
quit(app) quit()
} else {
globalShortcut.unregisterAll()
log.info('注销所有快捷键成功')
} }
}) })
app.on('second-instance', (event, commandLine, workingDirectory) => { app.on('second-instance', (event, commandLine, workingDirectory) => {
@ -361,7 +361,10 @@ if (!isFirstInstance) {
// On macOS it is common for applications and their menu bar // On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q // to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') { if (process.platform !== 'darwin') {
quit(app) quit()
} else {
globalShortcut.unregisterAll()
log.info('注销所有快捷键成功')
} }
}) })
@ -419,12 +422,12 @@ if (isDevelopment) {
if (process.platform === 'win32') { if (process.platform === 'win32') {
process.on('message', (data) => { process.on('message', (data) => {
if (data === 'graceful-exit') { if (data === 'graceful-exit') {
quit(app) quit()
} }
}) })
} else { } else {
process.on('SIGINT', () => { process.on('SIGINT', () => {
quit(app) quit()
}) })
} }
} }

View File

@ -279,7 +279,7 @@ export default {
return '' return ''
}, },
shortcutChange () { shortcutChange () {
this.config.app.showHideShortcut = '' this.config.app.showHideShortcut = ''
}, },
shortcutKeyDown (event) { shortcutKeyDown (event) {
// console.info(`code=${event.code}, key=${event.key}, keyCode=${event.keyCode}`) // console.info(`code=${event.code}, key=${event.key}, keyCode=${event.keyCode}`)
@ -289,16 +289,19 @@ export default {
const key = this.getEventKey(event) const key = this.getEventKey(event)
if (!key) { if (!key) {
this.config.app.showHideShortcut = '' this.config.app.showHideShortcut = ''
return return
} }
// CtrlAltShift // CtrlAltShiftWindow
let shortcut = event.ctrlKey ? 'Ctrl + ' : '' let shortcut = event.ctrlKey ? 'Ctrl + ' : ''
if (event.altKey) shortcut += 'Alt + ' if (event.altKey) shortcut += 'Alt + '
if (event.shiftKey) shortcut += 'Shift + ' if (event.shiftKey) shortcut += 'Shift + '
if (shortcut === '') { if (event.metaKey) shortcut += 'Meta + '
this.config.app.showHideShortcut = ''
// F1~F11F12DevTools
if (shortcut === '' && !key.match(/^F([1-9]|1[01])$/g)) {
this.config.app.showHideShortcut = '无'
return return
} }