Skip to content

Commit f92f875

Browse files
author
Alberto Iannaccone
committed
add settings to monitor model
1 parent a7433ee commit f92f875

File tree

6 files changed

+125
-33
lines changed

6 files changed

+125
-33
lines changed

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

+94-2
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,20 @@ export class MonitorModel implements FrontendApplicationContribution {
2626
protected _timestamp: boolean;
2727
protected _lineEnding: MonitorModel.EOL;
2828
protected _interpolate: boolean;
29+
protected _darkTheme: boolean;
30+
protected _wsPort: number;
31+
protected _serialPort: string;
32+
protected _connected: boolean;
2933

3034
constructor() {
3135
this._autoscroll = true;
3236
this._timestamp = false;
3337
this._interpolate = false;
3438
this._lineEnding = MonitorModel.EOL.DEFAULT;
39+
this._darkTheme = false;
40+
this._wsPort = 0;
41+
this._serialPort = '';
42+
this._connected = false;
3543

3644
this.onChangeEmitter = new Emitter<
3745
MonitorModel.State.Change<keyof MonitorModel.State>
@@ -60,6 +68,7 @@ export class MonitorModel implements FrontendApplicationContribution {
6068
this._timestamp = state.timestamp;
6169
this._lineEnding = state.lineEnding;
6270
this._interpolate = state.interpolate;
71+
this._serialPort = state.serialPort;
6372
}
6473

6574
protected async storeState(): Promise<void> {
@@ -68,6 +77,7 @@ export class MonitorModel implements FrontendApplicationContribution {
6877
timestamp: this._timestamp,
6978
lineEnding: this._lineEnding,
7079
interpolate: this._interpolate,
80+
serialPort: this._serialPort,
7181
});
7282
}
7383

@@ -151,16 +161,94 @@ export class MonitorModel implements FrontendApplicationContribution {
151161
);
152162
}
153163

164+
get darkTheme(): boolean {
165+
return this._darkTheme;
166+
}
167+
168+
set darkTheme(darkTheme: boolean) {
169+
if (darkTheme === this._darkTheme) return;
170+
this._darkTheme = darkTheme;
171+
this.monitorManagerProxy.changeSettings({
172+
monitorUISettings: { darkTheme },
173+
});
174+
this.onChangeEmitter.fire({
175+
property: 'darkTheme',
176+
value: this._darkTheme,
177+
});
178+
}
179+
180+
get wsPort(): number {
181+
return this._wsPort;
182+
}
183+
184+
set wsPort(wsPort: number) {
185+
if (wsPort === this._wsPort) return;
186+
this._wsPort = wsPort;
187+
this.monitorManagerProxy.changeSettings({
188+
monitorUISettings: { wsPort },
189+
});
190+
this.onChangeEmitter.fire({
191+
property: 'wsPort',
192+
value: this._wsPort,
193+
});
194+
}
195+
196+
get serialPort(): string {
197+
return this._serialPort;
198+
}
199+
200+
set serialPort(serialPort: string) {
201+
if (serialPort === this._serialPort) return;
202+
this._serialPort = serialPort;
203+
this.monitorManagerProxy.changeSettings({
204+
monitorUISettings: { serialPort },
205+
});
206+
this.storeState().then(() =>
207+
this.onChangeEmitter.fire({
208+
property: 'serialPort',
209+
value: this._serialPort,
210+
})
211+
);
212+
}
213+
214+
get connected(): boolean {
215+
return this._connected;
216+
}
217+
218+
set connected(connected: boolean) {
219+
if (connected === this._connected) return;
220+
this._connected = connected;
221+
this.monitorManagerProxy.changeSettings({
222+
monitorUISettings: { connected },
223+
});
224+
this.onChangeEmitter.fire({
225+
property: 'connected',
226+
value: this._connected,
227+
});
228+
}
229+
154230
protected onMonitorSettingsDidChange = (settings: MonitorSettings): void => {
155231
const { monitorUISettings } = settings;
156232
if (!monitorUISettings) return;
157-
const { autoscroll, interpolate, lineEnding, timestamp } =
158-
monitorUISettings;
233+
const {
234+
autoscroll,
235+
interpolate,
236+
lineEnding,
237+
timestamp,
238+
darkTheme,
239+
wsPort,
240+
serialPort,
241+
connected,
242+
} = monitorUISettings;
159243

160244
if (!isNullOrUndefined(autoscroll)) this.autoscroll = autoscroll;
161245
if (!isNullOrUndefined(interpolate)) this.interpolate = interpolate;
162246
if (!isNullOrUndefined(lineEnding)) this.lineEnding = lineEnding;
163247
if (!isNullOrUndefined(timestamp)) this.timestamp = timestamp;
248+
if (!isNullOrUndefined(darkTheme)) this.darkTheme = darkTheme;
249+
if (!isNullOrUndefined(wsPort)) this.wsPort = wsPort;
250+
if (!isNullOrUndefined(serialPort)) this.serialPort = serialPort;
251+
if (!isNullOrUndefined(connected)) this.connected = connected;
164252
};
165253
}
166254

