新增快捷键设置

This commit is contained in:
lyswhut
2020-06-24 01:20:56 +08:00
parent 2424c76c50
commit 9b82cdfe53
58 changed files with 2254 additions and 1450 deletions
+12
View File
@@ -0,0 +1,12 @@
const { app } = require('electron')
const { init, unRegisterHotkeyAll } = require('./utils')
const { hotKey: HOT_KEY_EVENT_NAME } = require('../../events/_name')
global.appHotKey.state = {}
app.on('will-quit', unRegisterHotkeyAll)
global.lx_event.hotKey.on(HOT_KEY_EVENT_NAME.init, init)
require('./rendererEvent')
+28
View File
@@ -0,0 +1,28 @@
const {
mainHandle,
NAMES: {
hotKey: ipcHotKeyNames,
},
} = require('../../../common/ipc')
const { init, registerHotkey, unRegisterHotkey, unRegisterHotkeyAll } = require('./utils')
mainHandle(ipcHotKeyNames.set_config, async(event, { action, data, source }) => {
switch (action) {
case 'config':
global.lx_event.hotKey.saveConfig(data, source)
return true
case 'enable':
data ? init(true) : unRegisterHotkeyAll()
return true
case 'register':
return registerHotkey(data)
case 'unregister':
return unRegisterHotkey(data)
}
})
mainHandle(ipcHotKeyNames.status, async() => global.appHotKey.state)
mainHandle(ipcHotKeyNames.enable, async(event, flag) => {
flag ? init() : unRegisterHotkeyAll()
})
+61
View File
@@ -0,0 +1,61 @@
const { globalShortcut } = require('electron')
const { log } = require('../../../common/utils')
const handleKeyDown = key => {
if (!global.appHotKey.enable) return
global.lx_event.hotKey.keyDown({ type: 'global', key })
}
const transformedKeyRxp = /(^|\+)[a-z]/g
const transformedKey = key => {
if (key.includes('arrow')) key = key.replace(/arrow/g, '')
return key.replace('mod', 'CommandOrControl').replace(transformedKeyRxp, l => l.toUpperCase())
}
exports.registerHotkey = ({ key, info }) => {
if (global.appHotKey.state[key] && global.appHotKey.state[key].status) return true
let transKey = transformedKey(key)
// console.log('Register key:', transKey)
if (!global.appHotKey.state[key]) {
global.appHotKey.state[key] = {
status: false,
info: null,
}
}
global.appHotKey.state[key].info = info
let status = global.appHotKey.state[key].status = globalShortcut.isRegistered(transKey) ? false : globalShortcut.register(transKey, () => {
handleKeyDown(key)
})
return status
}
exports.unRegisterHotkey = key => {
let transKey = transformedKey(key)
// console.log('Unregister key:', transKey)
globalShortcut.unregister(transKey)
delete global.appHotKey.state[key]
}
exports.unRegisterHotkeyAll = () => {
global.appHotKey.state = {}
globalShortcut.unregisterAll()
}
exports.handleKeyDown = handleKeyDown
exports.transformedKey = transformedKey
const handleRegisterHotkey = data => {
let ret = exports.registerHotkey(data)
if (!ret) log.info('Register hot key failed:', data.key)
}
exports.init = (isForce = false) => {
exports.unRegisterHotkeyAll()
if (!isForce && !global.appHotKey.config.global.enable) return
global.appHotKey.state = {}
// console.log(global.appHotKey.config.global.keys)
for (const key of Object.keys(global.appHotKey.config.global.keys)) {
handleRegisterHotkey({ key, info: global.appHotKey.config.global.keys[key] })
}
}
+1
View File
@@ -1,3 +1,4 @@
require('./appMenu')
require('./winLyric')
require('./tray')
require('./hotKey')
+12 -12
View File
@@ -9,22 +9,22 @@ global.lx_event.common.on(COMMON_EVENT_NAME.config, sourceName => {
isEnableTray = global.appSetting.tray.isToTray
global.appSetting.tray.isToTray ? createTray() : destroyTray()
}
createMenu(global.modals.tray)
createMenu(global.modules.tray)
})
const createTray = () => {
if ((global.modals.tray && !global.modals.tray.isDestroyed()) || !global.appSetting.tray || !global.appSetting.tray.isShow) return
if ((global.modules.tray && !global.modules.tray.isDestroyed()) || !global.appSetting.tray || !global.appSetting.tray.isShow) return
const iconPath = path.join(global.__static, 'images/tray', isWin ? 'trayTemplate@2x.ico' : 'trayTemplate.png')
// 托盘
global.modals.tray = new Tray(iconPath)
global.modules.tray = new Tray(iconPath)
global.modals.tray.setToolTip('洛雪音乐助手')
createMenu(global.modals.tray)
global.modals.tray.setIgnoreDoubleClickEvents(true)
global.modals.tray.on('click', () => {
const mainWindow = global.modals.mainWindow
global.modules.tray.setToolTip('洛雪音乐助手')
createMenu(global.modules.tray)
global.modules.tray.setIgnoreDoubleClickEvents(true)
global.modules.tray.on('click', () => {
const mainWindow = global.modules.mainWindow
if (!mainWindow) return
mainWindow.isVisible()
? mainWindow.focus()
@@ -33,13 +33,13 @@ const createTray = () => {
}
const destroyTray = () => {
if (!global.modals.tray) return
global.modals.tray.destroy()
global.modals.tray = null
if (!global.modules.tray) return
global.modules.tray.destroy()
global.modules.tray = null
}
const createMenu = tray => {
if (!global.modals.tray || !isWin) return
if (!global.modules.tray || !isWin) return
let menu = []
menu.push(global.appSetting.desktopLyric.enable ? {
label: '关闭桌面歌词',
+30 -8
View File
@@ -1,5 +1,6 @@
const { common: COMMON_EVENT_NAME, winLyric: WIN_LYRIC_EVENT_NAME, mainWindow: MAIN_WINDOW_EVENT_NAME } = require('../../events/_name')
const { common: COMMON_EVENT_NAME, winLyric: WIN_LYRIC_EVENT_NAME, hotKey: HOT_KEY_EVENT_NAME, mainWindow: MAIN_WINDOW_EVENT_NAME } = require('../../events/_name')
const { mainSend, NAMES: { winLyric: ipcWinLyricNames } } = require('../../../common/ipc')
const { desktop_lyric } = require('../../../common/hotKey')
let isLock = null
let isEnable = null
@@ -14,19 +15,19 @@ const setLrcConfig = isForceSet => {
global.lx_event.winLyric.close()
}
}
if (global.modals.lyricWindow) {
mainSend(global.modals.lyricWindow, ipcWinLyricNames.set_lyric_config, desktopLyric)
if (global.modules.lyricWindow) {
mainSend(global.modules.lyricWindow, ipcWinLyricNames.set_lyric_config, desktopLyric)
if (isForceSet || isLock != desktopLyric.isLock) {
isLock = desktopLyric.isLock
if (desktopLyric.isLock) {
global.modals.lyricWindow.setIgnoreMouseEvents(true, { forward: false })
global.modules.lyricWindow.setIgnoreMouseEvents(true, { forward: false })
} else {
global.modals.lyricWindow.setIgnoreMouseEvents(false)
global.modules.lyricWindow.setIgnoreMouseEvents(false)
}
}
if (isForceSet || isAlwaysOnTop != desktopLyric.isAlwaysOnTop) {
isAlwaysOnTop = desktopLyric.isAlwaysOnTop
global.modals.lyricWindow.setAlwaysOnTop(desktopLyric.isAlwaysOnTop, 'screen-saver')
global.modules.lyricWindow.setAlwaysOnTop(desktopLyric.isAlwaysOnTop, 'screen-saver')
}
}
}
@@ -37,6 +38,27 @@ global.lx_event.common.on(COMMON_EVENT_NAME.config, name => {
global.lx_event.winLyric.on(WIN_LYRIC_EVENT_NAME.inited, () => setLrcConfig(true))
global.lx_event.mainWindow.on(MAIN_WINDOW_EVENT_NAME.setLyricInfo, info => {
if (!global.modals.lyricWindow) return
mainSend(global.modals.lyricWindow, ipcWinLyricNames.set_lyric_info, info)
if (!global.modules.lyricWindow) return
mainSend(global.modules.lyricWindow, ipcWinLyricNames.set_lyric_info, info)
})
global.lx_event.hotKey.on(HOT_KEY_EVENT_NAME.keyDown, ({ type, key }) => {
let info = global.appHotKey.config.global.keys[key]
if (!info || info.type != WIN_LYRIC_EVENT_NAME.name) return
let desktopLyricSetting = JSON.parse(JSON.stringify(global.appSetting.desktopLyric))
let settingKey
switch (info.action) {
case desktop_lyric.toggle_visible.action:
settingKey = 'enable'
break
case desktop_lyric.toggle_lock.action:
settingKey = 'isLock'
break
case desktop_lyric.toggle_always_top.action:
settingKey = 'isAlwaysOnTop'
break
}
desktopLyricSetting[settingKey] = !desktopLyricSetting[settingKey]
global.lx_event.common.setAppConfig({ desktopLyric: desktopLyricSetting }, null)
})
+8 -8
View File
@@ -36,7 +36,7 @@ const winEvent = lyricWindow => {
// })
lyricWindow.on('closed', () => {
lyricWindow = global.modals.lyricWindow = null
lyricWindow = global.modules.lyricWindow = null
})
@@ -62,7 +62,7 @@ const winEvent = lyricWindow => {
lyricWindow.once('ready-to-show', () => {
lyricWindow.show()
let config = global.appSetting.desktopLyric
global.modals.lyricWindow.setBounds({
global.modules.lyricWindow.setBounds({
height: config.height,
width: config.width,
y: config.y,
@@ -74,7 +74,7 @@ const winEvent = lyricWindow => {
const createWindow = () => {
if (global.modals.lyricWindow) return
if (global.modules.lyricWindow) return
if (!global.appSetting.desktopLyric.enable) return
// const windowSizeInfo = getWindowSizeInfo(global.appSetting)
const { x, y, width, height, isAlwaysOnTop } = global.appSetting.desktopLyric
@@ -84,7 +84,7 @@ const createWindow = () => {
/**
* Initial window options
*/
global.modals.lyricWindow = new BrowserWindow({
global.modules.lyricWindow = new BrowserWindow({
height: height > screenHeight ? screenHeight : height,
width: width > screenWidth ? screenWidth : width,
x: Math.max(-8, screenWidth < (width + x) ? screenWidth - width : x),
@@ -110,13 +110,13 @@ const createWindow = () => {
},
})
global.modals.lyricWindow.loadURL(winURL)
global.modules.lyricWindow.loadURL(winURL)
winEvent(global.modals.lyricWindow)
winEvent(global.modules.lyricWindow)
// mainWindow.webContents.openDevTools()
}
const closeWindow = () => {
if (!global.modals.lyricWindow) return
global.modals.lyricWindow.close()
if (!global.modules.lyricWindow) return
global.modules.lyricWindow.close()
}
+5 -5
View File
@@ -10,8 +10,8 @@ const {
const { winLyric: WIN_LYRIC_EVENT_NAME } = require('../../events/_name')
mainOn(ipcWinLyricNames.get_lyric_info, (event, action) => {
if (!global.modals.mainWindow) return
mainSend(global.modals.mainWindow, ipcMainWindowNames.get_lyric_info, {
if (!global.modules.mainWindow) return
mainSend(global.modules.mainWindow, ipcMainWindowNames.get_lyric_info, {
name: ipcWinLyricNames.set_lyric_info,
modal: 'lyricWindow',
action,
@@ -32,8 +32,8 @@ let winY
let wasW
let wasY
mainOn(ipcWinLyricNames.set_win_bounds, (event, { x = 0, y = 0, w = 0, h = 0 }) => {
if (!global.modals.lyricWindow) return
bounds = global.modals.lyricWindow.getBounds()
if (!global.modules.lyricWindow) return
bounds = global.modules.lyricWindow.getBounds()
wasW = global.envParams.workAreaSize.width
wasY = global.envParams.workAreaSize.height + 8
@@ -71,5 +71,5 @@ mainOn(ipcWinLyricNames.set_win_bounds, (event, { x = 0, y = 0, w = 0, h = 0 })
}
// console.log(bounds)
global.modals.lyricWindow.setBounds(bounds)
global.modules.lyricWindow.setBounds(bounds)
})