From 45cbc15c0f82f6cf9a3a20f7549a90a31914ab96 Mon Sep 17 00:00:00 2001 From: alger Date: Wed, 15 Jan 2025 00:30:00 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20=E6=B7=BB=E5=8A=A0=E5=BF=AB?= =?UTF-8?q?=E6=8D=B7=E9=94=AE=20=E4=BB=A5=E5=8F=8A=E5=BF=AB=E6=8D=B7?= =?UTF-8?q?=E9=94=AE=E7=AE=A1=E7=90=86=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ref #39 --- src/main/index.ts | 18 +- src/main/modules/config.ts | 9 +- src/main/modules/shortcuts.ts | 88 ++++ src/renderer/components.d.ts | 1 + src/renderer/components/ShortcutToast.vue | 91 +++++ .../components/settings/ShortcutSettings.vue | 380 ++++++++++++++++++ src/renderer/layout/components/PlayBar.vue | 49 ++- src/renderer/utils/shortcutToast.ts | 40 ++ src/renderer/views/set/index.vue | 17 + 9 files changed, 681 insertions(+), 12 deletions(-) create mode 100644 src/main/modules/shortcuts.ts create mode 100644 src/renderer/components/ShortcutToast.vue create mode 100644 src/renderer/components/settings/ShortcutSettings.vue create mode 100644 src/renderer/utils/shortcutToast.ts diff --git a/src/main/index.ts b/src/main/index.ts index 33e5b36..85b9edf 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -1,11 +1,12 @@ import { electronApp, optimizer } from '@electron-toolkit/utils'; -import { app, globalShortcut, ipcMain, nativeImage } from 'electron'; +import { app, ipcMain, nativeImage } from 'electron'; import { join } from 'path'; import { loadLyricWindow } from './lyric'; import { initializeCacheManager } from './modules/cache'; import { initializeConfig } from './modules/config'; import { initializeFileManager } from './modules/fileManager'; +import { initializeShortcuts, registerShortcuts } from './modules/shortcuts'; import { initializeTray } from './modules/tray'; import { createMainWindow, initializeWindowManager } from './modules/window'; import { startMusicApi } from './server'; @@ -44,6 +45,9 @@ function initialize() { // 加载歌词窗口 loadLyricWindow(ipcMain, mainWindow); + + // 初始化快捷键 + initializeShortcuts(mainWindow); } // 应用程序准备就绪时的处理 @@ -65,15 +69,9 @@ app.whenReady().then(() => { }); }); -// 应用程序准备就绪后的快捷键设置 -app.on('ready', () => { - globalShortcut.register('CommandOrControl+Alt+Shift+M', () => { - if (mainWindow.isVisible()) { - mainWindow.hide(); - } else { - mainWindow.show(); - } - }); +// 监听快捷键更新 +ipcMain.on('update-shortcuts', () => { + registerShortcuts(mainWindow); }); // 所有窗口关闭时的处理 diff --git a/src/main/modules/config.ts b/src/main/modules/config.ts index 1d86464..a323687 100644 --- a/src/main/modules/config.ts +++ b/src/main/modules/config.ts @@ -2,6 +2,7 @@ import { app, ipcMain } from 'electron'; import Store from 'electron-store'; import set from '../set.json'; +import { defaultShortcuts } from './shortcuts'; interface StoreType { set: { @@ -12,6 +13,7 @@ interface StoreType { authorUrl: string; musicApiPort: number; }; + shortcuts: typeof defaultShortcuts; } let store: Store; @@ -23,7 +25,8 @@ export function initializeConfig() { store = new Store({ name: 'config', defaults: { - set + set, + shortcuts: defaultShortcuts } }); @@ -41,3 +44,7 @@ export function initializeConfig() { return store; } + +export function getStore() { + return store; +} diff --git a/src/main/modules/shortcuts.ts b/src/main/modules/shortcuts.ts new file mode 100644 index 0000000..0f0c2e0 --- /dev/null +++ b/src/main/modules/shortcuts.ts @@ -0,0 +1,88 @@ +import { globalShortcut, ipcMain } from 'electron'; + +import { getStore } from './config'; + +// 添加获取平台信息的 IPC 处理程序 +ipcMain.on('get-platform', (event) => { + event.returnValue = process.platform; +}); + +// 定义默认快捷键 +export const defaultShortcuts = { + togglePlay: 'CommandOrControl+Alt+P', + prevPlay: 'CommandOrControl+Alt+Left', + nextPlay: 'CommandOrControl+Alt+Right', + volumeUp: 'CommandOrControl+Alt+Up', + volumeDown: 'CommandOrControl+Alt+Down', + toggleFavorite: 'CommandOrControl+Alt+L', + toggleWindow: 'CommandOrControl+Alt+Shift+M' +}; + +let mainWindowRef: Electron.BrowserWindow | null = null; + +// 注册快捷键 +export function registerShortcuts(mainWindow: Electron.BrowserWindow) { + mainWindowRef = mainWindow; + const store = getStore(); + const shortcuts = store.get('shortcuts'); + + // 注销所有已注册的快捷键 + globalShortcut.unregisterAll(); + + // 显示/隐藏主窗口 + globalShortcut.register(shortcuts.toggleWindow, () => { + if (mainWindow.isVisible()) { + mainWindow.hide(); + } else { + mainWindow.show(); + } + }); + + // 播放/暂停 + globalShortcut.register(shortcuts.togglePlay, () => { + mainWindow.webContents.send('global-shortcut', 'togglePlay'); + }); + + // 上一首 + globalShortcut.register(shortcuts.prevPlay, () => { + mainWindow.webContents.send('global-shortcut', 'prevPlay'); + }); + + // 下一首 + globalShortcut.register(shortcuts.nextPlay, () => { + mainWindow.webContents.send('global-shortcut', 'nextPlay'); + }); + + // 音量增加 + globalShortcut.register(shortcuts.volumeUp, () => { + mainWindow.webContents.send('global-shortcut', 'volumeUp'); + }); + + // 音量减少 + globalShortcut.register(shortcuts.volumeDown, () => { + mainWindow.webContents.send('global-shortcut', 'volumeDown'); + }); + + // 收藏当前歌曲 + globalShortcut.register(shortcuts.toggleFavorite, () => { + mainWindow.webContents.send('global-shortcut', 'toggleFavorite'); + }); +} + +// 初始化快捷键 +export function initializeShortcuts(mainWindow: Electron.BrowserWindow) { + mainWindowRef = mainWindow; + registerShortcuts(mainWindow); + + // 监听禁用快捷键事件 + ipcMain.on('disable-shortcuts', () => { + globalShortcut.unregisterAll(); + }); + + // 监听启用快捷键事件 + ipcMain.on('enable-shortcuts', () => { + if (mainWindowRef) { + registerShortcuts(mainWindowRef); + } + }); +} diff --git a/src/renderer/components.d.ts b/src/renderer/components.d.ts index d3fd957..e23f024 100644 --- a/src/renderer/components.d.ts +++ b/src/renderer/components.d.ts @@ -11,6 +11,7 @@ declare module 'vue' { NBadge: typeof import('naive-ui')['NBadge'] NButton: typeof import('naive-ui')['NButton'] NButtonGroup: typeof import('naive-ui')['NButtonGroup'] + NCard: typeof import('naive-ui')['NCard'] NCheckbox: typeof import('naive-ui')['NCheckbox'] NCheckboxGroup: typeof import('naive-ui')['NCheckboxGroup'] NConfigProvider: typeof import('naive-ui')['NConfigProvider'] diff --git a/src/renderer/components/ShortcutToast.vue b/src/renderer/components/ShortcutToast.vue new file mode 100644 index 0000000..d7bd12b --- /dev/null +++ b/src/renderer/components/ShortcutToast.vue @@ -0,0 +1,91 @@ + + + + + diff --git a/src/renderer/components/settings/ShortcutSettings.vue b/src/renderer/components/settings/ShortcutSettings.vue new file mode 100644 index 0000000..5be8db1 --- /dev/null +++ b/src/renderer/components/settings/ShortcutSettings.vue @@ -0,0 +1,380 @@ + + + + + diff --git a/src/renderer/layout/components/PlayBar.vue b/src/renderer/layout/components/PlayBar.vue index 1038de3..f2d51e7 100644 --- a/src/renderer/layout/components/PlayBar.vue +++ b/src/renderer/layout/components/PlayBar.vue @@ -154,7 +154,7 @@