@@ -171,6 +259,10 @@ export namespace MonitorModel {
171259
timestamp: boolean;
172260
lineEnding: EOL;
173261
interpolate: boolean;
262+
darkTheme: boolean;
263+
wsPort: number;
264+
serialPort: string;
265+
connected: boolean;
174266
}
175267
export namespace State {
176268
export interface Change<K extends keyof State> {

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

+1
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ export class MonitorWidget extends ReactWidget {
207207
<SerialMonitorSendInput
208208
boardsServiceProvider={this.boardsServiceProvider}
209209
monitorManagerProxy={this.monitorManagerProxy}
210+
monitorModel={this.monitorModel}
210211
resolveFocus={this.onFocusResolved}
211212
onSend={this.onSend}
212213
/>

arduino-ide-extension/src/browser/serial/monitor/serial-monitor-send-input.tsx

+8-20
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import { isOSX } from '@theia/core/lib/common/os';
55
import { DisposableCollection, nls } from '@theia/core/lib/common';
66
import { MonitorManagerProxyClient } from '../../../common/protocol';
77
import { BoardsServiceProvider } from '../../boards/boards-service-provider';
8-
import { timeout } from '@theia/core/lib/common/promise-util';
8+
import { MonitorModel } from '../../monitor-model';
99

1010
export namespace SerialMonitorSendInput {
1111
export interface Props {
1212
readonly boardsServiceProvider: BoardsServiceProvider;
1313
readonly monitorManagerProxy: MonitorManagerProxyClient;
14+
readonly monitorModel: MonitorModel;
1415
readonly onSend: (text: string) => void;
1516
readonly resolveFocus: (element: HTMLElement | undefined) => void;
1617
}
@@ -35,25 +36,12 @@ export class SerialMonitorSendInput extends React.Component<
3536
}
3637

3738
componentDidMount(): void {
38-
this.setState({ connected: true });
39-
40-
const checkWSConnection = new Promise<boolean>((resolve) => {
41-
this.props.monitorManagerProxy.onWSConnectionChanged((connected) => {
42-
this.setState({ connected });
43-
resolve(true);
44-
});
45-
});
46-
47-
const checkWSTimeout = timeout(1000).then(() => false);
48-
49-
Promise.race<boolean>([checkWSConnection, checkWSTimeout]).then(
50-
async (resolved) => {
51-
if (!resolved) {
52-
const connected =
53-
await this.props.monitorManagerProxy.isWSConnected();
54-
this.setState({ connected });
55-
}
56-
}
39+
this.setState({ connected: this.props.monitorModel.connected });
40+
this.toDisposeBeforeUnmount.push(
41+
this.props.monitorModel.onChange(({ property }) => {
42+
if (property === 'connected')
43+
this.setState({ connected: this.props.monitorModel.connected });
44+
})
5745
);
5846
}
5947

arduino-ide-extension/src/common/protocol/monitor-service.ts

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export namespace Monitor {
6565
CHANGE_SETTINGS = 'CHANGE_SETTINGS',
6666
}
6767

68+
// Commands sent by the backend to the clients
6869
export enum MiddlewareCommand {
6970
ON_SETTINGS_DID_CHANGE = 'ON_SETTINGS_DID_CHANGE',
7071
}

arduino-ide-extension/src/node/monitor-manager.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export class MonitorManager extends CoreClientAware {
6262
if (!monitor) {
6363
monitor = this.createMonitor(board, port);
6464
}
65-
return await monitor.start(monitorID);
65+
return await monitor.start();
6666
}
6767

6868
/**
@@ -142,7 +142,7 @@ export class MonitorManager extends CoreClientAware {
142142
return Status.NOT_CONNECTED;
143143
}
144144
monitor.setUploadInProgress(false);
145-
return await monitor.start(monitorID);
145+
return await monitor.start();
146146
}
147147

148148
/**

arduino-ide-extension/src/node/monitor-service.ts

+19-9
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,11 @@ export class MonitorService extends CoreClientAware implements Disposable {
123123
* Start and connects a monitor using currently set board and port.
124124
* If a monitor is already started or board fqbn, port address and/or protocol
125125
* are missing nothing happens.
126-
* @param id
127126
* @returns a status to verify connection has been established.
128127
*/
129-
async start(monitorID: string): Promise<Status> {
128+
async start(): Promise<Status> {
130129
if (this.duplex) {
130+
this.updateClientsSettings({ monitorUISettings: { connected: true } });
131131
return Status.ALREADY_CONNECTED;
132132
}
133133

@@ -152,7 +152,7 @@ export class MonitorService extends CoreClientAware implements Disposable {
152152
pluggableMonitorSettings: {
153153
...this.settings.pluggableMonitorSettings,
154154
...(await this.monitorSettingsProvider.getSettings(
155-
monitorID,
155+
this.monitorID,
156156
defaultSettings
157157
)),
158158
},
@@ -165,12 +165,14 @@ export class MonitorService extends CoreClientAware implements Disposable {
165165
this.duplex
166166
.on('close', () => {
167167
this.duplex = null;
168+
this.updateClientsSettings({ monitorUISettings: { connected: false } });
168169
this.logger.info(
169170
`monitor to ${this.port?.address} using ${this.port?.protocol} closed by client`
170171
);
171172
})
172173
.on('end', () => {
173174
this.duplex = null;
175+
this.updateClientsSettings({ monitorUISettings: { connected: false } });
174176
this.logger.info(
175177
`monitor to ${this.port?.address} using ${this.port?.protocol} closed by server`
176178
);
@@ -223,6 +225,7 @@ export class MonitorService extends CoreClientAware implements Disposable {
223225
this.logger.info(
224226
`started monitor to ${this.port?.address} using ${this.port?.protocol}`
225227
);
228+
this.updateClientsSettings({ monitorUISettings: { connected: true } });
226229
resolve(Status.OK);
227230
return;
228231
}
@@ -385,12 +388,10 @@ export class MonitorService extends CoreClientAware implements Disposable {
385388
}
386389
}
387390

388-
const command: Monitor.Message = {
389-
command: Monitor.MiddlewareCommand.ON_SETTINGS_DID_CHANGE,
390-
data: { ...settings, pluggableMonitorSettings: reconciledSettings },
391-
};
392-
393-
this.webSocketProvider.sendMessage(JSON.stringify(command));
391+
this.updateClientsSettings({
392+
...settings,
393+
pluggableMonitorSettings: reconciledSettings,
394+
});
394395

395396
if (!this.duplex) {
396397
return Status.NOT_CONNECTED;
@@ -439,6 +440,15 @@ export class MonitorService extends CoreClientAware implements Disposable {
439440
}
440441
}
441442

443+
updateClientsSettings(settings: MonitorSettings): void {
444+
const command: Monitor.Message = {
445+
command: Monitor.MiddlewareCommand.ON_SETTINGS_DID_CHANGE,
446+
data: settings,
447+
};
448+
449+
this.webSocketProvider.sendMessage(JSON.stringify(command));
450+
}
451+
442452
/**
443453
* Stops the necessary handlers to send and receive messages to
444454
* and from the frontend and the running monitor

0 commit comments

Comments
 (0)