Skip to content

Commit

Permalink
Merge pull request #72 from AvaloniaUtils/focusFixes
Browse files Browse the repository at this point in the history
Fix popup focusing and focus escaping when no focusable controls in popup
  • Loading branch information
SKProCH authored Nov 17, 2024
2 parents c097656 + 772221b commit 61f336d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
2 changes: 2 additions & 0 deletions DialogHost.Avalonia/DialogOverlayPopupHost.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
TargetType="dialogHost:DialogOverlayPopupHost">
<Setter Property="Background" Value="{DynamicResource ThemeBackgroundBrush}" />
<Setter Property="KeyboardNavigation.TabNavigation" Value="Cycle" />
<Setter Property="Focusable" Value="True" />
<Setter Property="IsTabStop" Value="False" />
<Setter Property="Template">
<ControlTemplate>
<ContentPresenter Name="PART_ContentPresenter"
Expand Down
11 changes: 9 additions & 2 deletions DialogHost.Avalonia/DialogOverlayPopupHost.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,18 @@ private void UpdatePosition()
}

public (bool handled, IInputElement? next) GetNext(IInputElement element, NavigationDirection direction) {
// If current element isn't this popup host - ignoring
if (!element.Equals(this)) {
return (false, null);
}
var focusable = this.GetVisualDescendants().OfType<IInputElement>().FirstOrDefault(visual => visual.Focusable);
return (true, focusable);

// Finding the focusable descendant
var focusable = this.GetVisualDescendants()
.OfType<IInputElement>()
.FirstOrDefault(visual => visual.Focusable);

// Or returning the control itself to prevent focus escaping
return (true, focusable ?? this);
}

/// <inheritdoc />
Expand Down

0 comments on commit 61f336d

Please sign in to comment.