Skip to content

Commit 1a11de2

Browse files
silvanocerzaAlberto Iannaccone
authored and
Alberto Iannaccone
committed
Reworked port selection menu ordering
Now ports are shown in this order: 1. Serial with recognized boards 2. Serial with unrecognized boards 3. Network with recognized boards 4. Network with unrecognized boards 5. Other protocols with recognized boards 6. Other protocols with unrecognized boards
1 parent 5617f79 commit 1a11de2

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

arduino-ide-extension/src/browser/contributions/board-selection.ts

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -247,17 +247,25 @@ PID: ${PID}`;
247247
}
248248

249249
// Installed ports
250-
const registerPorts = (protocol: string, ports: AvailablePorts) => {
250+
const registerPorts = (
251+
protocol: string,
252+
protocolOrder: number,
253+
ports: AvailablePorts
254+
) => {
251255
const addresses = Object.keys(ports);
252256
if (!addresses.length) {
253257
return;
254258
}
255259

256260
// Register placeholder for protocol
257-
const menuPath = [...portsSubmenuPath, protocol];
261+
const menuPath = [
262+
...portsSubmenuPath,
263+
`${protocolOrder.toString()}_${protocol}`,
264+
];
258265
const placeholder = new PlaceholderMenuNode(
259266
menuPath,
260-
`${firstToUpperCase(protocol)} ports`
267+
`${firstToUpperCase(protocol)} ports`,
268+
{ order: protocolOrder.toString() }
261269
);
262270
this.menuModelRegistry.registerMenuNode(menuPath, placeholder);
263271
this.toDisposeBeforeMenuRebuild.push(
@@ -266,7 +274,17 @@ PID: ${PID}`;
266274
)
267275
);
268276

269-
for (const address of Object.keys(ports)) {
277+
// First we show addresses with recognized boards connected,
278+
// then all the rest.
279+
const sortedAddresses = Object.keys(ports);
280+
sortedAddresses.sort((left: string, right: string): number => {
281+
const [, leftBoards] = ports[left];
282+
const [, rightBoards] = ports[right];
283+
return rightBoards.length - leftBoards.length;
284+
});
285+
286+
for (let i = 0; i < sortedAddresses.length; i++) {
287+
const address = sortedAddresses[i];
270288
const [port, boards] = ports[address];
271289
if (!boards.length) {
272290
boards.push({ name: '' });
@@ -301,7 +319,7 @@ PID: ${PID}`;
301319
const menuAction = {
302320
commandId: id,
303321
label,
304-
order: `1${label}`, // `1` comes after the placeholder which has order `0`
322+
order: `${protocolOrder + i + 1}`,
305323
};
306324
this.commandRegistry.registerCommand(command, handler);
307325
this.toDisposeBeforeMenuRebuild.push(
@@ -315,18 +333,19 @@ PID: ${PID}`;
315333
};
316334

317335
const grouped = AvailablePorts.byProtocol(availablePorts);
336+
let protocolOrder = 100;
318337
// We first show serial and network ports, then all the rest
319338
['serial', 'network'].forEach((protocol) => {
320339
const ports = grouped.get(protocol);
321340
if (ports) {
322-
registerPorts(protocol, ports);
341+
registerPorts(protocol, protocolOrder, ports);
342+
grouped.delete(protocol);
343+
protocolOrder = protocolOrder + 100;
323344
}
324345
});
325346
grouped.forEach((ports, protocol) => {
326-
if (protocol === 'serial' || protocol === 'network') {
327-
return;
328-
}
329-
registerPorts(protocol, ports);
347+
registerPorts(protocol, protocolOrder, ports);
348+
protocolOrder = protocolOrder + 100;
330349
});
331350

332351
this.mainMenuManager.update();

0 commit comments

Comments
 (0)