Skip to content

Commit

Permalink
Show save on close messages
Browse files Browse the repository at this point in the history
  • Loading branch information
msujew committed May 25, 2022
1 parent 522a5c6 commit 9e96731
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ import {
IDEUpdaterDialogWidget,
} from './dialogs/ide-updater/ide-updater-dialog';
import { ElectronIpcConnectionProvider } from '@theia/core/lib/electron-browser/messaging/electron-ipc-connection-provider';
import { ShutdownRoutine } from './contributions/shutdown-routine';

const ElementQueries = require('css-element-queries/src/ElementQueries');

Expand All @@ -300,6 +301,9 @@ export default new ContainerModule((bind, unbind, isBound, rebind) => {
bind(ArduinoToolbarContribution).toSelf().inSingletonScope();
bind(FrontendApplicationContribution).toService(ArduinoToolbarContribution);

bind(ShutdownRoutine).toSelf().inSingletonScope();
bind(FrontendApplicationContribution).toService(ShutdownRoutine);

// Renderer for both the library and the core widgets.
bind(ListItemRenderer).toSelf().inSingletonScope();

Expand Down
72 changes: 1 addition & 71 deletions arduino-ide-extension/src/browser/contributions/close.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { EditorManager } from '@theia/editor/lib/browser/editor-manager';
import { ApplicationShell } from '@theia/core/lib/browser/shell/application-shell';
import { FrontendApplication } from '@theia/core/lib/browser/frontend-application';
import { ArduinoMenus } from '../menu/arduino-menus';
import { SaveAsSketch } from './save-as-sketch';
import {
SketchContribution,
Command,
Expand All @@ -33,76 +32,7 @@ export class Close extends SketchContribution {

registerCommands(registry: CommandRegistry): void {
registry.registerCommand(Close.Commands.CLOSE, {
execute: async () => {
// Close current editor if closeable.
const { currentEditor } = this.editorManager;
if (currentEditor && currentEditor.title.closable) {
currentEditor.close();
return;
}

// Close current widget from the main area if possible.
const { currentWidget } = this.shell;
if (currentWidget) {
const currentWidgetInMain = toArray(
this.shell.mainPanel.widgets()
).find((widget) => widget === currentWidget);
if (currentWidgetInMain && currentWidgetInMain.title.closable) {
return currentWidgetInMain.close();
}
}

// Close the sketch (window).
const sketch = await this.sketchServiceClient.currentSketch();
if (!sketch) {
return;
}
const isTemp = await this.sketchService.isTemp(sketch);
const uri = await this.sketchServiceClient.currentSketchFile();
if (!uri) {
return;
}
if (isTemp && (await this.wasTouched(uri))) {
const { response } = await remote.dialog.showMessageBox({
type: 'question',
buttons: [
nls.localize(
'vscode/abstractTaskService/saveBeforeRun.dontSave',
"Don't Save"
),
nls.localize('vscode/issueMainService/cancel', 'Cancel'),
nls.localize(
'vscode/abstractTaskService/saveBeforeRun.save',
'Save'
),
],
message: nls.localize(
'arduino/common/saveChangesToSketch',
'Do you want to save changes to this sketch before closing?'
),
detail: nls.localize(
'arduino/common/loseChanges',
"If you don't save, your changes will be lost."
),
});
if (response === 1) {
// Cancel
return;
}
if (response === 2) {
// Save
const saved = await this.commandService.executeCommand(
SaveAsSketch.Commands.SAVE_AS_SKETCH.id,
{ openAfterMove: false, execOnlyIfTemp: true }
);
if (!saved) {
// If it was not saved, do bail the close.
return;
}
}
}
window.close();
},
execute: () => remote.getCurrentWindow().close()
});
}

Expand Down
108 changes: 108 additions & 0 deletions arduino-ide-extension/src/browser/contributions/shutdown-routine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { FrontendApplicationContribution } from "@theia/core/lib/browser/frontend-application";
import { inject, injectable } from "@theia/core/shared/inversify";
import * as remote from '@theia/core/electron-shared/@electron/remote';
import { SketchesServiceClientImpl } from "../../common/protocol/sketches-service-client-impl";
import { SketchesService } from "../../common/protocol/sketches-service";
import { Dialog } from "@theia/core/lib/browser/dialogs";
import { nls } from "@theia/core/lib/common/nls";
import { CommandRegistry } from "@theia/core/lib/common/command";
import { ApplicationShell } from "@theia/core/lib/browser/shell/application-shell";
import { SaveAsSketch } from "./save-as-sketch";
import { WindowService } from "@theia/core/lib/browser/window/window-service";

@injectable()
export class ShutdownRoutine implements FrontendApplicationContribution {

@inject(SketchesServiceClientImpl)
protected readonly sketchServiceClient: SketchesServiceClientImpl;

@inject(SketchesService)
protected readonly sketchService: SketchesService;

@inject(CommandRegistry)
protected readonly commandRegistry: CommandRegistry;

@inject(ApplicationShell)
protected readonly appShell: ApplicationShell;

@inject(WindowService)
protected readonly windowService: WindowService;

initialize(): void {
this.setupShutdownRoutine(remote.getCurrentWindow());
}

setupShutdownRoutine(electronWindow: Electron.BrowserWindow): void {
window.addEventListener('beforeunload', event => {
if (!this.windowService.isSafeToShutDown()) {
event.preventDefault();
event.returnValue = false;
this.confirmShutdown(electronWindow).then(result => {
if (result) {
electronWindow.destroy();
}
});
}
});
}

private async confirmShutdown(window: Electron.BrowserWindow): Promise<boolean> {
const sketch = await this.sketchServiceClient.currentSketch();
if (sketch) {
const isTemp = await this.sketchService.isTemp(sketch);
if (isTemp) {
return this.showTempSketchDialog(window);
} else if (this.appShell.canSaveAll()) {
const dialogResult = await remote.dialog.showMessageBox(window, {
title: nls.localize('theia/core/quitTitle', 'Are you sure you want to quit?'),
message: nls.localize('theia/core/quitMessage', 'Any unsaved changes will not be saved.'),
buttons: [
Dialog.NO,
Dialog.YES
]
});
return dialogResult.response === 1;
}
}
return true;
}

private async showTempSketchDialog(window: Electron.BrowserWindow): Promise<boolean> {
const sketch = await this.sketchServiceClient.currentSketch();
if (!sketch) {
return true;
}
const isTemp = await this.sketchService.isTemp(sketch);
if (!isTemp) {
return true;
}
const messageBoxResult = await remote.dialog.showMessageBox(
window,
{
message: nls.localize('arduino/sketch/saveTempSketch', 'Save your sketch to open it again later.'),
title: 'Arduino-IDE',
type: 'question',
buttons: [
Dialog.CANCEL,
nls.localizeByDefault('Save As...'),
nls.localizeByDefault("Don't Save"),
],
}
)
const result = messageBoxResult.response;
if (result === 2) {
return true;
} else if (result === 1) {
return !!(await this.commandRegistry.executeCommand(
SaveAsSketch.Commands.SAVE_AS_SKETCH.id,
{
execOnlyIfTemp: false,
openAfterMove: false,
wipeOriginal: true
}
));
}
return false
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export class IDEUpdaterDialogWidget extends ReactWidget {
}

onCloseAndInstall(): void {
this.windowService.setSafeToShutDown();
this.updater.quitAndInstall();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,12 @@ export class ElectronWindowService extends TheiaElectronWindowService {
});
return response === 0; // 'Yes', close the window.
}

protected registerUnloadListeners(): void {
// NOOP
}

reload(): void {
window.location.reload();
}
}

0 comments on commit 9e96731

Please sign in to comment.