Browse Source

Fix ContextMenu freeze.

pull/8034/head
Tako 4 years ago
parent
commit
7c63e1a60b
  1. 2
      src/Avalonia.Controls/ItemsControl.cs
  2. 2
      src/Avalonia.Controls/Platform/DefaultMenuInteractionHandler.cs
  3. 45
      tests/Avalonia.Controls.UnitTests/Primitives/SelectingItemsControlTests.cs

2
src/Avalonia.Controls/ItemsControl.cs

@ -518,7 +518,7 @@ namespace Avalonia.Controls
}
c = result;
} while (c != null && c != from);
} while (c != null && c != from && direction != NavigationDirection.First && direction != NavigationDirection.Last);
return null;
}

2
src/Avalonia.Controls/Platform/DefaultMenuInteractionHandler.cs

@ -417,7 +417,7 @@ namespace Avalonia.Controls.Platform
protected internal virtual void MenuOpened(object? sender, RoutedEventArgs e)
{
if (e.Source == Menu)
if (e.Source is Menu)
{
Menu?.MoveSelection(NavigationDirection.First, true);
}

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

@ -6,6 +6,7 @@ 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;
@ -1614,6 +1615,50 @@ namespace Avalonia.Controls.UnitTests.Primitives
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()
{

Loading…
Cancel
Save