forked from killswitch1111/powerguivsx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchableTextBox.cs
More file actions
154 lines (128 loc) · 5.46 KB
/
Copy pathSearchableTextBox.cs
File metadata and controls
154 lines (128 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
namespace PowerShellTools.Explorer
{
public class SearchableTextBox : TextBox
{
public static readonly DependencyProperty HighlightBackgroundProperty =
DependencyProperty.Register("HighlightBackground", typeof(Brush), typeof(SearchableTextBox),
new UIPropertyMetadata(Brushes.Yellow, UpdateControlCallBack));
public static readonly DependencyProperty HighlightForegroundProperty =
DependencyProperty.Register("HighlightForeground", typeof(Brush), typeof(SearchableTextBox),
new UIPropertyMetadata(Brushes.Black, UpdateControlCallBack));
public static readonly DependencyProperty SearchTextProperty =
DependencyProperty.Register("SearchText", typeof(string), typeof(SearchableTextBox),
new UIPropertyMetadata(string.Empty, UpdateControlCallBack));
public static readonly DependencyProperty SearchResultCountProperty =
DependencyProperty.Register("SearchResultCount", typeof(int), typeof(SearchableTextBox),
new UIPropertyMetadata(0));
static SearchableTextBox()
{
Type ownerType = typeof(SearchableTextBox);
DefaultStyleKeyProperty.OverrideMetadata(ownerType, new FrameworkPropertyMetadata(ownerType));
}
public Brush HighlightBackground
{
get { return (Brush)GetValue(HighlightBackgroundProperty); }
set { SetValue(HighlightBackgroundProperty, value); }
}
public Brush HighlightForeground
{
get { return (Brush)GetValue(HighlightForegroundProperty); }
set { SetValue(HighlightForegroundProperty, value); }
}
public string SearchText
{
get { return (string)GetValue(SearchTextProperty); }
set { SetValue(SearchTextProperty, value); }
}
public int SearchResultCount
{
get { return (int)GetValue(SearchResultCountProperty); }
private set { SetValue(SearchResultCountProperty, value); }
}
public override void OnApplyTemplate()
{
this.TextChanged += OnSearchableTextBoxTextChanged;
base.OnApplyTemplate();
}
private void OnSearchableTextBoxTextChanged(object sender, TextChangedEventArgs e)
{
SearchableTextBox control = sender as SearchableTextBox;
if (control != null)
{
control.InvalidateVisual();
}
}
private static void UpdateControlCallBack(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
SearchableTextBox control = d as SearchableTextBox;
if (control != null)
{
control.InvalidateVisual();
}
}
protected override void OnRender(DrawingContext drawingContext)
{
string searchstring = this.SearchText.ToLowerInvariant();
string compareText = this.Text.ToLowerInvariant();
string displayText = this.Text;
int resultCount = 0;
TextBlock displayTextBlock = this.Template.FindName("PART_TEXT", this) as TextBlock;
if (displayTextBlock == null &&
string.IsNullOrWhiteSpace(this.Text) &&
string.IsNullOrWhiteSpace(this.SearchText))
{
this.SearchResultCount = resultCount;
base.OnRender(drawingContext);
return;
}
displayTextBlock.Inlines.Clear();
Run run = null;
while (!string.IsNullOrEmpty(searchstring) && compareText.IndexOf(searchstring) >= 0)
{
int position = compareText.IndexOf(searchstring);
run = GenerateRun(displayText.Substring(0, position), false);
if (run != null)
{
displayTextBlock.Inlines.Add(run);
}
run = GenerateRun(displayText.Substring(position, searchstring.Length), true);
if (run != null)
{
displayTextBlock.Inlines.Add(run);
resultCount++;
}
compareText = compareText.Substring(position + searchstring.Length);
displayText = displayText.Substring(position + searchstring.Length);
}
run = GenerateRun(displayText, false);
if (run != null)
{
displayTextBlock.Inlines.Add(run);
}
this.SearchResultCount = resultCount;
base.OnRender(drawingContext);
}
private Run GenerateRun(string searchedString, bool isHighlight)
{
if (!string.IsNullOrEmpty(searchedString))
{
Run run = new Run(searchedString)
{
Background = isHighlight ? this.HighlightBackground : this.Background,
Foreground = isHighlight ? this.HighlightForeground : this.Foreground,
};
return run;
}
return null;
}
}
}