Backspace on empty query in FilterBox navigates back (#260)

This is a feature request @plante-msft has had for a while, but I don't see an open issue for it.

Does what title says, navigates backwards when a user types `Backspace` and the filterbox is already empty.

The functionality works as follows: 

1. If a user presses down on the backspace key with an empty query and holds the backspace key, cmdpal will not navigate back until the key is released 

2. If a user presses down on the backspace key with a non-empty query and holds the backspace key, cmdpal will not navigate back even after the key is released. This is to ensure that folks can hold backspace and delete the entire text field without inadvertently getting sent backwards in the flow. In this case, they would have to explicitly hit backspace again. 

TLDR - hitting backspace will only ever navigate back on release AND if backspace was originally hit with an empty query.
This commit is contained in:
Jordi Adoumie
2025-01-07 04:48:39 -08:00
committed by GitHub
parent 0a22219080
commit bbff0fe7ab
2 changed files with 34 additions and 0 deletions

View File

@@ -16,6 +16,8 @@
VerticalContentAlignment="Stretch"
KeyDown="FilterBox_KeyDown"
PlaceholderText="Type here to search..."
PreviewKeyUp="FilterBox_PreviewKeyUp"
PreviewKeyDown="FilterBox_PreviewKeyDown"
Style="{StaticResource SearchTextBoxStyle}"
TextChanged="FilterBox_TextChanged" />

View File

@@ -26,6 +26,7 @@ public sealed partial class SearchBar : UserControl,
/// Gets the <see cref="DispatcherQueueTimer"/> that we create to track keyboard input and throttle/debounce before we make queries.
/// </summary>
private readonly DispatcherQueueTimer _debounceTimer = DispatcherQueue.GetForCurrentThread().CreateTimer();
private bool _isBackspaceHeld;
public PageViewModel? CurrentPageViewModel
{
@@ -129,6 +130,37 @@ public sealed partial class SearchBar : UserControl,
}
}
private void FilterBox_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == VirtualKey.Back)
{
if (string.IsNullOrEmpty(FilterBox.Text))
{
if (!_isBackspaceHeld)
{
// Navigate back on single backspace when empty
WeakReferenceMessenger.Default.Send<NavigateBackMessage>();
}
e.Handled = true;
}
else
{
// Mark backspace as held to handle continuous deletion
_isBackspaceHeld = true;
}
}
}
private void FilterBox_PreviewKeyUp(object sender, KeyRoutedEventArgs e)
{
if (e.Key == VirtualKey.Back)
{
// Reset the backspace state on key release
_isBackspaceHeld = false;
}
}
private void FilterBox_TextChanged(object sender, TextChangedEventArgs e)
{
Debug.WriteLine($"FilterBox_TextChanged: {FilterBox.Text}");