Signed-off-by: Akos Kitta <a.kitta@arduino.cc>
This commit is contained in:
Akos Kitta 2022-08-23 15:39:19 +02:00 committed by Akos Kitta
parent 0c22884729
commit d7bbfc515d
2 changed files with 66 additions and 84 deletions

View File

@ -5,17 +5,14 @@ import {
postConstruct, postConstruct,
} from '@theia/core/shared/inversify'; } from '@theia/core/shared/inversify';
import * as React from '@theia/core/shared/react'; import * as React from '@theia/core/shared/react';
import { SketchesService } from '../common/protocol';
import { import {
MAIN_MENU_BAR, MAIN_MENU_BAR,
MenuContribution, MenuContribution,
MenuModelRegistry, MenuModelRegistry,
} from '@theia/core'; } from '@theia/core';
import { import {
Dialog,
FrontendApplication, FrontendApplication,
FrontendApplicationContribution, FrontendApplicationContribution,
OnWillStopAction,
} from '@theia/core/lib/browser'; } from '@theia/core/lib/browser';
import { ColorContribution } from '@theia/core/lib/browser/color-application-contribution'; import { ColorContribution } from '@theia/core/lib/browser/color-application-contribution';
import { ColorRegistry } from '@theia/core/lib/browser/color-registry'; import { ColorRegistry } from '@theia/core/lib/browser/color-registry';
@ -34,14 +31,9 @@ import { EditorCommands, EditorMainMenu } from '@theia/editor/lib/browser';
import { MonacoMenus } from '@theia/monaco/lib/browser/monaco-menu'; import { MonacoMenus } from '@theia/monaco/lib/browser/monaco-menu';
import { FileNavigatorCommands } from '@theia/navigator/lib/browser/navigator-contribution'; import { FileNavigatorCommands } from '@theia/navigator/lib/browser/navigator-contribution';
import { TerminalMenus } from '@theia/terminal/lib/browser/terminal-frontend-contribution'; import { TerminalMenus } from '@theia/terminal/lib/browser/terminal-frontend-contribution';
import {
CurrentSketch,
SketchesServiceClientImpl,
} from '../common/protocol/sketches-service-client-impl';
import { ArduinoPreferences } from './arduino-preferences'; import { ArduinoPreferences } from './arduino-preferences';
import { BoardsServiceProvider } from './boards/boards-service-provider'; import { BoardsServiceProvider } from './boards/boards-service-provider';
import { BoardsToolBarItem } from './boards/boards-toolbar-item'; import { BoardsToolBarItem } from './boards/boards-toolbar-item';
import { SaveAsSketch } from './contributions/save-as-sketch';
import { ArduinoMenus } from './menu/arduino-menus'; import { ArduinoMenus } from './menu/arduino-menus';
import { MonitorViewContribution } from './serial/monitor/monitor-view-contribution'; import { MonitorViewContribution } from './serial/monitor/monitor-view-contribution';
import { ArduinoToolbar } from './toolbar/arduino-toolbar'; import { ArduinoToolbar } from './toolbar/arduino-toolbar';
@ -63,18 +55,12 @@ export class ArduinoFrontendContribution
@inject(BoardsServiceProvider) @inject(BoardsServiceProvider)
private readonly boardsServiceProvider: BoardsServiceProvider; private readonly boardsServiceProvider: BoardsServiceProvider;
@inject(SketchesService)
private readonly sketchService: SketchesService;
@inject(CommandRegistry) @inject(CommandRegistry)
private readonly commandRegistry: CommandRegistry; private readonly commandRegistry: CommandRegistry;
@inject(ArduinoPreferences) @inject(ArduinoPreferences)
private readonly arduinoPreferences: ArduinoPreferences; private readonly arduinoPreferences: ArduinoPreferences;
@inject(SketchesServiceClientImpl)
private readonly sketchServiceClient: SketchesServiceClientImpl;
@inject(FrontendApplicationStateService) @inject(FrontendApplicationStateService)
private readonly appStateService: FrontendApplicationStateService; private readonly appStateService: FrontendApplicationStateService;
@ -91,7 +77,7 @@ export class ArduinoFrontendContribution
} }
} }
async onStart(app: FrontendApplication): Promise<void> { onStart(app: FrontendApplication): void {
this.arduinoPreferences.onPreferenceChanged((event) => { this.arduinoPreferences.onPreferenceChanged((event) => {
if (event.newValue !== event.oldValue) { if (event.newValue !== event.oldValue) {
switch (event.preferenceName) { switch (event.preferenceName) {
@ -303,58 +289,4 @@ export class ArduinoFrontendContribution
} }
); );
} }
// TODO: should be handled by `Close` contribution. https://github.com/arduino/arduino-ide/issues/1016
onWillStop(): OnWillStopAction {
return {
reason: 'temp-sketch',
action: () => {
return this.showTempSketchDialog();
},
};
}
private async showTempSketchDialog(): Promise<boolean> {
const sketch = await this.sketchServiceClient.currentSketch();
if (!CurrentSketch.isValid(sketch)) {
return true;
}
const isTemp = await this.sketchService.isTemp(sketch);
if (!isTemp) {
return true;
}
const messageBoxResult = await remote.dialog.showMessageBox(
remote.getCurrentWindow(),
{
message: nls.localize(
'arduino/sketch/saveTempSketch',
'Save your sketch to open it again later.'
),
title: nls.localize(
'theia/core/quitTitle',
'Are you sure you want to quit?'
),
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;
}
} }

View File

@ -1,9 +1,6 @@
import { inject, injectable } from '@theia/core/shared/inversify'; import { injectable } from '@theia/core/shared/inversify';
import * as remote from '@theia/core/electron-shared/@electron/remote'; import * as remote from '@theia/core/electron-shared/@electron/remote';
import { MonacoEditor } from '@theia/monaco/lib/browser/monaco-editor'; import { MonacoEditor } from '@theia/monaco/lib/browser/monaco-editor';
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 { ArduinoMenus } from '../menu/arduino-menus';
import { import {
SketchContribution, SketchContribution,
@ -14,24 +11,19 @@ import {
URI, URI,
} from './contribution'; } from './contribution';
import { nls } from '@theia/core/lib/common'; import { nls } from '@theia/core/lib/common';
import { Dialog } from '@theia/core/lib/browser/dialogs';
import { CurrentSketch } from '../../common/protocol/sketches-service-client-impl';
import { SaveAsSketch } from './save-as-sketch';
import type { OnWillStopAction } from '@theia/core/lib/browser/frontend-application';
/** /**
* Closes the `current` closeable editor, or any closeable current widget from the main area, or the current sketch window. * Closes the `current` closeable editor, or any closeable current widget from the main area, or the current sketch window.
*/ */
@injectable() @injectable()
export class Close extends SketchContribution { export class Close extends SketchContribution {
@inject(EditorManager)
protected override readonly editorManager: EditorManager;
protected shell: ApplicationShell;
override onStart(app: FrontendApplication): void {
this.shell = app.shell;
}
override registerCommands(registry: CommandRegistry): void { override registerCommands(registry: CommandRegistry): void {
registry.registerCommand(Close.Commands.CLOSE, { registry.registerCommand(Close.Commands.CLOSE, {
execute: () => remote.getCurrentWindow().close() execute: () => remote.getCurrentWindow().close(),
}); });
} }
@ -50,6 +42,60 @@ export class Close extends SketchContribution {
}); });
} }
// `FrontendApplicationContribution#onWillStop`
onWillStop(): OnWillStopAction {
return {
reason: 'temp-sketch',
action: () => {
return this.showTempSketchDialog();
},
};
}
private async showTempSketchDialog(): Promise<boolean> {
const sketch = await this.sketchServiceClient.currentSketch();
if (!CurrentSketch.isValid(sketch)) {
return true;
}
const isTemp = await this.sketchService.isTemp(sketch);
if (!isTemp) {
return true;
}
const messageBoxResult = await remote.dialog.showMessageBox(
remote.getCurrentWindow(),
{
message: nls.localize(
'arduino/sketch/saveTempSketch',
'Save your sketch to open it again later.'
),
title: nls.localize(
'theia/core/quitTitle',
'Are you sure you want to quit?'
),
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.commandService.executeCommand(
SaveAsSketch.Commands.SAVE_AS_SKETCH.id,
{
execOnlyIfTemp: false,
openAfterMove: false,
wipeOriginal: true,
}
));
}
return false;
}
/** /**
* If the file was ever touched/modified. We get this based on the `version` of the monaco model. * If the file was ever touched/modified. We get this based on the `version` of the monaco model.
*/ */
@ -59,13 +105,17 @@ export class Close extends SketchContribution {
const { editor } = editorWidget; const { editor } = editorWidget;
if (editor instanceof MonacoEditor) { if (editor instanceof MonacoEditor) {
const versionId = editor.getControl().getModel()?.getVersionId(); const versionId = editor.getControl().getModel()?.getVersionId();
if (Number.isInteger(versionId) && versionId! > 1) { if (this.isInteger(versionId) && versionId > 1) {
return true; return true;
} }
} }
} }
return false; return false;
} }
private isInteger(arg: unknown): arg is number {
return Number.isInteger(arg);
}
} }
export namespace Close { export namespace Close {