Skip to content

Commit a89ed67

Browse files
author
Alberto Iannaccone
committed
spawn new window where to instantiate serial plotter app
1 parent 7503739 commit a89ed67

File tree

4 files changed

+93
-1
lines changed

4 files changed

+93
-1
lines changed

arduino-ide-extension/src/browser/arduino-ide-frontend-module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ import {
253253
UploadCertificateDialogProps,
254254
UploadCertificateDialogWidget,
255255
} from './dialogs/certificate-uploader/certificate-uploader-dialog';
256+
import { PlotterContribution } from './plotter/plotter-contribution';
256257

257258
const ElementQueries = require('css-element-queries/src/ElementQueries');
258259

@@ -596,6 +597,7 @@ export default new ContainerModule((bind, unbind, isBound, rebind) => {
596597
Contribution.configure(bind, AddFile);
597598
Contribution.configure(bind, ArchiveSketch);
598599
Contribution.configure(bind, AddZipLibrary);
600+
Contribution.configure(bind, PlotterContribution);
599601

600602
bind(ResponseServiceImpl)
601603
.toSelf()

arduino-ide-extension/src/browser/menu/arduino-menus.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export namespace ArduinoMenus {
8686

8787
// -- Tools
8888
export const TOOLS = [...MAIN_MENU_BAR, '4_tools'];
89-
// `Auto Format`, `Archive Sketch`, `Manage Libraries...`, `Serial Monitor`
89+
// `Auto Format`, `Archive Sketch`, `Manage Libraries...`, `Serial Monitor`, Serial Plotter
9090
export const TOOLS__MAIN_GROUP = [...TOOLS, '0_main'];
9191
// `WiFi101 / WiFiNINA Firmware Updater`
9292
export const TOOLS__FIRMWARE_UPLOADER_GROUP = [
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { injectable, inject } from 'inversify';
2+
import { Command, CommandRegistry, MenuModelRegistry } from '@theia/core';
3+
import { MonitorModel } from '../monitor/monitor-model';
4+
import { ArduinoMenus } from '../menu/arduino-menus';
5+
import { Contribution } from '../contributions/contribution';
6+
import { MonitorConfig } from '../../common/protocol';
7+
8+
export namespace SerialPlotter {
9+
export namespace Commands {
10+
export const OPEN: Command = {
11+
id: 'serial-plotter-open',
12+
label: 'Serial Plotter',
13+
category: 'Arduino',
14+
};
15+
}
16+
export type InitMessage = {
17+
baudrate: MonitorConfig.BaudRate;
18+
darkTheme: boolean;
19+
wsPort: number;
20+
};
21+
}
22+
23+
@injectable()
24+
export class PlotterContribution extends Contribution {
25+
protected window: Window | null;
26+
27+
@inject(MonitorModel)
28+
protected readonly model: MonitorModel;
29+
30+
registerCommands(registry: CommandRegistry): void {
31+
registry.registerCommand(SerialPlotter.Commands.OPEN, {
32+
execute: async () => this.open(),
33+
});
34+
}
35+
36+
registerMenus(menus: MenuModelRegistry): void {
37+
menus.registerMenuAction(ArduinoMenus.TOOLS__MAIN_GROUP, {
38+
commandId: SerialPlotter.Commands.OPEN.id,
39+
label: SerialPlotter.Commands.OPEN.label,
40+
order: '7',
41+
});
42+
}
43+
44+
protected async open(): Promise<void> {
45+
const url = 'http://localhost:8080/index.html';
46+
if (this.window) {
47+
this.window.focus();
48+
} else {
49+
this.window = window.open(url, 'serialPlotter');
50+
window.addEventListener('message', handlePostMessage);
51+
const initMessage: SerialPlotter.InitMessage = {
52+
baudrate: this.model.baudRate,
53+
darkTheme: false,
54+
wsPort: 0,
55+
};
56+
if (this.window) {
57+
this.window.postMessage(initMessage, url);
58+
this.window.onclose = () => {
59+
window.removeEventListener('message', handlePostMessage);
60+
this.window = null;
61+
};
62+
}
63+
}
64+
}
65+
}
66+
67+
const handlePostMessage = (event: MessageEvent) => {
68+
console.log(event.data);
69+
};

arduino-ide-extension/src/electron-main/theia/electron-main-application.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import {
1818
} from '@theia/core/lib/electron-main/electron-main-application';
1919
import { SplashServiceImpl } from '../splash/splash-service-impl';
2020

21+
app.commandLine.appendSwitch('disable-http-cache');
22+
2123
@injectable()
2224
export class ElectronMainApplication extends TheiaElectronMainApplication {
2325
protected _windows: BrowserWindow[] = [];
@@ -88,6 +90,25 @@ export class ElectronMainApplication extends TheiaElectronMainApplication {
8890
this.splashService.onCloseRequested
8991
);
9092
}
93+
94+
electronWindow.webContents.on(
95+
'new-window',
96+
(event, url, frameName, disposition, options, additionalFeatures) => {
97+
if (frameName === 'serialPlotter') {
98+
event.preventDefault();
99+
Object.assign(options, {
100+
webPreferences: {
101+
devTools: true,
102+
nativeWindowOpen: true,
103+
openerId: electronWindow?.webContents.id,
104+
},
105+
});
106+
event.newGuest = new BrowserWindow(options);
107+
event.newGuest?.loadURL(url);
108+
}
109+
}
110+
);
111+
91112
this._windows.push(electronWindow);
92113
electronWindow.on('closed', () => {
93114
if (electronWindow) {

0 commit comments

Comments
 (0)