|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | 3 | // See the LICENSE file in the project root for more information. |
4 | 4 |
|
5 | | -using Microsoft.UI.Xaml.Controls; |
6 | 5 | using System.Data; |
7 | 6 |
|
8 | 7 | namespace CommunityToolkit.WinUI.Controls; |
@@ -77,46 +76,45 @@ protected override Size MeasureOverride(Size availableSize) |
77 | 76 | _maxItemHeight = Math.Max(_maxItemHeight, child.DesiredSize.Height); |
78 | 77 | } |
79 | 78 |
|
80 | | - if (_visibleItemsCount > 0) |
| 79 | + // No children, no space taken |
| 80 | + if (_visibleItemsCount <= 0) |
| 81 | + return new Size(0, 0); |
| 82 | + |
| 83 | + // Determine if the desired alignment is stretched. |
| 84 | + // Don't stretch if infinite space is available though. Attempting to divide infinite space will result in a crash. |
| 85 | + bool stretch = Orientation switch |
81 | 86 | { |
82 | | - bool stretch = Orientation switch |
83 | | - { |
84 | | - Orientation.Horizontal => HorizontalAlignment is HorizontalAlignment.Stretch && !double.IsInfinity(availableSize.Width), |
85 | | - Orientation.Vertical or _ => VerticalAlignment is VerticalAlignment.Stretch && !double.IsInfinity(availableSize.Height), |
86 | | - }; |
87 | | - |
88 | | - // Define XY coords |
89 | | - double xSize = 0, ySize = 0; |
90 | | - |
91 | | - // Define UV coords for orientation agnostic XY manipulation |
92 | | - ref double uSize = ref SelectAxis(Orientation, ref xSize, ref ySize, true); |
93 | | - ref double vSize = ref SelectAxis(Orientation, ref xSize, ref ySize, false); |
94 | | - ref double maxItemU = ref SelectAxis(Orientation, ref _maxItemWidth, ref _maxItemHeight, true); |
95 | | - ref double maxItemV = ref SelectAxis(Orientation, ref _maxItemWidth, ref _maxItemHeight, false); |
96 | | - double availableU = Orientation is Orientation.Horizontal ? availableSize.Width : availableSize.Height; |
97 | | - |
98 | | - if (stretch) |
99 | | - { |
100 | | - // Adjust maxItemU to form equal rows/columns by available U space (adjust for spacing) |
101 | | - double totalU = availableU - (Spacing * (_visibleItemsCount - 1)); |
102 | | - maxItemU = totalU / _visibleItemsCount; |
103 | | - |
104 | | - // Set uSize/vSize for XY result construction |
105 | | - uSize = availableU; |
106 | | - vSize = maxItemV; |
107 | | - } |
108 | | - else |
109 | | - { |
110 | | - uSize = (maxItemU * _visibleItemsCount) + (Spacing * (_visibleItemsCount - 1)); |
111 | | - vSize = maxItemV; |
112 | | - } |
113 | | - |
114 | | - return new Size(xSize, ySize); |
| 87 | + Orientation.Horizontal => HorizontalAlignment is HorizontalAlignment.Stretch && !double.IsInfinity(availableSize.Width), |
| 88 | + Orientation.Vertical or _ => VerticalAlignment is VerticalAlignment.Stretch && !double.IsInfinity(availableSize.Height), |
| 89 | + }; |
| 90 | + |
| 91 | + // Define XY coords |
| 92 | + double xSize = 0, ySize = 0; |
| 93 | + |
| 94 | + // 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); |
| 99 | + double availableU = Orientation is Orientation.Horizontal ? availableSize.Width : availableSize.Height; |
| 100 | + |
| 101 | + if (stretch) |
| 102 | + { |
| 103 | + // Adjust maxItemU to form equal rows/columns by available U space (adjust for spacing) |
| 104 | + double totalU = availableU - (Spacing * (_visibleItemsCount - 1)); |
| 105 | + maxItemU = totalU / _visibleItemsCount; |
| 106 | + |
| 107 | + // Set uSize/vSize for XY result construction |
| 108 | + uSize = availableU; |
| 109 | + vSize = maxItemV; |
115 | 110 | } |
116 | 111 | else |
117 | 112 | { |
118 | | - return new Size(0, 0); |
| 113 | + uSize = (maxItemU * _visibleItemsCount) + (Spacing * (_visibleItemsCount - 1)); |
| 114 | + vSize = maxItemV; |
119 | 115 | } |
| 116 | + |
| 117 | + return new Size(xSize, ySize); |
120 | 118 | } |
121 | 119 |
|
122 | 120 | /// <inheritdoc/> |
|
0 commit comments