2022-03-19 17:03:29 +08:00
|
|
|
import './preload' // must be first
|
2022-03-27 15:21:48 +08:00
|
|
|
import './sentry'
|
2022-04-02 00:45:20 +08:00
|
|
|
import './server'
|
2022-03-13 14:40:38 +08:00
|
|
|
import {
|
|
|
|
BrowserWindow,
|
|
|
|
BrowserWindowConstructorOptions,
|
|
|
|
app,
|
|
|
|
shell,
|
|
|
|
} from 'electron'
|
|
|
|
import Store from 'electron-store'
|
|
|
|
import { release } from 'os'
|
2022-04-20 20:25:20 +08:00
|
|
|
import { join } from 'path'
|
2022-04-16 13:30:25 +08:00
|
|
|
import log from './log'
|
2022-04-09 00:19:58 +08:00
|
|
|
import { initIpcMain } from './ipcMain'
|
2022-04-20 20:25:20 +08:00
|
|
|
import { createTray, YPMTray } from './tray'
|
|
|
|
import { IpcChannels } from '@/shared/IpcChannels'
|
2022-04-29 21:16:46 +08:00
|
|
|
import { createTaskbar, Thumbar } from './windowsTaskbar'
|
2022-05-13 19:30:13 +08:00
|
|
|
import { createMenu } from './menu'
|
2022-05-01 19:53:25 +08:00
|
|
|
import { Store as State, initialState } from '@/shared/store'
|
2022-05-13 01:47:55 +08:00
|
|
|
import { isDev, isWindows, isLinux, isMac } from './utils'
|
2022-03-13 14:40:38 +08:00
|
|
|
|
2022-05-01 19:53:25 +08:00
|
|
|
export interface TypedElectronStore {
|
2022-03-13 14:40:38 +08:00
|
|
|
window: {
|
|
|
|
width: number
|
|
|
|
height: number
|
|
|
|
x?: number
|
|
|
|
y?: number
|
|
|
|
}
|
2022-05-01 19:53:25 +08:00
|
|
|
settings: State['settings']
|
2022-03-13 14:40:38 +08:00
|
|
|
}
|
|
|
|
|
2022-04-16 21:14:03 +08:00
|
|
|
class Main {
|
|
|
|
win: BrowserWindow | null = null
|
2022-04-20 20:25:20 +08:00
|
|
|
tray: YPMTray | null = null
|
2022-04-29 21:16:46 +08:00
|
|
|
thumbar: Thumbar | null = null
|
2022-04-16 21:14:03 +08:00
|
|
|
store = new Store<TypedElectronStore>({
|
|
|
|
defaults: {
|
|
|
|
window: {
|
|
|
|
width: 1440,
|
|
|
|
height: 960,
|
|
|
|
},
|
2022-05-01 19:53:25 +08:00
|
|
|
settings: initialState.settings,
|
2022-03-13 14:40:38 +08:00
|
|
|
},
|
2022-04-16 21:14:03 +08:00
|
|
|
})
|
2022-03-13 14:40:38 +08:00
|
|
|
|
2022-04-16 21:14:03 +08:00
|
|
|
constructor() {
|
|
|
|
log.info('[index] Main process start')
|
2022-04-02 00:45:20 +08:00
|
|
|
|
2022-04-16 21:14:03 +08:00
|
|
|
// Disable GPU Acceleration for Windows 7
|
|
|
|
if (release().startsWith('6.1')) app.disableHardwareAcceleration()
|
2022-03-13 14:40:38 +08:00
|
|
|
|
2022-04-16 21:14:03 +08:00
|
|
|
// Set application name for Windows 10+ notifications
|
|
|
|
if (process.platform === 'win32') app.setAppUserModelId(app.getName())
|
2022-03-13 14:40:38 +08:00
|
|
|
|
2022-04-16 21:14:03 +08:00
|
|
|
// Make sure the app only run on one instance
|
|
|
|
if (!app.requestSingleInstanceLock()) {
|
|
|
|
app.quit()
|
|
|
|
process.exit(0)
|
2022-03-13 14:40:38 +08:00
|
|
|
}
|
2022-04-16 21:14:03 +08:00
|
|
|
|
|
|
|
app.whenReady().then(() => {
|
|
|
|
log.info('[index] App ready')
|
|
|
|
this.createWindow()
|
|
|
|
this.handleAppEvents()
|
|
|
|
this.handleWindowEvents()
|
2022-04-20 20:25:20 +08:00
|
|
|
this.createTray()
|
2022-05-13 19:30:13 +08:00
|
|
|
createMenu(this.win!)
|
2022-04-29 21:16:46 +08:00
|
|
|
this.createThumbar()
|
2022-05-01 19:53:25 +08:00
|
|
|
initIpcMain(this.win, this.tray, this.thumbar, this.store)
|
2022-04-16 21:14:03 +08:00
|
|
|
this.initDevTools()
|
|
|
|
})
|
2022-03-13 14:40:38 +08:00
|
|
|
}
|
|
|
|
|
2022-04-16 21:14:03 +08:00
|
|
|
initDevTools() {
|
|
|
|
if (!isDev || !this.win) return
|
2022-03-13 14:40:38 +08:00
|
|
|
|
2022-04-16 21:14:03 +08:00
|
|
|
// Install devtool extension
|
2022-03-17 14:45:04 +08:00
|
|
|
const {
|
|
|
|
default: installExtension,
|
|
|
|
REACT_DEVELOPER_TOOLS,
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
|
|
} = require('electron-devtools-installer')
|
2022-05-13 19:30:13 +08:00
|
|
|
installExtension(REACT_DEVELOPER_TOOLS.id).catch((err: unknown) =>
|
2022-04-16 13:30:25 +08:00
|
|
|
log.info('An error occurred: ', err)
|
2022-03-13 14:40:38 +08:00
|
|
|
)
|
2022-04-16 21:14:03 +08:00
|
|
|
|
|
|
|
this.win.webContents.openDevTools()
|
2022-03-13 14:40:38 +08:00
|
|
|
}
|
2022-04-16 21:14:03 +08:00
|
|
|
|
2022-04-20 20:25:20 +08:00
|
|
|
createTray() {
|
|
|
|
if (isWindows || isLinux || isDev) {
|
|
|
|
this.tray = createTray(this.win!)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-29 21:16:46 +08:00
|
|
|
createThumbar() {
|
|
|
|
if (isWindows) this.thumbar = createTaskbar(this.win!)
|
|
|
|
}
|
|
|
|
|
2022-04-16 21:14:03 +08:00
|
|
|
createWindow() {
|
|
|
|
const options: BrowserWindowConstructorOptions = {
|
|
|
|
title: 'YesPlayMusic',
|
|
|
|
webPreferences: {
|
|
|
|
preload: join(__dirname, 'rendererPreload.js'),
|
|
|
|
},
|
|
|
|
width: this.store.get('window.width'),
|
|
|
|
height: this.store.get('window.height'),
|
|
|
|
minWidth: 1080,
|
|
|
|
minHeight: 720,
|
|
|
|
vibrancy: 'fullscreen-ui',
|
|
|
|
titleBarStyle: 'hiddenInset',
|
|
|
|
frame: !(isWindows || isLinux), // TODO: 适用于linux下独立的启用开关
|
|
|
|
}
|
|
|
|
if (this.store.get('window')) {
|
|
|
|
options.x = this.store.get('window.x')
|
|
|
|
options.y = this.store.get('window.y')
|
|
|
|
}
|
|
|
|
this.win = new BrowserWindow(options)
|
|
|
|
|
|
|
|
// Web server
|
|
|
|
const url = `http://localhost:${process.env.ELECTRON_WEB_SERVER_PORT}`
|
|
|
|
this.win.loadURL(url)
|
|
|
|
|
|
|
|
// Make all links open with the browser, not with the application
|
|
|
|
this.win.webContents.setWindowOpenHandler(({ url }) => {
|
|
|
|
if (url.startsWith('https:')) shell.openExternal(url)
|
|
|
|
return { action: 'deny' }
|
|
|
|
})
|
2022-05-01 19:53:25 +08:00
|
|
|
|
|
|
|
this.disableCORS()
|
|
|
|
}
|
|
|
|
|
|
|
|
disableCORS() {
|
|
|
|
if (!this.win) return
|
2022-05-13 19:30:13 +08:00
|
|
|
const upsertKeyValue = (
|
|
|
|
object: Record<string, string | string[]>,
|
|
|
|
keyToChange: string,
|
|
|
|
value: string[]
|
|
|
|
) => {
|
|
|
|
if (!object) return
|
|
|
|
for (const key of Object.keys(object)) {
|
|
|
|
if (key.toLowerCase() === keyToChange.toLowerCase()) {
|
|
|
|
object[key] = value
|
2022-05-01 19:53:25 +08:00
|
|
|
}
|
|
|
|
}
|
2022-05-13 19:30:13 +08:00
|
|
|
object[keyToChange] = value
|
2022-05-01 19:53:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
this.win.webContents.session.webRequest.onBeforeSendHeaders(
|
|
|
|
(details, callback) => {
|
|
|
|
const { requestHeaders, url } = details
|
2022-05-13 19:30:13 +08:00
|
|
|
upsertKeyValue(requestHeaders, 'access-control-allow-origin', ['*'])
|
2022-05-01 19:53:25 +08:00
|
|
|
|
2022-05-13 19:30:13 +08:00
|
|
|
// 不加这几个 header 的话,使用 axios 加载 YouTube 音频会很慢
|
2022-05-01 19:53:25 +08:00
|
|
|
if (url.includes('googlevideo.com')) {
|
|
|
|
requestHeaders['Sec-Fetch-Mode'] = 'no-cors'
|
|
|
|
requestHeaders['Sec-Fetch-Dest'] = 'audio'
|
|
|
|
requestHeaders['Range'] = 'bytes=0-'
|
|
|
|
}
|
|
|
|
|
|
|
|
callback({ requestHeaders })
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
this.win.webContents.session.webRequest.onHeadersReceived(
|
|
|
|
(details, callback) => {
|
|
|
|
const { responseHeaders } = details
|
2022-05-13 19:30:13 +08:00
|
|
|
if (responseHeaders) {
|
|
|
|
upsertKeyValue(responseHeaders, 'access-control-allow-origin', ['*'])
|
|
|
|
upsertKeyValue(responseHeaders, 'access-control-allow-headers', ['*'])
|
|
|
|
}
|
2022-05-01 19:53:25 +08:00
|
|
|
callback({
|
|
|
|
responseHeaders,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
2022-03-13 14:40:38 +08:00
|
|
|
}
|
2022-04-09 00:19:58 +08:00
|
|
|
|
2022-04-16 21:14:03 +08:00
|
|
|
handleWindowEvents() {
|
|
|
|
if (!this.win) return
|
2022-04-09 00:19:58 +08:00
|
|
|
|
2022-04-16 21:14:03 +08:00
|
|
|
// Window maximize and minimize
|
|
|
|
this.win.on('maximize', () => {
|
2022-04-20 20:25:20 +08:00
|
|
|
this.win && this.win.webContents.send(IpcChannels.IsMaximized, true)
|
2022-04-16 21:14:03 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
this.win.on('unmaximize', () => {
|
2022-04-20 20:25:20 +08:00
|
|
|
this.win && this.win.webContents.send(IpcChannels.IsMaximized, false)
|
2022-04-16 21:14:03 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
// Save window position
|
|
|
|
const saveBounds = () => {
|
|
|
|
const bounds = this.win?.getBounds()
|
|
|
|
if (bounds) {
|
|
|
|
this.store.set('window', bounds)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.win.on('resized', saveBounds)
|
|
|
|
this.win.on('moved', saveBounds)
|
|
|
|
}
|
|
|
|
|
|
|
|
handleAppEvents() {
|
|
|
|
app.on('window-all-closed', () => {
|
|
|
|
this.win = null
|
2022-04-29 21:16:46 +08:00
|
|
|
if (!isMac) app.quit()
|
2022-04-16 21:14:03 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
app.on('second-instance', () => {
|
|
|
|
if (!this.win) return
|
|
|
|
// Focus on the main window if the user tried to open another
|
|
|
|
if (this.win.isMinimized()) this.win.restore()
|
|
|
|
this.win.focus()
|
|
|
|
})
|
|
|
|
|
|
|
|
app.on('activate', () => {
|
|
|
|
const allWindows = BrowserWindow.getAllWindows()
|
|
|
|
if (allWindows.length) {
|
|
|
|
allWindows[0].focus()
|
|
|
|
} else {
|
|
|
|
this.createWindow()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2022-04-09 00:19:58 +08:00
|
|
|
}
|
2022-04-16 21:14:03 +08:00
|
|
|
|
|
|
|
new Main()
|