Compare commits

...

4 Commits

Author SHA1 Message Date
Jessica Dene Earley-Cha
63c4cb5916 add checks that the keydownevent for DeleteButton_KeyDown is triggered from DeleteButton 2025-08-12 13:33:21 -07:00
Jessica Dene Earley-Cha
b0e3143682 change button to content and used Uid to have screenreader annouce button 2025-08-11 13:32:38 -07:00
Jessica Dene Earley-Cha
5a70d392a4 clear search button and enter clears search box and moves focus back to search bar 2025-08-11 11:16:10 -07:00
Jessica Dene Earley-Cha
a629a2fff3 deletebutton appears and disapears based on input text and is tabable 2025-08-07 14:31:41 -07:00
4 changed files with 55 additions and 3 deletions

View File

@@ -8,6 +8,7 @@
xmlns:cpcontrols="using:Microsoft.CmdPal.UI.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
x:Name="RootSearchBar"
mc:Ignorable="d">
<UserControl.Resources>

View File

@@ -2,6 +2,8 @@
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Windows.Input;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using CommunityToolkit.WinUI;
using Microsoft.CmdPal.Core.ViewModels;
@@ -36,6 +38,9 @@ public sealed partial class SearchBar : UserControl,
private string? _lastText;
private string? _deletedSuggestion;
// An ICommand for clearing the search box, allowing the DeleteButton to invoke this logic via MVVM command binding.
public ICommand ClearSearchCommand { get; }
public PageViewModel? CurrentPageViewModel
{
get => (PageViewModel?)GetValue(CurrentPageViewModelProperty);
@@ -75,6 +80,11 @@ public sealed partial class SearchBar : UserControl,
WeakReferenceMessenger.Default.Register<GoHomeMessage>(this);
WeakReferenceMessenger.Default.Register<FocusSearchBoxMessage>(this);
WeakReferenceMessenger.Default.Register<UpdateSuggestionMessage>(this);
// Attach a keydown event handler Clear Button within the FilterBox to trigger search clearing logic.
FilterBox.AddHandler(Button.KeyDownEvent, new KeyEventHandler(DeleteButton_KeyDown), true);
ClearSearchCommand = new RelayCommand(() => ExecuteDeleteButtonAction());
}
public void ClearSearch()
@@ -92,6 +102,36 @@ public sealed partial class SearchBar : UserControl,
}));
}
private Button? GetDeleteButton()
{
// Try to find the DeleteButton in the FilterBox's template
return FilterBox?.FindName("DeleteButton") as Button;
}
private bool IsDeleteButtonFocused()
{
var deleteButton = GetDeleteButton();
var focusedElement = FocusManager.GetFocusedElement() as Button;
return deleteButton != null && focusedElement == deleteButton;
}
public void DeleteButton_KeyDown(object sender, KeyRoutedEventArgs e)
{
if (IsDeleteButtonFocused())
{
if (e.Key == VirtualKey.Enter)
{
ExecuteDeleteButtonAction();
}
}
}
private void ExecuteDeleteButtonAction()
{
ClearSearch();
FilterBox.Focus(Microsoft.UI.Xaml.FocusState.Programmatic);
}
public void SelectSearch()
{
// TODO GH #239 switch back when using the new MD text block

View File

@@ -434,4 +434,7 @@ Right-click to remove the key combination, thereby deactivating the shortcut.</v
<data name="StatusMessagesButton.[using:Microsoft.UI.Xaml.Controls]ToolTipService.ToolTip" xml:space="preserve">
<value>Show status messages</value>
</data>
<data name="DeleteButton.AutomationProperties.Name" xml:space="preserve">
<value>Clears the search box</value>
</data>
</root>

View File

@@ -14,6 +14,11 @@
EmptyValue="Visible"
NotEmptyValue="Collapsed" />
<converters:StringVisibilityConverter
x:Key="StringVisibilityConverter"
EmptyValue="Collapsed"
NotEmptyValue="Visible" />
<Style x:Key="SearchTextBoxStyle" TargetType="TextBox">
<Setter Property="Foreground" Value="{ThemeResource TextControlForeground}" />
<Setter Property="Background" Value="Transparent" />
@@ -166,18 +171,21 @@
Visibility="{Binding Description, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource ReverseStringVisibilityConverter}, Mode=OneWay}" />
<Button
x:Name="DeleteButton"
x:Uid="DeleteButton"
Grid.Column="2"
Margin="0,0,8,0"
Padding="4"
HorizontalAlignment="Right"
VerticalAlignment="Center"
AutomationProperties.AccessibilityView="Raw"
AutomationProperties.AccessibilityView="Content"
BorderThickness="{TemplateBinding BorderThickness}"
Command="{Binding ClearSearchCommand, ElementName=RootSearchBar}"
CornerRadius="{TemplateBinding CornerRadius}"
FontSize="{TemplateBinding FontSize}"
IsTabStop="False"
IsTabStop="True"
KeyDown="DeleteButton_KeyDown"
Style="{StaticResource DeleteButtonStyle}"
Visibility="Collapsed" />
Visibility="{Binding Text, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource StringVisibilityConverter}, Mode=OneWay}" />
<TextBlock
x:Name="DescriptionPresenter"
Grid.Column="1"