Browse Source

Fix leaks in attached context menu,

pull/3745/head
Steven Kirk 7 years ago
parent
commit
c443336928
  1. 21
      src/Avalonia.Controls/ContextMenu.cs

21
src/Avalonia.Controls/ContextMenu.cs

@ -20,7 +20,7 @@ namespace Avalonia.Controls
private static readonly ITemplate<IPanel> DefaultPanel = private static readonly ITemplate<IPanel> DefaultPanel =
new FuncTemplate<IPanel>(() => new StackPanel { Orientation = Orientation.Vertical }); new FuncTemplate<IPanel>(() => new StackPanel { Orientation = Orientation.Vertical });
private Popup _popup; private Popup _popup;
private bool _attachedToControl; private Control _attachedControl;
private IInputElement _previousFocus; private IInputElement _previousFocus;
/// <summary> /// <summary>
@ -74,12 +74,13 @@ namespace Avalonia.Controls
if (e.OldValue is ContextMenu oldMenu) if (e.OldValue is ContextMenu oldMenu)
{ {
control.PointerReleased -= ControlPointerReleased; control.PointerReleased -= ControlPointerReleased;
oldMenu._attachedToControl = false; oldMenu._attachedControl = null;
((ISetLogicalParent)oldMenu._popup).SetParent(null);
} }
if (e.NewValue is ContextMenu newMenu) if (e.NewValue is ContextMenu newMenu)
{ {
newMenu._attachedToControl = true; newMenu._attachedControl = control;
control.PointerReleased += ControlPointerReleased; control.PointerReleased += ControlPointerReleased;
} }
} }
@ -95,8 +96,18 @@ namespace Avalonia.Controls
/// <param name="control">The control.</param> /// <param name="control">The control.</param>
public void Open(Control control) public void Open(Control control)
{ {
if (control == null) if (control is null && _attachedControl is null)
{
throw new ArgumentNullException(nameof(control)); throw new ArgumentNullException(nameof(control));
}
if (control is object && _attachedControl is object && control != _attachedControl)
{
throw new ArgumentException(
"Cannot show ContentMenu on a different control to the one it is attached to.",
nameof(control));
}
if (IsOpen) if (IsOpen)
{ {
return; return;
@ -168,7 +179,7 @@ namespace Avalonia.Controls
SelectedIndex = -1; SelectedIndex = -1;
IsOpen = false; IsOpen = false;
if (!_attachedToControl) if (_attachedControl is null)
{ {
((ISetLogicalParent)_popup).SetParent(null); ((ISetLogicalParent)_popup).SetParent(null);
} }

Loading…
Cancel
Save