@@ -10,8 +10,12 @@ import {
10
10
BoardWithPackage ,
11
11
} from '../../common/protocol/boards-service' ;
12
12
import { NotificationCenter } from '../notification-center' ;
13
- import { BoardsServiceProvider } from './boards-service-provider' ;
13
+ import {
14
+ AvailableBoard ,
15
+ BoardsServiceProvider ,
16
+ } from './boards-service-provider' ;
14
17
import { nls } from '@theia/core/lib/browser/nls' ;
18
+ import { naturalCompare } from '../../common/utils' ;
15
19
16
20
export namespace BoardsConfig {
17
21
export interface Config {
@@ -184,11 +188,50 @@ export class BoardsConfig extends React.Component<
184
188
. filter ( notEmpty ) ;
185
189
}
186
190
191
+ protected get availableBoards ( ) : AvailableBoard [ ] {
192
+ return this . props . boardsServiceProvider . availableBoards ;
193
+ }
194
+
187
195
protected queryPorts = async (
188
196
availablePorts : MaybePromise < Port [ ] > = this . availablePorts
189
197
) => {
190
- const ports = await availablePorts ;
191
- return { knownPorts : ports . sort ( Port . compare ) } ;
198
+ // Available ports must be sorted in this order:
199
+ // 1. Serial with recognized boards
200
+ // 2. Serial with guessed boards
201
+ // 3. Serial with incomplete boards
202
+ // 4. Network with recognized boards
203
+ // 5. Other protocols with recognized boards
204
+ const ports = ( await availablePorts ) . sort ( ( left : Port , right : Port ) => {
205
+ if ( left . protocol === 'serial' && right . protocol !== 'serial' ) {
206
+ return - 1 ;
207
+ } else if ( left . protocol !== 'serial' && right . protocol === 'serial' ) {
208
+ return 1 ;
209
+ } else if ( left . protocol === 'network' && right . protocol !== 'network' ) {
210
+ return - 1 ;
211
+ } else if ( left . protocol !== 'network' && right . protocol === 'network' ) {
212
+ return 1 ;
213
+ } else if ( left . protocol === right . protocol ) {
214
+ // We show ports, including those that have guessed
215
+ // or unrecognized boards, so we must sort those too.
216
+ const leftBoard = this . availableBoards . find ( ( board ) =>
217
+ Port . sameAs ( board . port , left )
218
+ ) ;
219
+ const rightBoard = this . availableBoards . find ( ( board ) =>
220
+ Port . sameAs ( board . port , right )
221
+ ) ;
222
+ if ( leftBoard && ! rightBoard ) {
223
+ return - 1 ;
224
+ } else if ( ! leftBoard && rightBoard ) {
225
+ return 1 ;
226
+ } else if ( leftBoard ?. state ! < rightBoard ?. state ! ) {
227
+ return - 1 ;
228
+ } else if ( leftBoard ?. state ! > rightBoard ?. state ! ) {
229
+ return 1 ;
230
+ }
231
+ }
232
+ return naturalCompare ( left . address , right . address ) ;
233
+ } ) ;
234
+ return { knownPorts : ports } ;
192
235
} ;
193
236
194
237
protected toggleFilterPorts = ( ) => {
@@ -281,8 +324,25 @@ export class BoardsConfig extends React.Component<
281
324
}
282
325
283
326
protected renderPorts ( ) : React . ReactNode {
284
- const filter = this . state . showAllPorts ? ( ) => true : Port . isBoardPort ;
285
- const ports = this . state . knownPorts . filter ( filter ) ;
327
+ let ports = [ ] as Port [ ] ;
328
+ debugger ;
329
+ if ( this . state . showAllPorts ) {
330
+ ports = this . state . knownPorts ;
331
+ } else {
332
+ ports = this . state . knownPorts . filter ( ( port ) => {
333
+ if ( port . protocol === 'serial' ) {
334
+ return true ;
335
+ }
336
+ // All other ports with different protocol are
337
+ // only shown if there is a recognized board
338
+ // connected
339
+ for ( const board of this . availableBoards ) {
340
+ if ( board . port ?. address === port . address ) {
341
+ return true ;
342
+ }
343
+ }
344
+ } ) ;
345
+ }
286
346
return ! ports . length ? (
287
347
< div className = "loading noselect" > No ports discovered</ div >
288
348
) : (
0 commit comments