Browse Source

Merge pull request #4809 from amwx/MorePopupFocusFixes

More Popup focus fixes
pull/4834/head
Steven Kirk 6 years ago
committed by GitHub
parent
commit
c09cab10ea
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 22
      src/Avalonia.Controls/Primitives/Popup.cs
  2. 79
      tests/Avalonia.Controls.UnitTests/Primitives/PopupTests.cs

22
src/Avalonia.Controls/Primitives/Popup.cs

@ -358,7 +358,7 @@ namespace Avalonia.Controls.Primitives
return; return;
} }
var placementTarget = PlacementTarget ?? this.GetLogicalAncestors().OfType<IVisual>().FirstOrDefault(); var placementTarget = PlacementTarget ?? this.FindLogicalAncestorOfType<IControl>();
if (placementTarget == null) if (placementTarget == null)
{ {
@ -586,6 +586,26 @@ namespace Avalonia.Controls.Primitives
} }
Closed?.Invoke(this, EventArgs.Empty); Closed?.Invoke(this, EventArgs.Empty);
var focusCheck = FocusManager.Instance?.Current;
// Focus is set to null as part of popup closing, so we only want to
// set focus to PlacementTarget if this is the case
if (focusCheck == null)
{
if (PlacementTarget != null)
{
FocusManager.Instance?.Focus(PlacementTarget);
}
else
{
var anc = this.FindLogicalAncestorOfType<IControl>();
if (anc != null)
{
FocusManager.Instance?.Focus(anc);
}
}
}
} }
private void ListenForNonClientClick(RawInputEventArgs e) private void ListenForNonClientClick(RawInputEventArgs e)

79
tests/Avalonia.Controls.UnitTests/Primitives/PopupTests.cs

@ -485,6 +485,85 @@ namespace Avalonia.Controls.UnitTests.Primitives
} }
} }
[Fact]
public void Closing_Popup_Sets_Focus_On_PlacementTarget()
{
using (CreateServicesWithFocus())
{
var window = PreparedWindow();
var tb = new TextBox();
var p = new Popup
{
PlacementTarget = window,
Child = tb
};
((ISetLogicalParent)p).SetParent(p.PlacementTarget);
window.Show();
p.Open();
if (p.Host is OverlayPopupHost host)
{
//Need to measure/arrange for visual children to show up
//in OverlayPopupHost
host.Measure(Size.Infinity);
host.Arrange(new Rect(host.DesiredSize));
}
tb.Focus();
p.Close();
var focus = FocusManager.Instance?.Current;
Assert.True(focus == window);
}
}
[Fact]
public void Prog_Close_Popup_NoLightDismiss_Doesnt_Move_Focus_To_PlacementTarget()
{
using (CreateServicesWithFocus())
{
var window = PreparedWindow();
var windowTB = new TextBox();
window.Content = windowTB;
var popupTB = new TextBox();
var p = new Popup
{
PlacementTarget = window,
IsLightDismissEnabled = false,
Child = popupTB
};
((ISetLogicalParent)p).SetParent(p.PlacementTarget);
window.Show();
p.Open();
if (p.Host is OverlayPopupHost host)
{
//Need to measure/arrange for visual children to show up
//in OverlayPopupHost
host.Measure(Size.Infinity);
host.Arrange(new Rect(host.DesiredSize));
}
popupTB.Focus();
windowTB.Focus();
var focus = FocusManager.Instance?.Current;
Assert.True(focus == windowTB);
p.Close();
Assert.True(focus == windowTB);
}
}
private IDisposable CreateServices() private IDisposable CreateServices()
{ {
return UnitTestApplication.Start(TestServices.StyledWindow.With(windowingPlatform: return UnitTestApplication.Start(TestServices.StyledWindow.With(windowingPlatform:

Loading…
Cancel
Save