From d7bbfc515de3eacdf2d3ad4ff402d250665305dd Mon Sep 17 00:00:00 2001 From: Akos Kitta Date: Tue, 23 Aug 2022 15:39:19 +0200 Subject: [PATCH] init Signed-off-by: Akos Kitta --- .../browser/arduino-frontend-contribution.tsx | 70 +--------------- .../src/browser/contributions/close.ts | 80 +++++++++++++++---- 2 files changed, 66 insertions(+), 84 deletions(-) diff --git a/arduino-ide-extension/src/browser/arduino-frontend-contribution.tsx b/arduino-ide-extension/src/browser/arduino-frontend-contribution.tsx index f2b4af00..bcbd4747 100644 --- a/arduino-ide-extension/src/browser/arduino-frontend-contribution.tsx +++ b/arduino-ide-extension/src/browser/arduino-frontend-contribution.tsx @@ -5,17 +5,14 @@ import { postConstruct, } from '@theia/core/shared/inversify'; import * as React from '@theia/core/shared/react'; -import { SketchesService } from '../common/protocol'; import { MAIN_MENU_BAR, MenuContribution, MenuModelRegistry, } from '@theia/core'; import { - Dialog, FrontendApplication, FrontendApplicationContribution, - OnWillStopAction, } from '@theia/core/lib/browser'; import { ColorContribution } from '@theia/core/lib/browser/color-application-contribution'; 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 { FileNavigatorCommands } from '@theia/navigator/lib/browser/navigator-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 { BoardsServiceProvider } from './boards/boards-service-provider'; import { BoardsToolBarItem } from './boards/boards-toolbar-item'; -import { SaveAsSketch } from './contributions/save-as-sketch'; import { ArduinoMenus } from './menu/arduino-menus'; import { MonitorViewContribution } from './serial/monitor/monitor-view-contribution'; import { ArduinoToolbar } from './toolbar/arduino-toolbar'; @@ -63,18 +55,12 @@ export class ArduinoFrontendContribution @inject(BoardsServiceProvider) private readonly boardsServiceProvider: BoardsServiceProvider; - @inject(SketchesService) - private readonly sketchService: SketchesService; - @inject(CommandRegistry) private readonly commandRegistry: CommandRegistry; @inject(ArduinoPreferences) private readonly arduinoPreferences: ArduinoPreferences; - @inject(SketchesServiceClientImpl) - private readonly sketchServiceClient: SketchesServiceClientImpl; - @inject(FrontendApplicationStateService) private readonly appStateService: FrontendApplicationStateService; @@ -91,7 +77,7 @@ export class ArduinoFrontendContribution } } - async onStart(app: FrontendApplication): Promise { + onStart(app: FrontendApplication): void { this.arduinoPreferences.onPreferenceChanged((event) => { if (event.newValue !== event.oldValue) { 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 { - 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; - } } diff --git a/arduino-ide-extension/src/browser/contributions/close.ts b/arduino-ide-extension/src/browser/contributions/close.ts index 45597613..b13ddfda 100644 --- a/arduino-ide-extension/src/browser/contributions/close.ts +++ b/arduino-ide-extension/src/browser/contributions/close.ts @@ -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 { 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 { SketchContribution, @@ -14,24 +11,19 @@ import { URI, } from './contribution'; 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. */ @injectable() 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 { 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 { + 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. */ @@ -59,13 +105,17 @@ export class Close extends SketchContribution { const { editor } = editorWidget; if (editor instanceof MonacoEditor) { const versionId = editor.getControl().getModel()?.getVersionId(); - if (Number.isInteger(versionId) && versionId! > 1) { + if (this.isInteger(versionId) && versionId > 1) { return true; } } } return false; } + + private isInteger(arg: unknown): arg is number { + return Number.isInteger(arg); + } } export namespace Close {