2022-04-09 00:19:58 +08:00
|
|
|
|
import { BrowserWindow, ipcMain, app } from 'electron'
|
2022-03-30 16:54:11 +08:00
|
|
|
|
import { db, Tables } from './db'
|
2022-04-16 21:14:03 +08:00
|
|
|
|
import { IpcChannels, IpcChannelsParams } from '../shared/IpcChannels'
|
2022-04-12 01:48:14 +08:00
|
|
|
|
import cache from './cache'
|
2022-04-16 13:30:25 +08:00
|
|
|
|
import log from './log'
|
2022-04-09 00:28:37 +08:00
|
|
|
|
import fs from 'fs'
|
2022-05-01 19:53:25 +08:00
|
|
|
|
import Store from 'electron-store'
|
|
|
|
|
import { TypedElectronStore } from './index'
|
2022-04-16 21:14:03 +08:00
|
|
|
|
import { APIs } from '../shared/CacheAPIs'
|
2022-04-20 20:25:20 +08:00
|
|
|
|
import { YPMTray } from './tray'
|
2022-04-29 21:16:46 +08:00
|
|
|
|
import { Thumbar } from './windowsTaskbar'
|
2022-04-16 21:14:03 +08:00
|
|
|
|
|
|
|
|
|
const on = <T extends keyof IpcChannelsParams>(
|
|
|
|
|
channel: T,
|
|
|
|
|
listener: (event: Electron.IpcMainEvent, params: IpcChannelsParams[T]) => void
|
|
|
|
|
) => {
|
|
|
|
|
ipcMain.on(channel, listener)
|
|
|
|
|
}
|
2022-03-30 16:54:11 +08:00
|
|
|
|
|
2022-04-29 21:16:46 +08:00
|
|
|
|
export function initIpcMain(
|
|
|
|
|
win: BrowserWindow | null,
|
|
|
|
|
tray: YPMTray | null,
|
2022-05-01 19:53:25 +08:00
|
|
|
|
thumbar: Thumbar | null,
|
|
|
|
|
store: Store<TypedElectronStore>
|
2022-04-29 21:16:46 +08:00
|
|
|
|
) {
|
2022-04-20 20:25:20 +08:00
|
|
|
|
initWindowIpcMain(win)
|
|
|
|
|
initTrayIpcMain(tray)
|
2022-04-29 21:16:46 +08:00
|
|
|
|
initTaskbarIpcMain(thumbar)
|
2022-05-01 19:53:25 +08:00
|
|
|
|
initStoreIpcMain(store)
|
|
|
|
|
initOtherIpcMain()
|
2022-04-20 20:25:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-09 00:28:37 +08:00
|
|
|
|
/**
|
|
|
|
|
* 处理需要win对象的事件
|
|
|
|
|
* @param {BrowserWindow} win
|
|
|
|
|
*/
|
2022-04-20 20:25:20 +08:00
|
|
|
|
function initWindowIpcMain(win: BrowserWindow | null) {
|
2022-04-16 21:14:03 +08:00
|
|
|
|
on(IpcChannels.Minimize, () => {
|
2022-04-09 00:28:37 +08:00
|
|
|
|
win?.minimize()
|
|
|
|
|
})
|
|
|
|
|
|
2022-04-16 21:14:03 +08:00
|
|
|
|
on(IpcChannels.MaximizeOrUnmaximize, () => {
|
2022-04-09 00:28:37 +08:00
|
|
|
|
if (!win) return
|
|
|
|
|
win.isMaximized() ? win.unmaximize() : win.maximize()
|
|
|
|
|
})
|
|
|
|
|
|
2022-04-16 21:14:03 +08:00
|
|
|
|
on(IpcChannels.Close, () => {
|
2022-04-09 00:28:37 +08:00
|
|
|
|
app.exit()
|
|
|
|
|
})
|
2022-03-30 16:54:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-20 20:25:20 +08:00
|
|
|
|
/**
|
|
|
|
|
* 处理需要tray对象的事件
|
|
|
|
|
* @param {YPMTray} tray
|
|
|
|
|
*/
|
|
|
|
|
function initTrayIpcMain(tray: YPMTray | null) {
|
|
|
|
|
on(IpcChannels.SetTrayTooltip, (e, { text }) => tray?.setTooltip(text))
|
|
|
|
|
|
2022-05-01 19:53:25 +08:00
|
|
|
|
on(IpcChannels.Like, (e, { isLiked }) => tray?.setLikeState(isLiked))
|
2022-04-20 20:25:20 +08:00
|
|
|
|
|
|
|
|
|
on(IpcChannels.Play, () => tray?.setPlayState(true))
|
|
|
|
|
on(IpcChannels.Pause, () => tray?.setPlayState(false))
|
|
|
|
|
|
|
|
|
|
on(IpcChannels.Repeat, (e, { mode }) => tray?.setRepeatMode(mode))
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-29 21:16:46 +08:00
|
|
|
|
/**
|
|
|
|
|
* 处理需要thumbar对象的事件
|
|
|
|
|
* @param {Thumbar} thumbar
|
|
|
|
|
*/
|
|
|
|
|
function initTaskbarIpcMain(thumbar: Thumbar | null) {
|
|
|
|
|
on(IpcChannels.Play, () => thumbar?.setPlayState(true))
|
|
|
|
|
on(IpcChannels.Pause, () => thumbar?.setPlayState(false))
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-09 00:28:37 +08:00
|
|
|
|
/**
|
2022-05-01 19:53:25 +08:00
|
|
|
|
* 处理需要electron-store的事件
|
|
|
|
|
* @param {Store<TypedElectronStore>} store
|
2022-04-09 00:28:37 +08:00
|
|
|
|
*/
|
2022-05-01 19:53:25 +08:00
|
|
|
|
function initStoreIpcMain(store: Store<TypedElectronStore>) {
|
|
|
|
|
/**
|
|
|
|
|
* 同步设置到Main
|
|
|
|
|
*/
|
|
|
|
|
on(IpcChannels.SyncSettings, (event, settings) => {
|
|
|
|
|
store.set('settings', settings)
|
|
|
|
|
})
|
|
|
|
|
}
|
2022-04-09 00:19:58 +08:00
|
|
|
|
|
2022-04-09 00:28:37 +08:00
|
|
|
|
/**
|
2022-05-01 19:53:25 +08:00
|
|
|
|
* 处理其他事件
|
2022-04-09 00:28:37 +08:00
|
|
|
|
*/
|
2022-05-01 19:53:25 +08:00
|
|
|
|
function initOtherIpcMain() {
|
|
|
|
|
/**
|
|
|
|
|
* 清除API缓存
|
|
|
|
|
*/
|
|
|
|
|
on(IpcChannels.ClearAPICache, () => {
|
|
|
|
|
db.truncate(Tables.Track)
|
|
|
|
|
db.truncate(Tables.Album)
|
|
|
|
|
db.truncate(Tables.Artist)
|
|
|
|
|
db.truncate(Tables.Playlist)
|
|
|
|
|
db.truncate(Tables.ArtistAlbum)
|
|
|
|
|
db.truncate(Tables.AccountData)
|
|
|
|
|
db.truncate(Tables.Audio)
|
|
|
|
|
db.vacuum()
|
|
|
|
|
})
|
2022-04-09 00:19:58 +08:00
|
|
|
|
|
2022-05-01 19:53:25 +08:00
|
|
|
|
/**
|
|
|
|
|
* Get API cache
|
|
|
|
|
*/
|
|
|
|
|
on(IpcChannels.GetApiCacheSync, (event, args) => {
|
|
|
|
|
const { api, query } = args
|
|
|
|
|
const data = cache.get(api, query)
|
|
|
|
|
event.returnValue = data
|
|
|
|
|
})
|
2022-04-12 01:48:14 +08:00
|
|
|
|
|
2022-05-01 19:53:25 +08:00
|
|
|
|
/**
|
|
|
|
|
* 缓存封面颜色
|
|
|
|
|
*/
|
|
|
|
|
on(IpcChannels.CacheCoverColor, (event, args) => {
|
|
|
|
|
const { id, color } = args
|
|
|
|
|
cache.set(APIs.CoverColor, { id, color })
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 导出tables到json文件,方便查看table大小(dev环境)
|
|
|
|
|
*/
|
|
|
|
|
if (process.env.NODE_ENV === 'development') {
|
|
|
|
|
on(IpcChannels.DevDbExportJson, () => {
|
|
|
|
|
const tables = [
|
|
|
|
|
Tables.ArtistAlbum,
|
|
|
|
|
Tables.Playlist,
|
|
|
|
|
Tables.Album,
|
|
|
|
|
Tables.Track,
|
|
|
|
|
Tables.Artist,
|
|
|
|
|
Tables.Audio,
|
|
|
|
|
Tables.AccountData,
|
|
|
|
|
Tables.Lyric,
|
|
|
|
|
]
|
|
|
|
|
tables.forEach(table => {
|
|
|
|
|
const data = db.findAll(table)
|
|
|
|
|
|
|
|
|
|
fs.writeFile(
|
|
|
|
|
`./tmp/${table}.json`,
|
|
|
|
|
JSON.stringify(data),
|
|
|
|
|
function (err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return console.log(err)
|
|
|
|
|
}
|
|
|
|
|
console.log('The file was saved!')
|
|
|
|
|
}
|
|
|
|
|
)
|
2022-04-09 00:28:37 +08:00
|
|
|
|
})
|
|
|
|
|
})
|
2022-05-01 19:53:25 +08:00
|
|
|
|
}
|
2022-04-09 00:19:58 +08:00
|
|
|
|
}
|