Skip to content

Portable Tool for an Ideal Windows Setup

upd. textbox style with native clear behavior

Greedeks7817ecf4b5d2
diff --git a/.Source/GTweak/Core/ViewModel/ToolsetViewModel.cs b/.Source/GTweak/Core/ViewModel/ToolsetViewModel.cs
index dc54b70d..d4d76585 100644
--- a/.Source/GTweak/Core/ViewModel/ToolsetViewModel.cs
+++ b/.Source/GTweak/Core/ViewModel/ToolsetViewModel.cs
@@ -25,7 +25,6 @@ namespace GTweak.Core.ViewModel
public ICollectionView ToolsView { get; }
public ICommand SelectFolderCommand { get; }
public ICommand OpenFolderCommand { get; }
- public ICommand ClearCommand { get; }
public string SearchText
{
@@ -47,7 +46,6 @@ namespace GTweak.Core.ViewModel
{
SelectFolderCommand = new RelayCommand(_ => SelectFolder());
OpenFolderCommand = new RelayCommand(_ => OpenFolder());
- ClearCommand = new RelayCommand(_ => SearchText = string.Empty);
ToolsView = CollectionViewSource.GetDefaultView(Tools);
ToolsView.Filter = FilterTools;
diff --git a/.Source/GTweak/GTweak.csproj b/.Source/GTweak/GTweak.csproj
index 39924e5a..c4ca3dd4 100644
--- a/.Source/GTweak/GTweak.csproj
+++ b/.Source/GTweak/GTweak.csproj
@@ -197,6 +197,7 @@
<Compile Include="Core\ViewModel\ToolsetViewModel.cs" />
<Compile Include="Utilities\Animation\BlurAnimation.cs" />
<Compile Include="Utilities\Behaviors\ScrollFadeBehavior.cs" />
+ <Compile Include="Utilities\Behaviors\TextBoxClearBehavior.cs" />
<Compile Include="Utilities\Configuration\IconExtractProvider.cs" />
<Compile Include="Utilities\Configuration\NetworkProvider.cs" />
<Compile Include="Utilities\Configuration\StorageTypeIdentifier.cs" />
diff --git a/.Source/GTweak/Styles/Inputs/TextBox.xaml b/.Source/GTweak/Styles/Inputs/TextBox.xaml
index 3212ae1f..297230fc 100644
--- a/.Source/GTweak/Styles/Inputs/TextBox.xaml
+++ b/.Source/GTweak/Styles/Inputs/TextBox.xaml
@@ -30,23 +30,20 @@
<Border Grid.ColumnSpan="2" x:Name="border" Background="{StaticResource Brush_Background_TextBox}" BorderBrush="{StaticResource Brush_Accent}" BorderThickness="0,0,0,1" CornerRadius="5"/>
<ScrollViewer Grid.Column="0" x:Name="PART_ContentHost" Margin="5,0,0,0" VerticalAlignment="Center" />
<Label Grid.Column="0" Margin="5,0,5,0" x:Name="WaterMarkLabel" Content="{TemplateBinding Tag}" VerticalAlignment="Center" Visibility="Collapsed" Foreground="{StaticResource Brush_Inactive}" FontFamily="{StaticResource Jura}"/>
- <Button Grid.Column="1" x:Name="PART_ClearButton" Command="{Binding DataContext.ClearCommand, RelativeSource={RelativeSource AncestorType=UserControl}}" Style="{StaticResource ButtonClearTextBox}" Width="20" Height="20" Margin="0,0,5,0" Visibility="Visible"/>
+ <Button Grid.Column="1" x:Name="PART_ClearButton" Style="{StaticResource ButtonClearTextBox}" Width="20" Height="20" Margin="0,0,5,0" Visibility="Collapsed"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="Text" Value="">
<Setter Property="Visibility" TargetName="WaterMarkLabel" Value="Visible"/>
- <Setter Property="Visibility" TargetName="PART_ClearButton" Value="Collapsed"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="{StaticResource Brush_Highlighted}"/>
<Setter Property="BorderBrush" Value="{StaticResource Brush_TextBox_Border}" TargetName="border"/>
</Trigger>
<Trigger Property="IsFocused" Value="False">
- <Setter Property="Text" Value=""/>
<Setter Property="BorderBrush" Value="{StaticResource Brush_TextBox_Border}" TargetName="border"/>
</Trigger>
<Trigger Property="IsKeyboardFocused" Value="False">
- <Setter Property="Text" Value=""/>
<Setter Property="BorderBrush" Value="{StaticResource Brush_TextBox_Border}" TargetName="border"/>
</Trigger>
</ControlTemplate.Triggers>
@@ -54,4 +51,5 @@
</Setter.Value>
</Setter>
</Style>
+
</ResourceDictionary>
\ No newline at end of file
diff --git a/.Source/GTweak/Utilities/Behaviors/TextBoxClearBehavior.cs b/.Source/GTweak/Utilities/Behaviors/TextBoxClearBehavior.cs
new file mode 100644
index 00000000..14fe84de
--- /dev/null
+++ b/.Source/GTweak/Utilities/Behaviors/TextBoxClearBehavior.cs
@@ -0,0 +1,68 @@
+using System.Windows;
+using System.Windows.Controls;
+
+namespace GTweak.Utilities.Behaviors
+{
+ internal class TextBoxClearBehavior
+ {
+ public static readonly DependencyProperty ClearButtonProperty =
+ DependencyProperty.RegisterAttached("ClearButton", typeof(bool), typeof(TextBoxClearBehavior), new PropertyMetadata(false, OnClearButtonChanged));
+
+ public static void SetClearButton(UIElement element, bool value) => element.SetValue(ClearButtonProperty, value);
+ public static bool GetClearButton(UIElement element) => (bool)element.GetValue(ClearButtonProperty);
+
+ private static void OnClearButtonChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
+ {
+ if (d is TextBox textBox)
+ {
+ textBox.Loaded -= TextBox_Loaded;
+ textBox.TextChanged -= TextBox_TextChanged;
+
+ if (e.NewValue is bool isEnabled && isEnabled)
+ {
+ textBox.Loaded += TextBox_Loaded;
+ textBox.TextChanged += TextBox_TextChanged;
+ }
+ }
+ }
+
+ private static void TextBox_Loaded(object sender, RoutedEventArgs e)
+ {
+ if (sender is TextBox textBox)
+ {
+ UpdateVisibility(textBox);
+
+ if (textBox.Template?.FindName("PART_ClearButton", textBox) is Button clearButton)
+ {
+ clearButton.Click -= ClearButton_Click;
+ clearButton.Click += ClearButton_Click;
+ }
+ }
+ }
+
+ private static void TextBox_TextChanged(object sender, TextChangedEventArgs e)
+ {
+ if (sender is TextBox textBox)
+ {
+ UpdateVisibility(textBox);
+ }
+ }
+
+ private static void ClearButton_Click(object sender, RoutedEventArgs e)
+ {
+ if (sender is Button clearButton && clearButton.TemplatedParent is TextBox textBox)
+ {
+ textBox.Clear();
+ textBox.Focus();
+ }
+ }
+
+ private static void UpdateVisibility(TextBox textBox)
+ {
+ if (textBox.Template?.FindName("PART_ClearButton", textBox) is Button clearButton)
+ {
+ clearButton.Visibility = string.IsNullOrEmpty(textBox.Text) ? Visibility.Collapsed : Visibility.Visible;
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/.Source/GTweak/View/SystemView.xaml b/.Source/GTweak/View/SystemView.xaml
index 3ff933dd..f7e58d31 100644
--- a/.Source/GTweak/View/SystemView.xaml
+++ b/.Source/GTweak/View/SystemView.xaml
@@ -45,8 +45,8 @@
<TextBlock Style="{StaticResource TextDefault}" Margin="0,2,0,0" Text="{DynamicResource slider3_sys}"/>
<Slider x:Name="Slider3" Value="{Binding [Slider3].Value}" Style="{StaticResource Numeric_Slider}" IsSnapToTickEnabled="True" Minimum="0" Maximum="31" Margin="7,15,0,0" Width="170" ValueChanged="Slider_ValueChanged"/>
</StackPanel>
-
- <TextBox Margin="0,5,0,0" Tag="{DynamicResource text_sys}" Width="450" MaxLength="50" HorizontalAlignment="Left"/>
+
+ <TextBox Tag="{DynamicResource text_sys}" b:TextBoxClearBehavior.ClearButton="True" Margin="0,5,0,0" Width="450" MaxLength="50" HorizontalAlignment="Left"/>
<uc:ToggleButton x:Name="TglButton1" Style="{StaticResource ToggleStyle}" State="{Binding [TglButton1].State}" Margin="0,20,0,0" TextResource="{DynamicResource tgl1_sys}" Description="{DynamicResource tgl1_desc_sys}" />
<uc:ToggleButton x:Name="TglButton2" Style="{StaticResource ToggleStyle}" State="{Binding [TglButton2].State}" Margin="0,20,0,0" TextResource="{DynamicResource tgl2_sys}" Description="{DynamicResource tgl2_desc_sys}" />
diff --git a/.Source/GTweak/View/ToolsetView.xaml b/.Source/GTweak/View/ToolsetView.xaml
index 8841c3fe..ec6fdb15 100644
--- a/.Source/GTweak/View/ToolsetView.xaml
+++ b/.Source/GTweak/View/ToolsetView.xaml
@@ -21,7 +21,7 @@
<uc:FlowPanel Grid.Row="0" HorizontalAlignment="Left" Margin="10,5,10,0" ContentAlignment="Left" Orientation="Horizontal" HorizontalSpacing="10" VerticalSpacing="5">
- <TextBox Text="{Binding SearchText, UpdateSourceTrigger=PropertyChanged, Delay=250}" Width="300" MaxLength="55" HorizontalAlignment="Left" Height="29">
+ <TextBox Text="{Binding SearchText, UpdateSourceTrigger=PropertyChanged, Delay=250}" b:TextBoxClearBehavior.ClearButton="True" Width="300" MaxLength="55" HorizontalAlignment="Left" Height="29">
<TextBox.Tag>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
<ui:SymbolIcon Symbol="Search20" FontSize="14" Margin="0,0,6,0" VerticalAlignment="Center"/>