Browse Source

Hack to reset focus when context menu closed.

Including a fix to `FocusManager` to ensure that it doesn't try to set focus to an invisble focus scope. Fixes #3738.
pull/3745/head
Steven Kirk 6 years ago
parent
commit
1cb7d830c7
  1. 5
      src/Avalonia.Controls/ContextMenu.cs
  2. 2
      src/Avalonia.Input/FocusManager.cs

5
src/Avalonia.Controls/ContextMenu.cs

@ -21,6 +21,7 @@ namespace Avalonia.Controls
new FuncTemplate<IPanel>(() => new StackPanel { Orientation = Orientation.Vertical });
private Popup _popup;
private bool _attachedToControl;
private IInputElement _previousFocus;
/// <summary>
/// Initializes a new instance of the <see cref="ContextMenu"/> class.
@ -150,6 +151,7 @@ namespace Avalonia.Controls
private void PopupOpened(object sender, EventArgs e)
{
_previousFocus = FocusManager.Instance?.Current;
Focus();
}
@ -171,6 +173,9 @@ namespace Avalonia.Controls
((ISetLogicalParent)_popup).SetParent(null);
}
// HACK: Reset the focus when the popup is closed. We need to fix this so it's automatic.
FocusManager.Instance?.Focus(_previousFocus);
RaiseEvent(new RoutedEventArgs
{
RoutedEvent = MenuClosedEvent,

2
src/Avalonia.Input/FocusManager.cs

@ -168,7 +168,7 @@ namespace Avalonia.Input
{
var scope = control as IFocusScope;
if (scope != null)
if (scope != null && control.VisualRoot?.IsVisible == true)
{
yield return scope;
}

Loading…
Cancel
Save