lx-music-desktop/src/main/index.js

120 lines
2.8 KiB
JavaScript
Raw Normal View History

2020-04-03 12:13:54 +00:00
const { app, BrowserWindow, shell } = require('electron')
2019-08-16 09:27:23 +00:00
const path = require('path')
2019-09-04 05:08:32 +00:00
// 单例应用程序
if (!app.requestSingleInstanceLock()) {
app.quit()
return
}
app.on('second-instance', (event, argv, cwd) => {
if (mainWindow) {
if (mainWindow.isMinimized()) {
mainWindow.restore()
} else if (mainWindow.isVisible()) {
mainWindow.focus()
} else {
mainWindow.show()
}
2019-09-04 05:08:32 +00:00
} else {
app.quit()
}
})
const isDev = global.isDev = process.env.NODE_ENV !== 'production'
2020-03-29 06:48:02 +00:00
app.on('web-contents-created', (event, contents) => {
contents.on('will-navigate', (event, navigationUrl) => {
if (isDev) return console.log('navigation to url:', navigationUrl)
2020-03-29 06:48:02 +00:00
event.preventDefault()
})
contents.on('new-window', async(event, navigationUrl) => {
event.preventDefault()
if (/^devtools/.test(navigationUrl)) return
console.log(navigationUrl)
await shell.openExternal(navigationUrl)
})
})
2020-04-03 13:19:44 +00:00
// https://github.com/electron/electron/issues/22691
app.commandLine.appendSwitch('wm-window-animations-disabled')
2020-02-10 10:12:38 +00:00
2020-02-10 09:25:58 +00:00
// https://github.com/electron/electron/issues/18397
2020-02-10 10:12:38 +00:00
app.allowRendererProcessReuse = !isDev
2020-02-10 09:25:58 +00:00
const { getAppSetting, parseEnv, getWindowSizeInfo } = require('./utils')
global.envParams = parseEnv()
require('../common/error')
2019-08-16 09:27:23 +00:00
require('./events')
require('./rendererEvents')
const winEvent = require('./rendererEvents/winEvent')
2019-08-16 09:27:23 +00:00
const autoUpdate = require('./utils/autoUpdate')
const { isMac, isLinux } = require('../common/utils')
2019-08-16 09:27:23 +00:00
let mainWindow
let winURL
if (isDev) {
// eslint-disable-next-line no-undef
global.__static = __static
winURL = 'http://localhost:9080'
2019-08-16 09:27:23 +00:00
} else {
global.__static = path.join(__dirname, '/static')
winURL = `file://${__dirname}/index.html`
}
function createWindow() {
const windowSizeInfo = getWindowSizeInfo(global.appSetting)
2019-08-16 09:27:23 +00:00
/**
* Initial window options
*/
mainWindow = global.mainWindow = new BrowserWindow({
2019-10-21 17:25:03 +00:00
height: windowSizeInfo.height,
2019-08-16 09:27:23 +00:00
useContentSize: true,
2019-10-21 17:25:03 +00:00
width: windowSizeInfo.width,
2019-08-16 09:27:23 +00:00
frame: false,
transparent: !isLinux && !global.envParams.nt,
enableRemoteModule: false,
2019-08-23 11:28:54 +00:00
// icon: path.join(global.__static, isWin ? 'icons/256x256.ico' : 'icons/512x512.png'),
2019-08-16 09:27:23 +00:00
resizable: false,
maximizable: false,
fullscreenable: false,
2020-03-28 02:50:31 +00:00
show: false,
2019-08-16 09:27:23 +00:00
webPreferences: {
// contextIsolation: true,
webSecurity: !isDev,
nodeIntegration: true,
},
})
mainWindow.loadURL(winURL)
2020-03-02 07:50:05 +00:00
winEvent(mainWindow)
2019-08-16 09:27:23 +00:00
// mainWindow.webContents.openDevTools()
if (!isDev) autoUpdate()
2019-08-16 09:27:23 +00:00
}
function init() {
global.appSetting = getAppSetting()
createWindow()
global.lx_event.tray.create()
2019-08-23 11:28:54 +00:00
}
app.on('ready', init)
2019-08-16 09:27:23 +00:00
app.on('activate', () => {
if (mainWindow === null) return init()
2019-08-16 09:27:23 +00:00
})
app.on('window-all-closed', () => {
if (isMac) {
global.lx_event.tray.destroy()
} else {
app.quit()
2019-08-16 09:27:23 +00:00
}
})
require('./modules')