- Notifications
You must be signed in to change notification settings - Fork 440
/
Copy pathboards-data-menu-updater.ts
178 lines (173 loc) · 6.28 KB
/
boards-data-menu-updater.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import{
Disposable,
DisposableCollection,
}from'@theia/core/lib/common/disposable';
import{nls}from'@theia/core/lib/common/nls';
import{inject,injectable}from'@theia/core/shared/inversify';
importPQueuefrom'p-queue';
import{
BoardIdentifier,
ConfigOption,
isBoardIdentifierChangeEvent,
Programmer,
}from'../../common/protocol';
import{BoardsDataStore}from'../boards/boards-data-store';
import{BoardsServiceProvider}from'../boards/boards-service-provider';
import{ArduinoMenus,unregisterSubmenu}from'../menu/arduino-menus';
import{
CommandRegistry,
Contribution,
MenuModelRegistry,
}from'./contribution';
@injectable()
exportclassBoardsDataMenuUpdaterextendsContribution{
@inject(CommandRegistry)
privatereadonlycommandRegistry: CommandRegistry;
@inject(MenuModelRegistry)
privatereadonlymenuRegistry: MenuModelRegistry;
@inject(BoardsDataStore)
privatereadonlyboardsDataStore: BoardsDataStore;
@inject(BoardsServiceProvider)
privatereadonlyboardsServiceProvider: BoardsServiceProvider;
privatereadonlyqueue=newPQueue({autoStart: true,concurrency: 1});
privatereadonlytoDisposeOnBoardChange=newDisposableCollection();
overrideonStart(): void{
this.boardsDataStore.onDidChange(()=>
this.updateMenuActions(
this.boardsServiceProvider.boardsConfig.selectedBoard
)
);
this.boardsServiceProvider.onBoardsConfigDidChange((event)=>{
if(isBoardIdentifierChangeEvent(event)){
this.updateMenuActions(event.selectedBoard);
}
});
}
overrideonReady(): void{
this.boardsServiceProvider.ready.then(()=>
this.updateMenuActions(
this.boardsServiceProvider.boardsConfig.selectedBoard
)
);
}
privateasyncupdateMenuActions(
selectedBoard: BoardIdentifier|undefined
): Promise<void>{
returnthis.queue.add(async()=>{
this.toDisposeOnBoardChange.dispose();
this.menuManager.update();
if(selectedBoard){
const{ fqbn }=selectedBoard;
if(fqbn){
const{ configOptions, programmers, selectedProgrammer }=
awaitthis.boardsDataStore.getData(fqbn);
if(configOptions.length){
constboardsConfigMenuPath=[
...ArduinoMenus.TOOLS__BOARD_SETTINGS_GROUP,
'z01_boardsConfig',
];// `z_` is for ordering.
for(const{ label, option, values }ofconfigOptions.sort(
ConfigOption.LABEL_COMPARATOR
)){
constmenuPath=[...boardsConfigMenuPath,`${option}`];
constcommands=newMap<
string,
Disposable&{label: string}
>();
letselectedValue='';
for(constvalueofvalues){
constid=`${fqbn}-${option}--${value.value}`;
constcommand={ id };
consthandler={
execute: ()=>
this.boardsDataStore.selectConfigOption({
fqbn,
optionsToUpdate: [{ option,selectedValue: value.value}],
}),
isToggled: ()=>value.selected,
};
commands.set(
id,
Object.assign(
this.commandRegistry.registerCommand(command,handler),
{label: value.label}
)
);
if(value.selected){
selectedValue=value.label;
}
}
this.menuRegistry.registerSubmenu(
menuPath,
`${label}${selectedValue ? `: "${selectedValue}"` : ''}`
);
this.toDisposeOnBoardChange.pushAll([
...commands.values(),
Disposable.create(()=>
unregisterSubmenu(menuPath,this.menuRegistry)
),
...Array.from(commands.keys()).map((commandId,i)=>{
const{ label }=commands.get(commandId)!;
this.menuRegistry.registerMenuAction(menuPath,{
commandId,
order: String(i).padStart(4),
label,
});
returnDisposable.create(()=>
this.menuRegistry.unregisterMenuAction(commandId)
);
}),
]);
}
}
if(programmers.length){
constprogrammersMenuPath=[
...ArduinoMenus.TOOLS__BOARD_SETTINGS_GROUP,
'z02_programmers',
];
constprogrammerNls=nls.localize(
'arduino/board/programmer',
'Programmer'
);
constlabel=selectedProgrammer
? `${programmerNls}: "${selectedProgrammer.name}"`
: programmerNls;
this.menuRegistry.registerSubmenu(programmersMenuPath,label);
this.toDisposeOnBoardChange.push(
Disposable.create(()=>
unregisterSubmenu(programmersMenuPath,this.menuRegistry)
)
);
for(constprogrammerofprogrammers){
const{ id, name }=programmer;
constcommand={id: `${fqbn}-programmer--${id}`};
consthandler={
execute: ()=>
this.boardsDataStore.selectProgrammer({
fqbn,
selectedProgrammer: programmer,
}),
isToggled: ()=>
Programmer.equals(programmer,selectedProgrammer),
};
this.menuRegistry.registerMenuAction(programmersMenuPath,{
commandId: command.id,
label: name,
});
this.commandRegistry.registerCommand(command,handler);
this.toDisposeOnBoardChange.pushAll([
Disposable.create(()=>
this.commandRegistry.unregisterCommand(command)
),
Disposable.create(()=>
this.menuRegistry.unregisterMenuAction(command.id)
),
]);
}
}
this.menuManager.update();
}
}
});
}
}