deemix-gui/index.js

96 lines
2.3 KiB
JavaScript
Raw Normal View History

const { app, BrowserWindow, globalShortcut, ipcMain, shell, dialog, Menu} = require('electron')
const contextMenu = require('electron-context-menu')
const WindowStateManager = require('electron-window-state-manager')
2021-05-23 22:42:16 +02:00
const path = require('path')
2021-05-29 13:58:10 +02:00
const os = require('os')
2021-05-23 22:42:16 +02:00
const PORT = process.env.PORT || '6595'
function isDev() {
return process.argv[2] == '--dev';
}
let win
const windowState = new WindowStateManager('mainWindow', {
defaultWidth: 800,
defaultHeight: 600
})
2021-05-23 22:42:16 +02:00
function createWindow () {
require('./server/dist/app.js')
win = new BrowserWindow({
width: windowState.width,
height: windowState.height,
x: windowState.x,
y: windowState.y,
2021-05-23 22:42:16 +02:00
useContentSize: true,
autoHideMenuBar: true,
2021-05-29 13:58:10 +02:00
icon: path.join(__dirname, os.platform() === 'win32' ? 'build/icon.ico' : 'build/64x64.png'),
2021-05-23 22:42:16 +02:00
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
win.setMenu(null)
if (isDev()){
globalShortcut.register('f5', function() {
win.reload()
})
2021-05-23 22:42:16 +02:00
globalShortcut.register('f12', function() {
win.webContents.openDevTools()
})
2021-05-23 22:42:16 +02:00
}
// Open links in external browser
win.webContents.on('new-window', function(e, url) {
e.preventDefault()
shell.openExternal(url)
})
win.loadURL(`http://localhost:${PORT}`)
if (windowState.maximized) {
win.maximize();
}
win.on('close', (event)=>{
windowState.saveState(win);
})
2021-05-23 22:42:16 +02:00
}
app.whenReady().then(() => {
createWindow()
contextMenu({
showLookUpSelection: false,
showSearchWithGoogle: false,
showInspectElement: false
})
2021-05-23 22:42:16 +02: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)=>{
const { downloadLocation } = require('./server/dist/main.js').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])
})