This repository has been archived by the owner on Jun 21, 2023. It is now read-only.
/
ValidationMessage.cs
170 lines (150 loc) · 7.19 KB
/
ValidationMessage.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using System;
using System.Linq;
using System.Reactive.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Markup;
using System.Windows.Media;
using GitHub.Extensions.Reactive;
using GitHub.Validation;
using NullGuard;
using ReactiveUI;
namespace GitHub.UI
{
public class ValidationMessage : UserControl
{
const double defaultTextChangeThrottle = 0.2;
bool userHasInteracted;
public ValidationMessage()
{
this.WhenAny(x => x.ReactiveValidator.ValidationResult, x => x.Value)
.WhereNotNull()
.Subscribe(result =>
{
ShowError = result.IsValid == false;
Text = result.Message;
});
this.WhenAny(x => x.ValidatesControl, x => x.Value)
.WhereNotNull()
.Do(CreateBinding)
.Select(control =>
Observable.Merge(
this.WhenAnyValue(x => x.ShowError).Where(x => userHasInteracted),
control.Events().TextChanged
.Throttle(TimeSpan.FromSeconds(ShowError ? defaultTextChangeThrottle : TextChangeThrottle),
RxApp.MainThreadScheduler)
.Select(_ => ShowError),
control.Events().LostFocus
.Select(_ => ShowError),
control.Events().LostFocus
.Where(__ => string.IsNullOrEmpty(ValidatesControl.Text))
.Select(_ => false)))
.Switch()
.Subscribe(ShowValidateError);
}
public static readonly DependencyProperty IsShowingMessageProperty = DependencyProperty.Register("IsShowingMessage", typeof(bool), typeof(ValidationMessage));
public bool IsShowingMessage
{
get { return (bool)GetValue(IsShowingMessageProperty); }
private set { SetValue(IsShowingMessageProperty, value); }
}
public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(ValidationMessage));
public string Text
{
[return: AllowNull]
get { return (string)GetValue(TextProperty); }
private set { SetValue(TextProperty, value); }
}
public static readonly DependencyProperty ShowErrorProperty = DependencyProperty.Register("ShowError", typeof(bool), typeof(ValidationMessage));
public bool ShowError
{
get { return (bool)GetValue(ShowErrorProperty); }
set { SetValue(ShowErrorProperty, value); }
}
public static readonly DependencyProperty TextChangeThrottleProperty = DependencyProperty.Register("TextChangeThrottle", typeof(double), typeof(ValidationMessage), new PropertyMetadata(defaultTextChangeThrottle));
public double TextChangeThrottle
{
get { return (double)GetValue(TextChangeThrottleProperty); }
set { SetValue(TextChangeThrottleProperty, value); }
}
public static readonly DependencyProperty ValidatesControlProperty = DependencyProperty.Register("ValidatesControl", typeof(TextBox), typeof(ValidationMessage), new PropertyMetadata(default(TextBox)));
public TextBox ValidatesControl
{
[return: AllowNull]
get { return (TextBox)GetValue(ValidatesControlProperty); }
set { SetValue(ValidatesControlProperty, value); }
}
public static readonly DependencyProperty ReactiveValidatorProperty = DependencyProperty.Register("ReactiveValidator", typeof(ReactivePropertyValidator), typeof(ValidationMessage));
public ReactivePropertyValidator ReactiveValidator
{
[return: AllowNull]
get { return (ReactivePropertyValidator)GetValue(ReactiveValidatorProperty); }
set { SetValue(ReactiveValidatorProperty, value); }
}
public static readonly DependencyProperty IconProperty = DependencyProperty.Register("Icon", typeof(Octicon), typeof(ValidationMessage), new PropertyMetadata(Octicon.stop));
public Octicon Icon
{
[return: AllowNull]
get { return (Octicon) GetValue(IconProperty); }
set { SetValue(IconProperty, value); }
}
public static readonly DependencyProperty FillProperty =
DependencyProperty.Register("Fill", typeof(Brush), typeof(ValidationMessage), new PropertyMetadata(new SolidColorBrush(Color.FromRgb(0xe7, 0x4c, 0x3c))));
public Brush Fill
{
[return: AllowNull]
get { return (Brush)GetValue(FillProperty); }
set { SetValue(FillProperty, value); }
}
public static readonly DependencyProperty ErrorAdornerTemplateProperty = DependencyProperty.Register("ErrorAdornerTemplate", typeof(string), typeof(ValidationMessage), new PropertyMetadata("validationTemplate"));
[AllowNull]
public string ErrorAdornerTemplate
{
[return: AllowNull]
get { return (string)GetValue(ErrorAdornerTemplateProperty); }
set { SetValue(ErrorAdornerTemplateProperty, value); }
}
void ShowValidateError(bool showError)
{
IsShowingMessage = showError;
userHasInteracted = true;
if (ValidatesControl == null || !IsAdornerEnabled()) return;
var bindingExpression = ValidatesControl.GetBindingExpression(TextBox.TagProperty);
if (bindingExpression == null) return;
var opExpression = BindingOperations.GetBindingExpression(ValidatesControl, TextBox.TagProperty);
if (opExpression == null) return;
var validationError = new ValidationError(new ExceptionValidationRule(), opExpression);
if (showError)
{
validationError.ErrorContent = Text;
System.Windows.Controls.Validation.MarkInvalid(bindingExpression, validationError);
}
else
{
System.Windows.Controls.Validation.ClearInvalid(bindingExpression);
}
}
void CreateBinding(TextBox textBox)
{
if (textBox == null || !IsAdornerEnabled())
{
return;
}
var template = FindResource(ErrorAdornerTemplate) as ControlTemplate;
if (template != null)
System.Windows.Controls.Validation.SetErrorTemplate(textBox, template);
var validationBinding = new Binding { Source = textBox, Path = new PropertyPath("Tag") };
validationBinding.NotifyOnValidationError = true;
validationBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
validationBinding.ValidationRules.Add(new ExceptionValidationRule());
textBox.SetBinding(TagProperty, validationBinding);
}
bool IsAdornerEnabled()
{
return !string.IsNullOrEmpty(ErrorAdornerTemplate)
&& !ErrorAdornerTemplate.Equals("None", StringComparison.OrdinalIgnoreCase);
}
}
}