1
+ import { Endpoint } from '@theia/core/lib/browser/endpoint' ;
1
2
import { ThemeService } from '@theia/core/lib/browser/theming' ;
2
- import { injectable , inject } from '@theia/core/shared/inversify' ;
3
- import {
4
- Command ,
5
- CommandRegistry ,
6
- MaybePromise ,
7
- MenuModelRegistry ,
8
- } from '@theia/core' ;
9
- import { ArduinoMenus } from '../../menu/arduino-menus' ;
10
- import { Contribution } from '../../contributions/contribution' ;
11
- import { Endpoint , FrontendApplication } from '@theia/core/lib/browser' ;
12
- // import { ipcRenderer } from '@theia/electron/shared/electron';
3
+ import { Command , CommandRegistry } from '@theia/core/lib/common/command' ;
4
+ import { DisposableCollection } from '@theia/core/lib/common/disposable' ;
5
+ import type { MenuModelRegistry } from '@theia/core/lib/common/menu' ;
6
+ import { nls } from '@theia/core/lib/common/nls' ;
7
+ import { inject , injectable } from '@theia/core/shared/inversify' ;
8
+ import queryString from 'query-string' ;
13
9
import { MonitorManagerProxyClient } from '../../../common/protocol' ;
14
- import { BoardsServiceProvider } from '../../boards/boards-service-provider' ;
10
+ import { Contribution } from '../../contributions/contribution' ;
11
+ import { ArduinoMenus } from '../../menu/arduino-menus' ;
15
12
import { MonitorModel } from '../../monitor-model' ;
16
13
import { ArduinoToolbar } from '../../toolbar/arduino-toolbar' ;
17
14
18
- import { nls } from '@theia/core/lib/common/nls' ;
19
- // import {
20
- // CLOSE_PLOTTER_WINDOW,
21
- // SHOW_PLOTTER_WINDOW,
22
- // } from '../../../common/ipc-communication';
23
-
24
- const queryString = require ( 'query-string' ) ;
25
-
26
15
export namespace SerialPlotterContribution {
27
16
export namespace Commands {
28
17
export const OPEN : Command = Command . toLocalizedCommand (
@@ -45,38 +34,31 @@ export namespace SerialPlotterContribution {
45
34
46
35
@injectable ( )
47
36
export class PlotterFrontendContribution extends Contribution {
48
- protected window : Window | null ;
49
- protected url : string ;
50
- protected wsPort : number ;
37
+ private readonly endpointUrl = new Endpoint ( { path : '/plotter' } )
38
+ . getRestUrl ( )
39
+ . toString ( ) ;
40
+ private readonly toDispose = new DisposableCollection ( ) ;
41
+ private _plotterUrl : string | undefined ;
51
42
52
43
@inject ( MonitorModel )
53
- protected readonly model : MonitorModel ;
54
-
44
+ private readonly model : MonitorModel ;
55
45
@inject ( ThemeService )
56
- protected readonly themeService : ThemeService ;
57
-
46
+ private readonly themeService : ThemeService ;
58
47
@inject ( MonitorManagerProxyClient )
59
- protected readonly monitorManagerProxy : MonitorManagerProxyClient ;
60
-
61
- @inject ( BoardsServiceProvider )
62
- protected readonly boardsServiceProvider : BoardsServiceProvider ;
63
-
64
- override onStart ( app : FrontendApplication ) : MaybePromise < void > {
65
- this . url = new Endpoint ( { path : '/plotter' } ) . getRestUrl ( ) . toString ( ) ;
48
+ private readonly monitorManagerProxy : MonitorManagerProxyClient ;
66
49
67
- // ipcRenderer.on(CLOSE_PLOTTER_WINDOW, async () => {
68
- // if (!!this.window) {
69
- // this.window = null;
70
- // }
71
- // });
50
+ override onStart ( ) : void {
51
+ this . toDispose . push (
52
+ window . electronArduino . registerPlotterWindowCloseHandler ( ( ) => {
53
+ this . _plotterUrl = undefined ;
54
+ } )
55
+ ) ;
72
56
this . monitorManagerProxy . onMonitorShouldReset ( ( ) => this . reset ( ) ) ;
73
-
74
- return super . onStart ( app ) ;
75
57
}
76
58
77
59
override registerCommands ( registry : CommandRegistry ) : void {
78
60
registry . registerCommand ( SerialPlotterContribution . Commands . OPEN , {
79
- execute : this . startPlotter . bind ( this ) ,
61
+ execute : ( ) => this . startPlotter ( ) ,
80
62
} ) ;
81
63
registry . registerCommand ( SerialPlotterContribution . Commands . RESET , {
82
64
execute : ( ) => this . reset ( ) ,
@@ -86,7 +68,7 @@ export class PlotterFrontendContribution extends Contribution {
86
68
{
87
69
isVisible : ( widget ) =>
88
70
ArduinoToolbar . is ( widget ) && widget . side === 'right' ,
89
- execute : this . startPlotter . bind ( this ) ,
71
+ execute : ( ) => this . startPlotter ( ) ,
90
72
}
91
73
) ;
92
74
}
@@ -99,10 +81,13 @@ export class PlotterFrontendContribution extends Contribution {
99
81
} ) ;
100
82
}
101
83
102
- async startPlotter ( ) : Promise < void > {
84
+ private async startPlotter ( forceReload = false ) : Promise < void > {
103
85
await this . monitorManagerProxy . startMonitor ( ) ;
104
- if ( ! ! this . window ) {
105
- // ipcRenderer.send(SHOW_PLOTTER_WINDOW);
86
+ if ( this . _plotterUrl ) {
87
+ window . electronArduino . showPlotterWindow ( {
88
+ url : this . _plotterUrl ,
89
+ forceReload,
90
+ } ) ;
106
91
return ;
107
92
}
108
93
const wsPort = this . monitorManagerProxy . getWebSocketPort ( ) ;
@@ -118,26 +103,30 @@ export class PlotterFrontendContribution extends Contribution {
118
103
}
119
104
}
120
105
121
- protected async open ( wsPort : number ) : Promise < void > {
106
+ private open ( wsPort : number ) : void {
122
107
const initConfig = {
123
- darkTheme : this . themeService . getCurrentTheme ( ) . type === 'dark' ,
108
+ darkTheme : this . isDarkTheme ,
124
109
wsPort,
125
110
serialPort : this . model . serialPort ,
126
111
} ;
127
- const urlWithParams = queryString . stringifyUrl (
112
+ this . _plotterUrl = queryString . stringifyUrl (
128
113
{
129
- url : this . url ,
114
+ url : this . endpointUrl ,
130
115
query : initConfig ,
131
116
} ,
132
117
{ arrayFormat : 'comma' }
133
118
) ;
134
- this . window = window . open ( urlWithParams , 'serialPlotter' ) ;
119
+ window . electronArduino . showPlotterWindow ( { url : this . _plotterUrl } ) ;
120
+ }
121
+
122
+ private get isDarkTheme ( ) : boolean {
123
+ const themeType = this . themeService . getCurrentTheme ( ) . type ;
124
+ return themeType === 'dark' || themeType === 'hc' ;
135
125
}
136
126
137
- protected async reset ( ) : Promise < void > {
138
- if ( ! ! this . window ) {
139
- this . window . close ( ) ;
140
- await this . startPlotter ( ) ;
127
+ private async reset ( ) : Promise < void > {
128
+ if ( this . _plotterUrl ) {
129
+ await this . startPlotter ( true ) ;
141
130
}
142
131
}
143
132
}
0 commit comments