@@ -146,41 +146,64 @@ namespace user_interface_base {
146146 }
147147
148148
149+
149150 export class GridNavigator extends RowNavigator {
150151 private height : number ;
151- private width : number ;
152-
153- constructor ( height : number , width : number ) {
152+ private widths : number [ ] ;
153+
154+ constructor ( height : number , width ? : number , widths ?: number [ ] ) {
154155 super ( )
155156 this . height = height
156- this . width = width
157+
158+ if ( widths != null ) {
159+ this . widths = widths
160+ }
161+ else {
162+ width = ( width != null ) ? width : 1
163+ this . widths = [ ]
164+ for ( let _ = 0 ; _ < width ; _ ++ )
165+ this . widths . push ( width )
166+ }
167+ }
168+
169+ public addButtons ( btns : Button [ ] ) {
170+ this . buttonGroups . push ( btns )
157171 }
158172
159173 public move ( dir : CursorDir ) {
160174 switch ( dir ) {
161175 case CursorDir . Up : {
162176 this . row = ( ( ( this . row - 1 ) % this . height ) + this . height ) % this . height ; // Non-negative modulo
177+
178+ // Row above could have less cols, adjust if neccessary:
179+ if ( this . widths [ this . row ] <= this . col )
180+ this . col = this . widths [ this . row ] - 1
181+
163182 break
164183 }
165184
166185 case CursorDir . Down : {
167186 this . row = ( this . row + 1 ) % this . height ;
187+
188+ // Row below could have less cols, adjust if neccessary:
189+ if ( this . widths [ this . row ] <= this . col )
190+ this . col = this . widths [ this . row ] - 1
168191 break
169192 }
170193
171194 case CursorDir . Left : {
172195 if ( this . col == 0 )
173- this . col = this . width - 1
196+ this . col = this . widths [ this . row ] - 1
174197 else
175198 this . col -= 1
176199 break
177200 }
178201
179202 case CursorDir . Right : {
180- if ( this . col == this . width )
203+ if ( this . col == this . widths [ this . row ] )
181204 this . col = 0
182- else
183- this . col = ( this . col + 1 ) % this . width
205+ else
206+ this . col = ( this . col + 1 ) % this . widths [ this . row ]
184207 break
185208 }
186209
@@ -192,16 +215,19 @@ namespace user_interface_base {
192215 }
193216 }
194217
195- const btn = this . buttonGroups [ 0 ] [ ( this . row * this . width ) + this . col ]
218+ const index = this . widths . slice ( 0 , this . row ) . reduce ( ( p , c ) => p + c , 0 )
219+ const btn = this . buttonGroups [ 0 ] [ index + this . col ]
196220 this . reportAria ( btn )
197221 return btn
198222 }
199223
200224 public getCurrent ( ) : Button {
201- return this . buttonGroups [ 0 ] [ ( this . row * this . width ) + this . col ]
225+ const index = this . widths . slice ( 0 , this . row ) . reduce ( ( p , c ) => p + c , 0 )
226+ return this . buttonGroups [ 0 ] [ index + this . col ]
202227 }
203228 }
204229
230+
205231 // mostly a matrix, except for last row, which may be ragged
206232 // also supports delete button
207233 // add support for aria
@@ -210,7 +236,7 @@ namespace user_interface_base {
210236 protected row : number
211237 protected col : number
212238
213- constructor ( private picker : Picker ) { }
239+ constructor ( private picker : Picker ) { }
214240
215241 private get width ( ) {
216242 return this . picker . width
@@ -257,7 +283,7 @@ namespace user_interface_base {
257283 this . deleteButton = undefined
258284 }
259285
260- addButtons ( btns : ButtonBase [ ] ) { }
286+ addButtons ( btns : ButtonBase [ ] ) { }
261287
262288 addDelete ( btn : Button ) {
263289 this . deleteButton = btn
@@ -346,11 +372,11 @@ namespace user_interface_base {
346372 if ( this . row == - 1 ) {
347373 accessibility . setLiveContent ( <
348374 accessibility . TextAccessibilityMessage
349- > {
350- type : "text" ,
351- value : "delete_tile" ,
352- force : true ,
353- } )
375+ > {
376+ type : "text" ,
377+ value : "delete_tile" ,
378+ force : true ,
379+ } )
354380 }
355381 }
356382 }
@@ -368,13 +394,13 @@ namespace user_interface_base {
368394 const on = true // TODO: btn.getIcon() == "solid_red"
369395 accessibility . setLiveContent ( <
370396 accessibility . LEDAccessibilityMessage
371- > {
372- type : "led" ,
373- on,
374- x : this . col ,
375- y : this . row ,
376- force : true ,
377- } )
397+ > {
398+ type : "led" ,
399+ on,
400+ x : this . col ,
401+ y : this . row ,
402+ force : true ,
403+ } )
378404 }
379405 }
380406
@@ -392,12 +418,12 @@ namespace user_interface_base {
392418 const index = this . hasDelete ? this . row - 1 : this . row
393419 accessibility . setLiveContent ( <
394420 accessibility . NoteAccessibilityMessage
395- > {
396- type : "note" ,
397- on,
398- index,
399- force : true ,
400- } )
421+ > {
422+ type : "note" ,
423+ on,
424+ index,
425+ force : true ,
426+ } )
401427 }
402428 }
403429}
0 commit comments