Skip to content

Commit 97399cf

Browse files
silvanocerzaAlberto Iannaccone
authored and
Alberto Iannaccone
committed
Disable Sketch > Configure and Upload menu if board doesn't support user fields
1 parent 53b4f17 commit 97399cf

File tree

3 files changed

+92
-52
lines changed

3 files changed

+92
-52
lines changed

arduino-ide-extension/src/browser/arduino-frontend-contribution.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ export class ArduinoFrontendContribution
445445
'arduino/debug/optimizeForDebugging',
446446
'Optimize for Debugging'
447447
),
448-
order: '4',
448+
order: '5',
449449
});
450450
}
451451

arduino-ide-extension/src/browser/contributions/upload-sketch.ts

+90-50
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { inject, injectable, postConstruct } from 'inversify';
22
import { Emitter } from '@theia/core/lib/common/event';
33
import { BoardUserField, CoreService } from '../../common/protocol';
4-
import { ArduinoMenus } from '../menu/arduino-menus';
4+
import { ArduinoMenus, PlaceholderMenuNode } from '../menu/arduino-menus';
55
import { ArduinoToolbar } from '../toolbar/arduino-toolbar';
66
import { BoardsDataStore } from '../boards/boards-data-store';
77
import { SerialConnectionManager } from '../serial/serial-connection-manager';
@@ -16,6 +16,7 @@ import {
1616
} from './contribution';
1717
import { UserFieldsDialog } from '../dialogs/user-fields/user-fields-dialog';
1818
import { nls } from '@theia/core/lib/browser/nls';
19+
import { DisposableCollection } from '@theia/core';
1920

