diff --git a/src/main/index.ts b/src/main/index.ts index 1216049..5c06b61 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -12,7 +12,8 @@ import { deleteGame, getAllGames, getGame -} from './services/Database.service'; +} from './services/Database.service.js'; +import { appSettings } from './settings.js'; let mainWindow: BrowserWindow; let splash: BrowserWindow; @@ -44,10 +45,19 @@ function createWindow(): void { splash.center(); splash.show(); - // TODO show window according to the app settings mainWindow.on('ready-to-show', () => { splash.close(); - mainWindow.show() + + // Show window according to user preferences + if (appSettings.get('startMinimized')) { + if (appSettings.get('enableTray') && appSettings.get('minimizeToTray')) { + mainWindow.hide() + } else { + mainWindow.minimize() + } + } else { + mainWindow.show() + } }) mainWindow.webContents.setWindowOpenHandler((details) => { @@ -89,53 +99,64 @@ app.whenReady().then(() => { if (BrowserWindow.getAllWindows().length === 0) createWindow() }) - // TODO close/hide window according to the app settings // Prevent window from closing and quitting app // Instead make close simply hide main window // Clicking on tray icon will bring back main window mainWindow.on('close', event => { - event.preventDefault() - mainWindow.hide() + if (appSettings.get('enableTray') && appSettings.get('closeToTray')) { + event.preventDefault() + mainWindow.hide() + } + }) + + // Minimizes window to the tray icon if it exists + mainWindow.on('minimize', () => { + if (appSettings.get('enableTray') && appSettings.get('minimizeToTray')) { + mainWindow.hide() + } }) // Creating the tray icon - const icon = nativeImage.createFromPath('./resources/icon.png') - tray = new Tray(icon.resize({ width: 16, height: 16 })) - tray.setIgnoreDoubleClickEvents(true) + if (appSettings.get('enableTray')) { + const icon = nativeImage.createFromPath('./resources/icon.png') + tray = new Tray(icon.resize({ width: 16, height: 16 })) + tray.setIgnoreDoubleClickEvents(true) - const contextMenu = Menu.buildFromTemplate([ - ...(process.platform === 'darwin' ? [{ - label: 'Show', - click: _ => { + const contextMenu = Menu.buildFromTemplate([ + ...(process.platform === 'darwin' ? [{ + label: 'Show', + click: _ => { + mainWindow.show() + } + }] : []), + { + label: 'Hide', + click: _ => { + mainWindow.hide() + } + }, + { + label: 'Quit', + click: _ => { + console.log('Menu/Quit was clicked') + app.exit() + } + }, + ]) + tray.setToolTip(app.getName()) + tray.setContextMenu(contextMenu) + + // Prevent menu from being shown on left click + // Instead make main window visible (if it had been invisible) + if (process.platform !== 'darwin') { + tray.on('click', _ => { mainWindow.show() - } - }] : []), - { - label: 'Hide', - click: _ => { - mainWindow.hide() - } - }, - { - label: 'Quit', - click: _ => { - console.log('Menu/Quit was clicked') - app.exit() - } - }, - ]) - tray.setToolTip(app.getName()) - tray.setContextMenu(contextMenu) - - // Prevent menu from being shown on left click - // Instead make main window visible (if it had been invisible) - if (process.platform !== 'darwin') { - tray.on('click', _ => { - mainWindow.show() - }) + }) + } } + ipcMain.handle('game:insert', async (_, game: Game) => { addGame(game); });