Skip to content

Commit

Permalink
Save dialog for closing temp sketch
Browse files Browse the repository at this point in the history
  • Loading branch information
msujew committed Mar 9, 2022
1 parent 9e89964 commit 5564b91
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
FrontendApplication,
FrontendApplicationContribution,
LocalStorageService,
OnWillStopAction,
StatusBar,
StatusBarAlignment,
} from '@theia/core/lib/browser';
Expand Down Expand Up @@ -70,6 +71,7 @@ import { SaveAsSketch } from './contributions/save-as-sketch';
import { SketchbookWidgetContribution } from './widgets/sketchbook/sketchbook-widget-contribution';
import { IDEUpdaterDialog } from './dialogs/ide-updater/ide-updater-dialog';
import { IDEUpdater } from '../common/protocol/ide-updater';
import { TempSketchDialog } from './dialogs/temp-sketch-dialog';

const INIT_LIBS_AND_PACKAGES = 'initializedLibsAndPackages';
export const SKIP_IDE_VERSION = 'skipIDEVersion';
Expand Down Expand Up @@ -549,6 +551,37 @@ export class ArduinoFrontendContribution
}
}

onWillStop(): OnWillStopAction {
return {
reason: 'Temp Sketch',
action: async () => {
const sketch = await this.sketchServiceClient.currentSketch();
if (!sketch) {
return true;
}
const isTemp = await this.sketchService.isTemp(sketch);
if (!isTemp) {
return true;
}
const dialog = new TempSketchDialog();
const result = await dialog.open();
if (result === "Don't Save") {
return true;
} else if (result === 'Save As...') {
return !!(await this.commandRegistry.executeCommand(
SaveAsSketch.Commands.SAVE_AS_SKETCH.id,
{
execOnlyIfTemp: false,
openAfterMove: false,
wipeOriginal: true,
}
));
}
return false;
}
}
}

registerColors(colors: ColorRegistry): void {
colors.register(
{
Expand Down
2 changes: 1 addition & 1 deletion arduino-ide-extension/src/browser/arduino-preferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export const ArduinoConfigSchema: PreferenceSchema = {
default: 'https://downloads.arduino.cc/arduino-ide',
description: nls.localize(
'arduino/preferences/ide.updateBaseUrl',
`The base URL where to download updates from. Defaults to 'https://downloads.arduino.cc/arduino-ide'`
"The base URL where to download updates from. Defaults to 'https://downloads.arduino.cc/arduino-ide'"
),
},
'arduino.board.certificates': {
Expand Down
53 changes: 53 additions & 0 deletions arduino-ide-extension/src/browser/dialogs/temp-sketch-dialog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { nls } from "@theia/core";
import { AbstractDialog, Dialog, Message } from "@theia/core/lib/browser";

export class TempSketchDialog extends AbstractDialog<TempSketchDialog.Options> {
protected readonly dontSaveButton: HTMLButtonElement;
protected _value: TempSketchDialog.Options = 'Cancel';

get value(): TempSketchDialog.Options {
return this._value;
}

constructor() {
super({
title: nls.localize('theia/core/quitTitle', 'Are you sure you want to quit?')
});
const messageNode = document.createElement('div');
messageNode.textContent = nls.localize('arduino/sketch/saveTempSketch', 'Save your sketch to open it again later.');
this.contentNode.appendChild(messageNode);
this.dontSaveButton = this.createButton(nls.localizeByDefault(TempSketchDialog.Values["Don't Save"]));
this.dontSaveButton.classList.add('secondary');
this.controlPanel.appendChild(this.dontSaveButton);
this.appendCloseButton(Dialog.CANCEL);
this.appendAcceptButton(nls.localizeByDefault(TempSketchDialog.Values['Save As...']));
}

protected onAfterAttach(msg: Message): void {
super.onAfterAttach(msg);
this.addAction(this.dontSaveButton, () => this.dontSave(), 'click');
}

protected addAcceptAction<K extends keyof HTMLElementEventMap>(element: HTMLElement, ...additionalEventTypes: K[]): void {
this.addAction(element, () => this.doSave(), 'click');
}

protected dontSave(): void {
this._value = TempSketchDialog.Values["Don't Save"];
this.accept();
}

protected doSave(): void {
this._value = TempSketchDialog.Values['Save As...'];
this.accept();
}
}

export namespace TempSketchDialog {
export const enum Values {
"Don't Save" = "Don't Save",
Cancel = 'Cancel',
'Save As...' = 'Save As...',
};
export type Options = keyof typeof Values;
}
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ export class ElectronMainApplication extends TheiaElectronMainApplication {
}
});
this.attachClosedWorkspace(electronWindow);
this.attachCloseListeners(electronWindow, options);
this.attachReadyToShow(electronWindow);
this.attachSaveWindowState(electronWindow);
this.attachGlobalShortcuts(electronWindow);
Expand Down

0 comments on commit 5564b91

Please sign in to comment.