Skip to content

Commit

Permalink
Don't override Placement when Open called in code. (#13967)
Browse files Browse the repository at this point in the history
#6059 changed `ContextMenu.Open` to open the context menu at the bottom of the control when the `ContextRequestedEventArgs` doesn't contain a position, however it also had the side-effect of preventing the `Placement` property from being respected when opening a `ContextMenu` from code.

Change the private `Open` method to accept a `PlacementMode` instead of a boolean flag, and only pass `Bottom` here when `ContextRequestedEventArgs` has no requested position.

Fixes #12504
  • Loading branch information
grokys authored and maxkatz6 committed Jan 17, 2024
1 parent da28a6e commit a478b9e
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/Avalonia.Controls/ContextMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ public void Open(Control? control)
}

control ??= _attachedControls![0];
Open(control, PlacementTarget ?? control, false);
Open(control, PlacementTarget ?? control, Placement);
}

/// <summary>
Expand Down Expand Up @@ -308,7 +308,7 @@ void ISetterValue.Initialize(SetterBase setter)
remove => _popupHostChangedHandler -= value;
}

private void Open(Control control, Control placementTarget, bool requestedByPointer)
private void Open(Control control, Control placementTarget, PlacementMode placement)
{
if (IsOpen)
{
Expand All @@ -335,9 +335,7 @@ private void Open(Control control, Control placementTarget, bool requestedByPoin
((ISetLogicalParent)_popup).SetParent(control);
}

_popup.Placement = !requestedByPointer && Placement == PlacementMode.Pointer
? PlacementMode.Bottom
: Placement;
_popup.Placement = placement;

//Position of the line below is really important.
//All styles are being applied only when control has logical parent.
Expand Down Expand Up @@ -425,7 +423,10 @@ private static void ControlContextRequested(object? sender, ContextRequestedEven
&& !contextMenu.CancelOpening())
{
var requestedByPointer = e.TryGetPosition(null, out _);
contextMenu.Open(control, e.Source as Control ?? control, requestedByPointer);
contextMenu.Open(
control,
e.Source as Control ?? control,
requestedByPointer ? contextMenu.Placement : PlacementMode.Bottom);
e.Handled = true;
}
}
Expand Down

0 comments on commit a478b9e

Please sign in to comment.