Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added checkbox for keeping service running background after GUI closes #670

Merged
merged 2 commits into from
Dec 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions packages/gui/src/electron/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -478,16 +478,26 @@ if (ensureSingleInstance() && ensureCorrectEnvironment()) {
// if (!guessPackaged()) {
// mainWindow.webContents.openDevTools();
// }
mainWindow.on('close', (e) => {
mainWindow.on('close', async (e) => {
// if the daemon isn't local we aren't going to try to start/stop it
if (decidedToClose || !manageDaemonLifetime(NET)) {
return;
}
if (!mainWindow) {
throw new Error('`mainWindow` is empty');
}
e.preventDefault();

if (!isClosing) {
isClosing = true;
let keepBackgroundRunning: boolean | undefined;
const p = readPrefs();
if (typeof p.keepBackgroundRunning === 'boolean') {
keepBackgroundRunning = p.keepBackgroundRunning;
}

if (promptOnQuit) {
const choice = dialog.showMessageBoxSync({
const choice = await dialog.showMessageBox({
type: 'question',
buttons: [i18n._(/* i18n */ { id: 'No' }), i18n._(/* i18n */ { id: 'Yes' })],
title: i18n._(/* i18n */ { id: 'Confirm' }),
Expand All @@ -496,22 +506,36 @@ if (ensureSingleInstance() && ensureCorrectEnvironment()) {
id: 'Are you sure you want to quit?',
}
),
checkboxChecked: keepBackgroundRunning ?? false,
checkboxLabel: i18n._(/* i18n */ { id: 'Keep service running in the background' }),
});
if (choice === 0) {
if (keepBackgroundRunning !== choice.checkboxChecked) {
savePrefs({ ...p, keepBackgroundRunning: choice.checkboxChecked });
}
if (choice.response === 0) {
isClosing = false;
return;
}
keepBackgroundRunning = choice.checkboxChecked;
}
isClosing = false;
decidedToClose = true;
mainWindow.webContents.send('exit-daemon');

// save the window state and unmange so we don't restore the mini exiting state
mainWindowState.saveState(mainWindow);
mainWindowState.unmanage(mainWindow);
mainWindowState.unmanage();

if (keepBackgroundRunning) {
mainWindow.close();
openedWindows.forEach((win) => win.close());
return;
}

mainWindow.webContents.send('exit-daemon');
mainWindow.setBounds({ height: 500, width: 500 });
mainWindow.center();
ipcMain.on('daemon-exited', () => {
mainWindow.close();
mainWindow?.close();

openedWindows.forEach((win) => win.close());
});
Expand Down
Loading