Polish launcher keyboard navigation

This commit is contained in:
Ray Berezowski
2026-08-26 21:49:23 -04:00
parent f2ccca2ed1
commit 004a7e7c89
4 changed files with 108 additions and 31 deletions

View File

@@ -164,19 +164,7 @@ public partial class LauncherPopup : Window
button.ContextMenu = CreateItemContextMenu(item);
}
button.KeyDown += (_, e) =>
{
if (e.Key == Key.Enter)
{
launchItem(item);
e.Handled = true;
}
else if (e.Key == Key.Escape)
{
Close();
e.Handled = true;
}
};
button.KeyDown += LaunchButton_KeyDown;
visibleLaunchButtons.Add(button);
return button;
}
@@ -356,19 +344,7 @@ public partial class LauncherPopup : Window
};
button.Click += (_, _) => launchItem(match.Item);
button.KeyDown += (_, e) =>
{
if (e.Key == Key.Enter)
{
launchItem(match.Item);
e.Handled = true;
}
else if (e.Key == Key.Escape)
{
Close();
e.Handled = true;
}
};
button.KeyDown += LaunchButton_KeyDown;
visibleLaunchButtons.Add(button);
return button;
@@ -437,14 +413,112 @@ public partial class LauncherPopup : Window
return;
}
if (e.Key == Key.Down && visibleLaunchButtons.FirstOrDefault() is System.Windows.Controls.Button firstButton)
if (e.Key == Key.Down)
{
Keyboard.ClearFocus();
firstButton.Focus();
FocusLaunchButton(0);
e.Handled = true;
return;
}
if (e.Key == Key.Up)
{
FocusLaunchButton(visibleLaunchButtons.Count - 1);
e.Handled = true;
return;
}
if (e.Key == Key.Home)
{
SearchBox.CaretIndex = 0;
e.Handled = true;
return;
}
if (e.Key == Key.End)
{
SearchBox.CaretIndex = SearchBox.Text.Length;
e.Handled = true;
}
}
private void LaunchButton_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (sender is not System.Windows.Controls.Button button)
{
return;
}
if (e.Key == Key.Enter && button.Tag is LauncherItem item)
{
launchItem(item);
e.Handled = true;
return;
}
if (e.Key == Key.Escape)
{
Close();
e.Handled = true;
return;
}
int index = visibleLaunchButtons.IndexOf(button);
if (index < 0)
{
return;
}
if (e.Key == Key.Down)
{
FocusLaunchButton(index + 1);
e.Handled = true;
return;
}
if (e.Key == Key.Up)
{
if (index == 0)
{
SearchBox.Focus();
SearchBox.CaretIndex = SearchBox.Text.Length;
}
else
{
FocusLaunchButton(index - 1);
}
e.Handled = true;
return;
}
if (e.Key == Key.Home)
{
FocusLaunchButton(0);
e.Handled = true;
return;
}
if (e.Key == Key.End)
{
FocusLaunchButton(visibleLaunchButtons.Count - 1);
e.Handled = true;
}
}
private void FocusLaunchButton(int index)
{
if (visibleLaunchButtons.Count == 0)
{
return;
}
int clampedIndex = Math.Clamp(index, 0, visibleLaunchButtons.Count - 1);
System.Windows.Controls.Button button = visibleLaunchButtons[clampedIndex];
Keyboard.ClearFocus();
button.Focus();
button.BringIntoView();
}
protected override void OnKeyDown(System.Windows.Input.KeyEventArgs e)
{
if (e.Key == Key.Escape)