mirror of
https://github.com/arduino/arduino-ide.git
synced 2025-06-08 21:26:32 +00:00
queued the menu updates.
Signed-off-by: Akos Kitta <kittaakos@typefox.io>
This commit is contained in:
parent
f0015e3fb4
commit
3806d56e94
@ -1,3 +1,4 @@
|
|||||||
|
import * as PQueue from 'p-queue';
|
||||||
import { inject, injectable } from 'inversify';
|
import { inject, injectable } from 'inversify';
|
||||||
import { CommandRegistry } from '@theia/core/lib/common/command';
|
import { CommandRegistry } from '@theia/core/lib/common/command';
|
||||||
import { MenuModelRegistry, MenuNode } from '@theia/core/lib/common/menu';
|
import { MenuModelRegistry, MenuNode } from '@theia/core/lib/common/menu';
|
||||||
@ -27,71 +28,74 @@ export class BoardsDataMenuUpdater implements FrontendApplicationContribution {
|
|||||||
@inject(BoardsServiceClientImpl)
|
@inject(BoardsServiceClientImpl)
|
||||||
protected readonly boardsServiceClient: BoardsServiceClientImpl;
|
protected readonly boardsServiceClient: BoardsServiceClientImpl;
|
||||||
|
|
||||||
|
protected readonly queue = new PQueue({ autoStart: true, concurrency: 1 });
|
||||||
protected readonly toDisposeOnBoardChange = new DisposableCollection();
|
protected readonly toDisposeOnBoardChange = new DisposableCollection();
|
||||||
|
|
||||||
async onStart(): Promise<void> {
|
async onStart(): Promise<void> {
|
||||||
await this.updateMenuActions(this.boardsServiceClient.boardsConfig.selectedBoard);
|
this.updateMenuActions(this.boardsServiceClient.boardsConfig.selectedBoard);
|
||||||
this.boardsDataStore.onChanged(async () => await this.updateMenuActions(this.boardsServiceClient.boardsConfig.selectedBoard));
|
this.boardsDataStore.onChanged(() => this.updateMenuActions(this.boardsServiceClient.boardsConfig.selectedBoard));
|
||||||
this.boardsServiceClient.onBoardsConfigChanged(async ({ selectedBoard }) => await this.updateMenuActions(selectedBoard));
|
this.boardsServiceClient.onBoardsConfigChanged(({ selectedBoard }) => this.updateMenuActions(selectedBoard));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async updateMenuActions(selectedBoard: Board | undefined): Promise<void> {
|
protected async updateMenuActions(selectedBoard: Board | undefined): Promise<void> {
|
||||||
if (selectedBoard) {
|
return this.queue.add(async () => {
|
||||||
this.toDisposeOnBoardChange.dispose();
|
this.toDisposeOnBoardChange.dispose();
|
||||||
this.mainMenuManager.update();
|
this.mainMenuManager.update();
|
||||||
const { fqbn } = selectedBoard;
|
if (selectedBoard) {
|
||||||
if (fqbn) {
|
const { fqbn } = selectedBoard;
|
||||||
const { configOptions, programmers, selectedProgrammer } = await this.boardsDataStore.getData(fqbn);
|
if (fqbn) {
|
||||||
if (configOptions.length) {
|
const { configOptions, programmers, selectedProgrammer } = await this.boardsDataStore.getData(fqbn);
|
||||||
const boardsConfigMenuPath = [...ArduinoMenus.TOOLS__BOARD_SETTINGS_GROUP, 'z01_boardsConfig']; // `z_` is for ordering.
|
if (configOptions.length) {
|
||||||
for (const { label, option, values } of configOptions.sort(ConfigOption.LABEL_COMPARATOR)) {
|
const boardsConfigMenuPath = [...ArduinoMenus.TOOLS__BOARD_SETTINGS_GROUP, 'z01_boardsConfig']; // `z_` is for ordering.
|
||||||
const menuPath = [...boardsConfigMenuPath, `${option}`];
|
for (const { label, option, values } of configOptions.sort(ConfigOption.LABEL_COMPARATOR)) {
|
||||||
const commands = new Map<string, Disposable & { label: string }>()
|
const menuPath = [...boardsConfigMenuPath, `${option}`];
|
||||||
for (const value of values) {
|
const commands = new Map<string, Disposable & { label: string }>()
|
||||||
const id = `${fqbn}-${option}--${value.value}`;
|
for (const value of values) {
|
||||||
const command = { id };
|
const id = `${fqbn}-${option}--${value.value}`;
|
||||||
const selectedValue = value.value;
|
const command = { id };
|
||||||
const handler = {
|
const selectedValue = value.value;
|
||||||
execute: () => this.boardsDataStore.selectConfigOption({ fqbn, option, selectedValue }),
|
const handler = {
|
||||||
isToggled: () => value.selected
|
execute: () => this.boardsDataStore.selectConfigOption({ fqbn, option, selectedValue }),
|
||||||
};
|
isToggled: () => value.selected
|
||||||
commands.set(id, Object.assign(this.commandRegistry.registerCommand(command, handler), { label: value.label }));
|
};
|
||||||
|
commands.set(id, Object.assign(this.commandRegistry.registerCommand(command, handler), { label: value.label }));
|
||||||
|
}
|
||||||
|
this.menuRegistry.registerSubmenu(menuPath, label);
|
||||||
|
this.toDisposeOnBoardChange.pushAll([
|
||||||
|
...commands.values(),
|
||||||
|
Disposable.create(() => this.unregisterSubmenu(menuPath)), // We cannot dispose submenu entries: https://github.com/eclipse-theia/theia/issues/7299
|
||||||
|
...Array.from(commands.keys()).map((commandId, i) => {
|
||||||
|
const { label } = commands.get(commandId)!;
|
||||||
|
this.menuRegistry.registerMenuAction(menuPath, { commandId, order: `${i}`, label });
|
||||||
|
return Disposable.create(() => this.menuRegistry.unregisterMenuAction(commandId));
|
||||||
|
})
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
this.menuRegistry.registerSubmenu(menuPath, label);
|
|
||||||
this.toDisposeOnBoardChange.pushAll([
|
|
||||||
...commands.values(),
|
|
||||||
Disposable.create(() => this.unregisterSubmenu(menuPath)), // We cannot dispose submenu entries: https://github.com/eclipse-theia/theia/issues/7299
|
|
||||||
...Array.from(commands.keys()).map((commandId, i) => {
|
|
||||||
const { label } = commands.get(commandId)!;
|
|
||||||
this.menuRegistry.registerMenuAction(menuPath, { commandId, order: `${i}`, label });
|
|
||||||
return Disposable.create(() => this.menuRegistry.unregisterMenuAction(commandId));
|
|
||||||
})
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
}
|
if (programmers.length) {
|
||||||
if (programmers.length) {
|
const programmersMenuPath = [...ArduinoMenus.TOOLS__BOARD_SETTINGS_GROUP, 'z02_programmers'];
|
||||||
const programmersMenuPath = [...ArduinoMenus.TOOLS__BOARD_SETTINGS_GROUP, 'z02_programmers'];
|
const label = selectedProgrammer ? `Programmer: "${selectedProgrammer.name}"` : 'Programmer'
|
||||||
const label = selectedProgrammer ? `Programmer: "${selectedProgrammer.name}"` : 'Programmer'
|
this.menuRegistry.registerSubmenu(programmersMenuPath, label);
|
||||||
this.menuRegistry.registerSubmenu(programmersMenuPath, label);
|
this.toDisposeOnBoardChange.push(Disposable.create(() => this.unregisterSubmenu(programmersMenuPath)));
|
||||||
this.toDisposeOnBoardChange.push(Disposable.create(() => this.unregisterSubmenu(programmersMenuPath)));
|
for (const programmer of programmers) {
|
||||||
for (const programmer of programmers) {
|
const { id, name } = programmer;
|
||||||
const { id, name } = programmer;
|
const command = { id: `${fqbn}-programmer--${id}` };
|
||||||
const command = { id: `${fqbn}-programmer--${id}` };
|
const handler = {
|
||||||
const handler = {
|
execute: () => this.boardsDataStore.selectProgrammer({ fqbn, selectedProgrammer: programmer }),
|
||||||
execute: () => this.boardsDataStore.selectProgrammer({ fqbn, selectedProgrammer: programmer }),
|
isToggled: () => Programmer.equals(programmer, selectedProgrammer)
|
||||||
isToggled: () => Programmer.equals(programmer, selectedProgrammer)
|
};
|
||||||
};
|
this.menuRegistry.registerMenuAction(programmersMenuPath, { commandId: command.id, label: name });
|
||||||
this.menuRegistry.registerMenuAction(programmersMenuPath, { commandId: command.id, label: name });
|
this.commandRegistry.registerCommand(command, handler);
|
||||||
this.commandRegistry.registerCommand(command, handler);
|
this.toDisposeOnBoardChange.pushAll([
|
||||||
this.toDisposeOnBoardChange.pushAll([
|
Disposable.create(() => this.commandRegistry.unregisterCommand(command)),
|
||||||
Disposable.create(() => this.commandRegistry.unregisterCommand(command)),
|
Disposable.create(() => this.menuRegistry.unregisterMenuAction(command.id))
|
||||||
Disposable.create(() => this.menuRegistry.unregisterMenuAction(command.id))
|
]);
|
||||||
]);
|
}
|
||||||
}
|
}
|
||||||
|
this.mainMenuManager.update();
|
||||||
}
|
}
|
||||||
this.mainMenuManager.update();
|
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
protected unregisterSubmenu(menuPath: string[]): void {
|
protected unregisterSubmenu(menuPath: string[]): void {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user