1
1
import * as React from 'react' ;
2
- import { postConstruct , injectable , inject } from 'inversify' ;
2
+ import { injectable , inject } from 'inversify' ;
3
3
import { OptionsType } from 'react-select/src/types' ;
4
4
import { Emitter } from '@theia/core/lib/common/event' ;
5
5
import { Disposable } from '@theia/core/lib/common/disposable' ;
@@ -26,6 +26,8 @@ export class MonitorWidget extends ReactWidget {
26
26
) ;
27
27
static readonly ID = 'serial-monitor' ;
28
28
29
+ protected settings : MonitorSettings = { } ;
30
+
29
31
protected widgetHeight : number ;
30
32
31
33
/**
@@ -82,10 +84,24 @@ export class MonitorWidget extends ReactWidget {
82
84
) ;
83
85
}
84
86
85
- @postConstruct ( )
86
- protected init ( ) : void {
87
+ protected onAfterAttach ( msg : Message ) : void {
87
88
this . update ( ) ;
88
89
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 ( ) ;
89
105
}
90
106
91
107
clearConsole ( ) : void {
@@ -157,56 +173,33 @@ export class MonitorWidget extends ReactWidget {
157
173
] ;
158
174
}
159
175
160
- private getCurrentSettings ( ) : MonitorSettings {
176
+ private getCurrentSettings ( ) : Promise < MonitorSettings > {
161
177
const board = this . boardsServiceProvider . boardsConfig . selectedBoard ;
162
178
const port = this . boardsServiceProvider . boardsConfig . selectedPort ;
163
179
if ( ! board || ! port ) {
164
- return { } ;
180
+ return Promise . resolve ( this . settings || { } ) ;
165
181
}
166
182
return this . monitorManagerProxy . getCurrentSettings ( board , port ) ;
167
183
}
168
184
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 ;
194
189
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
+ ) ;
203
197
204
- protected render ( ) : React . ReactNode {
205
- const { baudRates, lineEndings } = this ;
206
198
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
+
210
203
return (
211
204
< div className = "serial-monitor" >
212
205
< div className = "head" >
@@ -222,20 +215,22 @@ export class MonitorWidget extends ReactWidget {
222
215
< div className = "select" >
223
216
< ArduinoSelect
224
217
maxMenuHeight = { this . widgetHeight - 40 }
225
- options = { lineEndings }
218
+ options = { this . lineEndings }
226
219
value = { lineEnding }
227
220
onChange = { this . onChangeLineEnding }
228
221
/>
229
222
</ 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
+ ) }
239
234
</ div >
240
235
</ div >
241
236
< div className = "body" >
@@ -257,16 +252,21 @@ export class MonitorWidget extends ReactWidget {
257
252
258
253
protected readonly onChangeLineEnding = (
259
254
option : SerialMonitorOutput . SelectOption < MonitorModel . EOL >
260
- ) => {
255
+ ) : void => {
261
256
this . monitorModel . lineEnding = option . value ;
262
257
} ;
263
258
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
+ } ) ;
271
271
} ;
272
272
}
0 commit comments