@@ -79,21 +79,20 @@ private static Size MeasureResizedNode(ResizedNode resized, AssetProvider assets
7979 private static Size MeasureStackNode ( StackNode stack , AssetProvider assets , AssetProvider measurer ,
8080 ConcurrentDictionary < Node , Size > measurementCache )
8181 {
82- List < Node > flowChildren = ExpandFlowChildren ( stack . Children ) ;
82+ IReadOnlyList < Node > flowChildren = [ .. ExpandFlowChildren ( stack . Children ) ] ;
8383 List < ( Node Node , Size Size ) > items = [ ] ;
8484 if ( flowChildren . Count > 0 )
8585 {
8686 Size [ ] sizes = new Size [ flowChildren . Count ] ;
8787 Parallel . For ( 0 , flowChildren . Count ,
8888 i => { sizes [ i ] = Measure ( flowChildren [ i ] , assets , measurer , measurementCache ) ; } ) ;
89- items . EnsureCapacity ( flowChildren . Count ) ;
90- items . AddRange ( flowChildren . Select ( ( t , i ) => ( t , sizes [ i ] ) ) ) ;
89+ items = [ .. flowChildren . Select ( ( child , index ) => ( child , sizes [ index ] ) ) ] ;
9190 }
9291
9392 bool isRow = stack . Direction is StackDirection . Row ;
9493 float wrapMain = ResolveMainAxisContainerSize ( isRow ? stack . Width : stack . Height , null , int . MaxValue ) ;
95- List < List < ( Node Node , Size Size ) > > lines = ResolveStackLines ( items , isRow , stack . Wrap , stack . Spacing , wrapMain ) ;
96- List < ( float Main , int Cross ) > lineSizes = ResolveStackLineSizes ( lines , isRow , stack . Spacing ) ;
94+ ImmutableArray < ImmutableArray < ( Node Node , Size Size ) > > lines = ResolveStackLines ( items , isRow , stack . Wrap , stack . Spacing , wrapMain ) ;
95+ ImmutableArray < ( float Main , int Cross ) > lineSizes = ResolveStackLineSizes ( [ .. lines ] , isRow , stack . Spacing ) ;
9796 ( float contentMain , float contentCross ) = ResolveStackContentSize ( lineSizes , stack . RunSpacing ) ;
9897
9998 int contentWidth = ( int ) Math . Ceiling ( isRow ? contentMain : contentCross ) ;
@@ -104,7 +103,7 @@ private static Size MeasureStackNode(StackNode stack, AssetProvider assets, Asse
104103 private static Size MeasureGridNode ( GridNode grid , AssetProvider assets , AssetProvider measurer ,
105104 ConcurrentDictionary < Node , Size > measurementCache )
106105 {
107- List < Node > flowChildren = ExpandFlowChildren ( grid . Children ) ;
106+ IReadOnlyList < Node > flowChildren = [ .. ExpandFlowChildren ( grid . Children ) ] ;
108107 if ( flowChildren . Count is 0 )
109108 {
110109 return Size . Empty ;
@@ -115,16 +114,8 @@ private static Size MeasureGridNode(GridNode grid, AssetProvider assets, AssetPr
115114 Size [ ] sizes = new Size [ flowChildren . Count ] ;
116115 Parallel . For ( 0 , flowChildren . Count ,
117116 i => { sizes [ i ] = Measure ( flowChildren [ i ] , assets , measurer , measurementCache ) ; } ) ;
118- int [ ] colWidths = new int [ columns ] ;
119- int [ ] rowHeights = new int [ rows ] ;
120- for ( int i = 0 ; i < flowChildren . Count ; i ++ )
121- {
122- Size size = sizes [ i ] ;
123- int r = i / columns ;
124- int c = i % columns ;
125- colWidths [ c ] = Math . Max ( colWidths [ c ] , size . Width ) ;
126- rowHeights [ r ] = Math . Max ( rowHeights [ r ] , size . Height ) ;
127- }
117+ IEnumerable < int > colWidths = Enumerable . Range ( 0 , columns ) . Select ( c => sizes . Where ( ( _ , i ) => i % columns == c ) . DefaultIfEmpty ( Size . Empty ) . Max ( size => size . Width ) ) ;
118+ IEnumerable < int > rowHeights = Enumerable . Range ( 0 , rows ) . Select ( r => sizes . Where ( ( _ , i ) => i / columns == r ) . DefaultIfEmpty ( Size . Empty ) . Max ( size => size . Height ) ) ;
128119
129120 int width = colWidths . Sum ( ) + ( Math . Max ( 0 , columns - 1 ) * grid . ColumnGap ) ;
130121 int height = rowHeights . Sum ( ) + ( Math . Max ( 0 , rows - 1 ) * grid . RowGap ) ;
@@ -147,32 +138,9 @@ private static Size MeasureLayerNode(LayerNode layer, AssetProvider assets, Asse
147138 private static Size MeasureChildren ( IEnumerable < Node > children , AssetProvider assets , AssetProvider measurer ,
148139 ConcurrentDictionary < Node , Size > measurementCache )
149140 {
150- int width = 0 ;
151- int height = 0 ;
152- foreach ( Node child in children )
153- {
154- Size size = Measure ( child , assets , measurer , measurementCache ) ;
155- width = Math . Max ( width , size . Width ) ;
156- height = Math . Max ( height , size . Height ) ;
157- }
158-
141+ ( int width , int height ) = children . Select ( child => Measure ( child , assets , measurer , measurementCache ) ) . Aggregate ( ( Width : 0 , Height : 0 ) , static ( acc , size ) => ( Math . Max ( acc . Width , size . Width ) , Math . Max ( acc . Height , size . Height ) ) ) ;
159142 return new ( width , height ) ;
160143 }
161144
162- private static List < Node > ExpandFlowChildren ( IEnumerable < Node > children )
163- {
164- List < Node > output = [ ] ;
165- foreach ( Node child in children )
166- {
167- if ( child is LayerNode { Opacity : 1 , Key : null , Children : var nested } )
168- {
169- output . AddRange ( ExpandFlowChildren ( nested ) ) ;
170- continue ;
171- }
172-
173- output . Add ( child ) ;
174- }
175-
176- return output ;
177- }
145+ private static IEnumerable < Node > ExpandFlowChildren ( IEnumerable < Node > children ) => children . SelectMany ( static child => child is LayerNode { Opacity : 1 , Key : null , Children : var nested } ? ExpandFlowChildren ( nested ) : [ child ] ) ;
178146}
0 commit comments