Skip to content

Commit

Permalink
Merge pull request AvaloniaUI#7963 from AvaloniaUI/disabled-items-sho…
Browse files Browse the repository at this point in the history
…uld-not-be-selectable-with-keyboard
  • Loading branch information
Takoooooo committed May 10, 2022
1 parent 4fab647 commit fa7294a
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 14 deletions.
20 changes: 8 additions & 12 deletions src/Avalonia.Controls/ComboBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -429,22 +429,18 @@ private void SelectFocusedItem()

private void SelectNext()
{
int next = SelectedIndex + 1;

if (next >= ItemCount)
next = 0;

SelectedIndex = next;
if (ItemCount >= 1)
{
MoveSelection(NavigationDirection.Next, WrapSelection);
}
}

private void SelectPrev()
{
int prev = SelectedIndex - 1;

if (prev < 0)
prev = ItemCount - 1;

SelectedIndex = prev;
if (ItemCount >= 1)
{
MoveSelection(NavigationDirection.Previous, WrapSelection);
}
}
}
}
1 change: 0 additions & 1 deletion src/Avalonia.Controls/ItemsControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,6 @@ protected static IInputElement GetNextControl(
do
{
result = container.GetControl(direction, c, wrap);
from = from ?? result;

if (result != null &&
result.Focusable &&
Expand Down
2 changes: 1 addition & 1 deletion src/Avalonia.Controls/StackPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ protected virtual IInputElement GetControlInDirection(NavigationDirection direct
index = Children.Count - 1;
break;
case NavigationDirection.Next:
if (index != -1) ++index;
++index;
break;
case NavigationDirection.Previous:
if (index != -1) --index;
Expand Down
75 changes: 75 additions & 0 deletions tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,81 @@ public void Clicking_On_Control_Toggles_IsDropDownOpen()
Assert.False(target.IsDropDownOpen);
}

[Fact]
public void WrapSelection_Should_Work()
{
using (UnitTestApplication.Start(TestServices.RealFocus))
{
var items = new[]
{
new ComboBoxItem() { Content = "bla" },
new ComboBoxItem() { Content = "dd" },
new ComboBoxItem() { Content = "sdf", IsEnabled = false }
};
var target = new ComboBox
{
Items = items,
Template = GetTemplate(),
WrapSelection = true
};
var root = new TestRoot(target);
target.ApplyTemplate();
target.Presenter.ApplyTemplate();
target.Focus();
Assert.Equal(target.SelectedIndex, -1);
Assert.True(target.IsFocused);
target.RaiseEvent(new KeyEventArgs
{
RoutedEvent = InputElement.KeyDownEvent,
Key = Key.Up,
});
Assert.Equal(target.SelectedIndex, 1);
target.RaiseEvent(new KeyEventArgs
{
RoutedEvent = InputElement.KeyDownEvent,
Key = Key.Down,
});
Assert.Equal(target.SelectedIndex, 0);
}
}

[Fact]
public void Focuses_Next_Item_On_Key_Down()
{
using (UnitTestApplication.Start(TestServices.RealFocus))
{
var items = new[]
{
new ComboBoxItem() { Content = "bla" },
new ComboBoxItem() { Content = "dd", IsEnabled = false },
new ComboBoxItem() { Content = "sdf" }
};
var target = new ComboBox
{
Items = items,
Template = GetTemplate()
};
var root = new TestRoot(target);
target.ApplyTemplate();
target.Presenter.ApplyTemplate();
target.Focus();
Assert.Equal(target.SelectedIndex, -1);
Assert.True(target.IsFocused);
target.RaiseEvent(new KeyEventArgs
{
RoutedEvent = InputElement.KeyDownEvent,
Key = Key.Down,
});
Assert.Equal(target.SelectedIndex, 0);
target.RaiseEvent(new KeyEventArgs
{
RoutedEvent = InputElement.KeyDownEvent,
Key = Key.Down,
});
Assert.Equal(target.SelectedIndex, 2);
}
}

[Fact]
public void SelectionBoxItem_Is_Rectangle_With_VisualBrush_When_Selection_Is_Control()
{
Expand Down

0 comments on commit fa7294a

Please sign in to comment.