Keyboard focus visual is lost when opening and closing popups (Flyout, ComboBox, TimePicker)
Describe the bug
If you have a control that opens a popup/flyout on click (e.g. a Button with a flyout, ComboBox, TimePicker, ...), and you navigate to that control with tab (giving it an outline) and click space/enter, the content inside of the popup/flyout is sometimes not focused, and when it is, the focus outline is not visible, which is not the expected behavior when using the keyboard to navigate. If you then "Esc" out, the focus is restored on the opening control, but again with NavigationMethod.Unspecified so the focus outline is not visible.
I would be open to work on this but before that I wanted to discuss what we should do.
I already have a draft fix from claude which works but it relies on controls like ComboBox and TimePicker checking whether they have the ":focus-visible" class applied and then passing NavigationMethod.Tab if yes. That doesn't seem like a clean solution though
I think WPF and WinUI both have correct behavior which we could replicate:
(analysis from claude)
WPF stores a single application-wide InputManager.Current.MostRecentInputDevice
On every keyboard focus change, FrameworkElement.OnGotKeyboardFocus calls ShowFocusVisual() That method shows the visual only if AlwaysShowFocusVisual || IsKeyboardMostRecentInputDevice()
WinUI
- Programmatic focus (their equivalent of Unspecified) is turned into Keyboard or Pointer based on the last input device (CFocusManager::CoerceFocusState).
- Popup open: WinUI saves the opener and how it was focused, then focuses the content as Programmatic (Popup.cpp:595-612). So opening with Space shows the visual and opening with a click doesn't.
- Popup close (Popup.cpp:873-882):
To Reproduce
In the video below you can see that when opening a flyout with space doesn't show an outline on the button inside the popup even though that button is focused
Opening the ComboBox with space doesn't focus the flyout it opens (when nothing is selected), pressing tab closes the flyout and navigates to the TimePicker below
Opening the TimePicker with space also doesn't focus the popup/flyout it opens, pressing tab or shift + tab navigates in the background and doesn't even close the time picker
source: PopupFocusVisual.zip or copy this:
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="PopupFocusVisual.MainWindow"
Title="PopupFocusVisual" Width="400" Height="400">
<StackPanel Margin="20" Spacing="10">
<Button Content="Flyout" Click="OnClick">
<Button.Flyout>
<Flyout>
<Button Content="Inside" Click="OnClick" />
</Flyout>
</Button.Flyout>
</Button>
<ComboBox SelectionChanged="OnSelectionChanged">
<ComboBoxItem Content="A" />
<ComboBoxItem Content="B" />
</ComboBox>
<TimePicker />
<TextBlock x:Name="Log" />
</StackPanel>
</Window>public partial class MainWindow : Window
{
public MainWindow() => InitializeComponent();
private void OnClick(object? sender, RoutedEventArgs e) => Write($"Clicked {((Button)sender!).Content}");
private void OnSelectionChanged(object? sender, SelectionChangedEventArgs e) =>
Write($"Selected {((ComboBoxItem?)((ComboBox)sender!).SelectedItem)?.Content}");
private void Write(string line) => Log.Text = line + "\n" + Log.Text;
}Expected behavior
- Opening the dropdown of a combobox should focus the first item if nothing is selected, so you can immediately start navigating with tab between the options
- Opening the timepicker should focus the hour "scroll" so you can immediately start changing the time with the keyboard without having to click inside it
Users who use keyboard navigation need the focus outline to be visible so they know where they are, so:
- If a popup/flyout was opened from a control which was navigated to with tab, then the focused element inside the popup/flyout should also have an outline
- When closing the popup/flyout and returning to the control that opened it, that control should have the focus outline if the focused control inside the popup/flyout had it
Avalonia version
12.1.3
OS
Windows
Additional context
No response
Source: AvaloniaUI/Avalonia