From fa9899d2f7a78b9b395dad477714425834ccc205 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=89=AF?= <841369634@qq.com> Date: Fri, 20 Sep 2024 12:03:23 +0800 Subject: [PATCH] =?UTF-8?q?feature:=20=E6=B7=BB=E5=8A=A0=E6=98=BE=E7=A4=BA?= =?UTF-8?q?/=E9=9A=90=E8=97=8F=E7=AA=97=E5=8F=A3=E7=9A=84=E5=85=A8?= =?UTF-8?q?=E5=B1=80=E5=BF=AB=E6=8D=B7=E9=94=AE=EF=BC=8C=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=EF=BC=9A`Alt+S`=20(#360)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/config/index.js | 1 + packages/gui/src/background.js | 38 +++++++++++++++++++++---- packages/gui/src/view/pages/setting.vue | 7 +++++ 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/packages/core/src/config/index.js b/packages/core/src/config/index.js index fbfb67b7..4ad15df0 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 ad9895d3..6277aba2 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 3d0ed74b..dd067553 100644 --- a/packages/gui/src/view/pages/setting.vue +++ b/packages/gui/src/view/pages/setting.vue @@ -88,6 +88,13 @@ 点击窗口右上角关闭按钮的效果 +