diff --git a/arduino-ide-extension/src/browser/theia/core/application-shell.ts b/arduino-ide-extension/src/browser/theia/core/application-shell.ts index d3d7cc2f..9f0ac6b7 100644 --- a/arduino-ide-extension/src/browser/theia/core/application-shell.ts +++ b/arduino-ide-extension/src/browser/theia/core/application-shell.ts @@ -1,28 +1,20 @@ -import { injectable, inject } from '@theia/core/shared/inversify'; -import { EditorWidget } from '@theia/editor/lib/browser'; -import { MessageService } from '@theia/core/lib/common/message-service'; -import { OutputWidget } from '@theia/output/lib/browser/output-widget'; -import { - ConnectionStatusService, - ConnectionStatus, -} from '@theia/core/lib/browser/connection-status-service'; import { ApplicationShell as TheiaApplicationShell, DockPanel, DockPanelRenderer as TheiaDockPanelRenderer, Panel, + SaveOptions, + SHELL_TABBAR_CONTEXT_MENU, TabBar, Widget, - SHELL_TABBAR_CONTEXT_MENU, - SaveOptions, } from '@theia/core/lib/browser'; -import { Sketch } from '../../../common/protocol'; import { - CurrentSketch, - SketchesServiceClientImpl, -} from '../../../common/protocol/sketches-service-client-impl'; -import { nls } from '@theia/core/lib/common'; -import URI from '@theia/core/lib/common/uri'; + ConnectionStatus, + ConnectionStatusService, +} from '@theia/core/lib/browser/connection-status-service'; +import { nls } from '@theia/core/lib/common/nls'; +import { MessageService } from '@theia/core/lib/common/message-service'; +import { inject, injectable } from '@theia/core/shared/inversify'; import { ToolbarAwareTabBar } from './tab-bars'; @injectable() @@ -30,40 +22,9 @@ export class ApplicationShell extends TheiaApplicationShell { @inject(MessageService) private readonly messageService: MessageService; - @inject(SketchesServiceClientImpl) - private readonly sketchesServiceClient: SketchesServiceClientImpl; - @inject(ConnectionStatusService) private readonly connectionStatusService: ConnectionStatusService; - protected override track(widget: Widget): void { - super.track(widget); - if (widget instanceof OutputWidget) { - widget.title.closable = false; // TODO: https://arduino.slack.com/archives/C01698YT7S4/p1598011990133700 - } - if (widget instanceof EditorWidget) { - // Make the editor un-closeable asynchronously. - this.sketchesServiceClient.currentSketch().then((sketch) => { - if (CurrentSketch.isValid(sketch)) { - if (!this.isSketchFile(widget.editor.uri, sketch.uri)) { - return; - } - if (Sketch.isInSketch(widget.editor.uri, sketch)) { - widget.title.closable = false; - } - } - }); - } - } - - private isSketchFile(uri: URI, sketchUriString: string): boolean { - const sketchUri = new URI(sketchUriString); - if (uri.parent.isEqual(sketchUri)) { - return true; - } - return false; - } - override async addWidget( widget: Widget, options: Readonly = {} diff --git a/arduino-ide-extension/src/browser/theia/core/widget-manager.ts b/arduino-ide-extension/src/browser/theia/core/widget-manager.ts index 4e8e1013..87d0ae74 100644 --- a/arduino-ide-extension/src/browser/theia/core/widget-manager.ts +++ b/arduino-ide-extension/src/browser/theia/core/widget-manager.ts @@ -7,6 +7,7 @@ import { postConstruct, } from '@theia/core/shared/inversify'; import { EditorWidget } from '@theia/editor/lib/browser'; +import { OutputWidget } from '@theia/output/lib/browser/output-widget'; import deepEqual = require('deep-equal'); import { CurrentSketch, @@ -21,19 +22,50 @@ export class WidgetManager extends TheiaWidgetManager { @postConstruct() protected init(): void { - this.sketchesServiceClient.onCurrentSketchDidChange((currentSketch) => { - if (CurrentSketch.isValid(currentSketch)) { - const sketchFileUris = new Set(Sketch.uris(currentSketch)); - for (const widget of this.widgets.values()) { - if (widget instanceof EditorWidget) { - const uri = widget.editor.uri.toString(); - if (sketchFileUris.has(uri)) { - widget.title.closable = false; - } - } + this.sketchesServiceClient.onCurrentSketchDidChange((sketch) => + this.maybeSetWidgetUncloseable( + sketch, + ...Array.from(this.widgets.values()) + ) + ); + } + + override getOrCreateWidget( + factoryId: string, + options?: unknown + ): Promise { + const unresolvedWidget = super.getOrCreateWidget(factoryId, options); + unresolvedWidget.then(async (widget) => { + const sketch = await this.sketchesServiceClient.currentSketch(); + this.maybeSetWidgetUncloseable(sketch, widget); + }); + return unresolvedWidget; + } + + private maybeSetWidgetUncloseable( + sketch: CurrentSketch, + ...widgets: Widget[] + ): void { + const sketchFileUris = + CurrentSketch.isValid(sketch) && new Set(Sketch.uris(sketch)); + for (const widget of widgets) { + if (widget instanceof OutputWidget) { + this.setWidgetUncloseable(widget); // TODO: https://arduino.slack.com/archives/C01698YT7S4/p1598011990133700 + } else if (widget instanceof EditorWidget) { + // Make the editor un-closeable asynchronously. + const uri = widget.editor.uri.toString(); + if (!!sketchFileUris && sketchFileUris.has(uri)) { + this.setWidgetUncloseable(widget); } } - }); + } + } + + private setWidgetUncloseable(widget: Widget): void { + const { title } = widget; + if (title.closable) { + title.closable = false; + } } /**