From bbff0fe7ab90621043cd6201a453642b473c3b09 Mon Sep 17 00:00:00 2001
From: Jordi Adoumie <98557455+joadoumie@users.noreply.github.com>
Date: Tue, 7 Jan 2025 04:48:39 -0800
Subject: [PATCH] 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.
---
.../Controls/SearchBar.xaml | 2 ++
.../Controls/SearchBar.xaml.cs | 32 +++++++++++++++++++
2 files changed, 34 insertions(+)
diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/SearchBar.xaml b/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/SearchBar.xaml
index 2847d3bdc1..544926082d 100644
--- a/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/SearchBar.xaml
+++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/SearchBar.xaml
@@ -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" />
diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/SearchBar.xaml.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/SearchBar.xaml.cs
index a635ce21dc..752e7747b6 100644
--- a/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/SearchBar.xaml.cs
+++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/SearchBar.xaml.cs
@@ -26,6 +26,7 @@ public sealed partial class SearchBar : UserControl,
/// Gets the that we create to track keyboard input and throttle/debounce before we make queries.
///
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();
+ }
+
+ 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}");