Skip to content

Commit

Permalink
Merge pull request AvaloniaUI#8514 from pr8x/move-first-last-skip-dis…
Browse files Browse the repository at this point in the history
…abled

Skip disabled controls when moving to first/last item
  • Loading branch information
maxkatz6 authored and grokys committed Dec 2, 2022
1 parent 3e43d6f commit d3723c6
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 25 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 32 additions & 11 deletions src/Avalonia.Controls/ItemsControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -503,29 +503,50 @@ 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);
from = from ?? result;
var result = container.GetControl(direction, current, wrap);

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

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

c = result;
} while (c != null && c != from && direction != NavigationDirection.First && direction != NavigationDirection.Last);
current = result;

return null;
if (current == from)
{
return null;
}

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

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

}
}
}

private void PresenterChildIndexChanged(object sender, ChildIndexChangedEventArgs e)
private void PresenterChildIndexChanged(object? sender, ChildIndexChangedEventArgs e)
{
_childIndexChanged?.Invoke(this, 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 @@ -52,7 +52,7 @@ public virtual void Attach(IMenu menu)
Menu.PointerPressed += PointerPressed;
Menu.PointerReleased += PointerReleased;
Menu.AddHandler(AccessKeyHandler.AccessKeyPressedEvent, AccessKeyPressed);
Menu.AddHandler(Avalonia.Controls.Menu.MenuOpenedEvent, this.MenuOpened);
Menu.AddHandler(MenuBase.MenuOpenedEvent, MenuOpened);
Menu.AddHandler(MenuItem.PointerEnterItemEvent, PointerEnter);
Menu.AddHandler(MenuItem.PointerLeaveItemEvent, PointerLeave);
Menu.AddHandler(InputElement.PointerMovedEvent, PointerMoved);
Expand Down Expand Up @@ -88,7 +88,7 @@ public virtual void Detach(IMenu menu)
Menu.PointerPressed -= PointerPressed;
Menu.PointerReleased -= PointerReleased;
Menu.RemoveHandler(AccessKeyHandler.AccessKeyPressedEvent, AccessKeyPressed);
Menu.RemoveHandler(Avalonia.Controls.Menu.MenuOpenedEvent, this.MenuOpened);
Menu.RemoveHandler(MenuBase.MenuOpenedEvent, MenuOpened);
Menu.RemoveHandler(MenuItem.PointerEnterItemEvent, PointerEnter);
Menu.RemoveHandler(MenuItem.PointerLeaveItemEvent, PointerLeave);
Menu.RemoveHandler(InputElement.PointerMovedEvent, PointerMoved);
Expand Down Expand Up @@ -169,7 +169,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 @@ -1605,8 +1605,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 @@ -1622,11 +1622,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 @@ -1640,32 +1663,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

0 comments on commit d3723c6

Please sign in to comment.