-
-
Notifications
You must be signed in to change notification settings - Fork 431
/
Copy pathplotter-frontend-contribution.ts
132 lines (121 loc) · 4.06 KB
/
plotter-frontend-contribution.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { Endpoint } from '@theia/core/lib/browser/endpoint';
import { ThemeService } from '@theia/core/lib/browser/theming';
import { Command, CommandRegistry } from '@theia/core/lib/common/command';
import { DisposableCollection } from '@theia/core/lib/common/disposable';
import type { MenuModelRegistry } from '@theia/core/lib/common/menu';
import { nls } from '@theia/core/lib/common/nls';
import { inject, injectable } from '@theia/core/shared/inversify';
import queryString from 'query-string';
import { MonitorManagerProxyClient } from '../../../common/protocol';
import { Contribution } from '../../contributions/contribution';
import { ArduinoMenus } from '../../menu/arduino-menus';
import { MonitorModel } from '../../monitor-model';
import { ArduinoToolbar } from '../../toolbar/arduino-toolbar';
export namespace SerialPlotterContribution {
export namespace Commands {
export const OPEN: Command = Command.toLocalizedCommand(
{
id: 'serial-plotter-open',
label: 'Serial Plotter',
category: 'Arduino',
},
'arduino/serial/openSerialPlotter'
);
export const RESET: Command = {
id: 'serial-plotter-reset',
};
export const OPEN_TOOLBAR: Command = {
id: 'serial-plotter-open-toolbar',
};
}
}
@injectable()
export class PlotterFrontendContribution extends Contribution {
private readonly endpointUrl = new Endpoint({ path: '/plotter' })
.getRestUrl()
.toString();
private readonly toDispose = new DisposableCollection();
private _plotterUrl: string | undefined;
@inject(MonitorModel)
private readonly model: MonitorModel;
@inject(ThemeService)
private readonly themeService: ThemeService;
@inject(MonitorManagerProxyClient)
private readonly monitorManagerProxy: MonitorManagerProxyClient;
override onStart(): void {
this.toDispose.push(
window.electronArduino.registerPlotterWindowCloseHandler(() => {
this._plotterUrl = undefined;
})
);
this.monitorManagerProxy.onMonitorShouldReset(() => this.reset());
}
override registerCommands(registry: CommandRegistry): void {
registry.registerCommand(SerialPlotterContribution.Commands.OPEN, {
execute: () => this.startPlotter(),
});
registry.registerCommand(SerialPlotterContribution.Commands.RESET, {
execute: () => this.reset(),
});
registry.registerCommand(
{ id: SerialPlotterContribution.Commands.OPEN_TOOLBAR.id },
{
isVisible: (widget) =>
ArduinoToolbar.is(widget) && widget.side === 'right',
execute: () => this.startPlotter(),
}
);
}
override registerMenus(menus: MenuModelRegistry): void {
menus.registerMenuAction(ArduinoMenus.TOOLS__MAIN_GROUP, {
commandId: SerialPlotterContribution.Commands.OPEN.id,
label: SerialPlotterContribution.Commands.OPEN.label,
order: '7',
});
}
private async startPlotter(forceReload = false): Promise<void> {
await this.monitorManagerProxy.startMonitor();
if (this._plotterUrl) {
window.electronArduino.showPlotterWindow({
url: this._plotterUrl,
forceReload,
});
return;
}
const wsPort = this.monitorManagerProxy.getWebSocketPort();
if (wsPort) {
this.open(wsPort);
} else {
this.messageService.error(
nls.localize(
'arduino/contributions/plotter/couldNotOpen',
"Couldn't open serial plotter"
)
);
}
}
private open(wsPort: number): void {
const initConfig = {
darkTheme: this.isDarkTheme,
wsPort,
serialPort: this.model.serialPort,
};
this._plotterUrl = queryString.stringifyUrl(
{
url: this.endpointUrl,
query: initConfig,
},
{ arrayFormat: 'comma' }
);
window.electronArduino.showPlotterWindow({ url: this._plotterUrl });
}
private get isDarkTheme(): boolean {
const themeType = this.themeService.getCurrentTheme().type;
return themeType === 'dark' || themeType === 'hc';
}
private async reset(): Promise<void> {
if (this._plotterUrl) {
await this.startPlotter(true);
}
}
}