Skip to content

Commit

Permalink
feature: 添加显示/隐藏窗口的全局快捷键,默认:Alt+S (#360)
Browse files Browse the repository at this point in the history
  • Loading branch information
wangliang181230 authored Sep 20, 2024
1 parent 6ed8e71 commit fa9899d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/core/src/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, // 是否自动检查更新
Expand Down
38 changes: 33 additions & 5 deletions packages/gui/src/background.js
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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
Expand Down Expand Up @@ -141,6 +142,7 @@ function hideWin () {
if (isMac && hideDockWhenWinClose) {
app.dock.hide()
}
winIsHidden = true
}
}

Expand All @@ -151,6 +153,15 @@ function showWin () {
if (app.dock) {
app.dock.show()
}
winIsHidden = false
}

function switchWin () {
if (winIsHidden) {
showWin()
} else {
hideWin()
}
}

function changeAppConfig (config) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -369,7 +397,7 @@ if (!isFirstInstance) {
})
}

setDock()
initApp()

// Exit cleanly on request from parent process in development mode.
if (isDevelopment) {
Expand Down
7 changes: 7 additions & 0 deletions packages/gui/src/view/pages/setting.vue
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@
点击窗口右上角关闭按钮的效果
</div>
</a-form-item>
<hr/>
<a-form-item label="打开窗口快捷键" :label-col="labelCol" :wrapper-col="wrapperCol">
<a-input v-model="config.app.showHideShortcut"></a-input>
<div class="form-help">
当前版本,修改快捷键后,需重启 ds 才会生效
</div>
</a-form-item>
<a-form-item label="启动时打开窗口" :label-col="labelCol" :wrapper-col="wrapperCol">
<a-radio-group v-model="config.app.startShowWindow" default-value="true" button-style="solid">
<a-radio-button :value="true">
Expand Down

0 comments on commit fa9899d

Please sign in to comment.