@@ -3,27 +3,71 @@ import {
3
3
ipcRenderer ,
4
4
} from '@theia/core/electron-shared/electron' ;
5
5
import { Disposable } from '@theia/core/lib/common/disposable' ;
6
- import { CHANNEL_REQUEST_RELOAD } from '@theia/core/lib/electron-common/electron-api' ;
6
+ import {
7
+ CHANNEL_REQUEST_RELOAD ,
8
+ MenuDto ,
9
+ } from '@theia/core/lib/electron-common/electron-api' ;
10
+ import { v4 } from 'uuid' ;
7
11
import type { Sketch } from '../common/protocol/sketches-service' ;
8
12
import {
9
13
CHANNEL_APP_VERSION ,
10
14
CHANNEL_IS_FIRST_WINDOW ,
15
+ CHANNEL_MAIN_MENU_ITEM_DID_CLICK ,
11
16
CHANNEL_OPEN_PATH ,
12
17
CHANNEL_PLOTTER_WINDOW_DID_CLOSE ,
13
18
CHANNEL_QUIT_APP ,
14
19
CHANNEL_SCHEDULE_DELETION ,
15
20
CHANNEL_SEND_STARTUP_TASKS ,
21
+ CHANNEL_SET_MENU_WITH_NODE_ID ,
16
22
CHANNEL_SET_REPRESENTED_FILENAME ,
17
23
CHANNEL_SHOW_MESSAGE_BOX ,
18
24
CHANNEL_SHOW_OPEN_DIALOG ,
19
25
CHANNEL_SHOW_PLOTTER_WINDOW ,
20
26
CHANNEL_SHOW_SAVE_DIALOG ,
21
27
ElectronArduino ,
28
+ InternalMenuDto ,
22
29
MessageBoxOptions ,
23
30
OpenDialogOptions ,
24
31
SaveDialogOptions ,
25
32
} from '../electron-common/electron-arduino' ;
26
- import { StartupTasks , hasStartupTasks } from '../electron-common/startup-task' ;
33
+ import { hasStartupTasks , StartupTasks } from '../electron-common/startup-task' ;
34
+
35
+ let mainMenuHandlers : Map < string , ( ) => void > = new Map ( ) ;
36
+
37
+ function convertMenu (
38
+ menu : MenuDto [ ] | undefined ,
39
+ handlerMap : Map < string , ( ) => void >
40
+ ) : InternalMenuDto [ ] | undefined {
41
+ if ( ! menu ) {
42
+ return undefined ;
43
+ }
44
+
45
+ return menu . map ( ( item ) => {
46
+ let nodeId = v4 ( ) ;
47
+ if ( item . execute ) {
48
+ if ( ! item . id ) {
49
+ throw new Error (
50
+ "A menu item having the 'execute' property must have an 'id' too."
51
+ ) ;
52
+ }
53
+ nodeId = item . id ;
54
+ handlerMap . set ( nodeId , item . execute ) ;
55
+ }
56
+
57
+ return {
58
+ id : item . id ,
59
+ submenu : convertMenu ( item . submenu , handlerMap ) ,
60
+ accelerator : item . accelerator ,
61
+ label : item . label ,
62
+ nodeId,
63
+ checked : item . checked ,
64
+ enabled : item . enabled ,
65
+ role : item . role ,
66
+ type : item . type ,
67
+ visible : item . visible ,
68
+ } ;
69
+ } ) ;
70
+ }
27
71
28
72
const api : ElectronArduino = {
29
73
showMessageBox : ( options : MessageBoxOptions ) =>
@@ -68,9 +112,20 @@ const api: ElectronArduino = {
68
112
) ;
69
113
} ,
70
114
openPath : ( fsPath : string ) => ipcRenderer . send ( CHANNEL_OPEN_PATH , fsPath ) ,
115
+ setMenu : ( menu : MenuDto [ ] | undefined ) : void => {
116
+ mainMenuHandlers = new Map ( ) ;
117
+ const internalMenu = convertMenu ( menu , mainMenuHandlers ) ;
118
+ ipcRenderer . send ( CHANNEL_SET_MENU_WITH_NODE_ID , internalMenu ) ;
119
+ } ,
71
120
} ;
72
121
73
122
export function preload ( ) : void {
74
123
contextBridge . exposeInMainWorld ( 'electronArduino' , api ) ;
124
+ ipcRenderer . on ( CHANNEL_MAIN_MENU_ITEM_DID_CLICK , ( _ , nodeId : string ) => {
125
+ const handler = mainMenuHandlers . get ( nodeId ) ;
126
+ if ( handler ) {
127
+ handler ( ) ;
128
+ }
129
+ } ) ;
75
130
console . log ( 'Exposed Arduino IDE electron API' ) ;
76
131
}
0 commit comments