diff --git a/packages/core/src/config/index.js b/packages/core/src/config/index.js index fbfb67b7d..4ad15df04 100644 --- a/packages/core/src/config/index.js +++ b/packages/core/src/config/index.js @@ -27,6 +27,7 @@ module.exports = { personalUrl: '' }, startShowWindow: true, // 启动时是否打开窗口:true=打开窗口, false=隐藏窗口 + showHideShortcut: 'Alt+S', // 显示/隐藏窗口快捷键 windowSize: { width: 900, height: 750 }, // 启动时,窗口的尺寸 theme: 'dark', // 主题:light=亮色, dark=暗色 autoChecked: true, // 是否自动检查更新 diff --git a/packages/gui/src/background.js b/packages/gui/src/background.js index ad9895d3e..6277aba2d 100644 --- a/packages/gui/src/background.js +++ b/packages/gui/src/background.js @@ -1,7 +1,7 @@ 'use strict' /* global __static */ import path from 'path' -import { app, protocol, BrowserWindow, Menu, Tray, ipcMain, dialog, powerMonitor, nativeImage, nativeTheme } from 'electron' +import { app, protocol, BrowserWindow, Menu, Tray, ipcMain, dialog, powerMonitor, nativeImage, nativeTheme, globalShortcut } from 'electron' import { createProtocol } from 'vue-cli-plugin-electron-builder/lib' import backend from './bridge/backend' import DevSidecar from '@docmirror/dev-sidecar' @@ -15,6 +15,7 @@ const isDevelopment = process.env.NODE_ENV !== 'production' // Keep a global reference of the window object, if you don't, the window will // be closed automatically when the JavaScript object is garbage collected. let win +let winIsHidden = false // eslint-disable-next-line no-unused-vars let tray // 防止被内存清理 let forceClose = false @@ -141,6 +142,7 @@ function hideWin () { if (isMac && hideDockWhenWinClose) { app.dock.hide() } + winIsHidden = true } } @@ -151,6 +153,15 @@ function showWin () { if (app.dock) { app.dock.show() } + winIsHidden = false +} + +function switchWin () { + if (winIsHidden) { + showWin() + } else { + hideWin() + } } function changeAppConfig (config) { @@ -179,6 +190,7 @@ function createWindow (startHideWindow) { // eslint-disable-next-line no-undef icon: path.join(__static, 'icon.png') }) + winIsHidden = !!startHideWindow Menu.setApplicationMenu(null) win.setMenu(null) @@ -261,13 +273,29 @@ async function quit () { app.quit() } -// eslint-disable-next-line no-unused-vars -function setDock () { +function initApp () { if (isMac) { app.whenReady().then(() => { app.dock.setIcon(path.join(__dirname, '../build/mac/512x512.png')) }) } + + // 全局监听快捷键,用于 显示/隐藏 窗口 + app.whenReady().then(() => { + globalShortcut.unregisterAll() + if (DevSidecar.api.config.get().app.showHideShortcut) { + globalShortcut.register(DevSidecar.api.config.get().app.showHideShortcut, () => { + if (winIsHidden) { + showWin() + } else { + // linux,快捷键不关闭窗口 + if (!isLinux()) { + hideWin() + } + } + }) + } + }) } // -------------执行开始--------------- app.disableHardwareAcceleration() // 禁用gpu @@ -332,7 +360,7 @@ if (!isFirstInstance) { } }) - // setDock() + // initApp() // This method will be called when Electron has finished // initialization and is ready to create browser windows. @@ -369,7 +397,7 @@ if (!isFirstInstance) { }) } -setDock() +initApp() // Exit cleanly on request from parent process in development mode. if (isDevelopment) { diff --git a/packages/gui/src/view/pages/setting.vue b/packages/gui/src/view/pages/setting.vue index 3d0ed74b9..dd0675530 100644 --- a/packages/gui/src/view/pages/setting.vue +++ b/packages/gui/src/view/pages/setting.vue @@ -88,6 +88,13 @@ 点击窗口右上角关闭按钮的效果 +
+ + +
+ 当前版本,修改快捷键后,需重启 ds 才会生效 +
+