1
1
import * as React from 'react' ;
2
2
import { Key , KeyCode } from '@theia/core/lib/browser/keys' ;
3
3
import { Board , Port } from '../../../common/protocol/boards-service' ;
4
- import { SerialConfig } from '../../../common/protocol/serial-service' ;
5
4
import { isOSX } from '@theia/core/lib/common/os' ;
6
- import { nls } from '@theia/core/lib/common' ;
5
+ import { DisposableCollection , nls } from '@theia/core/lib/common' ;
6
+ import { SerialConnectionManager } from '../serial-connection-manager' ;
7
+ import { SerialPlotter } from '../plotter/protocol' ;
7
8
8
9
export namespace SerialMonitorSendInput {
9
10
export interface Props {
10
- readonly serialConfig ?: SerialConfig ;
11
+ readonly serialConnection : SerialConnectionManager ;
11
12
readonly onSend : ( text : string ) => void ;
12
13
readonly resolveFocus : ( element : HTMLElement | undefined ) => void ;
13
14
}
14
15
export interface State {
15
16
text : string ;
17
+ connected : boolean ;
16
18
}
17
19
}
18
20
19
21
export class SerialMonitorSendInput extends React . Component <
20
22
SerialMonitorSendInput . Props ,
21
23
SerialMonitorSendInput . State
22
24
> {
25
+ protected toDisposeBeforeUnmount = new DisposableCollection ( ) ;
26
+
23
27
constructor ( props : Readonly < SerialMonitorSendInput . Props > ) {
24
28
super ( props ) ;
25
- this . state = { text : '' } ;
29
+ this . state = { text : '' , connected : false } ;
26
30
this . onChange = this . onChange . bind ( this ) ;
27
31
this . onSend = this . onSend . bind ( this ) ;
28
32
this . onKeyDown = this . onKeyDown . bind ( this ) ;
29
33
}
30
34
35
+ componentDidMount ( ) : void {
36
+ this . props . serialConnection . isBESerialConnected ( ) . then ( ( connected ) => {
37
+ this . setState ( { connected } ) ;
38
+ } ) ;
39
+
40
+ this . toDisposeBeforeUnmount . pushAll ( [
41
+ this . props . serialConnection . onRead ( ( { messages } ) => {
42
+ if (
43
+ messages . command ===
44
+ SerialPlotter . Protocol . Command . MIDDLEWARE_CONFIG_CHANGED &&
45
+ 'connected' in messages . data
46
+ ) {
47
+ this . setState ( { connected : messages . data . connected } ) ;
48
+ }
49
+ } ) ,
50
+ ] ) ;
51
+ }
52
+
53
+ componentWillUnmount ( ) : void {
54
+ // TODO: "Your preferred browser's local storage is almost full." Discard `content` before saving layout?
55
+ this . toDisposeBeforeUnmount . dispose ( ) ;
56
+ }
57
+
31
58
render ( ) : React . ReactNode {
32
59
return (
33
60
< input
34
61
ref = { this . setRef }
35
62
type = "text"
36
- className = { `theia-input ${ this . props . serialConfig ? '' : 'warning' } ` }
63
+ className = { `theia-input ${ this . state . connected ? '' : 'warning' } ` }
37
64
placeholder = { this . placeholder }
38
65
value = { this . state . text }
39
66
onChange = { this . onChange }
@@ -43,8 +70,8 @@ export class SerialMonitorSendInput extends React.Component<
43
70
}
44
71
45
72
protected get placeholder ( ) : string {
46
- const { serialConfig } = this . props ;
47
- if ( ! serialConfig ) {
73
+ const serialConfig = this . props . serialConnection . getConfig ( ) ;
74
+ if ( ! this . state . connected || ! serialConfig ) {
48
75
return nls . localize (
49
76
'arduino/serial/notConnected' ,
50
77
'Not connected. Select a board and a port to connect automatically.'
@@ -55,10 +82,12 @@ export class SerialMonitorSendInput extends React.Component<
55
82
'arduino/serial/message' ,
56
83
"Message ({0} + Enter to send message to '{1}' on '{2}'" ,
57
84
isOSX ? '⌘' : nls . localize ( 'vscode/keybindingLabels/ctrlKey' , 'Ctrl' ) ,
58
- Board . toString ( board , {
59
- useFqbn : false ,
60
- } ) ,
61
- Port . toString ( port )
85
+ board
86
+ ? Board . toString ( board , {
87
+ useFqbn : false ,
88
+ } )
89
+ : 'unknown' ,
90
+ port ? Port . toString ( port ) : 'unknown'
62
91
) ;
63
92
}
64
93
0 commit comments