Skip to content

Commit fa9f223

Browse files
Fixed focus not being cleared when a parent of the focused element is hidden (#18916)
Enabled the relevant test
1 parent c114507 commit fa9f223

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/Avalonia.Base/Input/InputElement.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -720,9 +720,19 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
720720
{
721721
PseudoClasses.Set(":focus-within", change.GetNewValue<bool>());
722722
}
723-
else if (change.Property == IsVisibleProperty && !change.GetNewValue<bool>() && IsFocused)
723+
else if (change.Property == IsVisibleProperty)
724724
{
725-
FocusManager.GetFocusManager(this)?.ClearFocus();
725+
if (!change.GetNewValue<bool>() && IsKeyboardFocusWithin && FocusManager.GetFocusManager(this) is { } focusManager)
726+
{
727+
if (focusManager.GetFocusedElement() is { } focusedElement && VisualParent != null)
728+
{
729+
focusManager.ClearFocusOnElementRemoved(focusedElement, VisualParent);
730+
}
731+
else
732+
{
733+
focusManager.ClearFocus();
734+
}
735+
}
726736
}
727737
}
728738

tests/Avalonia.Base.UnitTests/Input/InputElement_Focus.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public void Focus_Should_Be_Cleared_When_Control_Is_Hidden()
203203
}
204204
}
205205

206-
[Fact(Skip = "Need to implement IsEffectivelyVisible change notifications.")]
206+
[Fact]
207207
public void Focus_Should_Be_Cleared_When_Control_Is_Effectively_Hidden()
208208
{
209209
Border container;

0 commit comments

Comments
 (0)