Skip to content

Commit 812ac2a

Browse files
committed
refactor: convert searchbox into usercontrol
1 parent 5ff0819 commit 812ac2a

File tree

5 files changed

+253
-257
lines changed

5 files changed

+253
-257
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<UserControl
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6+
xmlns:local="clr-namespace:UnityLauncherPro"
7+
mc:Ignorable="d" x:Class="UnityLauncherPro.Controls.SearchBoxControl"
8+
Height="20" Width="222" Background="{DynamicResource ThemeTextBoxBackground}">
9+
<Grid>
10+
<!-- Placeholder text -->
11+
<TextBlock Margin="3,2" MinWidth="100" Text="Search" Foreground="{DynamicResource ThemeSearchPlaceholder}" Height="24">
12+
<TextBlock.Style>
13+
<Style TargetType="TextBlock">
14+
<Setter Property="Visibility" Value="Collapsed" />
15+
<Style.Triggers>
16+
<DataTrigger Binding="{Binding Text, ElementName=txtSearchBox}" Value="">
17+
<Setter Property="Visibility" Value="Visible" />
18+
</DataTrigger>
19+
</Style.Triggers>
20+
</Style>
21+
</TextBlock.Style>
22+
</TextBlock>
23+
24+
<!-- Search TextBox -->
25+
<TextBox MinWidth="100" CaretBrush="{DynamicResource ThemeSearchCaret}" x:Name="txtSearchBox" Background="Transparent" BorderBrush="{x:Null}" Foreground="{DynamicResource ThemeSearchForeground}" SelectionBrush="{DynamicResource ThemeSearchSelection}" BorderThickness="0" Margin="2,2,0,0" UndoLimit="64" PreviewKeyDown="TxtSearchBox_PreviewKeyDown" TextChanged="TxtSearchBox_TextChanged" />
26+
27+
<!-- Clear button -->
28+
<Button x:Name="btnClearSearch" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" BorderThickness="0" HorizontalAlignment="Right" VerticalAlignment="Top" Height="23" Width="23" Background="Transparent" Padding="0,2" BorderBrush="{x:Null}" Click="OnClearSearchClick" IsTabStop="False">
29+
<TextBlock Text="" FontSize="8" Foreground="{DynamicResource ThemeSearchClose}" Padding="5,3,4,4" HorizontalAlignment="Center">
30+
<TextBlock.Style>
31+
<Style TargetType="{x:Type TextBlock}">
32+
<Style.Triggers>
33+
<DataTrigger Binding="{Binding Text, ElementName=txtSearchBox}" Value="">
34+
<Setter Property="Visibility" Value="Hidden"/>
35+
</DataTrigger>
36+
<DataTrigger Binding="{Binding Text, ElementName=txtSearchBox}" Value="{x:Null}">
37+
<Setter Property="Visibility" Value="Hidden"/>
38+
</DataTrigger>
39+
</Style.Triggers>
40+
</Style>
41+
</TextBlock.Style>
42+
</TextBlock>
43+
</Button>
44+
</Grid>
45+
</UserControl>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System.Windows;
2+
using System.Windows.Controls;
3+
using System.Windows.Input;
4+
5+
namespace UnityLauncherPro.Controls
6+
{
7+
public partial class SearchBoxControl : UserControl
8+
{
9+
public event TextChangedEventHandler SearchTextChanged;
10+
public event RoutedEventHandler SearchCleared;
11+
12+
public event KeyEventHandler SearchKeyDown;
13+
14+
public SearchBoxControl()
15+
{
16+
InitializeComponent();
17+
}
18+
19+
public string SearchText
20+
{
21+
get { return txtSearchBox.Text; }
22+
set { txtSearchBox.Text = value; }
23+
}
24+
25+
public new void Focus()
26+
{
27+
txtSearchBox.Focus();
28+
txtSearchBox.Select(txtSearchBox.Text.Length, 0);
29+
}
30+
31+
public void Clear()
32+
{
33+
txtSearchBox.Text = "";
34+
}
35+
36+
private void TxtSearchBox_PreviewKeyDown(object sender, KeyEventArgs e)
37+
{
38+
SearchKeyDown?.Invoke(this, e);
39+
}
40+
41+
private void TxtSearchBox_TextChanged(object sender, TextChangedEventArgs e)
42+
{
43+
SearchTextChanged?.Invoke(this, e);
44+
}
45+
46+
private void OnClearSearchClick(object sender, RoutedEventArgs e)
47+
{
48+
Clear();
49+
SearchCleared?.Invoke(this, e);
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)