-
-
Notifications
You must be signed in to change notification settings - Fork 436
/
Copy pathwidget-manager.ts
73 lines (68 loc) · 2.42 KB
/
widget-manager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import type { Widget } from '@theia/core/lib/browser';
import { WidgetManager as TheiaWidgetManager } from '@theia/core/lib/browser/widget-manager';
import {
inject,
injectable,
postConstruct,
} from '@theia/core/shared/inversify';
import { EditorWidget } from '@theia/editor/lib/browser';
import { OutputWidget } from '@theia/output/lib/browser/output-widget';
import {
CurrentSketch,
SketchesServiceClientImpl,
} from '../../../common/protocol/sketches-service-client-impl';
@injectable()
export class WidgetManager extends TheiaWidgetManager {
@inject(SketchesServiceClientImpl)
private readonly sketchesServiceClient: SketchesServiceClientImpl;
@postConstruct()
protected init(): void {
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.mainFileUri, ...sketch.rootFolderFileUris]);
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;
}
// Show the dirty indicator on uncloseable widgets when hovering over the title. Instead of showing the `X` for close.
const uncloseableClass = 'a-mod-uncloseable';
if (!title.className.includes(uncloseableClass)) {
title.className += title.className + ` ${uncloseableClass}`;
}
}
}