Skip to content

Commit 3d55aaa

Browse files
author
Akos Kitta
committed
generalized which widgets are closeable.
instead of a whitelisting, we blacklist the files those belong to sketch Signed-off-by: Akos Kitta <kittaakos@typefox.io>
1 parent fc86646 commit 3d55aaa

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

arduino-ide-extension/src/browser/theia/core/application-shell.ts

+25-17
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
import { injectable, inject } from 'inversify';
33
import { EditorWidget } from '@theia/editor/lib/browser';
44
import { CommandService } from '@theia/core/lib/common/command';
5-
import { PreferencesWidget } from '@theia/preferences/lib/browser/views/preference-widget';
65
import { ApplicationShell as TheiaApplicationShell, Widget } from '@theia/core/lib/browser';
6+
import { Sketch } from '../../../common/protocol';
77
import { EditorMode } from '../../editor-mode';
88
import { SaveAsSketch } from '../../contributions/save-as-sketch';
9+
import { SketchesServiceClientImpl } from '../../../common/protocol/sketches-service-client-impl';
910

1011
@injectable()
1112
export class ApplicationShell extends TheiaApplicationShell {
@@ -16,26 +17,33 @@ export class ApplicationShell extends TheiaApplicationShell {
1617
@inject(CommandService)
1718
protected readonly commandService: CommandService;
1819

19-
protected track(widget: Widget): void {
20-
super.track(widget);
21-
if (!this.editorMode.proMode) {
22-
if (widget instanceof EditorWidget) {
23-
// Always allow closing the whitelisted files.
24-
// TODO: It would be better to blacklist the sketch files only.
25-
if (['tasks.json',
26-
'launch.json',
27-
'settings.json',
28-
'arduino-cli.yaml'].some(fileName => widget.editor.uri.toString().endsWith(fileName))) {
29-
return;
30-
}
31-
}
32-
if (widget instanceof PreferencesWidget) {
33-
return;
20+
@inject(SketchesServiceClientImpl)
21+
protected readonly sketchesServiceClient: SketchesServiceClientImpl;
22+
23+
protected sketch?: Sketch;
24+
25+
async addWidget(widget: Widget, options: Readonly<TheiaApplicationShell.WidgetOptions> = {}): Promise<void> {
26+
// Get the current sketch before adding a widget. This wil trigger an update.
27+
this.sketch = await this.sketchesServiceClient.currentSketch();
28+
super.addWidget(widget, options);
29+
}
30+
31+
async setLayoutData(layoutData: TheiaApplicationShell.LayoutData): Promise<void> {
32+
// I could not find other ways to get sketch in async fashion for sync `track`.
33+
this.sketch = await this.sketchesServiceClient.currentSketch();
34+
super.setLayoutData(layoutData);
35+
}
36+
37+
track(widget: Widget): void {
38+
if (!this.editorMode.proMode && this.sketch && widget instanceof EditorWidget) {
39+
if (Sketch.isInSketch(widget.editor.uri, this.sketch)) {
40+
widget.title.closable = false;
3441
}
35-
widget.title.closable = false;
3642
}
43+
super.track(widget);
3744
}
3845

46+
3947
async saveAll(): Promise<void> {
4048
await super.saveAll();
4149
await this.commandService.executeCommand(SaveAsSketch.Commands.SAVE_AS_SKETCH.id, { execOnlyIfTemp: true, openAfterMove: true });

arduino-ide-extension/src/common/protocol/sketches-service.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import URI from '@theia/core/lib/common/uri';
2+
13
export const SketchesServicePath = '/services/sketches-service';
24
export const SketchesService = Symbol('SketchesService');
35
export interface SketchesService {
@@ -60,9 +62,9 @@ export namespace Sketch {
6062
export const ADDITIONAL = ['.h', '.c', '.hpp', '.hh', '.cpp', '.s'];
6163
export const ALL = Array.from(new Set([...MAIN, ...SOURCE, ...ADDITIONAL]));
6264
}
63-
export function isInSketch(uri: string, sketch: Sketch): boolean {
65+
export function isInSketch(uri: string | URI, sketch: Sketch): boolean {
6466
const { mainFileUri, otherSketchFileUris, additionalFileUris } = sketch;
65-
return [mainFileUri, ...otherSketchFileUris, additionalFileUris].indexOf(uri) !== -1;
67+
return [mainFileUri, ...otherSketchFileUris, additionalFileUris].indexOf(uri.toString()) !== -1;
6668
}
6769
}
6870

0 commit comments

Comments
 (0)