Skip to content

Commit

Permalink
Merge pull request AvaloniaUI#8034 from AvaloniaUI/fix-contextMenu-fr…
Browse files Browse the repository at this point in the history
…eeze

Fix ContextMenu freeze.
# Conflicts:
#	tests/Avalonia.Controls.UnitTests/Primitives/SelectingItemsControlTests.cs
  • Loading branch information
Takoooooo authored and danwalmsley committed May 10, 2022
1 parent 4fab647 commit 46bf28a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Avalonia.Controls/ItemsControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ protected static IInputElement GetNextControl(
}

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

return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ protected internal virtual void PointerReleased(object sender, PointerReleasedEv

protected internal virtual void MenuOpened(object sender, RoutedEventArgs e)
{
if (e.Source == Menu)
if (e.Source is Menu)
{
Menu?.MoveSelection(NavigationDirection.First, true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Reactive.Disposables;
using System.Threading.Tasks;
using Avalonia.Collections;
using Avalonia.Controls.Presenters;
using Avalonia.Controls.Primitives;
Expand Down Expand Up @@ -1610,6 +1612,50 @@ public void MoveSelection_Wrap_Does_Not_Hang_With_No_Focusable_Controls()
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()
{
var target = new TestSelector
{
Template = Template(),
Items = new[]
{
new ListBoxItem { Focusable = false },
new ListBoxItem(),
}
};

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.
// 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()
{
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));

// Timeout in xUnit doesen'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);
}

[Fact]
public void MoveSelection_Does_Select_Disabled_Controls()
{
Expand Down

0 comments on commit 46bf28a

Please sign in to comment.