deemixer/index.js

110 lines
2.9 KiB
JavaScript
Raw Permalink Normal View History

const { app, BrowserWindow, ipcMain, shell, dialog, Menu, MenuItem } = require('electron')
const contextMenu = require('electron-context-menu')
const WindowStateManager = require('electron-window-state-manager')
2021-05-23 20:42:16 +00:00
const path = require('path')
2021-05-29 11:58:10 +00:00
const os = require('os')
const yargs = require('yargs/yargs')
const { hideBin } = require('yargs/helpers')
const argv = yargs(hideBin(process.argv)).options({
port: { type: 'string', default: '6595' },
host: { type: 'string', default: '127.0.0.1' },
dev: { type: 'boolean', default: false}
}).argv
2022-02-05 00:15:26 +00:00
const { DeemixServer }= require('./server/dist/app.js')
2021-05-23 20:42:16 +00:00
const PORT = process.env.DEEMIX_SERVER_PORT || argv.port
process.env.DEEMIX_SERVER_PORT = PORT
process.env.DEEMIX_HOST = argv.host
2021-05-23 20:42:16 +00:00
const server = new DeemixServer(argv.host, PORT, '/', false)
2022-02-05 00:15:26 +00:00
server.init()
let win
const windowState = new WindowStateManager('mainWindow', {
defaultWidth: 800,
defaultHeight: 600
})
2021-05-23 20:42:16 +00:00
function createWindow () {
win = new BrowserWindow({
width: windowState.width,
height: windowState.height,
x: windowState.x,
y: windowState.y,
2021-05-23 20:42:16 +00:00
useContentSize: true,
autoHideMenuBar: true,
2021-05-29 11:58:10 +00:00
icon: path.join(__dirname, os.platform() === 'win32' ? 'build/icon.ico' : 'build/64x64.png'),
2021-05-23 20:42:16 +00:00
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
win.setMenu(null)
if (argv.dev){
const menu = new Menu()
menu.append(new MenuItem({
label: 'DevTools',
submenu: [
{ role: 'reload', accelerator: 'f5', click: () => { win.reload() } },
{ role: 'devtools', accelerator: 'f12', click: () => { win.webContents.toggleDevTools() } }
]
}))
Menu.setApplicationMenu(menu)
2021-05-23 20:42:16 +00:00
}
// Open links in external browser
win.webContents.on('new-window', function(e, url) {
e.preventDefault()
shell.openExternal(url)
})
win.loadURL(`http://${argv.host}:${PORT}`)
if (windowState.maximized) {
win.maximize();
}
win.on('close', (event)=>{
windowState.saveState(win);
if (server.deemixApp.getSettings().settings.clearQueueOnExit){
server.deemixApp.cancelAllDownloads()
}
})
2021-05-23 20:42:16 +00:00
}
app.whenReady().then(() => {
createWindow()
contextMenu({
showLookUpSelection: false,
showSearchWithGoogle: false,
showInspectElement: false
})
2021-05-23 20:42:16 +00:00
// Only one istance per time
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
ipcMain.on('openDownloadsFolder', (event)=>{
2022-02-05 00:15:26 +00:00
const { downloadLocation } = server.deemixApp.getSettings().settings
shell.openPath(downloadLocation)
})
ipcMain.on('selectDownloadFolder', async (event, downloadLocation)=>{
let path = await dialog.showOpenDialog(win, {
defaultPath: downloadLocation,
properties: ["openDirectory", "createDirectory"]
})
if (path.filePaths[0]) win.webContents.send("downloadFolderSelected", path.filePaths[0])
})