Skip to content

Commit 4b3fb0e

Browse files
committed
Addressed simple code style suggestions from Arlo
1 parent fe533f5 commit 4b3fb0e

File tree

2 files changed

+40
-42
lines changed

2 files changed

+40
-42
lines changed

components/Segmented/src/EqualPanel.cs

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
using Microsoft.UI.Xaml.Controls;
65
using System.Data;
76

87
namespace CommunityToolkit.WinUI.Controls;
@@ -77,46 +76,45 @@ protected override Size MeasureOverride(Size availableSize)
7776
_maxItemHeight = Math.Max(_maxItemHeight, child.DesiredSize.Height);
7877
}
7978

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
8186
{
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;
115110
}
116111
else
117112
{
118-
return new Size(0, 0);
113+
uSize = (maxItemU * _visibleItemsCount) + (Spacing * (_visibleItemsCount - 1));
114+
vSize = maxItemV;
119115
}
116+
117+
return new Size(xSize, ySize);
120118
}
121119

122120
/// <inheritdoc/>

components/Segmented/src/SegmentedItem/SegmentedItem.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public SegmentedItem()
3232
protected override void OnApplyTemplate()
3333
{
3434
base.OnApplyTemplate();
35-
UpdateState();
35+
UpdateVisualStates();
3636
}
3737

3838
/// <summary>
@@ -41,21 +41,21 @@ protected override void OnApplyTemplate()
4141
protected override void OnContentChanged(object oldContent, object newContent)
4242
{
4343
base.OnContentChanged(oldContent, newContent);
44-
UpdateState();
44+
UpdateVisualStates();
4545
}
4646

4747
/// <summary>
4848
/// Handles changes to the Icon property.
4949
/// </summary>
50-
protected virtual void OnIconPropertyChanged(IconElement oldValue, IconElement newValue) => UpdateState();
50+
protected virtual void OnIconPropertyChanged(IconElement oldValue, IconElement newValue) => UpdateVisualStates();
5151

5252
internal void UpdateOrientation(Orientation orientation)
5353
{
5454
_isVertical = orientation is Orientation.Vertical;
55-
UpdateState();
55+
UpdateVisualStates();
5656
}
5757

58-
private void UpdateState()
58+
private void UpdateVisualStates()
5959
{
6060
string contentState = (Icon is null, Content is null) switch
6161
{
@@ -65,7 +65,7 @@ private void UpdateState()
6565
(true, true) => ContentOnlyState, // Invalid state. Treat as content only
6666
};
6767

68-
// Update states
68+
// Update visual states
6969
VisualStateManager.GoToState(this, contentState, true);
7070
VisualStateManager.GoToState(this, _isVertical ? VerticalState : HorizontalState, true);
7171
}

0 commit comments

Comments
 (0)