Skip to content

Commit 723664d

Browse files
authored
Merge branch 'main' into fix_WrapLayoutSample
2 parents 172c09b + b97f909 commit 723664d

File tree

1 file changed

+34
-55
lines changed

1 file changed

+34
-55
lines changed

components/Segmented/src/Segmented/Segmented.cs

Lines changed: 34 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,20 @@ public Segmented()
3030

3131
/// <inheritdoc/>
3232
protected override bool IsItemItsOwnContainerOverride(object item)
33-
{
34-
return item is SegmentedItem;
35-
}
33+
=> item is SegmentedItem;
3634

3735
/// <inheritdoc/>
3836
protected override void OnApplyTemplate()
3937
{
4038
base.OnApplyTemplate();
39+
4140
if (!_hasLoaded)
4241
{
4342
SelectedIndex = -1;
4443
SelectedIndex = _internalSelectedIndex;
4544
_hasLoaded = true;
4645
}
46+
4747
PreviewKeyDown -= Segmented_PreviewKeyDown;
4848
PreviewKeyDown += Segmented_PreviewKeyDown;
4949
}
@@ -52,82 +52,61 @@ protected override void OnApplyTemplate()
5252
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
5353
{
5454
base.PrepareContainerForItemOverride(element, item);
55+
5556
if (element is SegmentedItem segmentedItem)
5657
{
57-
segmentedItem.Loaded += SegmentedItem_Loaded;
58+
segmentedItem.UpdateOrientation(Orientation);
5859
}
5960
}
6061

6162
private void Segmented_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
6263
{
63-
switch (e.Key)
64+
var dir = e.Key switch
6465
{
65-
case VirtualKey.Left: e.Handled = MoveFocus(MoveDirection.Previous); break;
66-
case VirtualKey.Right: e.Handled = MoveFocus(MoveDirection.Next); break;
67-
}
68-
}
66+
#if !HAS_UNO
67+
// Invert left/right when the flow direction is right to left
68+
VirtualKey.Left when FlowDirection is FlowDirection.RightToLeft => 1,
69+
VirtualKey.Right when FlowDirection is FlowDirection.RightToLeft => -1,
70+
#endif
6971

70-
private void SegmentedItem_Loaded(object sender, RoutedEventArgs e)
71-
{
72-
if (sender is SegmentedItem segmentedItem)
73-
{
74-
segmentedItem.Loaded -= SegmentedItem_Loaded;
75-
}
76-
}
72+
// Decrement if left/up or increment if right/up
73+
VirtualKey.Left or VirtualKey.Up => -1,
74+
VirtualKey.Right or VirtualKey.Down => 1,
7775

78-
/// <inheritdoc/>
79-
protected override void OnItemsChanged(object e)
80-
{
81-
base.OnItemsChanged(e);
82-
}
76+
// Otherwise don't adjust
77+
_ => 0,
78+
};
8379

84-
private enum MoveDirection
85-
{
86-
Next,
87-
Previous
80+
if (dir is not 0)
81+
{
82+
e.Handled = MoveFocus(dir);
83+
}
8884
}
8985

9086
/// <summary>
9187
/// Adjust the selected item and range based on keyboard input.
9288
/// This is used to override the ListView behaviors for up/down arrow manipulation vs left/right for a horizontal control
9389
/// </summary>
94-
/// <param name="direction">direction to move the selection</param>
90+
/// <param name="adjustment">The signed number of indicies to shift the focus.</param>
9591
/// <returns>True if the focus was moved, false otherwise</returns>
96-
private bool MoveFocus(MoveDirection direction)
92+
private bool MoveFocus(int adjustment)
9793
{
98-
bool retVal = false;
9994
var currentContainerItem = GetCurrentContainerItem();
95+
if (currentContainerItem is null)
96+
return false;
10097

101-
if (currentContainerItem != null)
102-
{
103-
var currentItem = ItemFromContainer(currentContainerItem);
104-
var previousIndex = Items.IndexOf(currentItem);
105-
var index = previousIndex;
98+
var currentItem = ItemFromContainer(currentContainerItem);
99+
var previousIndex = Items.IndexOf(currentItem);
106100

107-
if (direction == MoveDirection.Previous)
108-
{
109-
if (previousIndex > 0)
110-
{
111-
index -= 1;
112-
}
113-
}
114-
else if (direction == MoveDirection.Next)
115-
{
116-
if (previousIndex < Items.Count - 1)
117-
{
118-
index += 1;
119-
}
120-
}
101+
// Apply the adjustment, with a clamp
102+
var index = Math.Clamp(previousIndex + adjustment, 0, Items.Count);
121103

122-
// Only do stuff if the index is actually changing
123-
if (index != previousIndex && ContainerFromIndex(index) is SegmentedItem newItem)
124-
{
125-
newItem.Focus(FocusState.Keyboard);
126-
retVal = true;
127-
}
128-
}
104+
// Only do stuff if the index is actually changing
105+
if (index == previousIndex || ContainerFromIndex(index) is not SegmentedItem newItem)
106+
return false;
129107

130-
return retVal;
108+
newItem.Focus(FocusState.Keyboard);
109+
return true;
131110
}
132111

133112
private SegmentedItem? GetCurrentContainerItem()

0 commit comments

Comments
 (0)