/
ValidationPlaceholder.cs
242 lines (203 loc) · 8.1 KB
/
ValidationPlaceholder.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
using DevZest.Data.Presenters;
using DevZest.Data.Presenters.Primitives;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace DevZest.Data.Views
{
/// <summary>
/// Represents the placeholder to display validation error from specified sources.
/// </summary>
public class ValidationPlaceholder : Control, IRowElement, IScalarElement
{
private sealed class MouseTracker
{
public MouseTracker(BasePresenter presenter)
{
_presenter = presenter;
View = (UIElement)presenter.View;
_presenter.ViewChanged += OnViewChanged;
}
private void OnViewChanged(object sender, EventArgs e)
{
View = (UIElement)Presenter.View;
}
private readonly BasePresenter _presenter;
public BasePresenter Presenter
{
get { return _presenter; }
}
private UIElement _view;
public event EventHandler<EventArgs> EnterMouseTrack;
public event EventHandler<EventArgs> ExitMouseTrack;
private UIElement View
{
get { return _view; }
set
{
if (_view == value)
return;
if (_view != null)
{
_view.MouseEnter -= TrackMouse;
_view.PreviewMouseMove -= TrackMouse;
_view.MouseLeave -= OnMouseLeave;
}
_view = value;
if (_view != null)
{
_view.MouseEnter += TrackMouse;
_view.PreviewMouseMove += TrackMouse;
_view.MouseLeave += OnMouseLeave;
}
}
}
private void TrackMouse(object sender, MouseEventArgs e)
{
TrackMouse(sender, e, true);
}
private void OnMouseLeave(object sender, MouseEventArgs e)
{
TrackMouse(sender, e, false);
}
private void TrackMouse(object sender, MouseEventArgs e, bool hitTest)
{
var enterMouseTrack = EnterMouseTrack;
var exitMouseTrack = ExitMouseTrack;
if (enterMouseTrack == null || exitMouseTrack == null)
return;
enterMouseTrack(this, EventArgs.Empty);
if (hitTest)
HitTest(sender, e);
exitMouseTrack(this, EventArgs.Empty);
}
private void HitTest(object sender, MouseEventArgs e)
{
var view = (UIElement)sender;
var parameters = new PointHitTestParameters(e.GetPosition(view));
VisualTreeHelper.HitTest(view, element =>
{
var validationPlaceholder = element as ValidationPlaceholder;
if (validationPlaceholder != null)
{
validationPlaceholder.IsMouseIndirectlyOver = true;
return HitTestFilterBehavior.ContinueSkipChildren;
}
return HitTestFilterBehavior.Continue;
}, result => HitTestResultBehavior.Continue, parameters);
}
}
private static readonly DependencyPropertyKey IsActivePropertyKey = DependencyProperty.RegisterReadOnly(nameof(IsActive), typeof(bool), typeof(ValidationPlaceholder),
new FrameworkPropertyMetadata(BooleanBoxes.False));
/// <summary>
/// Identifies the <see cref="IsActive"/> property.
/// </summary>
public static readonly DependencyProperty IsActiveProperty = IsActivePropertyKey.DependencyProperty;
static ValidationPlaceholder()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ValidationPlaceholder), new FrameworkPropertyMetadata(typeof(ValidationPlaceholder)));
}
/// <summary>
/// Gets a value indicates whether this control is active (either mouse over or has keyboard focus within). This is a dependency property.
/// </summary>
public bool IsActive
{
get { return (bool)GetValue(IsActiveProperty); }
private set { SetValue(IsActivePropertyKey, BooleanBoxes.Box(value)); }
}
private static readonly ConditionalWeakTable<BasePresenter, MouseTracker> s_mouseTrackers = new ConditionalWeakTable<BasePresenter, MouseTracker>();
private static MouseTracker GetMouseTracker(BasePresenter presenter)
{
return s_mouseTrackers.GetValue(presenter, CreateMouseTracker);
}
private static MouseTracker CreateMouseTracker(BasePresenter presenter)
{
return new MouseTracker(presenter);
}
private void CoerceIsActive()
{
IsActive = GetIsActive();
}
private bool GetIsActive()
{
if (IsMouseIndirectlyOver)
return true;
if (_containingElements == null)
return false;
for (int i = 0; i < _containingElements.Count; i++)
{
if (_containingElements[i].IsKeyboardFocusWithin)
return true;
}
return false;
}
void IRowElement.Setup(RowPresenter rowPresenter)
{
WireMouseTrack(rowPresenter.DataPresenter);
}
void IRowElement.Refresh(RowPresenter rowPresenter)
{
}
void IRowElement.Cleanup(RowPresenter rowPresenter)
{
UnwireMouseTrack(rowPresenter.DataPresenter);
}
void IScalarElement.Setup(ScalarPresenter scalarPresenter)
{
WireMouseTrack(scalarPresenter.Presenter);
}
void IScalarElement.Refresh(ScalarPresenter scalarPresenter)
{
}
void IScalarElement.Cleanup(ScalarPresenter scalarPresenter)
{
UnwireMouseTrack(scalarPresenter.Presenter);
}
private void WireMouseTrack(BasePresenter presenter)
{
var mouseTracker = GetMouseTracker(presenter);
mouseTracker.EnterMouseTrack += OnEnterMouseTrack;
mouseTracker.ExitMouseTrack += OnExitMouseTrack;
}
private void UnwireMouseTrack(BasePresenter presenter)
{
var mouseTracker = GetMouseTracker(presenter);
mouseTracker.EnterMouseTrack -= OnEnterMouseTrack;
mouseTracker.ExitMouseTrack -= OnExitMouseTrack;
}
private bool IsMouseIndirectlyOver { get; set; }
private void OnEnterMouseTrack(object sender, EventArgs e)
{
IsMouseIndirectlyOver = false;
}
private void OnExitMouseTrack(object sender, EventArgs e)
{
CoerceIsActive();
}
private IReadOnlyList<UIElement> _containingElements;
internal void Setup(IReadOnlyList<UIElement> containingElements)
{
Debug.Assert(_containingElements == null && containingElements != null);
_containingElements = containingElements;
for (int i = 0; i < _containingElements.Count; i++)
_containingElements[i].IsKeyboardFocusWithinChanged += OnContainingElementIsKeyboardFocusWithinChanged;
CoerceIsActive();
}
private void OnContainingElementIsKeyboardFocusWithinChanged(object sender, DependencyPropertyChangedEventArgs e)
{
CoerceIsActive();
}
internal void Cleanup()
{
Debug.Assert(_containingElements != null);
for (int i = 0; i < _containingElements.Count; i++)
_containingElements[i].IsKeyboardFocusWithinChanged += OnContainingElementIsKeyboardFocusWithinChanged;
_containingElements = null;
}
}
}