From 83760281db420c97b139e73bb5b96eb7fdf36732 Mon Sep 17 00:00:00 2001 From: everoddandeven Date: Thu, 24 Oct 2024 16:36:33 +0200 Subject: [PATCH] Minimized auto launch option implementation --- app/main.ts | 48 +++++++++++++------ app/preload.js | 4 +- .../services/electron/electron.service.ts | 4 +- src/app/pages/settings/settings.component.ts | 22 +++++++-- src/polyfills.ts | 2 +- 5 files changed, 56 insertions(+), 24 deletions(-) diff --git a/app/main.ts b/app/main.ts index 25d65be..586f416 100644 --- a/app/main.ts +++ b/app/main.ts @@ -60,7 +60,7 @@ if (!gotInstanceLock) { app.quit(); } -const autoLauncher = new AutoLaunch({ +let autoLauncher = new AutoLaunch({ name: 'monerod-gui', path: process.execPath, options: { @@ -71,6 +71,7 @@ const autoLauncher = new AutoLaunch({ }); const isAutoLaunched: boolean = process.argv.includes('--auto-launch'); +const minimized: boolean = process.argv.includes('--hidden'); dialog.showErrorBox(`Info`, `is auto launched: ${isAutoLaunched}, process.argv: ${process.argv.join(' ')}`) @@ -118,6 +119,7 @@ function createWindow(): BrowserWindow { console.log(`createWindow(): icon = ${wdwIcon}`); console.log(`app.isPackaged: ${app.isPackaged}`); + // Create the browser window. win = new BrowserWindow({ x: 0, @@ -132,11 +134,14 @@ function createWindow(): BrowserWindow { devTools: !app.isPackaged, sandbox: false }, + show: !minimized, autoHideMenuBar: true, icon: wdwIcon - }); + }); - if (!app.isPackaged)win.webContents.openDevTools(); + isHidden = minimized; + + if (!app.isPackaged) win.webContents.openDevTools(); if (serve) { const debug = require('electron-debug'); @@ -154,6 +159,7 @@ function createWindow(): BrowserWindow { } const url = new URL(path.join('file:', dirname, pathIndex)); + win.loadURL(url.href); } @@ -596,21 +602,21 @@ try { win?.webContents.send('on-is-auto-launched', isAutoLaunched); }); - ipcMain.handle('quit', (event) => { + ipcMain.handle('quit', (event: IpcMainInvokeEvent) => { isQuitting = true; app.quit(); }); - ipcMain.handle('start-monerod', (event, configFilePath: string[]) => { + ipcMain.handle('start-monerod', (event: IpcMainInvokeEvent, configFilePath: string[]) => { startMoneroDaemon(configFilePath); }) - ipcMain.handle('get-monero-version', (event, configFilePath: string) => { + ipcMain.handle('get-monero-version', (event: IpcMainInvokeEvent, configFilePath: string) => { getMonerodVersion(configFilePath); }); // Gestione IPC - ipcMain.handle('download-monerod', async (event, downloadUrl: string, destination: string) => { + ipcMain.handle('download-monerod', async (event: IpcMainInvokeEvent, downloadUrl: string, destination: string) => { try { //const fileName = path.basename(downloadUrl); //const filePath = path.join(destination, fileName); @@ -641,7 +647,7 @@ try { } }); - ipcMain.handle('select-file', async (event: any, extensions?: string[]) => { + ipcMain.handle('select-file', async (event: IpcMainInvokeEvent, extensions?: string[]) => { if (!win) { return; @@ -661,7 +667,7 @@ try { win.webContents.send('selected-file', path ? `${path}` : ''); }); - ipcMain.handle('select-folder', async (event: any) => { + ipcMain.handle('select-folder', async (event: IpcMainInvokeEvent) => { if (!win) { return; } @@ -676,11 +682,11 @@ try { win.webContents.send('selected-folder', path ? `${path}` : ''); }); - ipcMain.handle('is-wifi-connected', async (event) => { + ipcMain.handle('is-wifi-connected', async (event: IpcMainInvokeEvent) => { isWifiConnected(); }); - ipcMain.handle('get-os-type', (event) => { + ipcMain.handle('get-os-type', (event: IpcMainInvokeEvent) => { win?.webContents.send('got-os-type', { platform: os.platform(), arch: os.arch() }); }) @@ -696,6 +702,8 @@ try { showNotification(options); }); + // #region Auto Launch + ipcMain.handle('is-auto-launch-enabled', (event: IpcMainInvokeEvent) => { autoLauncher.isEnabled().then((enabled: boolean) => { win?.webContents.send('on-is-auto-launch-enabled', enabled); @@ -705,13 +713,24 @@ try { }); }); - ipcMain.handle('enable-auto-launch', (event:IpcMainInvokeEvent) => { + ipcMain.handle('enable-auto-launch', (event: IpcMainInvokeEvent, minimized: boolean) => { autoLauncher.isEnabled().then((enabled: boolean) => { if (enabled) { win?.webContents.send('on-enable-auto-launch-error', 'already enabled'); return; } + autoLauncher = new AutoLaunch({ + name: 'monerod-gui', + path: process.execPath, + options: { + launchInBackground: minimized, + extraArguments: [ + '--auto-launch' + ] + } + }); + autoLauncher.enable().then(() => { autoLauncher.isEnabled().then((enabled: boolean) => { if (enabled) { @@ -732,8 +751,7 @@ try { }); }); - - ipcMain.handle('disable-auto-launch', (event:IpcMainInvokeEvent) => { + ipcMain.handle('disable-auto-launch', (event: IpcMainInvokeEvent) => { autoLauncher.isEnabled().then((enabled: boolean) => { if (!enabled) { win?.webContents.send('on-disable-auto-launch-error', 'already disabled'); @@ -760,6 +778,8 @@ try { }); }); + // #endregion + ipcMain.handle('is-app-image', (event: IpcMainInvokeEvent) => { const isAppImage: boolean = !!process.env.APPIMAGE; diff --git a/app/preload.js b/app/preload.js index 39812cc..1c65192 100644 --- a/app/preload.js +++ b/app/preload.js @@ -82,8 +82,8 @@ contextBridge.exposeInMainWorld('electronAPI', { quit: () => { ipcRenderer.invoke('quit'); }, - enableAutoLaunch: () => { - ipcRenderer.invoke('enable-auto-launch'); + enableAutoLaunch: (minimized) => { + ipcRenderer.invoke('enable-auto-launch', minimized); }, isAutoLaunchEnabled: () => { ipcRenderer.invoke('is-auto-launch-enabled'); diff --git a/src/app/core/services/electron/electron.service.ts b/src/app/core/services/electron/electron.service.ts index 636e7c5..330f050 100644 --- a/src/app/core/services/electron/electron.service.ts +++ b/src/app/core/services/electron/electron.service.ts @@ -70,7 +70,7 @@ export class ElectronService { return await promise; } - public async enableAutoLaunch(): Promise { + public async enableAutoLaunch(minimized: boolean): Promise { if (await this.isAppImage()) { throw new Error("Cannot enable auto launch"); } @@ -93,7 +93,7 @@ export class ElectronService { }); }); - window.electronAPI.enableAutoLaunch(); + window.electronAPI.enableAutoLaunch(minimized); await promise; } diff --git a/src/app/pages/settings/settings.component.ts b/src/app/pages/settings/settings.component.ts index b9efa90..c0961c2 100644 --- a/src/app/pages/settings/settings.component.ts +++ b/src/app/pages/settings/settings.component.ts @@ -139,19 +139,27 @@ export class SettingsComponent { } } - private async refreshAutoLanch(): Promise { + private async refreshAutoLanch(minimizeChanged: boolean): Promise { if (await this.electronService.isAppImage()) { return; } const enabled = await this.electronService.isAutoLaunchEnabled(); - if (this.originalSettings.startAtLogin && !enabled) { - await this.electronService.enableAutoLaunch(); + const shouldEnable = this.originalSettings.startAtLogin && !enabled; + const shouldDisable = !this.originalSettings.startAtLogin && enabled; + const shouldUpdate = enabled && minimizeChanged; + + if (shouldEnable) { + await this.electronService.enableAutoLaunch(this.originalSettings.startAtLoginMinimized); } - else if (!this.originalSettings.startAtLogin && enabled) { + else if (shouldDisable) { await this.electronService.disableAutoLaunch(); } + else if (shouldUpdate) { + await this.electronService.disableAutoLaunch(); + await this.electronService.enableAutoLaunch(this.originalSettings.startAtLoginMinimized); + } } public async OnSave(): Promise { @@ -166,12 +174,16 @@ export class SettingsComponent { throw new Error('You must set a download path for monerod updates when enabling automatic upgrade'); } + const oldStartMinimized: boolean = this.originalSettings.startAtLoginMinimized; + await this.daemonService.saveSettings(this.currentSettings); this.originalSettings = this.currentSettings.clone(); + const minimizedChanged: boolean = oldStartMinimized == this.originalSettings.startAtLoginMinimized; + try { - await this.refreshAutoLanch(); + await this.refreshAutoLanch(minimizedChanged); } catch(error: any) { console.error(error); } diff --git a/src/polyfills.ts b/src/polyfills.ts index 61bac42..3c9e96c 100644 --- a/src/polyfills.ts +++ b/src/polyfills.ts @@ -100,7 +100,7 @@ declare global { isAutoLaunchEnabled: () => void; onIsAutoLaunchEnabled: (callback: (event: any, enabled: boolean) => void) => void; - enableAutoLaunch: () => void; + enableAutoLaunch: (minimized: boolean) => void; onEnableAutoLaunchError: (callback: (event: any, error: string) => void) => void; onEnableAutoLaunchSuccess: (callback: (event: any) => void) => void;