@@ -194,9 +194,20 @@ class FlexibleGridLayout<T extends object> extends Layout<Node<T>, GridLayoutOpt
194
194
}
195
195
}
196
196
197
+ class WaterfallLayoutInfo extends LayoutInfo {
198
+ column = 0 ;
199
+
200
+ copy ( ) : WaterfallLayoutInfo {
201
+ let res = super . copy ( ) as WaterfallLayoutInfo ;
202
+ res . column = this . column ;
203
+ return res ;
204
+ }
205
+ }
206
+
197
207
class WaterfallLayout < T extends object > extends Layout < Node < T > , GridLayoutOptions > implements LayoutDelegate {
198
208
protected contentSize : Size = new Size ( ) ;
199
- protected layoutInfos : Map < Key , LayoutInfo > = new Map ( ) ;
209
+ protected layoutInfos : Map < Key , WaterfallLayoutInfo > = new Map ( ) ;
210
+ protected numColumns = 0 ;
200
211
201
212
update ( invalidationContext : InvalidationContext < GridLayoutOptions > ) : void {
202
213
let {
@@ -246,15 +257,18 @@ class WaterfallLayout<T extends object> extends Layout<Node<T>, GridLayoutOption
246
257
}
247
258
248
259
// Figure out which column to place the item in, and compute its position.
249
- let column = columnHeights . reduce ( ( minIndex , h , i ) => h < columnHeights [ minIndex ] ? i : minIndex , 0 ) ;
260
+ // Preserve the previous column index so items don't jump around during resizing unless the number of columns changed.
261
+ let prevColumn = numColumns === this . numColumns ? oldLayoutInfo ?. column : undefined ;
262
+ let column = prevColumn ?? columnHeights . reduce ( ( minIndex , h , i ) => h < columnHeights [ minIndex ] ? i : minIndex , 0 ) ;
250
263
let x = horizontalSpacing + column * ( itemWidth + horizontalSpacing ) ;
251
264
let y = columnHeights [ column ] ;
252
265
253
266
let rect = new Rect ( x , y , itemWidth , height ) ;
254
- let layoutInfo = new LayoutInfo ( node . type , key , rect ) ;
267
+ let layoutInfo = new WaterfallLayoutInfo ( node . type , key , rect ) ;
255
268
layoutInfo . estimatedSize = estimatedSize ;
256
269
layoutInfo . allowOverflow = true ;
257
270
layoutInfo . content = node ;
271
+ layoutInfo . column = column ;
258
272
newLayoutInfos . set ( key , layoutInfo ) ;
259
273
260
274
columnHeights [ column ] += layoutInfo . rect . height + minSpace . height ;
@@ -283,6 +297,7 @@ class WaterfallLayout<T extends object> extends Layout<Node<T>, GridLayoutOption
283
297
let maxHeight = Math . max ( ...columnHeights ) ;
284
298
this . contentSize = new Size ( this . virtualizer . visibleRect . width , maxHeight ) ;
285
299
this . layoutInfos = newLayoutInfos ;
300
+ this . numColumns = numColumns ;
286
301
}
287
302
288
303
getLayoutInfo ( key : Key ) : LayoutInfo {
0 commit comments