@@ -92,26 +92,24 @@ protected override Size MeasureOverride(Size availableSize)
9292 double xSize = 0 , ySize = 0 ;
9393
9494 // Define UV coords for orientation agnostic XY manipulation
95- ref double uSize = ref SelectAxis ( Orientation , ref xSize , ref ySize , true ) ;
96- ref double vSize = ref SelectAxis ( Orientation , ref xSize , ref ySize , false ) ;
97- ref double maxItemU = ref SelectAxis ( Orientation , ref _maxItemWidth , ref _maxItemHeight , true ) ;
98- ref double maxItemV = ref SelectAxis ( Orientation , ref _maxItemWidth , ref _maxItemHeight , false ) ;
95+ var uvSize = new UVCoord ( ref xSize , ref ySize , Orientation ) ;
96+ var maxItemSize = new UVCoord ( ref _maxItemWidth , ref _maxItemHeight , Orientation ) ;
9997 double availableU = Orientation is Orientation . Horizontal ? availableSize . Width : availableSize . Height ;
10098
10199 if ( stretch )
102100 {
103101 // Adjust maxItemU to form equal rows/columns by available U space (adjust for spacing)
104102 double totalU = availableU - ( Spacing * ( _visibleItemsCount - 1 ) ) ;
105- maxItemU = totalU / _visibleItemsCount ;
103+ maxItemSize . U = totalU / _visibleItemsCount ;
106104
107105 // Set uSize/vSize for XY result construction
108- uSize = availableU ;
109- vSize = maxItemV ;
106+ uvSize . U = availableU ;
107+ uvSize . V = maxItemSize . V ;
110108 }
111109 else
112110 {
113- uSize = ( maxItemU * _visibleItemsCount ) + ( Spacing * ( _visibleItemsCount - 1 ) ) ;
114- vSize = maxItemV ;
111+ uvSize . U = ( maxItemSize . U * _visibleItemsCount ) + ( Spacing * ( _visibleItemsCount - 1 ) ) ;
112+ uvSize . V = maxItemSize . V ;
115113 }
116114
117115 return new Size ( xSize , ySize ) ;
@@ -125,12 +123,11 @@ protected override Size ArrangeOverride(Size finalSize)
125123 double y = 0 ;
126124
127125 // Define UV axis
128- ref double u = ref x ;
126+ var pos = new UVCoord ( ref x , ref y , Orientation ) ;
129127 ref double maxItemU = ref _maxItemWidth ;
130128 double finalSizeU = finalSize . Width ;
131129 if ( Orientation is Orientation . Vertical )
132130 {
133- u = ref y ;
134131 maxItemU = ref _maxItemHeight ;
135132 finalSizeU = finalSize . Height ;
136133 }
@@ -146,7 +143,7 @@ protected override Size ArrangeOverride(Size finalSize)
146143 {
147144 // NOTE: The arrange method is still in X/Y coordinate system
148145 child . Arrange ( new Rect ( x , y , _maxItemWidth , _maxItemHeight ) ) ;
149- u += maxItemU + Spacing ;
146+ pos . U += maxItemU + Spacing ;
150147 }
151148 return finalSize ;
152149 }
@@ -162,11 +159,55 @@ private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChan
162159 panel . InvalidateMeasure ( ) ;
163160 }
164161
165- private static ref double SelectAxis ( Orientation orientation , ref double x , ref double y , bool u )
162+ /// <summary>
163+ /// A struct for mapping X/Y coordinates to an orientation adjusted U/V coordinate system.
164+ /// </summary>
165+ private ref struct UVCoord
166166 {
167- if ( ( orientation is Orientation . Horizontal && u ) || ( orientation is Orientation . Vertical && ! u ) )
168- return ref x ;
169- else
170- return ref y ;
167+ private readonly bool _vertical ;
168+
169+ private ref double _x ;
170+ private ref double _y ;
171+
172+ public UVCoord ( ref double x , ref double y , Orientation orientation )
173+ {
174+ _x = ref x ;
175+ _y = ref y ;
176+ _vertical = orientation is Orientation . Vertical ;
177+ }
178+
179+ public ref double X => ref _x ;
180+
181+ public ref double Y => ref _y ;
182+
183+ public ref double U
184+ {
185+ get
186+ {
187+ if ( _vertical )
188+ {
189+ return ref Y ;
190+ }
191+ else
192+ {
193+ return ref X ;
194+ }
195+ }
196+ }
197+
198+ public ref double V
199+ {
200+ get
201+ {
202+ if ( _vertical )
203+ {
204+ return ref X ;
205+ }
206+ else
207+ {
208+ return ref Y ;
209+ }
210+ }
211+ }
171212 }
172213}
0 commit comments