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

Skip disabled controls when moving to first/last item #8514

Merged
merged 15 commits into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 24 additions & 9 deletions src/Avalonia.Controls/ItemsControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -502,25 +502,40 @@ private void UpdatePseudoClasses(int itemCount)
IInputElement? from,
bool wrap)
{
IInputElement? result;
var c = from;
var current = from;

do
for (;;)
{
result = container.GetControl(direction, c, wrap);
var result = container.GetControl(direction, current, wrap);

if (result != null &&
result.Focusable &&
if (result is null || current == from)
{
return null;
}

if (result.Focusable &&
result.IsEffectivelyEnabled &&
result.IsEffectivelyVisible)
{
return result;
}

c = result;
} while (c != null && c != from && direction != NavigationDirection.First && direction != NavigationDirection.Last);
switch (direction)
{
//We did not find an enabled first item. Move downwards until we find one.
case NavigationDirection.First:
direction = NavigationDirection.Down;
break;

return null;
//We did not find an enabled last item. Move upwards until we find one.
case NavigationDirection.Last:
direction = NavigationDirection.Up;
break;

}

current = result;
}
}

private void PresenterChildIndexChanged(object? sender, ChildIndexChangedEventArgs e)
Expand Down
10 changes: 7 additions & 3 deletions src/Avalonia.Controls/Platform/DefaultMenuInteractionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public virtual void Attach(IMenu menu)
Menu.PointerPressed += PointerPressed;
Menu.PointerReleased += PointerReleased;
Menu.AddHandler(AccessKeyHandler.AccessKeyPressedEvent, AccessKeyPressed);
Menu.AddHandler(Avalonia.Controls.Menu.MenuOpenedEvent, MenuOpened);
Menu.AddHandler(MenuBase.MenuOpenedEvent, MenuOpened);
Menu.AddHandler(MenuItem.PointerEnteredItemEvent, PointerEntered);
Menu.AddHandler(MenuItem.PointerExitedItemEvent, PointerExited);
Menu.AddHandler(InputElement.PointerMovedEvent, PointerMoved);
Expand Down Expand Up @@ -89,7 +89,7 @@ public virtual void Detach(IMenu menu)
Menu.PointerPressed -= PointerPressed;
Menu.PointerReleased -= PointerReleased;
Menu.RemoveHandler(AccessKeyHandler.AccessKeyPressedEvent, AccessKeyPressed);
Menu.RemoveHandler(Avalonia.Controls.Menu.MenuOpenedEvent, MenuOpened);
Menu.RemoveHandler(MenuBase.MenuOpenedEvent, MenuOpened);
Menu.RemoveHandler(MenuItem.PointerEnteredItemEvent, PointerEntered);
Menu.RemoveHandler(MenuItem.PointerExitedItemEvent, PointerExited);
Menu.RemoveHandler(InputElement.PointerMovedEvent, PointerMoved);
Expand Down Expand Up @@ -175,7 +175,11 @@ protected internal virtual void KeyDown(IMenuItem? item, KeyEventArgs e)

case Key.Left:
{
if (item?.Parent is IMenuItem parent && !parent.IsTopLevel && parent.IsSubMenuOpen)
if (item is { IsSubMenuOpen: true, SelectedItem: null })
{
item.Close();
}
else if (item?.Parent is IMenuItem { IsTopLevel: false, IsSubMenuOpen: true } parent)
{
parent.Close();
parent.Focus();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1595,8 +1595,8 @@ public void Can_Set_Both_SelectedItem_And_SelectedItems_During_Initialization()
Assert.Equal(new[] { "Bar" }, selectedItems);
}

[Fact]
public void MoveSelection_Wrap_Does_Not_Hang_With_No_Focusable_Controls()
[Fact(Timeout = 2000)]
public async Task MoveSelection_Wrap_Does_Not_Hang_With_No_Focusable_Controls()
{
// Issue #3094.
var target = new TestSelector
Expand All @@ -1612,11 +1612,34 @@ public void MoveSelection_Wrap_Does_Not_Hang_With_No_Focusable_Controls()

target.Measure(new Size(100, 100));
target.Arrange(new Rect(0, 0, 100, 100));
target.MoveSelection(NavigationDirection.Next, true);

// Timeout in xUnit doesn't work with synchronous methods so we need to apply hack below.
// https://github.com/xunit/xunit/issues/2222
await Task.Run(() => target.MoveSelection(NavigationDirection.Next, true));
}

[Fact(Timeout = 2000)]
public async Task MoveSelection_Does_Not_Hang_With_No_Focusable_Controls_And_Moving_Selection_To_The_First_Item()
[Fact]
public void MoveSelection_Skips_Non_Focusable_Controls_When_Moving_To_Last_Item()
{
var target = new TestSelector
{
Template = Template(),
Items = new[]
{
new ListBoxItem(),
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(0, target.SelectedIndex);
}

[Fact]
public void MoveSelection_Skips_Non_Focusable_Controls_When_Moving_To_First_Item()
{
var target = new TestSelector
{
Expand All @@ -1630,32 +1653,54 @@ public async Task MoveSelection_Does_Not_Hang_With_No_Focusable_Controls_And_Mov

target.Measure(new Size(100, 100));
target.Arrange(new Rect(0, 0, 100, 100));
target.MoveSelection(NavigationDirection.Last, true);

Assert.Equal(1, target.SelectedIndex);
}

// Timeout in xUnit doesen't work with synchronous methods so we need to apply hack below.
[Fact(Timeout = 2000)]
public async Task 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));

// Timeout in xUnit doesn't work with synchronous methods so we need to apply hack below.
// https://github.com/xunit/xunit/issues/2222
await Task.Run(() => target.MoveSelection(NavigationDirection.First, true));

Assert.Equal(-1, target.SelectedIndex);
}

[Fact(Timeout = 2000)]
public async Task MoveSelection_Does_Not_Hang_With_No_Focusable_Controls_And_Moving_Selection_To_The_Last_Item()
public async Task 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(),
new ListBoxItem { Focusable = false },
new ListBoxItem { Focusable = false },
}
};

target.Measure(new Size(100, 100));
target.Arrange(new Rect(0, 0, 100, 100));

// Timeout in xUnit doesen't work with synchronous methods so we need to apply hack below.
// Timeout in xUnit doesn't work with synchronous methods so we need to apply hack below.
// https://github.com/xunit/xunit/issues/2222
await Task.Run(() => target.MoveSelection(NavigationDirection.Last, true));

Assert.Equal(-1, target.SelectedIndex);
}

Expand Down