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();
}
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;

View file

@ -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');

View file

@ -70,7 +70,7 @@ export class ElectronService {
return await promise;
}
public async enableAutoLaunch(): Promise<void> {
public async enableAutoLaunch(minimized: boolean): Promise<void> {
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;
}

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()) {
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<void> {
@ -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);
}

View file

@ -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;