-
-
Notifications
You must be signed in to change notification settings - Fork 464
/
FindAndReplaceControl.xaml.cs
144 lines (123 loc) · 5.01 KB
/
FindAndReplaceControl.xaml.cs
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
namespace Notepads.Controls.FindAndReplace
{
using System;
using Notepads.Services;
using Windows.System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
public sealed partial class FindAndReplaceControl : UserControl
{
public event EventHandler<RoutedEventArgs> OnDismissKeyDown;
public event EventHandler<FindAndReplaceEventArgs> OnFindAndReplaceButtonClicked;
public FindAndReplaceControl()
{
InitializeComponent();
SetSelectionHighlightColor();
ThemeSettingsService.OnAccentColorChanged += (sender, color) =>
{
SetSelectionHighlightColor();
};
Loaded += (sender, args) => { Focus(FindAndReplaceMode.FindOnly); };
}
public double GetHeight(bool showReplaceBar)
{
if (showReplaceBar)
{
return FindAndReplaceRootGrid.Height + ReplaceBarPlaceHolder.Height;
}
else
{
return FindAndReplaceRootGrid.Height;
}
}
private void SetSelectionHighlightColor()
{
FindBar.SelectionHighlightColor =
Application.Current.Resources["SystemControlForegroundAccentBrush"] as SolidColorBrush;
FindBar.SelectionHighlightColorWhenNotFocused =
Application.Current.Resources["SystemControlForegroundAccentBrush"] as SolidColorBrush;
FindBar.SelectionHighlightColor =
Application.Current.Resources["SystemControlForegroundAccentBrush"] as SolidColorBrush;
FindBar.SelectionHighlightColorWhenNotFocused =
Application.Current.Resources["SystemControlForegroundAccentBrush"] as SolidColorBrush;
}
public void Focus(FindAndReplaceMode mode)
{
if (mode == FindAndReplaceMode.FindOnly)
{
if (!string.IsNullOrEmpty(FindBar.Text))
{
FindBar.SelectionStart = 0;
FindBar.SelectionLength = FindBar.Text.Length;
}
FindBar.Focus(FocusState.Programmatic);
}
else
{
if (!string.IsNullOrEmpty(FindBar.Text))
{
FindBar.SelectionStart = FindBar.Text.Length;
}
if (!string.IsNullOrEmpty(ReplaceBar.Text))
{
ReplaceBar.SelectionStart = ReplaceBar.Text.Length;
}
ReplaceBar.Focus(FocusState.Programmatic);
}
}
public void ShowReplaceBar(bool showReplaceBar)
{
ReplaceBarPlaceHolder.Visibility = showReplaceBar ? Visibility.Visible : Visibility.Collapsed;
}
private void DismissButton_OnClick(object sender, RoutedEventArgs e)
{
OnDismissKeyDown?.Invoke(sender, e);
}
private void FindBar_OnTextChanged(object sender, TextChangedEventArgs e)
{
SearchButton.Visibility = !string.IsNullOrEmpty(FindBar.Text) ? Visibility.Visible : Visibility.Collapsed;
if (!string.IsNullOrEmpty(FindBar.Text))
{
ReplaceButton.IsEnabled = true;
ReplaceAllButton.IsEnabled = true;
}
else
{
ReplaceButton.IsEnabled = false;
ReplaceAllButton.IsEnabled = false;
}
}
private void SearchButton_OnClick(object sender, RoutedEventArgs e)
{
if (sender is MenuFlyout) return;
OnFindAndReplaceButtonClicked?.Invoke(sender, new FindAndReplaceEventArgs(FindBar.Text, null, MatchCaseToggle.IsChecked, MatchWholeWordToggle.IsChecked, FindAndReplaceMode.FindOnly));
}
private void FindBar_OnKeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == VirtualKey.Enter && !string.IsNullOrEmpty(FindBar.Text))
{
SearchButton_OnClick(sender, e);
}
if (e.Key == VirtualKey.Tab)
{
e.Handled = true;
}
}
private void ReplaceBar_OnKeyDown(object sender, KeyRoutedEventArgs e)
{
}
private void ReplaceBar_OnTextChanged(object sender, TextChangedEventArgs e)
{
}
private void ReplaceButton_OnClick(object sender, RoutedEventArgs e)
{
OnFindAndReplaceButtonClicked?.Invoke(sender, new FindAndReplaceEventArgs(FindBar.Text, ReplaceBar.Text, MatchCaseToggle.IsChecked, MatchWholeWordToggle.IsChecked, FindAndReplaceMode.Replace));
}
private void ReplaceAllButton_OnClick(object sender, RoutedEventArgs e)
{
OnFindAndReplaceButtonClicked?.Invoke(sender, new FindAndReplaceEventArgs(FindBar.Text, ReplaceBar.Text, MatchCaseToggle.IsChecked, MatchWholeWordToggle.IsChecked, FindAndReplaceMode.ReplaceAll));
}
}
}