YesPlayMusic/src/main/index.ts

155 lines
3.6 KiB
TypeScript
Raw Normal View History

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-03-19 17:03:29 +08:00
import path, { join } from 'path'
2022-03-13 14:40:38 +08:00
import logger from './logger'
import { initIpcMain } from './ipcMain'
2022-03-13 14:40:38 +08:00
const isWindows = process.platform === 'win32'
const isMac = process.platform === 'darwin'
const isLinux = process.platform === 'linux'
const isDev = process.env.NODE_ENV === 'development'
2022-03-13 14:40:38 +08:00
2022-04-02 00:45:20 +08:00
logger.info('[index] Main process start')
2022-03-13 14:40:38 +08:00
// Disable GPU Acceleration for Windows 7
if (release().startsWith('6.1')) app.disableHardwareAcceleration()
// Set application name for Windows 10+ notifications
if (process.platform === 'win32') app.setAppUserModelId(app.getName())
if (!app.requestSingleInstanceLock()) {
app.quit()
process.exit(0)
}
interface TypedElectronStore {
window: {
width: number
height: number
x?: number
y?: number
}
}
const store = new Store<TypedElectronStore>({
defaults: {
window: {
width: 1440,
height: 960,
},
},
})
let win: BrowserWindow | null = null
async function createWindow() {
// Create window
2022-03-13 14:40:38 +08:00
const options: BrowserWindowConstructorOptions = {
title: 'Main window',
webPreferences: {
2022-04-02 00:45:20 +08:00
preload: join(__dirname, 'rendererPreload.js'),
2022-03-13 14:40:38 +08:00
},
width: store.get('window.width'),
height: store.get('window.height'),
minWidth: 1080,
minHeight: 720,
vibrancy: 'fullscreen-ui',
titleBarStyle: 'hiddenInset',
frame: !(isWindows || isLinux), // TODO: 适用于linux下独立的启用开关
2022-03-13 14:40:38 +08:00
}
if (store.get('window')) {
options.x = store.get('window.x')
options.y = store.get('window.y')
}
win = new BrowserWindow(options)
// Web server
2022-04-02 00:45:20 +08:00
const url = `http://localhost:${process.env.ELECTRON_WEB_SERVER_PORT}`
win.loadURL(url)
if (isDev) {
2022-03-13 14:40:38 +08:00
win.webContents.openDevTools()
}
// Make all links open with the browser, not with the application
win.webContents.setWindowOpenHandler(({ url }) => {
if (url.startsWith('https:')) shell.openExternal(url)
return { action: 'deny' }
})
// Save window position
const saveBounds = () => {
const bounds = win?.getBounds()
if (bounds) {
store.set('window', bounds)
}
}
win.on('resized', saveBounds)
win.on('moved', saveBounds)
}
app.whenReady().then(async () => {
2022-04-02 00:45:20 +08:00
logger.info('[index] App ready')
2022-03-13 14:40:38 +08:00
createWindow()
handleWindowEvents()
initIpcMain(win)
2022-03-13 14:40:38 +08:00
// Install devtool extension
if (isDev) {
2022-03-17 14:45:04 +08:00
const {
default: installExtension,
REACT_DEVELOPER_TOOLS,
REDUX_DEVTOOLS,
// eslint-disable-next-line @typescript-eslint/no-var-requires
} = require('electron-devtools-installer')
2022-03-13 14:40:38 +08:00
installExtension(REACT_DEVELOPER_TOOLS.id).catch(err =>
2022-03-19 17:03:29 +08:00
logger.info('An error occurred: ', err)
2022-03-13 14:40:38 +08:00
)
installExtension(REDUX_DEVTOOLS.id).catch(err =>
2022-03-19 17:03:29 +08:00
logger.info('An error occurred: ', err)
2022-03-13 14:40:38 +08:00
)
}
})
app.on('window-all-closed', () => {
win = null
if (process.platform !== 'darwin') app.quit()
})
app.on('second-instance', () => {
if (win) {
// Focus on the main window if the user tried to open another
if (win.isMinimized()) win.restore()
win.focus()
}
})
app.on('activate', () => {
const allWindows = BrowserWindow.getAllWindows()
if (allWindows.length) {
allWindows[0].focus()
} else {
createWindow()
}
})
const handleWindowEvents = () => {
win?.on('maximize', () => {
win?.webContents.send('is-maximized', true)
})
win?.on('unmaximize', () => {
win?.webContents.send('is-maximized', false)
})
}