From 57c50fefe37805c39756232587686cfc48dd7d7a Mon Sep 17 00:00:00 2001 From: Akos Kitta Date: Wed, 3 Feb 2021 15:55:38 +0100 Subject: [PATCH] ATL-885: Refined the 'Close' behavior. Signed-off-by: Akos Kitta --- .../browser/arduino-ide-frontend-module.ts | 4 +- .../{close-sketch.ts => close.ts} | 52 +++++++++++++++---- 2 files changed, 43 insertions(+), 13 deletions(-) rename arduino-ide-extension/src/browser/contributions/{close-sketch.ts => close.ts} (64%) diff --git a/arduino-ide-extension/src/browser/arduino-ide-frontend-module.ts b/arduino-ide-extension/src/browser/arduino-ide-frontend-module.ts index 0ef4fb5d..1bd31804 100644 --- a/arduino-ide-extension/src/browser/arduino-ide-frontend-module.ts +++ b/arduino-ide-extension/src/browser/arduino-ide-frontend-module.ts @@ -85,7 +85,7 @@ import { WorkspaceFrontendContribution, ArduinoFileMenuContribution } from './th import { Contribution } from './contributions/contribution'; import { NewSketch } from './contributions/new-sketch'; import { OpenSketch } from './contributions/open-sketch'; -import { CloseSketch } from './contributions/close-sketch'; +import { Close } from './contributions/close'; import { SaveAsSketch } from './contributions/save-as-sketch'; import { SaveSketch } from './contributions/save-sketch'; import { VerifySketch } from './contributions/verify-sketch'; @@ -326,7 +326,7 @@ export default new ContainerModule((bind, unbind, isBound, rebind) => { Contribution.configure(bind, NewSketch); Contribution.configure(bind, OpenSketch); - Contribution.configure(bind, CloseSketch); + Contribution.configure(bind, Close); Contribution.configure(bind, SaveSketch); Contribution.configure(bind, SaveAsSketch); Contribution.configure(bind, VerifySketch); diff --git a/arduino-ide-extension/src/browser/contributions/close-sketch.ts b/arduino-ide-extension/src/browser/contributions/close.ts similarity index 64% rename from arduino-ide-extension/src/browser/contributions/close-sketch.ts rename to arduino-ide-extension/src/browser/contributions/close.ts index 3eeda59a..46bd72ce 100644 --- a/arduino-ide-extension/src/browser/contributions/close-sketch.ts +++ b/arduino-ide-extension/src/browser/contributions/close.ts @@ -1,20 +1,50 @@ import { inject, injectable } from 'inversify'; +import { toArray } from '@phosphor/algorithm'; import { remote } from 'electron'; -import { ArduinoMenus } from '../menu/arduino-menus'; -import { SketchContribution, Command, CommandRegistry, MenuModelRegistry, KeybindingRegistry, URI } from './contribution'; -import { SaveAsSketch } from './save-as-sketch'; -import { EditorManager } from '@theia/editor/lib/browser'; 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 { SaveAsSketch } from './save-as-sketch'; +import { SketchContribution, Command, CommandRegistry, MenuModelRegistry, KeybindingRegistry, URI } from './contribution'; +/** + * Closes the `current` closeable editor, or any closeable current widget from the main area, or the current sketch window. + */ @injectable() -export class CloseSketch extends SketchContribution { +export class Close extends SketchContribution { @inject(EditorManager) protected readonly editorManager: EditorManager; + protected shell: ApplicationShell; + + onStart(app: FrontendApplication): void { + this.shell = app.shell; + } + registerCommands(registry: CommandRegistry): void { - registry.registerCommand(CloseSketch.Commands.CLOSE_SKETCH, { + 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; @@ -48,7 +78,7 @@ export class CloseSketch extends SketchContribution { registerMenus(registry: MenuModelRegistry): void { registry.registerMenuAction(ArduinoMenus.FILE__SKETCH_GROUP, { - commandId: CloseSketch.Commands.CLOSE_SKETCH.id, + commandId: Close.Commands.CLOSE.id, label: 'Close', order: '5' }); @@ -56,7 +86,7 @@ export class CloseSketch extends SketchContribution { registerKeybindings(registry: KeybindingRegistry): void { registry.registerKeybinding({ - command: CloseSketch.Commands.CLOSE_SKETCH.id, + command: Close.Commands.CLOSE.id, keybinding: 'CtrlCmd+W' }); } @@ -80,10 +110,10 @@ export class CloseSketch extends SketchContribution { } -export namespace CloseSketch { +export namespace Close { export namespace Commands { - export const CLOSE_SKETCH: Command = { - id: 'arduino-close-sketch' + export const CLOSE: Command = { + id: 'arduino-close' }; } }