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

handle unsaved changes on reboot #2132

Merged
merged 3 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions src/common/safeIpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ export interface SendChannels {
'enable-menu': SendChannelInfo;
'save-before-exit': SendChannelInfo;
'exit-after-save': SendChannelInfo;
'save-before-reboot': SendChannelInfo;
'reboot-after-save': SendChannelInfo;
'set-progress-bar': ChannelInfo<void, [progress: number | null]>;
'export-viewport': SendChannelInfo<[kind: 'file' | 'clipboard']>;

Expand Down
88 changes: 61 additions & 27 deletions src/main/gui/main-window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,6 @@ const registerEventHandlerPostSetup = (
app.on('before-quit', () => backend.tryKill());
}

ipcMain.handle('relaunch-application', () => {
if (backend.owned) {
backend.tryKill();
}
app.relaunch();
app.exit();
});

ipcMain.handle('restart-backend', () => {
if (backend.owned) {
backend.restart();
Expand All @@ -190,38 +182,80 @@ const registerEventHandlerPostSetup = (
ipcMain.on('update-has-unsaved-changes', (_, value) => {
hasUnsavedChanges = value;
});

let forceExit = false;
ipcMain.on('exit-after-save', () => {
forceExit = true;
mainWindow.close();
});

const restartChainner = (): void => {
if (backend.owned) {
backend.tryKill();
}
app.relaunch();
app.exit();
};

ipcMain.on('reboot-after-save', () => {
restartChainner();
});

const handleUnsavedChanges = (
event: Electron.Event,
onSave: () => void,
onDontSave?: () => void
) => {
const choice = dialog.showMessageBoxSync(mainWindow, {
type: 'question',
title: 'Unsaved changes',
message: 'The current chain has unsaved changes.',
buttons: ['&Save', "Do&n't Save", 'Cancel'],
defaultId: 0,
cancelId: 2,
noLink: true,
normalizeAccessKeys: true,
});
if (choice === 1) {
// Don't save
if (onDontSave) {
onDontSave(); // Restart the application
}
} else if (choice === 2) {
// Cancel
event.preventDefault();
} else {
// Save
event.preventDefault();
onSave();
}
};

ipcMain.handle('relaunch-application', (event) => {
if (hasUnsavedChanges) {
handleUnsavedChanges(
event,
() => {
mainWindow.webContents.send('save-before-reboot');
},
() => {
restartChainner();
}
);
} else {
restartChainner();
}
});

mainWindow.on('close', (event) => {
if (forceExit) {
// we want to exit and nothing in here may stop this
return;
}
if (hasUnsavedChanges) {
const choice = dialog.showMessageBoxSync(mainWindow, {
type: 'question',
title: 'Unsaved changes',
message: 'The current chain has unsaved changes.',
buttons: ['&Save', "Do&n't Save", 'Cancel'],
defaultId: 0,
cancelId: 2,
noLink: true,
normalizeAccessKeys: true,
});
if (choice === 1) {
// Don't save, so do nothing
} else if (choice === 2) {
// Cancel
event.preventDefault();
} else {
// Save
event.preventDefault();
handleUnsavedChanges(event, () => {
mainWindow.webContents.send('save-before-exit');
}
});
}
});
};
Expand Down
13 changes: 13 additions & 0 deletions src/renderer/contexts/GlobalNodeState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,19 @@ export const GlobalProvider = memo(
}, [performSave])
);

useIpcRendererListener(
'save-before-reboot',
useCallback(() => {
performSave(false)
.then((result) => {
if (result === SaveResult.Saved) {
ipcRenderer.send('reboot-after-save');
}
})
.catch(log.error);
}, [performSave])
);

const setStateFromJSON = useCallback(
async (savedData: ParsedSaveData, path: string, loadPosition = false) => {
if ((await saveUnsavedChanges()) === SaveResult.Canceled) {
Expand Down