From e54c606c6de32cd54e8e5b5ea8f2e4c0cd98af3f Mon Sep 17 00:00:00 2001 From: qier222 Date: Wed, 9 Jun 2021 20:39:00 +0800 Subject: [PATCH] feat: custom shortcuts --- src/background.js | 22 +-- src/electron/globalShortcut.js | 77 ++++++++--- src/electron/ipcMain.js | 54 ++++++-- src/electron/menu.js | 22 ++- src/store/initLocalStorage.js | 2 + src/store/mutations.js | 13 ++ src/utils/shortcuts.js | 47 +++++++ src/utils/updateApp.js | 15 ++ src/views/settings.vue | 243 ++++++++++++++++++++++++++++++++- 9 files changed, 439 insertions(+), 56 deletions(-) create mode 100644 src/utils/shortcuts.js diff --git a/src/background.js b/src/background.js index 1d0ca8a..caeba33 100644 --- a/src/background.js +++ b/src/background.js @@ -366,14 +366,10 @@ class Background { this.initOSDLyrics(); // init ipcMain - initIpcMain( - this.window, - { - resizeOSDLyrics: height => this.resizeOSDLyrics(height), - toggleOSDLyrics: () => this.toggleOSDLyrics(), - }, - this.store - ); + initIpcMain(this.window, this.store, { + resizeOSDLyrics: height => this.resizeOSDLyrics(height), + toggleOSDLyrics: () => this.toggleOSDLyrics(), + }); // set proxy const proxyRules = this.store.get('proxy'); @@ -387,13 +383,7 @@ class Background { this.checkForUpdates(); // create menu - createMenu(this.window, { - openDevTools: () => { - if (this.osdlyrics) { - this.osdlyrics.webContents.openDevTools(); - } - }, - }); + createMenu(this.window, this.store); // create tray if ( @@ -411,7 +401,7 @@ class Background { // register global shortcuts if (this.store.get('settings.enableGlobalShortcut')) { - registerGlobalShortcut(this.window); + registerGlobalShortcut(this.window, this.store); } }); diff --git a/src/electron/globalShortcut.js b/src/electron/globalShortcut.js index d53a9c5..c352eaf 100644 --- a/src/electron/globalShortcut.js +++ b/src/electron/globalShortcut.js @@ -1,25 +1,58 @@ +import defaultShortcuts from '@/utils/shortcuts'; const { globalShortcut } = require('electron'); -export function registerGlobalShortcut(win) { - globalShortcut.register('Alt+CommandOrControl+P', () => { - win.webContents.send('play'); - }); - globalShortcut.register('Alt+CommandOrControl+Right', () => { - win.webContents.send('next'); - }); - globalShortcut.register('Alt+CommandOrControl+Left', () => { - win.webContents.send('previous'); - }); - globalShortcut.register('Alt+CommandOrControl+Up', () => { - win.webContents.send('increaseVolume'); - }); - globalShortcut.register('Alt+CommandOrControl+Down', () => { - win.webContents.send('decreaseVolume'); - }); - globalShortcut.register('Alt+CommandOrControl+L', () => { - win.webContents.send('like'); - }); - globalShortcut.register('Alt+CommandOrControl+M', () => { - win.isVisible() ? win.hide() : win.show(); - }); +const clc = require('cli-color'); +const log = text => { + console.log(`${clc.blueBright('[globalShortcut.js]')} ${text}`); +}; + +export function registerGlobalShortcut(win, store) { + log('registerGlobalShortcut'); + let shortcuts = store.get('settings.shortcuts'); + if (shortcuts === undefined) { + shortcuts = defaultShortcuts; + } + + globalShortcut.register( + shortcuts.find(s => s.id === 'play').globalShortcut, + () => { + win.webContents.send('play'); + } + ); + globalShortcut.register( + shortcuts.find(s => s.id === 'next').globalShortcut, + () => { + win.webContents.send('next'); + } + ); + globalShortcut.register( + shortcuts.find(s => s.id === 'previous').globalShortcut, + () => { + win.webContents.send('previous'); + } + ); + globalShortcut.register( + shortcuts.find(s => s.id === 'increaseVolume').globalShortcut, + () => { + win.webContents.send('increaseVolume'); + } + ); + globalShortcut.register( + shortcuts.find(s => s.id === 'decreaseVolume').globalShortcut, + () => { + win.webContents.send('decreaseVolume'); + } + ); + globalShortcut.register( + shortcuts.find(s => s.id === 'like').globalShortcut, + () => { + win.webContents.send('like'); + } + ); + globalShortcut.register( + shortcuts.find(s => s.id === 'minimize').globalShortcut, + () => { + win.isVisible() ? win.hide() : win.show(); + } + ); } diff --git a/src/electron/ipcMain.js b/src/electron/ipcMain.js index 9a92524..b6b774f 100644 --- a/src/electron/ipcMain.js +++ b/src/electron/ipcMain.js @@ -1,10 +1,18 @@ import { app, dialog, globalShortcut, ipcMain } from 'electron'; import match from '@revincx/unblockneteasemusic'; import { registerGlobalShortcut } from '@/electron/globalShortcut'; +import cloneDeep from 'lodash/cloneDeep'; +import shortcuts from '@/utils/shortcuts'; +import { createMenu } from './menu'; + +const clc = require('cli-color'); +const log = text => { + console.log(`${clc.blueBright('[ipcMain.js]')} ${text}`); +}; const client = require('discord-rich-presence')('818936529484906596'); -export function initIpcMain(win, lrc, store) { +export function initIpcMain(win, store, lrc) { ipcMain.on('unblock-music', (event, track) => { // 兼容 unblockneteasemusic 所使用的 api 字段 track.alias = track.alia || []; @@ -23,7 +31,7 @@ export function initIpcMain(win, lrc, store) { event.returnValue = res; }) .catch(err => { - console.log('unblock music error: ', err); + log('unblock music error: ', err); event.returnValue = null; }); }); @@ -53,7 +61,7 @@ export function initIpcMain(win, lrc, store) { } }) .catch(err => { - console.log(err); + log(err); }); }); @@ -69,13 +77,10 @@ export function initIpcMain(win, lrc, store) { ipcMain.on('settings', (event, options) => { store.set('settings', options); - const isRegisterShortcut = globalShortcut.isRegistered( - 'Alt+CommandOrControl+P' - ); if (options.enableGlobalShortcut) { - !isRegisterShortcut && registerGlobalShortcut(win); + registerGlobalShortcut(win, store); } else { - isRegisterShortcut && globalShortcut.unregisterAll(); + globalShortcut.unregisterAll(); } }); @@ -112,13 +117,13 @@ export function initIpcMain(win, lrc, store) { proxyRules, }, () => { - console.log('finished setProxy'); + log('finished setProxy'); } ); }); ipcMain.on('removeProxy', (event, arg) => { - console.log('removeProxy'); + log('removeProxy'); win.webContents.session.setProxy({}); store.set('proxy', ''); }); @@ -130,4 +135,33 @@ export function initIpcMain(win, lrc, store) { ipcMain.on('toggleOSDLyrics', () => { lrc.toggleOSDLyrics(); }); + + ipcMain.on('switchGlobalShortcutStatusTemporary', (e, status) => { + if (status === 'disable') { + globalShortcut.unregisterAll(); + } else { + registerGlobalShortcut(win, store); + } + }); + + ipcMain.on('updateShortcut', (e, { id, type, shortcut }) => { + log('updateShortcut'); + let shortcuts = store.get('settings.shortcuts'); + let newShortcut = shortcuts.find(s => s.id === id); + newShortcut[type] = shortcut; + store.set('settings.shortcuts', shortcuts); + + createMenu(win, store); + globalShortcut.unregisterAll(); + registerGlobalShortcut(win, store); + }); + + ipcMain.on('restoreDefaultShortcuts', () => { + log('restoreDefaultShortcuts'); + store.set('settings.shortcuts', cloneDeep(shortcuts)); + + createMenu(win, store); + globalShortcut.unregisterAll(); + registerGlobalShortcut(win, store); + }); } diff --git a/src/electron/menu.js b/src/electron/menu.js index 347827d..005a167 100644 --- a/src/electron/menu.js +++ b/src/electron/menu.js @@ -1,10 +1,16 @@ +import defaultShortcuts from '@/utils/shortcuts'; const { app, Menu } = require('electron'); // import { autoUpdater } from "electron-updater" // const version = app.getVersion(); const isMac = process.platform === 'darwin'; -export function createMenu(win, lrc) { +export function createMenu(win, store, lrc) { + let shortcuts = store.get('settings.shortcuts'); + if (shortcuts === undefined) { + shortcuts = defaultShortcuts; + } + let menu = null; const template = [ ...(isMac @@ -19,7 +25,7 @@ export function createMenu(win, lrc) { { type: 'separator' }, { label: 'Preferences...', - accelerator: (() => (isMac ? 'CmdOrCtrl+,' : 'Ctrl+,'))(), + accelerator: 'CmdOrCtrl+,', click: () => { win.webContents.send('changeRouteTo', '/settings'); }, @@ -69,41 +75,42 @@ export function createMenu(win, lrc) { submenu: [ { label: 'Play', + accelerator: shortcuts.find(s => s.id === 'play').shortcut, click: () => { win.webContents.send('play'); }, }, { label: 'Next', - accelerator: 'CmdOrCtrl+Right', + accelerator: shortcuts.find(s => s.id === 'next').shortcut, click: () => { win.webContents.send('next'); }, }, { label: 'Previous', - accelerator: 'CmdOrCtrl+Left', + accelerator: shortcuts.find(s => s.id === 'previous').shortcut, click: () => { win.webContents.send('previous'); }, }, { label: 'Increase Volume', - accelerator: 'CmdOrCtrl+Up', + accelerator: shortcuts.find(s => s.id === 'increaseVolume').shortcut, click: () => { win.webContents.send('increaseVolume'); }, }, { label: 'Decrease Volume', - accelerator: 'CmdOrCtrl+Down', + accelerator: shortcuts.find(s => s.id === 'decreaseVolume').shortcut, click: () => { win.webContents.send('decreaseVolume'); }, }, { label: 'Like', - accelerator: 'CmdOrCtrl+L', + accelerator: shortcuts.find(s => s.id === 'like').shortcut, click: () => { win.webContents.send('like'); }, @@ -208,6 +215,7 @@ export function createMenu(win, lrc) { // ], // }); // } + menu = Menu.buildFromTemplate(template); Menu.setApplicationMenu(menu); } diff --git a/src/store/initLocalStorage.js b/src/store/initLocalStorage.js index b2d12e8..b0a9cdc 100644 --- a/src/store/initLocalStorage.js +++ b/src/store/initLocalStorage.js @@ -1,4 +1,5 @@ import { playlistCategories } from '@/utils/staticData'; +import shortcuts from '@/utils/shortcuts'; console.debug('[debug][initLocalStorage.js]'); const enabledPlaylistCategories = playlistCategories @@ -31,6 +32,7 @@ let localStorage = { server: '', port: null, }, + shortcuts: shortcuts, }, data: { user: {}, diff --git a/src/store/mutations.js b/src/store/mutations.js index 52bceac..b1ac036 100644 --- a/src/store/mutations.js +++ b/src/store/mutations.js @@ -1,4 +1,6 @@ import { disableScrolling, enableScrolling } from '@/utils/ui'; +import shortcuts from '@/utils/shortcuts'; +import cloneDeep from 'lodash/cloneDeep'; export default { updateLikedXXX(state, { name, data }) { @@ -58,4 +60,15 @@ export default { updateLastfm(state, session) { state.lastfm = session; }, + updateShortcut(state, { id, type, shortcut }) { + let newShortcut = state.settings.shortcuts.find(s => s.id === id); + newShortcut[type] = shortcut; + state.settings.shortcuts = state.settings.shortcuts.map(s => { + if (s.id !== id) return s; + return newShortcut; + }); + }, + restoreDefaultShortcuts(state) { + state.settings.shortcuts = cloneDeep(shortcuts); + }, }; diff --git a/src/utils/shortcuts.js b/src/utils/shortcuts.js new file mode 100644 index 0000000..1a6582c --- /dev/null +++ b/src/utils/shortcuts.js @@ -0,0 +1,47 @@ +// default shortcuts +// for more info, check https://www.electronjs.org/docs/api/accelerator + +export default [ + { + id: 'play', + name: '播放/暂停', + shortcut: 'CommandOrControl+P', + globalShortcut: 'Alt+CommandOrControl+P', + }, + { + id: 'next', + name: '下一首', + shortcut: 'CommandOrControl+Right', + globalShortcut: 'Alt+CommandOrControl+Right', + }, + { + id: 'previous', + name: '上一首', + shortcut: 'CommandOrControl+Left', + globalShortcut: 'Alt+CommandOrControl+Left', + }, + { + id: 'increaseVolume', + name: '增加音量', + shortcut: 'CommandOrControl+Up', + globalShortcut: 'Alt+CommandOrControl+Up', + }, + { + id: 'decreaseVolume', + name: '减少音量', + shortcut: 'CommandOrControl+Down', + globalShortcut: 'Alt+CommandOrControl+Down', + }, + { + id: 'like', + name: '喜欢歌曲', + shortcut: 'CommandOrControl+L', + globalShortcut: 'Alt+CommandOrControl+L', + }, + { + id: 'minimize', + name: '隐藏/显示播放器', + shortcut: 'CommandOrControl+M', + globalShortcut: 'Alt+CommandOrControl+M', + }, +]; diff --git a/src/utils/updateApp.js b/src/utils/updateApp.js index 9b3c4ba..aed91a0 100644 --- a/src/utils/updateApp.js +++ b/src/utils/updateApp.js @@ -8,6 +8,21 @@ const updateSetting = () => { ...parsedSettings, }; + if ( + settings.shortcuts.length !== initLocalStorage.settings.shortcuts.length + ) { + // 当新增 shortcuts 时 + const oldShortcutsId = settings.shortcuts.map(s => s.id); + const newShortcutsId = initLocalStorage.settings.shortcuts.filter( + s => oldShortcutsId.includes(s.id) === false + ); + newShortcutsId.map(id => { + settings.shortcuts.push( + initLocalStorage.settings.shortcuts.find(s => s.id === id) + ); + }); + } + localStorage.setItem('settings', JSON.stringify(settings)); }; diff --git a/src/views/settings.vue b/src/views/settings.vue index f11ba96..c890cb0 100644 --- a/src/views/settings.vue +++ b/src/views/settings.vue @@ -1,5 +1,5 @@