Skip to content

Commit

Permalink
handle unsaved changes on reboot (chaiNNer-org#2132)
Browse files Browse the repository at this point in the history
* handle unsaved changes on reboot

fixes chaiNNer-org#1338

* renamed parameters

* don’t kill the backend on Cancel
  • Loading branch information
stonerl authored Aug 23, 2023
1 parent 7b8737a commit 703f384
Show file tree
Hide file tree
Showing 3 changed files with 76 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
88 changes: 61 additions & 27 deletions src/main/gui/main-window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,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 @@ -204,38 +196,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

0 comments on commit 703f384

Please sign in to comment.