新增桌面歌词

This commit is contained in:
lyswhut
2020-06-11 21:16:59 +08:00
parent 25fc0226aa
commit 2a81d41c2c
107 changed files with 6935 additions and 1543 deletions
+17
View File
@@ -0,0 +1,17 @@
const parseEnv = () => {
const params = {}
const rx = /^-\w+/
for (let param of process.argv) {
if (!rx.test(param)) continue
param = param.substring(1)
let index = param.indexOf('=')
if (index < 0) {
params[param] = true
} else {
params[param.substring(0, index)] = param.substring(index + 1)
}
}
return params
}
global.envParams.cmdParams = parseEnv()
+4
View File
@@ -0,0 +1,4 @@
global.envParams = {}
require('./cmdParams')
require('./screen')
+11
View File
@@ -0,0 +1,11 @@
const { app, screen } = require('electron')
const initScreenParams = () => {
global.envParams.workAreaSize = screen.getPrimaryDisplay().workAreaSize
}
app.on('ready', () => {
screen.on('display-metrics-changed', initScreenParams)
initScreenParams()
})
+9
View File
@@ -0,0 +1,9 @@
const { common: COMMON_EVENT_NAME, mainWindow: MAIN_WINDOW_EVENT_NAME } = require('./events/_name')
const { mainSend, NAMES: { mainWindow: ipcMainWindowNames } } = require('./../common/ipc')
global.lx_event.common.on(COMMON_EVENT_NAME.config, name => {
if (MAIN_WINDOW_EVENT_NAME.name === name) return
if (global.modals.mainWindow) mainSend(global.modals.mainWindow, ipcMainWindowNames.set_config, global.appSetting)
})
+18
View File
@@ -0,0 +1,18 @@
const { EventEmitter } = require('events')
const { common: COMMON_EVENT_NAME } = require('./_name')
const { updateSetting } = require('../utils')
class Common extends EventEmitter {
initSetting() {
this.emit(COMMON_EVENT_NAME.initConfig)
this.emit(COMMON_EVENT_NAME.config, null)
}
setAppConfig(config, name) {
if (config) updateSetting(config)
this.emit(COMMON_EVENT_NAME.config, name)
}
}
module.exports = Common
+10
View File
@@ -0,0 +1,10 @@
const { EventEmitter } = require('events')
const { mainWindow: MAIN_WINDOW_EVENT_NAME } = require('./_name')
class MainWindow extends EventEmitter {
setLyricInfo(info) {
this.emit(MAIN_WINDOW_EVENT_NAME.setLyricInfo, info)
}
}
module.exports = MainWindow
+18
View File
@@ -1,5 +1,23 @@
exports.common = {
initConfig: 'initConfig',
config: 'config',
}
exports.mainWindow = {
name: 'mainWindow',
setLyricInfo: 'setLyricInfo',
destroy: 'destroy',
}
exports.tray = {
name: 'tray',
create: 'create',
destroy: 'destroy',
}
exports.winLyric = {
name: 'winLyric',
create: 'create',
close: 'close',
inited: 'inited',
}
+7 -1
View File
@@ -1,5 +1,11 @@
global.lx_event = {}
const Tray = require('./tray')
const Common = require('./Common')
const MainWindow = require('./MainWindow')
const Tray = require('./Tray')
const WinLyric = require('./WinLyric')
if (!global.lx_event.common) global.lx_event.common = new Common()
if (!global.lx_event.mainWindow) global.lx_event.mainWindow = new MainWindow()
if (!global.lx_event.tray) global.lx_event.tray = new Tray()
if (!global.lx_event.winLyric) global.lx_event.winLyric = new WinLyric()
+19
View File
@@ -0,0 +1,19 @@
const { EventEmitter } = require('events')
const { winLyric: WIN_LYRIC_EVENT_NAME } = require('./_name')
// const { updateSetting } = require('../utils')
class WinLyric extends EventEmitter {
create() {
this.emit(WIN_LYRIC_EVENT_NAME.create)
}
close() {
this.emit(WIN_LYRIC_EVENT_NAME.close)
}
inited() {
this.emit(WIN_LYRIC_EVENT_NAME.inited)
}
}
module.exports = WinLyric
+23 -22
View File
@@ -6,14 +6,15 @@ if (!app.requestSingleInstanceLock()) {
app.quit()
return
}
if (!global.modals) global.modals = {}
app.on('second-instance', (event, argv, cwd) => {
if (global.mainWindow) {
if (global.mainWindow.isMinimized()) {
global.mainWindow.restore()
} else if (global.mainWindow.isVisible()) {
global.mainWindow.focus()
if (global.modals.mainWindow) {
if (global.modals.mainWindow.isMinimized()) {
global.modals.mainWindow.restore()
} else if (global.modals.mainWindow.isVisible()) {
global.modals.mainWindow.focus()
} else {
global.mainWindow.show()
global.modals.mainWindow.show()
}
} else {
app.quit()
@@ -21,11 +22,10 @@ app.on('second-instance', (event, argv, cwd) => {
})
const isDev = global.isDev = process.env.NODE_ENV !== 'production'
require('./env')
const { navigationUrlWhiteList } = require('../common/config')
const { getAppSetting, parseEnv, getWindowSizeInfo } = require('./utils')
const { isMac, isLinux } = require('../common/utils')
global.envParams = parseEnv()
const { getWindowSizeInfo } = require('./utils')
const { isMac, isLinux, initSetting } = require('../common/utils')
// https://github.com/electron/electron/issues/22691
@@ -67,6 +67,7 @@ app.on('web-contents-created', (event, contents) => {
require('../common/error')
require('./events')
require('./event')
require('./rendererEvents')
const winEvent = require('./rendererEvents/winEvent')
const autoUpdate = require('./utils/autoUpdate')
@@ -88,7 +89,7 @@ function createWindow() {
/**
* Initial window options
*/
global.mainWindow = new BrowserWindow({
global.modals.mainWindow = new BrowserWindow({
height: windowSizeInfo.height,
useContentSize: true,
width: windowSizeInfo.width,
@@ -107,32 +108,32 @@ function createWindow() {
},
})
global.mainWindow.loadURL(winURL)
global.modals.mainWindow.loadURL(winURL)
winEvent(global.mainWindow)
winEvent(global.modals.mainWindow)
// mainWindow.webContents.openDevTools()
if (!isDev) autoUpdate()
}
function init() {
global.appSetting = getAppSetting()
global.appSetting = initSetting()
global.lx_event.common.initSetting()
createWindow()
global.lx_event.tray.create()
}
app.on('ready', init)
app.on('activate', () => {
if (global.mainWindow) {
if (global.mainWindow.isMinimized()) {
global.mainWindow.restore()
} else if (global.mainWindow.isVisible()) {
global.mainWindow.focus()
if (global.modals.mainWindow) {
if (global.modals.mainWindow.isMinimized()) {
global.modals.mainWindow.restore()
} else if (global.modals.mainWindow.isVisible()) {
global.modals.mainWindow.focus()
} else {
global.mainWindow.show()
global.modals.mainWindow.show()
}
} else if (global.mainWindow === null) {
} else if (global.modals.mainWindow === null) {
init()
}
})
+1
View File
@@ -1,2 +1,3 @@
require('./appMenu')
require('./winLyric')
require('./tray')
+61 -24
View File
@@ -1,27 +1,30 @@
const { app, Tray, Menu } = require('electron')
const { isWin } = require('../../common/utils')
const { tray: TRAY_EVENT_NAME } = require('../events/_name')
const { tray: TRAY_EVENT_NAME, common: COMMON_EVENT_NAME } = require('../events/_name')
const path = require('path')
global.lx_event.tray.on(TRAY_EVENT_NAME.create, () => {
createTray()
})
global.lx_event.tray.on(TRAY_EVENT_NAME.destroy, () => {
destroyTray()
let isEnableTray = null
global.lx_event.common.on(COMMON_EVENT_NAME.config, sourceName => {
if (sourceName === TRAY_EVENT_NAME.name) return
if (isEnableTray !== global.appSetting.tray.isToTray) {
isEnableTray = global.appSetting.tray.isToTray
global.appSetting.tray.isToTray ? createTray() : destroyTray()
}
createMenu(global.modals.tray)
})
const createTray = () => {
if ((global.tray && !global.tray.isDestroyed()) || !global.appSetting.tray || !global.appSetting.tray.isShow) return
if ((global.modals.tray && !global.modals.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.tray = new Tray(iconPath)
global.modals.tray = new Tray(iconPath)
global.tray.setToolTip('洛雪音乐助手')
if (isWin) createMenu(global.tray)
global.tray.setIgnoreDoubleClickEvents(true)
global.tray.on('click', () => {
const mainWindow = global.mainWindow
global.modals.tray.setToolTip('洛雪音乐助手')
createMenu(global.modals.tray)
global.modals.tray.setIgnoreDoubleClickEvents(true)
global.modals.tray.on('click', () => {
const mainWindow = global.modals.mainWindow
if (!mainWindow) return
mainWindow.isVisible()
? mainWindow.focus()
@@ -30,21 +33,55 @@ const createTray = () => {
}
const destroyTray = () => {
if (!global.tray) return
global.tray.destroy()
global.tray = null
if (!global.modals.tray) return
global.modals.tray.destroy()
global.modals.tray = null
}
const createMenu = tray => {
const contextMenu = Menu.buildFromTemplate([
{
label: '退出',
click() {
global.isQuitting = true
app.quit()
},
if (!global.modals.tray || !isWin) return
let menu = []
menu.push(global.appSetting.desktopLyric.enable ? {
label: '关闭桌面歌词',
click() {
global.lx_event.common.setAppConfig({ desktopLyric: { enable: false } }, TRAY_EVENT_NAME.name)
},
])
} : {
label: '开启桌面歌词',
click() {
global.lx_event.common.setAppConfig({ desktopLyric: { enable: true } }, TRAY_EVENT_NAME.name)
},
})
menu.push(global.appSetting.desktopLyric.isLock ? {
label: '解锁桌面歌词',
click() {
global.lx_event.common.setAppConfig({ desktopLyric: { isLock: false } }, TRAY_EVENT_NAME.name)
},
} : {
label: '锁定桌面歌词',
click() {
global.lx_event.common.setAppConfig({ desktopLyric: { isLock: true } }, TRAY_EVENT_NAME.name)
},
})
menu.push(global.appSetting.desktopLyric.isAlwaysOnTop ? {
label: '取消置顶',
click() {
global.lx_event.common.setAppConfig({ desktopLyric: { isAlwaysOnTop: false } }, TRAY_EVENT_NAME.name)
},
} : {
label: '置顶歌词',
click() {
global.lx_event.common.setAppConfig({ desktopLyric: { isAlwaysOnTop: true } }, TRAY_EVENT_NAME.name)
},
})
menu.push({
label: '退出',
click() {
global.isQuitting = true
app.quit()
},
})
const contextMenu = Menu.buildFromTemplate(menu)
tray.setContextMenu(contextMenu)
}
+42
View File
@@ -0,0 +1,42 @@
const { common: COMMON_EVENT_NAME, winLyric: WIN_LYRIC_EVENT_NAME, mainWindow: MAIN_WINDOW_EVENT_NAME } = require('../../events/_name')
const { mainSend, NAMES: { winLyric: ipcWinLyricNames } } = require('../../../common/ipc')
let isLock = null
let isEnable = null
let isAlwaysOnTop = null
const setLrcConfig = isForceSet => {
let desktopLyric = global.appSetting.desktopLyric
if (isEnable != desktopLyric.enable) {
isEnable = desktopLyric.enable
if (desktopLyric.enable) {
global.lx_event.winLyric.create()
} else {
global.lx_event.winLyric.close()
}
}
if (global.modals.lyricWindow) {
mainSend(global.modals.lyricWindow, ipcWinLyricNames.set_lyric_config, desktopLyric)
if (isForceSet || isLock != desktopLyric.isLock) {
isLock = desktopLyric.isLock
if (desktopLyric.isLock) {
global.modals.lyricWindow.setIgnoreMouseEvents(true, { forward: false })
} else {
global.modals.lyricWindow.setIgnoreMouseEvents(false)
}
}
if (isForceSet || isAlwaysOnTop != desktopLyric.isAlwaysOnTop) {
isAlwaysOnTop = desktopLyric.isAlwaysOnTop
global.modals.lyricWindow.setAlwaysOnTop(desktopLyric.isAlwaysOnTop, 'screen-saver')
}
}
}
global.lx_event.common.on(COMMON_EVENT_NAME.config, name => {
if (WIN_LYRIC_EVENT_NAME.name === name) return
setLrcConfig(false)
})
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)
})
+122
View File
@@ -0,0 +1,122 @@
const { BrowserWindow } = require('electron')
const { winLyric: WIN_LYRIC_EVENT_NAME } = require('../../events/_name')
const { debounce } = require('../../../common/utils')
require('./event')
require('./rendererEvent')
global.lx_event.winLyric.on(WIN_LYRIC_EVENT_NAME.create, () => {
createWindow()
})
global.lx_event.winLyric.on(WIN_LYRIC_EVENT_NAME.close, () => {
closeWindow()
})
let winURL = global.isDev ? 'http://localhost:9081/lyric.html' : `file://${__dirname}/lyric.html`
const setLyricsConfig = debounce(config => {
// if (x != null) bounds.x = x
// if (y != null) bounds.y = y
// if (width != null) bounds.width = width
// if (height != null) bounds.height = height
global.lx_event.common.setAppConfig({ desktopLyric: config }, WIN_LYRIC_EVENT_NAME.name)
}, 500)
const winEvent = lyricWindow => {
// let bounds
// lyricWindow.on('close', event => {
// if (global.isQuitting || !global.appSetting.tray.isToTray || (!isWin && !global.isTrafficLightClose)) {
// lyricWindow.setProgressBar(-1)
// return
// }
// if (global.isTrafficLightClose) global.isTrafficLightClose = false
// event.preventDefault()
// lyricWindow.hide()
// })
lyricWindow.on('closed', () => {
lyricWindow = global.modals.lyricWindow = null
})
lyricWindow.on('move', event => {
// bounds = lyricWindow.getBounds()
// console.log(bounds)
setLyricsConfig(lyricWindow.getBounds())
})
lyricWindow.on('resize', event => {
// bounds = lyricWindow.getBounds()
// console.log(bounds)
setLyricsConfig(lyricWindow.getBounds())
})
// lyricWindow.on('restore', () => {
// lyricWindow.webContents.send('restore')
// })
// lyricWindow.on('focus', () => {
// lyricWindow.webContents.send('focus')
// })
lyricWindow.once('ready-to-show', () => {
lyricWindow.show()
let config = global.appSetting.desktopLyric
global.modals.lyricWindow.setBounds({
height: config.height,
width: config.width,
y: config.y,
x: config.x,
})
global.lx_event.winLyric.inited()
})
}
const createWindow = () => {
if (global.modals.lyricWindow) return
if (!global.appSetting.desktopLyric.enable) return
// const windowSizeInfo = getWindowSizeInfo(global.appSetting)
const { x, y, width, height, isAlwaysOnTop } = global.appSetting.desktopLyric
let { width: screenWidth, height: screenHeight } = global.envParams.workAreaSize
screenWidth += 16
screenHeight += 16
/**
* Initial window options
*/
global.modals.lyricWindow = new BrowserWindow({
height: height > screenHeight ? screenHeight : height,
width: width > screenWidth ? screenWidth : width,
x: Math.max(-8, screenWidth < (width + x) ? screenWidth - width : x),
y: Math.max(-8, screenHeight < (height + y) ? screenHeight - height : y),
minWidth: 300,
minHeight: 300,
useContentSize: true,
frame: false,
transparent: true,
enableRemoteModule: false,
// icon: path.join(global.__static, isWin ? 'icons/256x256.ico' : 'icons/512x512.png'),
resizable: false,
minimizable: false,
maximizable: false,
fullscreenable: false,
show: false,
alwaysOnTop: isAlwaysOnTop,
skipTaskbar: true,
webPreferences: {
// contextIsolation: true,
webSecurity: !global.isDev,
nodeIntegration: true,
},
})
global.modals.lyricWindow.loadURL(winURL)
winEvent(global.modals.lyricWindow)
// mainWindow.webContents.openDevTools()
}
const closeWindow = () => {
if (!global.modals.lyricWindow) return
global.modals.lyricWindow.close()
}
@@ -0,0 +1,75 @@
const {
mainOn,
mainHandle,
mainSend,
NAMES: {
mainWindow: ipcMainWindowNames,
winLyric: ipcWinLyricNames,
},
} = require('../../../common/ipc')
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, {
name: ipcWinLyricNames.set_lyric_info,
modal: 'lyricWindow',
action,
})
})
mainOn(ipcWinLyricNames.set_lyric_config, (event, config) => {
global.lx_event.common.setAppConfig({ desktopLyric: config }, WIN_LYRIC_EVENT_NAME.name)
})
mainHandle(ipcWinLyricNames.get_lyric_config, async() => {
return global.appSetting.desktopLyric
})
let bounds
let winX
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()
wasW = global.envParams.workAreaSize.width
wasY = global.envParams.workAreaSize.height + 8
bounds.width = w + bounds.width
bounds.height = h + bounds.height
if (bounds.width > wasW - 8) {
bounds.width = wasW - 8
} else if (bounds.width < 300) {
bounds.width = 300
}
if (bounds.height > wasY) {
bounds.height = wasY + 8
} else if (bounds.height < 120) {
bounds.height = 120
}
if (x != 0) {
winX = bounds.x + x
if (winX > wasW - bounds.width + 8) {
winX = wasW - bounds.width + 8
} else if (winX < -8) {
winX = -8
}
bounds.x = winX
}
if (y != 0) {
winY = bounds.y + y
if (winY > wasY - bounds.height) {
winY = wasY - bounds.height
} else if (winY < -8) {
winY = -8
}
bounds.y = winY
}
// console.log(bounds)
global.modals.lyricWindow.setBounds(bounds)
})
+2 -2
View File
@@ -1,9 +1,9 @@
const { mainOn } = require('../../common/ipc')
const { mainOn, NAMES: { mainWindow: ipcMainWindowNames } } = require('../../common/ipc')
const { app } = require('electron')
const { name: defaultName } = require('../../../package.json')
mainOn('appName', (event, params) => {
mainOn(ipcMainWindowNames.set_app_name, (event, params) => {
if (params == null) {
app.setName(defaultName)
} else {
+4 -4
View File
@@ -1,8 +1,8 @@
const { mainHandle } = require('../../common/ipc')
const { mainHandle, NAMES: { mainWindow: ipcMainWindowNames } } = require('../../common/ipc')
mainHandle('clearCache', async(event, options) => {
if (!global.mainWindow) throw new Error('mainwindow is undefined')
return global.mainWindow.webContents.session.clearCache()
mainHandle(ipcMainWindowNames.clear_cache, async(event, options) => {
if (!global.modals.mainWindow) throw new Error('mainWindow is undefined')
return global.modals.mainWindow.webContents.session.clearCache()
})
+4 -4
View File
@@ -1,7 +1,7 @@
const { mainHandle } = require('../../common/ipc')
const { mainHandle, NAMES: { mainWindow: ipcMainWindowNames } } = require('../../common/ipc')
mainHandle('getCacheSize', async(event, options) => {
if (!global.mainWindow) throw new Error('mainwindow is undefined')
return global.mainWindow.webContents.session.getCacheSize()
mainHandle(ipcMainWindowNames.get_cache_size, async(event, options) => {
if (!global.modals.mainWindow) throw new Error('mainWindow is undefined')
return global.modals.mainWindow.webContents.session.getCacheSize()
})
+3 -3
View File
@@ -1,6 +1,6 @@
const { mainHandle } = require('../../common/ipc')
const { mainHandle, NAMES: { mainWindow: ipcMainWindowNames } } = require('../../common/ipc')
mainHandle('getEnvParams', async(event, options) => {
return global.envParams
mainHandle(ipcMainWindowNames.get_env_params, async(event, options) => {
return global.envParams.cmdParams
})
+2 -2
View File
@@ -11,8 +11,8 @@ require('./clearCache')
require('./getCacheSize')
require('./setIgnoreMouseEvent')
require('./getEnvParams')
require('./tray')
require('./updateSetting')
require('./setAppSetting')
require('./setLyricInfo')
require('./xm_verify')
+3 -3
View File
@@ -1,6 +1,6 @@
const { inflate } = require('zlib')
const iconv = require('iconv-lite')
const { mainHandle } = require('../../common/ipc')
const { mainHandle, NAMES: { mainWindow: ipcMainWindowNames } } = require('../../common/ipc')
const handleInflate = data => new Promise((resolve, reject) => {
inflate(data, (err, result) => {
@@ -38,8 +38,8 @@ const decodeLyric = async(buf, isGetLyricx) => {
return iconv.decode(Buffer.from(output), 'gb18030')
}
mainHandle('kw_decodeLyric', async(event, { lrcBase64, isGetLyricx }) => {
if (!global.mainWindow) throw new Error('mainwindow is undefined')
mainHandle(ipcMainWindowNames.handle_kw_decode_lyric, async(event, { lrcBase64, isGetLyricx }) => {
if (!global.modals.mainWindow) throw new Error('mainWindow is undefined')
const lrc = await decodeLyric(Buffer.from(lrcBase64, 'base64'), isGetLyricx)
return Buffer.from(lrc).toString('base64')
})
+2 -2
View File
@@ -1,6 +1,6 @@
const { mainOn } = require('../../common/ipc')
const { mainOn, NAMES: { mainWindow: ipcMainWindowNames } } = require('../../common/ipc')
const { setMeta } = require('../utils/musicMeta')
mainOn('setMusicMeta', (event, { filePath, meta }) => {
mainOn(ipcMainWindowNames.set_music_meta, (event, { filePath, meta }) => {
setMeta(filePath, meta)
})
+3 -3
View File
@@ -1,9 +1,9 @@
const { mainOn } = require('../../common/ipc')
const { mainOn, NAMES: { mainWindow: ipcMainWindowNames } } = require('../../common/ipc')
mainOn('progress', (event, params) => {
mainOn(ipcMainWindowNames.progress, (event, params) => {
// console.log(params)
global.mainWindow && global.mainWindow.setProgressBar(params.status, {
global.modals.mainWindow && global.modals.mainWindow.setProgressBar(params.status, {
mode: params.mode || 'normal',
})
})
+3 -3
View File
@@ -1,10 +1,10 @@
const request = require('request')
const { mainOn } = require('../../common/ipc')
const { mainOn, NAMES: { mainWindow: ipcMainWindowNames } } = require('../../common/ipc')
const tasks = []
mainOn('request', (event, options) => {
mainOn(ipcMainWindowNames.handle_request, (event, options) => {
// console.log(args)
if (!options) return
let index = fetchData(options, (err, resp) => {
@@ -19,7 +19,7 @@ mainOn('request', (event, options) => {
event.returnValue = index
})
mainOn('cancelRequest', (event, index) => {
mainOn(ipcMainWindowNames.cancel_request, (event, index) => {
if (index == null) return
let r = tasks[index]
if (r == null) return
+2 -2
View File
@@ -1,7 +1,7 @@
const { mainOn } = require('../../common/ipc')
const { mainOn, NAMES: { mainWindow: ipcMainWindowNames } } = require('../../common/ipc')
mainOn('restartWindow', (event, name) => {
mainOn(ipcMainWindowNames.restart_window, (event, name) => {
console.log(name)
switch (name) {
case 'main':
+4 -4
View File
@@ -1,8 +1,8 @@
const { mainHandle } = require('../../common/ipc')
const { mainHandle, NAMES: { mainWindow: ipcMainWindowNames } } = require('../../common/ipc')
const { dialog } = require('electron')
mainHandle('selectDir', async(event, options) => {
if (!global.mainWindow) throw new Error('mainwindow is undefined')
return dialog.showOpenDialog(global.mainWindow, options)
mainHandle(ipcMainWindowNames.select_dir, async(event, options) => {
if (!global.modals.mainWindow) throw new Error('mainWindow is undefined')
return dialog.showOpenDialog(global.modals.mainWindow, options)
})
+7
View File
@@ -0,0 +1,7 @@
const { mainWindow: MAIN_WINDOW_EVENT_NAME } = require('../events/_name')
const { mainOn, NAMES: { mainWindow: ipcMainWindowNames } } = require('../../common/ipc')
mainOn(ipcMainWindowNames.set_app_setting, (event, config) => {
if (!config) return
global.lx_event.common.setAppConfig(config, MAIN_WINDOW_EVENT_NAME.name)
})
@@ -1,8 +1,8 @@
const { mainOn } = require('../../common/ipc')
const { mainOn, NAMES: { mainWindow: ipcMainWindowNames } } = require('../../common/ipc')
mainOn('setIgnoreMouseEvents', (event, isIgnored) => {
if (!global.mainWindow) return
mainOn(ipcMainWindowNames.set_ignore_mouse_events, (event, isIgnored) => {
if (!global.modals.mainWindow) return
isIgnored
? global.mainWindow.setIgnoreMouseEvents(true, { forward: true })
: global.mainWindow.setIgnoreMouseEvents(false)
? global.modals.mainWindow.setIgnoreMouseEvents(true, { forward: true })
: global.modals.mainWindow.setIgnoreMouseEvents(false)
})
+10
View File
@@ -0,0 +1,10 @@
const { mainOn, mainSend, NAMES: { mainWindow: ipcMainWindowNames } } = require('../../common/ipc')
mainOn(ipcMainWindowNames.set_lyric_info, (event, info) => {
if (info.info == null) {
global.lx_event.mainWindow.setLyricInfo(info)
return
}
mainSend(global.modals[info.info.modal], info.info.name, info)
})
+4 -4
View File
@@ -1,7 +1,7 @@
const { mainOn } = require('../../common/ipc')
const { mainOn, NAMES: { mainWindow: ipcMainWindowNames } } = require('../../common/ipc')
mainOn('setWindowSize', (event, options) => {
if (!global.mainWindow) return
global.mainWindow.setBounds(options)
mainOn(ipcMainWindowNames.set_window_size, (event, options) => {
if (!global.modals.mainWindow) return
global.modals.mainWindow.setBounds(options)
})
+4 -4
View File
@@ -1,8 +1,8 @@
const { mainHandle } = require('../../common/ipc')
const { mainHandle, NAMES: { mainWindow: ipcMainWindowNames } } = require('../../common/ipc')
const { dialog } = require('electron')
mainHandle('showSaveDialog', async(event, options) => {
if (!global.mainWindow) throw new Error('mainwindow is undefined')
return dialog.showSaveDialog(global.mainWindow, options)
mainHandle(ipcMainWindowNames.show_save_dialog, async(event, options) => {
if (!global.modals.mainWindow) throw new Error('mainWindow is undefined')
return dialog.showSaveDialog(global.modals.mainWindow, options)
})
+9 -9
View File
@@ -1,18 +1,18 @@
const { app } = require('electron')
const { mainOn } = require('../../common/ipc')
const { mainOn, NAMES: { mainWindow: ipcMainWindowNames } } = require('../../common/ipc')
mainOn('min', event => {
if (global.mainWindow) {
global.mainWindow.minimize()
mainOn(ipcMainWindowNames.min, event => {
if (global.modals.mainWindow) {
global.modals.mainWindow.minimize()
}
})
mainOn('max', event => {
if (global.mainWindow) {
global.mainWindow.maximize()
mainOn(ipcMainWindowNames.max, event => {
if (global.modals.mainWindow) {
global.modals.mainWindow.maximize()
}
})
mainOn('close', (event, isForce) => {
mainOn(ipcMainWindowNames.close, (event, isForce) => {
if (isForce) return app.exit(0)
global.isTrafficLightClose = true
if (global.mainWindow) global.mainWindow.close()
if (global.modals.mainWindow) global.modals.mainWindow.close()
})
-12
View File
@@ -1,12 +0,0 @@
// const { app } = require('electron')
const { mainOn } = require('../../common/ipc')
mainOn('changeTray', (event, setting) => {
global.appSetting.tray = setting
if (setting.isToTray) {
global.lx_event.tray.create()
} else {
global.lx_event.tray.destroy()
}
})
-7
View File
@@ -1,7 +0,0 @@
const { mainOn } = require('../../common/ipc')
mainOn('updateAppSetting', (event, setting) => {
if (!setting) return
global.appSetting = setting
})
+4 -2
View File
@@ -1,4 +1,5 @@
const { isWin } = require('../../common/utils')
const { mainSend, NAMES: { mainWindow: ipcMainWindowNames } } = require('../../common/ipc')
global.isQuitting = false
global.isTrafficLightClose = false // 是否点击软件上的关闭按钮关闭
@@ -6,6 +7,7 @@ module.exports = mainWindow => {
mainWindow.on('close', event => {
if (global.isQuitting || !global.appSetting.tray.isToTray || (!isWin && !global.isTrafficLightClose)) {
mainWindow.setProgressBar(-1)
global.lx_event.winLyric.close()
return
}
@@ -15,14 +17,14 @@ module.exports = mainWindow => {
})
mainWindow.on('closed', () => {
mainWindow = global.mainWindow = null
mainWindow = global.modals.mainWindow = null
})
// mainWindow.on('restore', () => {
// mainWindow.webContents.send('restore')
// })
mainWindow.on('focus', () => {
mainWindow.webContents.send('focus')
mainSend(mainWindow, ipcMainWindowNames.focus)
})
mainWindow.once('ready-to-show', () => {
@@ -1,5 +1,5 @@
const { BrowserView } = require('electron')
const { mainHandle } = require('../../../common/ipc')
const { mainHandle, NAMES: { mainWindow: ipcMainWindowNames } } = require('../../../common/ipc')
const { getWindowSizeInfo } = require('../../utils')
let view
@@ -9,16 +9,16 @@ let rejectFn
const closeView = async() => {
if (!view) return
// await view.webContents.session.clearCache()
if (global.mainWindow) global.mainWindow.removeBrowserView(view)
if (global.modals.mainWindow) global.modals.mainWindow.removeBrowserView(view)
await view.webContents.session.clearStorageData()
view.destroy()
view = null
}
mainHandle('xm_verify_open', (event, url) => new Promise((resolve, reject) => {
if (!global.mainWindow) return reject(new Error('mainwindow is undefined'))
mainHandle(ipcMainWindowNames.handle_xm_verify_open, (event, url) => new Promise((resolve, reject) => {
if (!global.modals.mainWindow) return reject(new Error('mainWindow is undefined'))
if (view) {
global.mainWindow.removeBrowserView(view)
global.modals.mainWindow.removeBrowserView(view)
view.destroy()
}
@@ -48,7 +48,7 @@ mainHandle('xm_verify_open', (event, url) => new Promise((resolve, reject) => {
})
})
global.mainWindow.setBrowserView(view)
global.modals.mainWindow.setBrowserView(view)
const windowSizeInfo = getWindowSizeInfo(global.appSetting)
view.setBounds({ x: (windowSizeInfo.width - 380) / 2, y: ((windowSizeInfo.height - 320 + 52) / 2), width: 380, height: 320 })
view.webContents.loadURL(url, {
@@ -57,7 +57,7 @@ mainHandle('xm_verify_open', (event, url) => new Promise((resolve, reject) => {
// view.webContents.openDevTools()
}))
mainHandle('xm_verify_close', async() => {
mainHandle(ipcMainWindowNames.handle_xm_verify_close, async() => {
await closeView()
if (!rejectFn) return
if (!isActioned) rejectFn(new Error('canceled verify'))
@@ -1,5 +1,5 @@
const { BrowserWindow } = require('electron')
const { mainHandle } = require('../../../common/ipc')
const { mainHandle, NAMES: { mainWindow: ipcMainWindowNames } } = require('../../../common/ipc')
const { getWindowSizeInfo } = require('../../utils')
let win
@@ -7,7 +7,7 @@ let win
const closeWin = async() => {
if (!win) return
// await win.webContents.session.clearCache()
// if (global.mainWindow) global.mainWindow.removeBrowserView(win)
// if (global.modals.mainWindow) global.modals.mainWindow.removeBrowserView(win)
if (win.isDestroyed()) {
win = null
return
@@ -17,16 +17,16 @@ const closeWin = async() => {
win = null
}
mainHandle('xm_verify_open', (event, url) => new Promise((resolve, reject) => {
if (!global.mainWindow) return reject(new Error('mainwindow is undefined'))
mainHandle(ipcMainWindowNames.handle_xm_verify_open, (event, url) => new Promise((resolve, reject) => {
if (!global.modals.mainWindow) return reject(new Error('mainWindow is undefined'))
if (win) win.destroy()
let isActioned = false
const mainWindowSizeInfo = global.mainWindow.getBounds()
const mainWindowSizeInfo = global.modals.mainWindow.getBounds()
const windowSizeInfo = getWindowSizeInfo(global.appSetting)
win = new BrowserWindow({
parent: global.mainWindow,
parent: global.modals.mainWindow,
width: 460,
height: 370,
resizable: false,
@@ -72,6 +72,6 @@ mainHandle('xm_verify_open', (event, url) => new Promise((resolve, reject) => {
// win.webContents.openDevTools()
}))
mainHandle('xm_verify_close', async() => {
mainHandle(ipcMainWindowNames.handle_xm_verify_close, async() => {
await closeWin()
})
+12 -10
View File
@@ -1,6 +1,6 @@
const { log } = require('../../common/utils')
const { autoUpdater } = require('electron-updater')
const { mainOn } = require('../../common/ipc')
const { mainOn, mainSend, NAMES: { mainWindow: ipcMainWindowNames } } = require('../../common/ipc')
autoUpdater.logger = log
// autoUpdater.autoDownload = false
@@ -61,9 +61,10 @@ function sendStatusToWindow(text) {
let waitEvent = []
const handleSendEvent = action => {
if (global.mainWindow) {
if (global.modals.mainWindow) {
setTimeout(() => { // 延迟发送事件,过早发送可能渲染进程还没启动完成
global.mainWindow.webContents.send(action.type, action.info)
if (!global.modals.mainWindow) return
mainSend(global.modals.mainWindow, action.type, action.info)
}, 2000)
} else {
waitEvent.push(action)
@@ -75,7 +76,8 @@ module.exports = () => {
if (waitEvent.length) {
waitEvent.forEach((event, index) => {
setTimeout(() => { // 延迟发送事件,过早发送可能渲染进程还没启动完成
global.mainWindow.webContents.send(event.type, event.info)
if (!global.modals.mainWindow) return
mainSend(global.modals.mainWindow, event.type, event.info)
}, 2000 * (index + 1))
})
waitEvent = []
@@ -89,28 +91,28 @@ module.exports = () => {
})
autoUpdater.on('update-available', info => {
sendStatusToWindow('Update available.')
handleSendEvent({ type: 'update-available', info })
handleSendEvent({ type: ipcMainWindowNames.update_available, info })
})
autoUpdater.on('update-not-available', info => {
sendStatusToWindow('Update not available.')
handleSendEvent({ type: 'update-not-available', info })
handleSendEvent({ type: ipcMainWindowNames.update_not_available, info })
})
autoUpdater.on('error', err => {
sendStatusToWindow('Error in auto-updater.')
handleSendEvent({ type: 'update-error', info: err.message })
handleSendEvent({ type: ipcMainWindowNames.update_error, info: err.message })
})
autoUpdater.on('download-progress', progressObj => {
let log_message = 'Download speed: ' + progressObj.bytesPerSecond
log_message = log_message + ' - Downloaded ' + progressObj.percent + '%'
log_message = log_message + ' (' + progressObj.transferred + '/' + progressObj.total + ')'
sendStatusToWindow(log_message)
handleSendEvent({ type: 'update-progress', info: progressObj })
handleSendEvent({ type: ipcMainWindowNames.update_progress, info: progressObj })
})
autoUpdater.on('update-downloaded', info => {
sendStatusToWindow('Update downloaded.')
handleSendEvent({ type: 'update-downloaded', info })
handleSendEvent({ type: ipcMainWindowNames.update_downloaded, info })
})
mainOn('quit-update', () => {
mainOn(ipcMainWindowNames.quit_update, () => {
global.isQuitting = true
setTimeout(() => {
+8 -24
View File
@@ -1,35 +1,19 @@
const Store = require('electron-store')
const { windowSizeList } = require('../../common/config')
const { objectDeepMerge } = require('../../common/utils')
exports.getWindowSizeInfo = ({ windowSizeId = 1 } = {}) => {
return windowSizeList.find(i => i.id === windowSizeId) || windowSizeList[0]
}
exports.getAppSetting = () => {
let electronStore = new Store()
const defaultSetting = {
windowSizeId: 1,
tray: {
isShow: false,
isToTray: false,
},
}
return Object.assign(defaultSetting, electronStore.get('setting') || {})
const electronStore_config = new Store({
name: 'config',
})
return electronStore_config.get('setting')
}
exports.parseEnv = () => {
const params = {}
const rx = /^-\w+/
for (let param of process.argv) {
if (!rx.test(param)) continue
param = param.substring(1)
let index = param.indexOf('=')
if (index < 0) {
params[param] = true
} else {
params[param.substring(0, index)] = param.substring(index + 1)
}
}
return params
}
exports.updateSetting = settings => {
objectDeepMerge(global.appSetting, settings)
}