mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-29 16:36:40 +01:00
Compare commits
4 Commits
jay/ls-tel
...
search-cle
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
63c4cb5916 | ||
|
|
b0e3143682 | ||
|
|
5a70d392a4 | ||
|
|
a629a2fff3 |
@@ -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>
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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>
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user