Browse Source

Fix infinite loop when all items are disabled

pull/8514/head
Luis von der Eltz 4 years ago
parent
commit
107fc7162e
  1. 16
      src/Avalonia.Controls/ItemsControl.cs
  2. 40
      tests/Avalonia.Controls.UnitTests/Primitives/SelectingItemsControlTests.cs

16
src/Avalonia.Controls/ItemsControl.cs

@ -518,15 +518,21 @@ namespace Avalonia.Controls
return result;
}
direction = direction switch
switch (direction)
{
//We did not find an enabled first item. Move downwards until we find one.
NavigationDirection.First => NavigationDirection.Down,
case NavigationDirection.First:
direction = NavigationDirection.Down;
wrap = false;
break;
//We did not find an enabled last item. Move upwards until we find one.
NavigationDirection.Last => NavigationDirection.Up,
_ => direction
};
case NavigationDirection.Last:
direction = NavigationDirection.Up;
wrap = false;
break;
}
from = result;
}

40
tests/Avalonia.Controls.UnitTests/Primitives/SelectingItemsControlTests.cs

@ -1655,6 +1655,46 @@ namespace Avalonia.Controls.UnitTests.Primitives
Assert.Equal(1, target.SelectedIndex);
}
[Fact(Timeout = 2000)]
public void MoveSelection_Does_Not_Hang_When_All_Items_Are_Non_Focusable_And_We_Move_To_First_Item()
{
var target = new TestSelector
{
Template = Template(),
Items = new[]
{
new ListBoxItem { Focusable = false },
new ListBoxItem { Focusable = false },
}
};
target.Measure(new Size(100, 100));
target.Arrange(new Rect(0, 0, 100, 100));
target.MoveSelection(NavigationDirection.First, true);
Assert.Equal(-1, target.SelectedIndex);
}
[Fact(Timeout = 2000)]
public void MoveSelection_Does_Not_Hang_When_All_Items_Are_Non_Focusable_And_We_Move_To_Last_Item()
{
var target = new TestSelector
{
Template = Template(),
Items = new[]
{
new ListBoxItem { Focusable = false },
new ListBoxItem { Focusable = false },
}
};
target.Measure(new Size(100, 100));
target.Arrange(new Rect(0, 0, 100, 100));
target.MoveSelection(NavigationDirection.Last, true);
Assert.Equal(-1, target.SelectedIndex);
}
[Fact]
public void MoveSelection_Does_Select_Disabled_Controls()
{

Loading…
Cancel
Save