Skip to content

Commit fa7294a

Browse files
committed
Merge pull request #7963 from AvaloniaUI/disabled-items-should-not-be-selectable-with-keyboard
1 parent 4fab647 commit fa7294a

File tree

4 files changed

+84
-14
lines changed

4 files changed

+84
-14
lines changed

src/Avalonia.Controls/ComboBox.cs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -429,22 +429,18 @@ private void SelectFocusedItem()
429429

430430
private void SelectNext()
431431
{
432-
int next = SelectedIndex + 1;
433-
434-
if (next >= ItemCount)
435-
next = 0;
436-
437-
SelectedIndex = next;
432+
if (ItemCount >= 1)
433+
{
434+
MoveSelection(NavigationDirection.Next, WrapSelection);
435+
}
438436
}
439437

440438
private void SelectPrev()
441439
{
442-
int prev = SelectedIndex - 1;
443-
444-
if (prev < 0)
445-
prev = ItemCount - 1;
446-
447-
SelectedIndex = prev;
440+
if (ItemCount >= 1)
441+
{
442+
MoveSelection(NavigationDirection.Previous, WrapSelection);
443+
}
448444
}
449445
}
450446
}

src/Avalonia.Controls/ItemsControl.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,6 @@ protected static IInputElement GetNextControl(
509509
do
510510
{
511511
result = container.GetControl(direction, c, wrap);
512-
from = from ?? result;
513512

514513
if (result != null &&
515514
result.Focusable &&

src/Avalonia.Controls/StackPanel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ protected virtual IInputElement GetControlInDirection(NavigationDirection direct
123123
index = Children.Count - 1;
124124
break;
125125
case NavigationDirection.Next:
126-
if (index != -1) ++index;
126+
++index;
127127
break;
128128
case NavigationDirection.Previous:
129129
if (index != -1) --index;

tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,81 @@ public void Clicking_On_Control_Toggles_IsDropDownOpen()
3636
Assert.False(target.IsDropDownOpen);
3737
}
3838

39+
[Fact]
40+
public void WrapSelection_Should_Work()
41+
{
42+
using (UnitTestApplication.Start(TestServices.RealFocus))
43+
{
44+
var items = new[]
45+
{
46+
new ComboBoxItem() { Content = "bla" },
47+
new ComboBoxItem() { Content = "dd" },
48+
new ComboBoxItem() { Content = "sdf", IsEnabled = false }
49+
};
50+
var target = new ComboBox
51+
{
52+
Items = items,
53+
Template = GetTemplate(),
54+
WrapSelection = true
55+
};
56+
var root = new TestRoot(target);
57+
target.ApplyTemplate();
58+
target.Presenter.ApplyTemplate();
59+
target.Focus();
60+
Assert.Equal(target.SelectedIndex, -1);
61+
Assert.True(target.IsFocused);
62+
target.RaiseEvent(new KeyEventArgs
63+
{
64+
RoutedEvent = InputElement.KeyDownEvent,
65+
Key = Key.Up,
66+
});
67+
Assert.Equal(target.SelectedIndex, 1);
68+
target.RaiseEvent(new KeyEventArgs
69+
{
70+
RoutedEvent = InputElement.KeyDownEvent,
71+
Key = Key.Down,
72+
});
73+
Assert.Equal(target.SelectedIndex, 0);
74+
}
75+
}
76+
77+
[Fact]
78+
public void Focuses_Next_Item_On_Key_Down()
79+
{
80+
using (UnitTestApplication.Start(TestServices.RealFocus))
81+
{
82+
var items = new[]
83+
{
84+
new ComboBoxItem() { Content = "bla" },
85+
new ComboBoxItem() { Content = "dd", IsEnabled = false },
86+
new ComboBoxItem() { Content = "sdf" }
87+
};
88+
var target = new ComboBox
89+
{
90+
Items = items,
91+
Template = GetTemplate()
92+
};
93+
var root = new TestRoot(target);
94+
target.ApplyTemplate();
95+
target.Presenter.ApplyTemplate();
96+
target.Focus();
97+
Assert.Equal(target.SelectedIndex, -1);
98+
Assert.True(target.IsFocused);
99+
target.RaiseEvent(new KeyEventArgs
100+
{
101+
RoutedEvent = InputElement.KeyDownEvent,
102+
Key = Key.Down,
103+
});
104+
Assert.Equal(target.SelectedIndex, 0);
105+
target.RaiseEvent(new KeyEventArgs
106+
{
107+
RoutedEvent = InputElement.KeyDownEvent,
108+
Key = Key.Down,
109+
});
110+
Assert.Equal(target.SelectedIndex, 2);
111+
}
112+
}
113+
39114
[Fact]
40115
public void SelectionBoxItem_Is_Rectangle_With_VisualBrush_When_Selection_Is_Control()
41116
{

0 commit comments

Comments
 (0)