Skip to content

Commit

Permalink
handle unsaved changes on reboot
Browse files Browse the repository at this point in the history
  • Loading branch information
stonerl committed Aug 22, 2023
1 parent 785b616 commit a6a562c
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 27 deletions.
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
86 changes: 59 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,78 @@ const registerEventHandlerPostSetup = (
ipcMain.on('update-has-unsaved-changes', (_, value) => {
hasUnsavedChanges = value;
});

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

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

const handleUnsavedChanges = (
event: Electron.Event,
callback: () => void,
restartCallback?: () => 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 (restartCallback) {
restartCallback(); // Restart the application
}
} else if (choice === 2) {
// Cancel
event.preventDefault();
} else {
// Save
event.preventDefault();
callback();
}
};

ipcMain.handle('relaunch-application', (event) => {
if (backend.owned) {
backend.tryKill();
}
if (hasUnsavedChanges) {
handleUnsavedChanges(
event,
() => {
mainWindow.webContents.send('save-before-reboot');
},
() => {
app.relaunch(); // Restart the application
app.exit();
}
);
} else {
app.relaunch();
app.exit();
}
});

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

0 comments on commit a6a562c

Please sign in to comment.