Minimized auto launch option implementation

This commit is contained in:
everoddandeven 2024-10-24 16:36:33 +02:00
parent 5592afcd95
commit 83760281db
5 changed files with 56 additions and 24 deletions

View file

@ -60,7 +60,7 @@ if (!gotInstanceLock) {
app.quit(); app.quit();
} }
const autoLauncher = new AutoLaunch({ let autoLauncher = new AutoLaunch({
name: 'monerod-gui', name: 'monerod-gui',
path: process.execPath, path: process.execPath,
options: { options: {
@ -71,6 +71,7 @@ const autoLauncher = new AutoLaunch({
}); });
const isAutoLaunched: boolean = process.argv.includes('--auto-launch'); 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(' ')}`) 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(`createWindow(): icon = ${wdwIcon}`);
console.log(`app.isPackaged: ${app.isPackaged}`); console.log(`app.isPackaged: ${app.isPackaged}`);
// Create the browser window. // Create the browser window.
win = new BrowserWindow({ win = new BrowserWindow({
x: 0, x: 0,
@ -132,11 +134,14 @@ function createWindow(): BrowserWindow {
devTools: !app.isPackaged, devTools: !app.isPackaged,
sandbox: false sandbox: false
}, },
show: !minimized,
autoHideMenuBar: true, autoHideMenuBar: true,
icon: wdwIcon icon: wdwIcon
}); });
if (!app.isPackaged)win.webContents.openDevTools(); isHidden = minimized;
if (!app.isPackaged) win.webContents.openDevTools();
if (serve) { if (serve) {
const debug = require('electron-debug'); const debug = require('electron-debug');
@ -154,6 +159,7 @@ function createWindow(): BrowserWindow {
} }
const url = new URL(path.join('file:', dirname, pathIndex)); const url = new URL(path.join('file:', dirname, pathIndex));
win.loadURL(url.href); win.loadURL(url.href);
} }
@ -596,21 +602,21 @@ try {
win?.webContents.send('on-is-auto-launched', isAutoLaunched); win?.webContents.send('on-is-auto-launched', isAutoLaunched);
}); });
ipcMain.handle('quit', (event) => { ipcMain.handle('quit', (event: IpcMainInvokeEvent) => {
isQuitting = true; isQuitting = true;
app.quit(); app.quit();
}); });
ipcMain.handle('start-monerod', (event, configFilePath: string[]) => { ipcMain.handle('start-monerod', (event: IpcMainInvokeEvent, configFilePath: string[]) => {
startMoneroDaemon(configFilePath); startMoneroDaemon(configFilePath);
}) })
ipcMain.handle('get-monero-version', (event, configFilePath: string) => { ipcMain.handle('get-monero-version', (event: IpcMainInvokeEvent, configFilePath: string) => {
getMonerodVersion(configFilePath); getMonerodVersion(configFilePath);
}); });
// Gestione IPC // Gestione IPC
ipcMain.handle('download-monerod', async (event, downloadUrl: string, destination: string) => { ipcMain.handle('download-monerod', async (event: IpcMainInvokeEvent, downloadUrl: string, destination: string) => {
try { try {
//const fileName = path.basename(downloadUrl); //const fileName = path.basename(downloadUrl);
//const filePath = path.join(destination, fileName); //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) if (!win)
{ {
return; return;
@ -661,7 +667,7 @@ try {
win.webContents.send('selected-file', path ? `${path}` : ''); win.webContents.send('selected-file', path ? `${path}` : '');
}); });
ipcMain.handle('select-folder', async (event: any) => { ipcMain.handle('select-folder', async (event: IpcMainInvokeEvent) => {
if (!win) { if (!win) {
return; return;
} }
@ -676,11 +682,11 @@ try {
win.webContents.send('selected-folder', path ? `${path}` : ''); win.webContents.send('selected-folder', path ? `${path}` : '');
}); });
ipcMain.handle('is-wifi-connected', async (event) => { ipcMain.handle('is-wifi-connected', async (event: IpcMainInvokeEvent) => {
isWifiConnected(); 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() }); win?.webContents.send('got-os-type', { platform: os.platform(), arch: os.arch() });
}) })
@ -696,6 +702,8 @@ try {
showNotification(options); showNotification(options);
}); });
// #region Auto Launch
ipcMain.handle('is-auto-launch-enabled', (event: IpcMainInvokeEvent) => { ipcMain.handle('is-auto-launch-enabled', (event: IpcMainInvokeEvent) => {
autoLauncher.isEnabled().then((enabled: boolean) => { autoLauncher.isEnabled().then((enabled: boolean) => {
win?.webContents.send('on-is-auto-launch-enabled', enabled); 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) => { autoLauncher.isEnabled().then((enabled: boolean) => {
if (enabled) { if (enabled) {
win?.webContents.send('on-enable-auto-launch-error', 'already enabled'); win?.webContents.send('on-enable-auto-launch-error', 'already enabled');
return; return;
} }
autoLauncher = new AutoLaunch({
name: 'monerod-gui',
path: process.execPath,
options: {
launchInBackground: minimized,
extraArguments: [
'--auto-launch'
]
}
});
autoLauncher.enable().then(() => { autoLauncher.enable().then(() => {
autoLauncher.isEnabled().then((enabled: boolean) => { autoLauncher.isEnabled().then((enabled: boolean) => {
if (enabled) { 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) => { autoLauncher.isEnabled().then((enabled: boolean) => {
if (!enabled) { if (!enabled) {
win?.webContents.send('on-disable-auto-launch-error', 'already disabled'); win?.webContents.send('on-disable-auto-launch-error', 'already disabled');
@ -760,6 +778,8 @@ try {
}); });
}); });
// #endregion
ipcMain.handle('is-app-image', (event: IpcMainInvokeEvent) => { ipcMain.handle('is-app-image', (event: IpcMainInvokeEvent) => {
const isAppImage: boolean = !!process.env.APPIMAGE; const isAppImage: boolean = !!process.env.APPIMAGE;

View file

@ -82,8 +82,8 @@ contextBridge.exposeInMainWorld('electronAPI', {
quit: () => { quit: () => {
ipcRenderer.invoke('quit'); ipcRenderer.invoke('quit');
}, },
enableAutoLaunch: () => { enableAutoLaunch: (minimized) => {
ipcRenderer.invoke('enable-auto-launch'); ipcRenderer.invoke('enable-auto-launch', minimized);
}, },
isAutoLaunchEnabled: () => { isAutoLaunchEnabled: () => {
ipcRenderer.invoke('is-auto-launch-enabled'); ipcRenderer.invoke('is-auto-launch-enabled');

View file

@ -70,7 +70,7 @@ export class ElectronService {
return await promise; return await promise;
} }
public async enableAutoLaunch(): Promise<void> { public async enableAutoLaunch(minimized: boolean): Promise<void> {
if (await this.isAppImage()) { if (await this.isAppImage()) {
throw new Error("Cannot enable auto launch"); throw new Error("Cannot enable auto launch");
} }
@ -93,7 +93,7 @@ export class ElectronService {
}); });
}); });
window.electronAPI.enableAutoLaunch(); window.electronAPI.enableAutoLaunch(minimized);
await promise; await promise;
} }

View file

@ -139,19 +139,27 @@ export class SettingsComponent {
} }
} }
private async refreshAutoLanch(): Promise<void> { private async refreshAutoLanch(minimizeChanged: boolean): Promise<void> {
if (await this.electronService.isAppImage()) { if (await this.electronService.isAppImage()) {
return; return;
} }
const enabled = await this.electronService.isAutoLaunchEnabled(); const enabled = await this.electronService.isAutoLaunchEnabled();
if (this.originalSettings.startAtLogin && !enabled) { const shouldEnable = this.originalSettings.startAtLogin && !enabled;
await this.electronService.enableAutoLaunch(); 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(); await this.electronService.disableAutoLaunch();
} }
else if (shouldUpdate) {
await this.electronService.disableAutoLaunch();
await this.electronService.enableAutoLaunch(this.originalSettings.startAtLoginMinimized);
}
} }
public async OnSave(): Promise<void> { public async OnSave(): Promise<void> {
@ -166,12 +174,16 @@ export class SettingsComponent {
throw new Error('You must set a download path for monerod updates when enabling automatic upgrade'); 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); await this.daemonService.saveSettings(this.currentSettings);
this.originalSettings = this.currentSettings.clone(); this.originalSettings = this.currentSettings.clone();
const minimizedChanged: boolean = oldStartMinimized == this.originalSettings.startAtLoginMinimized;
try { try {
await this.refreshAutoLanch(); await this.refreshAutoLanch(minimizedChanged);
} catch(error: any) { } catch(error: any) {
console.error(error); console.error(error);
} }

View file

@ -100,7 +100,7 @@ declare global {
isAutoLaunchEnabled: () => void; isAutoLaunchEnabled: () => void;
onIsAutoLaunchEnabled: (callback: (event: any, enabled: boolean) => void) => void; onIsAutoLaunchEnabled: (callback: (event: any, enabled: boolean) => void) => void;
enableAutoLaunch: () => void; enableAutoLaunch: (minimized: boolean) => void;
onEnableAutoLaunchError: (callback: (event: any, error: string) => void) => void; onEnableAutoLaunchError: (callback: (event: any, error: string) => void) => void;
onEnableAutoLaunchSuccess: (callback: (event: any) => void) => void; onEnableAutoLaunchSuccess: (callback: (event: any) => void) => void;