Skip to content

Commit dc91725

Browse files
author
Akos Kitta
committed
fix: restored the plotter app
Signed-off-by: Akos Kitta <a.kitta@arduino.cc>
1 parent ccf4a27 commit dc91725

File tree

6 files changed

+162
-93
lines changed

6 files changed

+162
-93
lines changed
Lines changed: 44 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,17 @@
1+
import { Endpoint } from '@theia/core/lib/browser/endpoint';
12
import { ThemeService } from '@theia/core/lib/browser/theming';
2-
import { injectable, inject } from '@theia/core/shared/inversify';
3-
import {
4-
Command,
5-
CommandRegistry,
6-
MaybePromise,
7-
MenuModelRegistry,
8-
} from '@theia/core';
9-
import { ArduinoMenus } from '../../menu/arduino-menus';
10-
import { Contribution } from '../../contributions/contribution';
11-
import { Endpoint, FrontendApplication } from '@theia/core/lib/browser';
12-
// import { ipcRenderer } from '@theia/electron/shared/electron';
3+
import { Command, CommandRegistry } from '@theia/core/lib/common/command';
4+
import { DisposableCollection } from '@theia/core/lib/common/disposable';
5+
import type { MenuModelRegistry } from '@theia/core/lib/common/menu';
6+
import { nls } from '@theia/core/lib/common/nls';
7+
import { inject, injectable } from '@theia/core/shared/inversify';
8+
import queryString from 'query-string';
139
import { MonitorManagerProxyClient } from '../../../common/protocol';
14-
import { BoardsServiceProvider } from '../../boards/boards-service-provider';
10+
import { Contribution } from '../../contributions/contribution';
11+
import { ArduinoMenus } from '../../menu/arduino-menus';
1512
import { MonitorModel } from '../../monitor-model';
1613
import { ArduinoToolbar } from '../../toolbar/arduino-toolbar';
1714

