Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disabled items in ComboBox souldn't be selectable with arrow keys. #7963

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 4 additions & 28 deletions src/Avalonia.Controls/ComboBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -453,42 +453,18 @@ private void SelectFocusedItem()

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

if (next >= ItemCount)
if (ItemCount >= 1)
{
if (WrapSelection == true)
{
next = 0;
}
else
{
return;
}
MoveSelection(NavigationDirection.Next, WrapSelection);
}



SelectedIndex = next;
}

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

if (prev < 0)
if (ItemCount >= 1)
{
if (WrapSelection == true)
{
prev = ItemCount - 1;
}
else
{
return;
}
MoveSelection(NavigationDirection.Previous, WrapSelection);
}

SelectedIndex = prev;
}
}
}
1 change: 0 additions & 1 deletion src/Avalonia.Controls/ItemsControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,6 @@ private void UpdatePseudoClasses(int itemCount)
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 @@ public Orientation Orientation
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