@@ -238,17 +238,18 @@ PID: ${PID}`;
238238 }
239239
240240 // Installed ports
241- const registerPorts = ( protocol : string , ports : AvailablePorts ) => {
241+ const registerPorts = ( protocol : string , protocolOrder : number , ports : AvailablePorts ) => {
242242 const addresses = Object . keys ( ports ) ;
243243 if ( ! addresses . length ) {
244244 return ;
245245 }
246246
247247 // Register placeholder for protocol
248- const menuPath = [ ...portsSubmenuPath , protocol ] ;
248+ const menuPath = [ ...portsSubmenuPath , ` ${ protocolOrder . toString ( ) } _ ${ protocol } ` ] ;
249249 const placeholder = new PlaceholderMenuNode (
250250 menuPath ,
251- `${ firstToUpperCase ( protocol ) } ports`
251+ `${ firstToUpperCase ( protocol ) } ports` ,
252+ { order : protocolOrder . toString ( ) } ,
252253 ) ;
253254 this . menuModelRegistry . registerMenuNode ( menuPath , placeholder ) ;
254255 this . toDisposeBeforeMenuRebuild . push (
@@ -257,7 +258,17 @@ PID: ${PID}`;
257258 )
258259 ) ;
259260
260- for ( const address of Object . keys ( ports ) ) {
261+ // First we show addresses with recognized boards connected,
262+ // then all the rest.
263+ let sortedAddresses = Object . keys ( ports ) ;
264+ sortedAddresses . sort ( ( left : string , right : string ) : number => {
265+ const [ , leftBoards ] = ports [ left ] ;
266+ const [ , rightBoards ] = ports [ right ] ;
267+ return rightBoards . length - leftBoards . length ;
268+ } )
269+
270+ for ( let i = 0 ; i < sortedAddresses . length ; i ++ ) {
271+ const address = sortedAddresses [ i ] ;
261272 const [ port , boards ] = ports [ address ] ;
262273 if ( ! boards . length ) {
263274 boards . push ( { name : '' } ) ;
@@ -281,7 +292,7 @@ PID: ${PID}`;
281292 const menuAction = {
282293 commandId : id ,
283294 label,
284- order : `1 ${ label } ` , // `1` comes after the placeholder which has order `0`
295+ order : `${ protocolOrder + i + 1 } ` ,
285296 } ;
286297 this . commandRegistry . registerCommand ( command , handler ) ;
287298 this . toDisposeBeforeMenuRebuild . push (
@@ -295,18 +306,19 @@ PID: ${PID}`;
295306 } ;
296307
297308 const grouped = AvailablePorts . byProtocol ( availablePorts ) ;
309+ let protocolOrder = 100 ;
298310 // We first show serial and network ports, then all the rest
299311 [ "serial" , "network" ] . forEach ( protocol => {
300312 const ports = grouped . get ( protocol ) ;
301313 if ( ports ) {
302- registerPorts ( protocol , ports ) ;
314+ registerPorts ( protocol , protocolOrder , ports ) ;
315+ grouped . delete ( protocol ) ;
316+ protocolOrder = protocolOrder + 100 ;
303317 }
304318 } ) ;
305319 grouped . forEach ( ( ports , protocol ) => {
306- if ( protocol === "serial" || protocol === "network" ) {
307- return ;
308- }
309- registerPorts ( protocol , ports ) ;
320+ registerPorts ( protocol , protocolOrder , ports ) ;
321+ protocolOrder = protocolOrder + 100 ;
310322 } )
311323
312324 this . mainMenuManager . update ( ) ;
0 commit comments