Skip to content

Commit 49efa51

Browse files
authored
Merge pull request #6818 from simonhaines/fixes/6412-disabled-menu-items
Filter pointer pressed events from disabled sub-menu items
2 parents f6b9438 + 007361b commit 49efa51

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/Avalonia.Controls/Platform/DefaultMenuInteractionHandler.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using Avalonia.Controls.Primitives;
23
using Avalonia.Input;
34
using Avalonia.Input.Raw;
45
using Avalonia.Interactivity;
@@ -376,7 +377,10 @@ protected internal virtual void PointerPressed(object sender, PointerPressedEven
376377
{
377378
if (item.IsSubMenuOpen)
378379
{
379-
if (item.IsTopLevel)
380+
// PointerPressed events may bubble from disabled items in sub-menus. In this case,
381+
// keep the sub-menu open.
382+
var popup = (e.Source as ILogical)?.FindLogicalAncestorOfType<Popup>();
383+
if (item.IsTopLevel && popup == null)
380384
{
381385
CloseMenu(item);
382386
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using Avalonia.Controls.Platform;
3+
using Avalonia.Controls.Primitives;
34
using Avalonia.Input;
45
using Avalonia.Interactivity;
56
using Avalonia.VisualTree;
@@ -540,6 +541,22 @@ public void PointerPressed_On_Item_With_SubMenu_Causes_Opens_Submenu()
540541
Mock.Get(item).Verify(x => x.MoveSelection(NavigationDirection.First, true), Times.Never);
541542
Assert.True(e.Handled);
542543
}
544+
545+
[Fact]
546+
public void PointerPressed_On_Disabled_Item_Doesnt_Close_SubMenu()
547+
{
548+
var target = new DefaultMenuInteractionHandler(false);
549+
var menu = Mock.Of<IMenu>();
550+
var parentItem = Mock.Of<IMenuItem>(x => x.IsTopLevel == true && x.HasSubMenu == true && x.IsSubMenuOpen == true && x.Parent == menu);
551+
var popup = new Popup();
552+
var e = CreatePressed(popup);
553+
554+
((ISetLogicalParent)popup).SetParent(parentItem);
555+
target.PointerPressed(parentItem, e);
556+
557+
Mock.Get(parentItem).Verify(x => x.Close(), Times.Never);
558+
Assert.True(e.Handled);
559+
}
543560
}
544561

545562
public class ContextMenu

0 commit comments

Comments
 (0)