Skip to content

Commit 9ea51b2

Browse files
author
Alberto Iannaccone
committed
fix monitor settings client communication
1 parent 077dff8 commit 9ea51b2

File tree

10 files changed

+210
-106
lines changed

10 files changed

+210
-106
lines changed

arduino-ide-extension/src/browser/arduino-frontend-contribution.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ import {
4646
} from '@theia/editor/lib/browser';
4747
import { ProblemContribution } from '@theia/markers/lib/browser/problem/problem-contribution';
4848
import { MonacoMenus } from '@theia/monaco/lib/browser/monaco-menu';
49-
import { FileNavigatorCommands, FileNavigatorContribution } from '@theia/navigator/lib/browser/navigator-contribution';
49+
import {
50+
FileNavigatorCommands,
51+
FileNavigatorContribution,
52+
} from '@theia/navigator/lib/browser/navigator-contribution';
5053
import { OutlineViewContribution } from '@theia/outline-view/lib/browser/outline-view-contribution';
5154
import { OutputContribution } from '@theia/output/lib/browser/output-contribution';
5255
import { ScmContribution } from '@theia/scm/lib/browser/scm-contribution';
@@ -84,7 +87,8 @@ export class ArduinoFrontendContribution
8487
TabBarToolbarContribution,
8588
CommandContribution,
8689
MenuContribution,
87-
ColorContribution {
90+
ColorContribution
91+
{
8892
@inject(ILogger)
8993
protected logger: ILogger;
9094

@@ -157,6 +161,7 @@ export class ArduinoFrontendContribution
157161
@inject(SketchesServiceClientImpl)
158162
protected readonly sketchServiceClient: SketchesServiceClientImpl;
159163

164+
@inject(FrontendApplicationStateService)
160165
protected readonly appStateService: FrontendApplicationStateService;
161166

162167
@inject(LocalStorageService)
@@ -344,7 +349,7 @@ export class ArduinoFrontendContribution
344349

345350
app.shell.leftPanelHandler.removeBottomMenu('settings-menu');
346351

347-
this.fileSystemFrontendContribution.onDidChangeEditorFile(e => {
352+
this.fileSystemFrontendContribution.onDidChangeEditorFile((e) => {
348353
if (e.type === FileChangeType.DELETED) {
349354
const editorWidget = e.editor;
350355
if (SaveableWidget.is(editorWidget)) {
@@ -494,7 +499,7 @@ export class ArduinoFrontendContribution
494499
EditorCommands.SPLIT_EDITOR_UP,
495500
EditorCommands.SPLIT_EDITOR_VERTICAL,
496501
EditorCommands.SPLIT_EDITOR_HORIZONTAL,
497-
FileNavigatorCommands.REVEAL_IN_NAVIGATOR
502+
FileNavigatorCommands.REVEAL_IN_NAVIGATOR,
498503
]) {
499504
registry.unregisterCommand(command);
500505
}

arduino-ide-extension/src/browser/monitor-manager-proxy-client-impl.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ export class MonitorManagerProxyClientImpl
2424
}>();
2525
readonly onMessagesReceived = this.onMessagesReceivedEmitter.event;
2626

27+
protected readonly onMonitorSettingsDidChangeEmitter =
28+
new Emitter<MonitorSettings>();
29+
readonly onMonitorSettingsDidChange =
30+
this.onMonitorSettingsDidChangeEmitter.event;
31+
2732
protected readonly onWSConnectionChangedEmitter = new Emitter<boolean>();
2833
readonly onWSConnectionChanged = this.onWSConnectionChangedEmitter.event;
2934

@@ -61,9 +66,11 @@ export class MonitorManagerProxyClientImpl
6166
return;
6267
}
6368

64-
this.webSocket.onmessage = (res) => {
65-
const messages = JSON.parse(res.data);
66-
this.onMessagesReceivedEmitter.fire({ messages });
69+
this.webSocket.onmessage = (message) => {
70+
const parsedMessage = JSON.parse(message.data);
71+
if (Array.isArray(parsedMessage))
72+
this.onMessagesReceivedEmitter.fire({ messages: parsedMessage });
73+
else this.onMonitorSettingsDidChangeEmitter.fire(parsedMessage);
6774
};
6875
this.wsPort = addressPort;
6976
}
@@ -93,7 +100,7 @@ export class MonitorManagerProxyClientImpl
93100
return this.server().startMonitor(board, port, settings);
94101
}
95102

96-
getCurrentSettings(board: Board, port: Port): MonitorSettings {
103+
getCurrentSettings(board: Board, port: Port): Promise<MonitorSettings> {
97104
return this.server().getCurrentSettings(board, port);
98105
}
99106

arduino-ide-extension/src/browser/monitor-model.ts

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import {
44
LocalStorageService,
55
} from '@theia/core/lib/browser';
66
import { inject, injectable } from '@theia/core/shared/inversify';
7+
import { MonitorManagerProxyClient } from '../common/protocol';
8+
import { isNullOrUndefined } from '../common/utils';
9+
import { MonitorSettings } from '../node/monitor-settings/monitor-settings-provider';
710

811
@injectable()
912
export class MonitorModel implements FrontendApplicationContribution {
@@ -12,6 +15,9 @@ export class MonitorModel implements FrontendApplicationContribution {
1215
@inject(LocalStorageService)
1316
protected readonly localStorageService: LocalStorageService;
1417

18+
@inject(MonitorManagerProxyClient)
19+
protected readonly monitorManagerProxy: MonitorManagerProxyClient;
20+
1521
protected readonly onChangeEmitter: Emitter<
1622
MonitorModel.State.Change<keyof MonitorModel.State>
1723
>;
@@ -35,7 +41,11 @@ export class MonitorModel implements FrontendApplicationContribution {
3541
onStart(): void {
3642
this.localStorageService
3743
.getData<MonitorModel.State>(MonitorModel.STORAGE_ID)
38-
.then(this.restoreState);
44+
.then(this.restoreState.bind(this));
45+
46+
this.monitorManagerProxy.onMonitorSettingsDidChange(
47+
this.onMonitorSettingsDidChange.bind(this)
48+
);
3949
}
4050

4151
get onChange(): Event<MonitorModel.State.Change<keyof MonitorModel.State>> {
@@ -65,22 +75,34 @@ export class MonitorModel implements FrontendApplicationContribution {
6575
return this._autoscroll;
6676
}
6777

68-
toggleAutoscroll(): void {
69-
this._autoscroll = !this._autoscroll;
78+
set autoscroll(autoscroll: boolean) {
79+
if (autoscroll === this._autoscroll) return;
80+
this._autoscroll = autoscroll;
81+
this.monitorManagerProxy.changeSettings({
82+
monitorUISettings: { autoscroll },
83+
});
7084
this.storeState().then(() => {
7185
this.onChangeEmitter.fire({
7286
property: 'autoscroll',
73-
value: this._timestamp,
87+
value: this._autoscroll,
7488
});
7589
});
7690
}
7791

92+
toggleAutoscroll(): void {
93+
this.autoscroll = !this._autoscroll;
94+
}
95+
7896
get timestamp(): boolean {
7997
return this._timestamp;
8098
}
8199

82-
toggleTimestamp(): void {
83-
this._timestamp = !this._timestamp;
100+
set timestamp(timestamp: boolean) {
101+
if (timestamp === this._timestamp) return;
102+
this._timestamp = timestamp;
103+
this.monitorManagerProxy.changeSettings({
104+
monitorUISettings: { timestamp },
105+
});
84106
this.storeState().then(() =>
85107
this.onChangeEmitter.fire({
86108
property: 'timestamp',
@@ -89,12 +111,20 @@ export class MonitorModel implements FrontendApplicationContribution {
89111
);
90112
}
91113

114+
toggleTimestamp(): void {
115+
this.timestamp = !this._timestamp;
116+
}
117+
92118
get lineEnding(): MonitorModel.EOL {
93119
return this._lineEnding;
94120
}
95121

96122
set lineEnding(lineEnding: MonitorModel.EOL) {
123+
if (lineEnding === this._lineEnding) return;
97124
this._lineEnding = lineEnding;
125+
this.monitorManagerProxy.changeSettings({
126+
monitorUISettings: { lineEnding },
127+
});
98128
this.storeState().then(() =>
99129
this.onChangeEmitter.fire({
100130
property: 'lineEnding',
@@ -107,15 +137,31 @@ export class MonitorModel implements FrontendApplicationContribution {
107137
return this._interpolate;
108138
}
109139

110-
set interpolate(i: boolean) {
111-
this._interpolate = i;
140+
set interpolate(interpolate: boolean) {
141+
if (interpolate === this._interpolate) return;
142+
this._interpolate = interpolate;
143+
this.monitorManagerProxy.changeSettings({
144+
monitorUISettings: { interpolate },
145+
});
112146
this.storeState().then(() =>
113147
this.onChangeEmitter.fire({
114148
property: 'interpolate',
115149
value: this._interpolate,
116150
})
117151
);
118152
}
153+
154+
protected onMonitorSettingsDidChange = (settings: MonitorSettings): void => {
155+
const { monitorUISettings } = settings;
156+
if (!monitorUISettings) return;
157+
const { autoscroll, interpolate, lineEnding, timestamp } =
158+
monitorUISettings;
159+
160+
if (!isNullOrUndefined(autoscroll)) this.autoscroll = autoscroll;
161+
if (!isNullOrUndefined(interpolate)) this.interpolate = interpolate;
162+
if (!isNullOrUndefined(lineEnding)) this.lineEnding = lineEnding;
163+
if (!isNullOrUndefined(timestamp)) this.timestamp = timestamp;
164+
};
119165
}
120166

121167
// TODO: Move this to /common

arduino-ide-extension/src/browser/serial/monitor/monitor-widget.tsx

Lines changed: 61 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from 'react';
2-
import { postConstruct, injectable, inject } from 'inversify';
2+
import { injectable, inject } from 'inversify';
33
import { OptionsType } from 'react-select/src/types';
44
import { Emitter } from '@theia/core/lib/common/event';
55
import { Disposable } from '@theia/core/lib/common/disposable';
@@ -26,6 +26,8 @@ export class MonitorWidget extends ReactWidget {
2626
);
2727
static readonly ID = 'serial-monitor';
2828

29+
protected settings: MonitorSettings = {};
30+
2931
protected widgetHeight: number;
3032

3133
/**
@@ -82,10 +84,24 @@ export class MonitorWidget extends ReactWidget {
8284
);
8385
}
8486

85-
@postConstruct()
86-
protected init(): void {
87+
protected onAfterAttach(msg: Message): void {
8788
this.update();
8889
this.toDispose.push(this.monitorModel.onChange(() => this.update()));
90+
this.getCurrentSettings().then(this.onMonitorSettingsDidChange.bind(this));
91+
this.monitorManagerProxy.onMonitorSettingsDidChange(
92+
this.onMonitorSettingsDidChange.bind(this)
93+
);
94+
}
95+
96+
onMonitorSettingsDidChange(settings: MonitorSettings): void {
97+
this.settings = {
98+
...this.settings,
99+
pluggableMonitorSettings: {
100+
...this.settings.pluggableMonitorSettings,
101+
...settings.pluggableMonitorSettings,
102+
},
103+
};
104+
this.update();
89105
}
90106

91107
clearConsole(): void {
@@ -157,56 +173,33 @@ export class MonitorWidget extends ReactWidget {
157173
];
158174
}
159175

160-
private getCurrentSettings(): MonitorSettings {
176+
private getCurrentSettings(): Promise<MonitorSettings> {
161177
const board = this.boardsServiceProvider.boardsConfig.selectedBoard;
162178
const port = this.boardsServiceProvider.boardsConfig.selectedPort;
163179
if (!board || !port) {
164-
return {};
180+
return Promise.resolve(this.settings || {});
165181
}
166182
return this.monitorManagerProxy.getCurrentSettings(board, port);
167183
}
168184

169-
//////////////////////////////////////////////////
170-
////////////////////IMPORTANT/////////////////////
171-
//////////////////////////////////////////////////
172-
// baudRates and selectedBaudRates as of now are hardcoded
173-
// like this to retrieve the baudrate settings from the ones
174-
// received by the monitor.
175-
// We're doing it like since the frontend as of now doesn't
176-
// support a fully customizable list of options that would
177-
// be require to support pluggable monitors completely.
178-
// As soon as the frontend UI is updated to support
179-
// any custom settings this methods MUST be removed and
180-
// made generic.
181-
//
182-
// This breaks if the user tries to open a monitor that
183-
// doesn't support the baudrate setting.
184-
protected get baudRates(): string[] {
185-
const { pluggableMonitorSettings } = this.getCurrentSettings();
186-
if (!pluggableMonitorSettings || !pluggableMonitorSettings['baudrate']) {
187-
return [];
188-
}
189-
190-
const baudRateSettings = pluggableMonitorSettings['baudrate'];
191-
192-
return baudRateSettings.values;
193-
}
185+
protected render(): React.ReactNode {
186+
const baudrate = this.settings?.pluggableMonitorSettings
187+
? this.settings.pluggableMonitorSettings.baudrate
188+
: undefined;
194189

195-
protected get selectedBaudRate(): string {
196-
const { pluggableMonitorSettings } = this.getCurrentSettings();
197-
if (!pluggableMonitorSettings || !pluggableMonitorSettings['baudrate']) {
198-
return '';
199-
}
200-
const baudRateSettings = pluggableMonitorSettings['baudrate'];
201-
return baudRateSettings.selectedValue;
202-
}
190+
const baudrateOptions = baudrate?.values.map((b) => ({
191+
label: b + ' baud',
192+
value: b,
193+
}));
194+
const baudrateSelectedOption = baudrateOptions?.find(
195+
(b) => b.value === baudrate?.selectedValue
196+
);
203197

204-
protected render(): React.ReactNode {
205-
const { baudRates, lineEndings } = this;
206198
const lineEnding =
207-
lineEndings.find((item) => item.value === this.monitorModel.lineEnding) ||
208-
lineEndings[1]; // Defaults to `\n`.
209-
const baudRate = baudRates.find((item) => item === this.selectedBaudRate);
199+
this.lineEndings.find(
200+
(item) => item.value === this.monitorModel.lineEnding
201+
) || this.lineEndings[1]; // Defaults to `\n`.
202+
210203
return (
211204
<div className="serial-monitor">
212205
<div className="head">
@@ -222,20 +215,22 @@ export class MonitorWidget extends ReactWidget {
222215
<div className="select">
223216
<ArduinoSelect
224217
maxMenuHeight={this.widgetHeight - 40}
225-
options={lineEndings}
218+
options={this.lineEndings}
226219
value={lineEnding}
227220
onChange={this.onChangeLineEnding}
228221
/>
229222
</div>
230-
<div className="select">
231-
<ArduinoSelect
232-
className="select"
233-
maxMenuHeight={this.widgetHeight - 40}
234-
options={baudRates}
235-
value={baudRate}
236-
onChange={this.onChangeBaudRate}
237-
/>
238-
</div>
223+
{baudrateOptions && baudrateSelectedOption && (
224+
<div className="select">
225+
<ArduinoSelect
226+
className="select"
227+
maxMenuHeight={this.widgetHeight - 40}
228+
options={baudrateOptions}
229+
value={baudrateSelectedOption}
230+
onChange={this.onChangeBaudRate}
231+
/>
232+
</div>
233+
)}
239234
</div>
240235
</div>
241236
<div className="body">
@@ -257,16 +252,21 @@ export class MonitorWidget extends ReactWidget {
257252

258253
protected readonly onChangeLineEnding = (
259254
option: SerialMonitorOutput.SelectOption<MonitorModel.EOL>
260-
) => {
255+
): void => {
261256
this.monitorModel.lineEnding = option.value;
262257
};
263258

264-
protected readonly onChangeBaudRate = (value: string) => {
265-
const { pluggableMonitorSettings } = this.getCurrentSettings();
266-
if (!pluggableMonitorSettings || !pluggableMonitorSettings['baudrate'])
267-
return;
268-
const baudRateSettings = pluggableMonitorSettings['baudrate'];
269-
baudRateSettings.selectedValue = value;
270-
this.monitorManagerProxy.changeSettings(pluggableMonitorSettings);
259+
protected readonly onChangeBaudRate = ({
260+
value,
261+
}: {
262+
value: string;
263+
}): void => {
264+
this.getCurrentSettings().then(({ pluggableMonitorSettings }) => {
265+
if (!pluggableMonitorSettings || !pluggableMonitorSettings['baudrate'])
266+
return;
267+
const baudRateSettings = pluggableMonitorSettings['baudrate'];
268+
baudRateSettings.selectedValue = value;
269+
this.monitorManagerProxy.changeSettings({ pluggableMonitorSettings });
270+
});
271271
};
272272
}

0 commit comments

Comments
 (0)