Skip to content

Commit

Permalink
Moved uncloseable widget tracking to manager.
Browse files Browse the repository at this point in the history
Signed-off-by: Akos Kitta <[email protected]>
  • Loading branch information
Akos Kitta committed Aug 25, 2022
1 parent 68530d7 commit 9829c6b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 58 deletions.
55 changes: 8 additions & 47 deletions arduino-ide-extension/src/browser/theia/core/application-shell.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,30 @@
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()
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<TheiaApplicationShell.WidgetOptions> = {}
Expand Down
54 changes: 43 additions & 11 deletions arduino-ide-extension/src/browser/theia/core/widget-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<T extends Widget>(
factoryId: string,
options?: unknown
): Promise<T> {
const unresolvedWidget = super.getOrCreateWidget<T>(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;
}
}

/**
Expand Down

0 comments on commit 9829c6b

Please sign in to comment.