Browse Source
Set IsKeyboardFocusWithin to false when control is detached from visual tree (#19369)
* add failing IsKeyboardFocusWithin test for popup
* Set IsKeyboardWithin to false when detached from visual tree
pull/19383/head
Emmanuel Hansen
1 year ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with
47 additions and
0 deletions
-
src/Avalonia.Base/Input/InputElement.cs
-
tests/Avalonia.Controls.UnitTests/Primitives/PopupTests.cs
|
|
|
@ -566,6 +566,8 @@ namespace Avalonia.Input |
|
|
|
{ |
|
|
|
FocusManager.GetFocusManager(this)?.ClearFocusOnElementRemoved(this, e.Parent); |
|
|
|
} |
|
|
|
|
|
|
|
IsKeyboardFocusWithin = false; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
@ -669,6 +669,51 @@ namespace Avalonia.Controls.UnitTests.Primitives |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Popup_Should_Clear_Keyboard_Focus_From_Children_When_Closed() |
|
|
|
{ |
|
|
|
using (CreateServicesWithFocus()) |
|
|
|
{ |
|
|
|
var winButton = new Button(); |
|
|
|
var window = PreparedWindow(new Panel { Children = { winButton }}); |
|
|
|
|
|
|
|
var border1 = new Border(); |
|
|
|
var border2 = new Border(); |
|
|
|
var button = new Button(); |
|
|
|
border1.Child = border2; |
|
|
|
border2.Child = button; |
|
|
|
var popup = new Popup |
|
|
|
{ |
|
|
|
PlacementTarget = window, |
|
|
|
Child = new StackPanel |
|
|
|
{ |
|
|
|
Children = |
|
|
|
{ |
|
|
|
border1 |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
((ISetLogicalParent)popup).SetParent(popup.PlacementTarget); |
|
|
|
window.Show(); |
|
|
|
winButton.Focus(); |
|
|
|
popup.Open(); |
|
|
|
|
|
|
|
button.Focus(); |
|
|
|
|
|
|
|
var inputRoot = Assert.IsAssignableFrom<IInputRoot>(popup.Host); |
|
|
|
|
|
|
|
var focusManager = inputRoot.FocusManager!; |
|
|
|
Assert.Same(button, focusManager.GetFocusedElement()); |
|
|
|
|
|
|
|
border1.Child = null; |
|
|
|
|
|
|
|
winButton.Focus(); |
|
|
|
|
|
|
|
Assert.False(border2.IsKeyboardFocusWithin); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Closing_Popup_Sets_Focus_On_PlacementTarget() |
|
|
|
{ |
|
|
|
|