18-
import { nls } from '@theia/core/lib/common/nls';
19-
// import {
20-
// CLOSE_PLOTTER_WINDOW,
21-
// SHOW_PLOTTER_WINDOW,
22-
// } from '../../../common/ipc-communication';
23-
24-
const queryString = require('query-string');
25-
2615
export namespace SerialPlotterContribution {
2716
export namespace Commands {
2817
export const OPEN: Command = Command.toLocalizedCommand(
@@ -45,38 +34,31 @@ export namespace SerialPlotterContribution {
4534

4635
@injectable()
4736
export class PlotterFrontendContribution extends Contribution {
48-
protected window: Window | null;
49-
protected url: string;
50-
protected wsPort: number;
37+
private readonly endpointUrl = new Endpoint({ path: '/plotter' })
38+
.getRestUrl()
39+
.toString();
40+
private readonly toDispose = new DisposableCollection();
41+
private _plotterUrl: string | undefined;
5142

5243
@inject(MonitorModel)
53-
protected readonly model: MonitorModel;
54-
44+
private readonly model: MonitorModel;
5545
@inject(ThemeService)
56-
protected readonly themeService: ThemeService;
57-
46+
private readonly themeService: ThemeService;
5847
@inject(MonitorManagerProxyClient)
59-
protected readonly monitorManagerProxy: MonitorManagerProxyClient;
60-
61-
@inject(BoardsServiceProvider)
62-
protected readonly boardsServiceProvider: BoardsServiceProvider;
63-
64-
override onStart(app: FrontendApplication): MaybePromise<void> {
65-
this.url = new Endpoint({ path: '/plotter' }).getRestUrl().toString();
48+
private readonly monitorManagerProxy: MonitorManagerProxyClient;
6649

67-
// ipcRenderer.on(CLOSE_PLOTTER_WINDOW, async () => {
68-
// if (!!this.window) {
69-
// this.window = null;
70-
// }
71-
// });
50+
override onStart(): void {
51+
this.toDispose.push(
52+
window.electronArduino.registerPlotterWindowCloseHandler(() => {
53+
this._plotterUrl = undefined;
54+
})
55+
);
7256
this.monitorManagerProxy.onMonitorShouldReset(() => this.reset());
73-
74-
return super.onStart(app);
7557
}
7658

7759
override registerCommands(registry: CommandRegistry): void {
7860
registry.registerCommand(SerialPlotterContribution.Commands.OPEN, {
79-
execute: this.startPlotter.bind(this),
61+
execute: () => this.startPlotter(),
8062
});
8163
registry.registerCommand(SerialPlotterContribution.Commands.RESET, {
8264
execute: () => this.reset(),
@@ -86,7 +68,7 @@ export class PlotterFrontendContribution extends Contribution {
8668
{
8769
isVisible: (widget) =>
8870
ArduinoToolbar.is(widget) && widget.side === 'right',
89-
execute: this.startPlotter.bind(this),
71+
execute: () => this.startPlotter(),
9072
}
9173
);
9274
}
@@ -99,10 +81,13 @@ export class PlotterFrontendContribution extends Contribution {
9981
});
10082
}
10183

102-
async startPlotter(): Promise<void> {
84+
private async startPlotter(forceReload = false): Promise<void> {
10385
await this.monitorManagerProxy.startMonitor();
104-
if (!!this.window) {
105-
// ipcRenderer.send(SHOW_PLOTTER_WINDOW);
86+
if (this._plotterUrl) {
87+
window.electronArduino.showPlotterWindow({
88+
url: this._plotterUrl,
89+
forceReload,
90+
});
10691
return;
10792
}
10893
const wsPort = this.monitorManagerProxy.getWebSocketPort();
@@ -118,26 +103,30 @@ export class PlotterFrontendContribution extends Contribution {
118103
}
119104
}
120105

121-
protected async open(wsPort: number): Promise<void> {
106+
private open(wsPort: number): void {
122107
const initConfig = {
123-
darkTheme: this.themeService.getCurrentTheme().type === 'dark',
108+
darkTheme: this.isDarkTheme,
124109
wsPort,
125110
serialPort: this.model.serialPort,
126111
};
127-
const urlWithParams = queryString.stringifyUrl(
112+
this._plotterUrl = queryString.stringifyUrl(
128113
{
129-
url: this.url,
114+
url: this.endpointUrl,
130115
query: initConfig,
131116
},
132117
{ arrayFormat: 'comma' }
133118
);
134-
this.window = window.open(urlWithParams, 'serialPlotter');
119+
window.electronArduino.showPlotterWindow({ url: this._plotterUrl });
120+
}
121+
122+
private get isDarkTheme(): boolean {
123+
const themeType = this.themeService.getCurrentTheme().type;
124+
return themeType === 'dark' || themeType === 'hc';
135125
}
136126

137-
protected async reset(): Promise<void> {
138-
if (!!this.window) {
139-
this.window.close();
140-
await this.startPlotter();
127+
private async reset(): Promise<void> {
128+
if (this._plotterUrl) {
129+
await this.startPlotter(true);
141130
}
142131
}
143132
}

arduino-ide-extension/src/browser/utils/dom.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { notEmpty } from '@theia/core';
1+
import { notEmpty } from '@theia/core/lib/common/objects';
22

33
/**
44
* Finds the closest child HTMLButtonElement representing a Theia button.

arduino-ide-extension/src/common/ipc-communication.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

arduino-ide-extension/src/electron-browser/preload.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ import {
1010
CHANNEL_APP_VERSION,
1111
CHANNEL_CLOSE_WINDOW,
1212
CHANNEL_IS_FIRST_WINDOW,
13+
CHANNEL_PLOTTER_WINDOW_DID_CLOSE,
1314
CHANNEL_QUIT_APP,
1415
CHANNEL_SCHEDULE_DELETION,
1516
CHANNEL_SEND_STARTUP_TASKS,
1617
CHANNEL_SHOW_MESSAGE_BOX,
1718
CHANNEL_SHOW_OPEN_DIALOG,
19+
CHANNEL_SHOW_PLOTTER_WINDOW,
1820
CHANNEL_SHOW_SAVE_DIALOG,
1921
ElectronArduino,
2022
MessageBoxOptions,
@@ -57,9 +59,18 @@ const api: ElectronArduino = {
5759
},
5860
scheduleDeletion: (sketch: Sketch) =>
5961
ipcRenderer.invoke(CHANNEL_SCHEDULE_DELETION, sketch),
62+
showPlotterWindow: (params: { url: string; forceReload?: boolean }) =>
63+
ipcRenderer.send(CHANNEL_SHOW_PLOTTER_WINDOW, params),
64+
registerPlotterWindowCloseHandler: (handler: () => MaybePromise<void>) => {
65+
const listener = () => handler();
66+
ipcRenderer.on(CHANNEL_PLOTTER_WINDOW_DID_CLOSE, listener);
67+
return Disposable.create(() =>
68+
ipcRenderer.removeListener(CHANNEL_PLOTTER_WINDOW_DID_CLOSE, listener)
69+
);
70+
},
6071
};
6172

6273
export function preload(): void {
63-
console.log('Exposing Arduino IDE electron API');
6474
contextBridge.exposeInMainWorld('electronArduino', api);
75+
console.log('Exposed Arduino IDE electron API');
6576
}

arduino-ide-extension/src/electron-common/electron-arduino.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,21 @@ export type OpenDialogReturnValue = ElectronOpenDialogReturnValue;
2121
export type SaveDialogOptions = ElectronSaveDialogOptions;
2222
export type SaveDialogReturnValue = ElectronSaveDialogReturnValue;
2323

24+
export interface ShowPlotterWindowParams {
25+
readonly url: string;
26+
readonly forceReload?: boolean;
27+
}
28+
export function isShowPlotterWindowParams(
29+
arg: unknown
30+
): arg is ShowPlotterWindowParams {
31+
return (
32+
typeof arg === 'object' &&
33+
typeof (<ShowPlotterWindowParams>arg).url === 'string' &&
34+
((<ShowPlotterWindowParams>arg).forceReload === undefined ||
35+
typeof (<ShowPlotterWindowParams>arg).forceReload === 'boolean')
36+
);
37+
}
38+
2439
export interface ElectronArduino {
2540
showMessageBox(options: MessageBoxOptions): Promise<MessageBoxReturnValue>;
2641
showOpenDialog(options: OpenDialogOptions): Promise<OpenDialogReturnValue>;
@@ -34,6 +49,10 @@ export interface ElectronArduino {
3449
handler: (tasks: StartupTasks) => MaybePromise<void>
3550
): Disposable;
3651
scheduleDeletion(sketch: Sketch): void;
52+
showPlotterWindow(params: { url: string; forceReload?: boolean }): void;
53+
registerPlotterWindowCloseHandler(
54+
handler: () => MaybePromise<void>
55+
): Disposable;
3756
}
3857

3958
declare global {
@@ -51,5 +70,7 @@ export const CHANNEL_QUIT_APP = 'Arduino:QuitApp';
5170
export const CHANNEL_CLOSE_WINDOW = 'Arduino:CloseWindow';
5271
export const CHANNEL_IS_FIRST_WINDOW = 'Arduino:IsFirstWindow';
5372
export const CHANNEL_SCHEDULE_DELETION = 'Arduino:ScheduleDeletion';
73+
export const CHANNEL_SHOW_PLOTTER_WINDOW = 'Arduino:ShowPlotterWindow';
5474
// main to renderer
5575
export const CHANNEL_SEND_STARTUP_TASKS = 'Arduino:SendStartupTasks';
76+
export const CHANNEL_PLOTTER_WINDOW_DID_CLOSE = 'Arduino:PlotterWindowDidClose';

0 commit comments

Comments
 (0)