Browse Source

Merge pull request #6818 from simonhaines/fixes/6412-disabled-menu-items

Filter pointer pressed events from disabled sub-menu items
pull/6868/head
Tako 5 years ago
committed by GitHub
parent
commit
49efa51a91
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      src/Avalonia.Controls/Platform/DefaultMenuInteractionHandler.cs
  2. 17
      tests/Avalonia.Controls.UnitTests/Platform/DefaultMenuInteractionHandlerTests.cs

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

@ -1,4 +1,5 @@
using System;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Input.Raw;
using Avalonia.Interactivity;
@ -376,7 +377,10 @@ namespace Avalonia.Controls.Platform
{
if (item.IsSubMenuOpen)
{
if (item.IsTopLevel)
// PointerPressed events may bubble from disabled items in sub-menus. In this case,
// keep the sub-menu open.
var popup = (e.Source as ILogical)?.FindLogicalAncestorOfType<Popup>();
if (item.IsTopLevel && popup == null)
{
CloseMenu(item);
}

17
tests/Avalonia.Controls.UnitTests/Platform/DefaultMenuInteractionHandlerTests.cs

@ -1,5 +1,6 @@
using System;
using Avalonia.Controls.Platform;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.VisualTree;
@ -540,6 +541,22 @@ namespace Avalonia.Controls.UnitTests.Platform
Mock.Get(item).Verify(x => x.MoveSelection(NavigationDirection.First, true), Times.Never);
Assert.True(e.Handled);
}
[Fact]
public void PointerPressed_On_Disabled_Item_Doesnt_Close_SubMenu()
{
var target = new DefaultMenuInteractionHandler(false);
var menu = Mock.Of<IMenu>();
var parentItem = Mock.Of<IMenuItem>(x => x.IsTopLevel == true && x.HasSubMenu == true && x.IsSubMenuOpen == true && x.Parent == menu);
var popup = new Popup();
var e = CreatePressed(popup);
((ISetLogicalParent)popup).SetParent(parentItem);
target.PointerPressed(parentItem, e);
Mock.Get(parentItem).Verify(x => x.Close(), Times.Never);
Assert.True(e.Handled);
}
}
public class ContextMenu

Loading…
Cancel
Save