2021
@injectable()
2122
export class UploadSketch extends SketchContribution {
@@ -25,6 +26,9 @@ export class UploadSketch extends SketchContribution {
2526
@inject(SerialConnectionManager)
2627
protected readonly serialConnection: SerialConnectionManager;
2728

29+
@inject(MenuModelRegistry)
30+
protected readonly menuRegistry: MenuModelRegistry;
31+
2832
@inject(BoardsDataStore)
2933
protected readonly boardsDataStore: BoardsDataStore;
3034

@@ -42,25 +46,29 @@ export class UploadSketch extends SketchContribution {
4246
protected uploadInProgress = false;
4347
protected boardRequiresUserFields = false;
4448

49+
protected readonly menuActionsDisposables = new DisposableCollection();
50+
4551
@postConstruct()
4652
protected init(): void {
4753
this.boardsServiceClientImpl.onBoardsConfigChanged(async () => {
48-
const userFields = await this.boardsServiceClientImpl.selectedBoardUserFields();
54+
const userFields =
55+
await this.boardsServiceClientImpl.selectedBoardUserFields();
4956
this.boardRequiresUserFields = userFields.length > 0;
50-
})
57+
this.registerMenus(this.menuRegistry);
58+
});
5159
}
5260

5361
private selectedFqbnAddress(): string {
5462
const { boardsConfig } = this.boardsServiceClientImpl;
5563
const fqbn = boardsConfig.selectedBoard?.fqbn;
5664
if (!fqbn) {
57-
return "";
65+
return '';
5866
}
59-
const address = boardsConfig.selectedBoard?.port?.address
67+
const address = boardsConfig.selectedBoard?.port?.address;
6068
if (!address) {
61-
return "";
69+
return '';
6270
}
63-
return fqbn + "|" + address;
71+
return fqbn + '|' + address;
6472
}
6573

6674
registerCommands(registry: CommandRegistry): void {
@@ -72,7 +80,9 @@ export class UploadSketch extends SketchContribution {
7280
}
7381
if (this.boardRequiresUserFields && !this.cachedUserFields.has(key)) {
7482
// Deep clone the array of board fields to avoid editing the cached ones
75-
this.userFieldsDialog.value = (await this.boardsServiceClientImpl.selectedBoardUserFields()).map(f => ({ ...f }));
83+
this.userFieldsDialog.value = (
84+
await this.boardsServiceClientImpl.selectedBoardUserFields()
85+
).map((f) => ({ ...f }));
7686
const result = await this.userFieldsDialog.open();
7787
if (!result) {
7888
return;
@@ -83,29 +93,29 @@ export class UploadSketch extends SketchContribution {
8393
},
8494
isEnabled: () => !this.uploadInProgress,
8595
});
86-
registry.registerCommand(
87-
UploadSketch.Commands.UPLOAD_WITH_CONFIGURATION,
88-
{
89-
execute: async () => {
90-
const key = this.selectedFqbnAddress();
91-
if (!key) {
92-
return;
93-
}
96+
registry.registerCommand(UploadSketch.Commands.UPLOAD_WITH_CONFIGURATION, {
97+
execute: async () => {
98+
const key = this.selectedFqbnAddress();
99+
if (!key) {
100+
return;
101+
}
94102

95-
const cached = this.cachedUserFields.get(key);
96-
// Deep clone the array of board fields to avoid editing the cached ones
97-
this.userFieldsDialog.value = (cached ?? await this.boardsServiceClientImpl.selectedBoardUserFields()).map(f => ({ ...f }));
103+
const cached = this.cachedUserFields.get(key);
104+
// Deep clone the array of board fields to avoid editing the cached ones
105+
this.userFieldsDialog.value = (
106+
cached ??
107+
(await this.boardsServiceClientImpl.selectedBoardUserFields())
108+
).map((f) => ({ ...f }));
98109

99-
const result = await this.userFieldsDialog.open()
100-
if (!result) {
101-
return;
102-
}
103-
this.cachedUserFields.set(key, result);
104-
this.uploadSketch();
105-
},
106-
isEnabled: () => !this.uploadInProgress && this.boardRequiresUserFields,
107-
}
108-
);
110+
const result = await this.userFieldsDialog.open();
111+
if (!result) {
112+
return;
113+
}
114+
this.cachedUserFields.set(key, result);
115+
this.uploadSketch();
116+
},
117+
isEnabled: () => !this.uploadInProgress && this.boardRequiresUserFields,
118+
});
109119
registry.registerCommand(
110120
UploadSketch.Commands.UPLOAD_SKETCH_USING_PROGRAMMER,
111121
{
@@ -124,24 +134,46 @@ export class UploadSketch extends SketchContribution {
124134
}
125135

126136
registerMenus(registry: MenuModelRegistry): void {
127-
registry.registerMenuAction(ArduinoMenus.SKETCH__MAIN_GROUP, {
128-
commandId: UploadSketch.Commands.UPLOAD_SKETCH.id,
129-
label: nls.localize('arduino/sketch/upload', 'Upload'),
130-
order: '1',
131-
});
132-
registry.registerMenuAction(ArduinoMenus.SKETCH__MAIN_GROUP, {
133-
commandId: UploadSketch.Commands.UPLOAD_WITH_CONFIGURATION.id,
134-
label: UploadSketch.Commands.UPLOAD_WITH_CONFIGURATION.label,
135-
order: '2',
136-
});
137-
registry.registerMenuAction(ArduinoMenus.SKETCH__MAIN_GROUP, {
138-
commandId: UploadSketch.Commands.UPLOAD_SKETCH_USING_PROGRAMMER.id,
139-
label: nls.localize(
140-
'arduino/sketch/uploadUsingProgrammer',
141-
'Upload Using Programmer'
142-
),
143-
order: '3',
144-
});
137+
this.menuActionsDisposables.dispose();
138+
139+
this.menuActionsDisposables.push(
140+
registry.registerMenuAction(ArduinoMenus.SKETCH__MAIN_GROUP, {
141+
commandId: UploadSketch.Commands.UPLOAD_SKETCH.id,
142+
label: nls.localize('arduino/sketch/upload', 'Upload'),
143+
order: '1',
144+
})
145+
);
146+
if (this.boardRequiresUserFields) {
147+
this.menuActionsDisposables.push(
148+
registry.registerMenuAction(ArduinoMenus.SKETCH__MAIN_GROUP, {
149+
commandId: UploadSketch.Commands.UPLOAD_WITH_CONFIGURATION.id,
150+
label: UploadSketch.Commands.UPLOAD_WITH_CONFIGURATION.label,
151+
order: '2',
152+
})
153+
);
154+
} else {
155+
this.menuActionsDisposables.push(
156+
registry.registerMenuNode(
157+
ArduinoMenus.SKETCH__MAIN_GROUP,
158+
new PlaceholderMenuNode(
159+
ArduinoMenus.SKETCH__MAIN_GROUP,
160+
// commandId: UploadSketch.Commands.UPLOAD_WITH_CONFIGURATION.id,
161+
UploadSketch.Commands.UPLOAD_WITH_CONFIGURATION.label!,
162+
{ order: '2' }
163+
)
164+
)
165+
);
166+
}
167+
this.menuActionsDisposables.push(
168+
registry.registerMenuAction(ArduinoMenus.SKETCH__MAIN_GROUP, {
169+
commandId: UploadSketch.Commands.UPLOAD_SKETCH_USING_PROGRAMMER.id,
170+
label: nls.localize(
171+
'arduino/sketch/uploadUsingProgrammer',
172+
'Upload Using Programmer'
173+
),
174+
order: '3',
175+
})
176+
);
145177
}
146178

147179
registerKeybindings(registry: KeybindingRegistry): void {
@@ -200,7 +232,12 @@ export class UploadSketch extends SketchContribution {
200232
const port = selectedPort;
201233
const userFields = this.cachedUserFields.get(this.selectedFqbnAddress());
202234
if (!userFields) {
203-
this.messageService.error(nls.localize('arduino/sketch/userFieldsNotFoundError', "Can't find user fields for connected board"));
235+
this.messageService.error(
236+
nls.localize(
237+
'arduino/sketch/userFieldsNotFoundError',
238+
"Can't find user fields for connected board"
239+
)
240+
);
204241
return;
205242
}
206243

@@ -277,9 +314,12 @@ export namespace UploadSketch {
277314
};
278315
export const UPLOAD_WITH_CONFIGURATION: Command = {
279316
id: 'arduino-upload-with-configuration-sketch',
280-
label: nls.localize('arduino/sketch/configureAndUpload', 'Configure And Upload'),
317+
label: nls.localize(
318+
'arduino/sketch/configureAndUpload',
319+
'Configure And Upload'
320+
),
281321
category: 'Arduino',
282-
}
322+
};
283323
export const UPLOAD_SKETCH_USING_PROGRAMMER: Command = {
284324
id: 'arduino-upload-sketch-using-programmer',
285325
};

arduino-ide-extension/src/browser/contributions/verify-sketch.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export class VerifySketch extends SketchContribution {
6262
'arduino/sketch/exportBinary',
6363
'Export Compiled Binary'
6464
),
65-
order: '3',
65+
order: '4',
6666
});
6767
}
6868

0 commit comments

Comments
 (0)