This repository was archived by the owner on Feb 25, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathRangeSelector.cs
More file actions
294 lines (251 loc) · 10.3 KB
/
RangeSelector.cs
File metadata and controls
294 lines (251 loc) · 10.3 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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Shapes;
namespace Microsoft.Toolkit.Uwp.UI.Controls
{
/// <summary>
/// RangeSelector is a "double slider" control for range values.
/// </summary>
[TemplateVisualState(Name = "Normal", GroupName = "CommonStates")]
[TemplateVisualState(Name = "MinPressed", GroupName = "CommonStates")]
[TemplateVisualState(Name = "MaxPressed", GroupName = "CommonStates")]
[TemplateVisualState(Name = "Disabled", GroupName = "CommonStates")]
[TemplatePart(Name = "OutOfRangeContentContainer", Type = typeof(Border))]
[TemplatePart(Name = "ActiveRectangle", Type = typeof(Rectangle))]
[TemplatePart(Name = "MinThumb", Type = typeof(Thumb))]
[TemplatePart(Name = "MaxThumb", Type = typeof(Thumb))]
[TemplatePart(Name = "ContainerCanvas", Type = typeof(Canvas))]
[TemplatePart(Name = "ControlGrid", Type = typeof(Grid))]
[TemplatePart(Name = "ToolTip", Type = typeof(Grid))]
[TemplatePart(Name = "ToolTipText", Type = typeof(TextBlock))]
public partial class RangeSelector : Control
{
private const double Epsilon = 0.01;
private const double DefaultMinimum = 0.0;
private const double DefaultMaximum = 1.0;
private const double DefaultStepFrequency = 1;
private static readonly TimeSpan TimeToHideToolTipOnKeyUp = TimeSpan.FromSeconds(1);
private Border _outOfRangeContentContainer;
private Rectangle _activeRectangle;
private RangeThumb _minThumb;
private RangeThumb _maxThumb;
private Canvas _containerCanvas;
private Grid _controlGrid;
private double _oldValue;
private bool _valuesAssigned;
private bool _minSet;
private bool _maxSet;
private bool _pointerManipulatingMin;
private bool _pointerManipulatingMax;
private double _absolutePosition;
private Grid _toolTip;
private TextBlock _toolTipText;
/// <summary>
/// Initializes a new instance of the <see cref="RangeSelector"/> class.
/// Create a default range selector control.
/// </summary>
public RangeSelector()
{
DefaultStyleKey = typeof(RangeSelector);
}
/// <summary>
/// Update the visual state of the control when its template is changed.
/// </summary>
protected override void OnApplyTemplate()
{
if (_minThumb != null)
{
_minThumb.DragCompleted -= Thumb_DragCompleted;
_minThumb.DragDelta -= MinThumb_DragDelta;
_minThumb.DragStarted -= MinThumb_DragStarted;
_minThumb.KeyDown -= MinThumb_KeyDown;
}
if (_maxThumb != null)
{
_maxThumb.DragCompleted -= Thumb_DragCompleted;
_maxThumb.DragDelta -= MaxThumb_DragDelta;
_maxThumb.DragStarted -= MaxThumb_DragStarted;
_maxThumb.KeyDown -= MaxThumb_KeyDown;
}
if (_containerCanvas != null)
{
_containerCanvas.SizeChanged -= ContainerCanvas_SizeChanged;
_containerCanvas.PointerPressed -= ContainerCanvas_PointerPressed;
_containerCanvas.PointerMoved -= ContainerCanvas_PointerMoved;
_containerCanvas.PointerReleased -= ContainerCanvas_PointerReleased;
_containerCanvas.PointerExited -= ContainerCanvas_PointerExited;
}
IsEnabledChanged -= RangeSelector_IsEnabledChanged;
// Need to make sure the values can be set in XAML and don't overwrite each other
VerifyValues();
_valuesAssigned = true;
_outOfRangeContentContainer = GetTemplateChild("OutOfRangeContentContainer") as Border;
_activeRectangle = GetTemplateChild("ActiveRectangle") as Rectangle;
_minThumb = GetTemplateChild("MinThumb") as RangeThumb;
_maxThumb = GetTemplateChild("MaxThumb") as RangeThumb;
_containerCanvas = GetTemplateChild("ContainerCanvas") as Canvas;
_controlGrid = GetTemplateChild("ControlGrid") as Grid;
_toolTip = GetTemplateChild("ToolTip") as Grid;
_toolTipText = GetTemplateChild("ToolTipText") as TextBlock;
if (_minThumb != null)
{
_minThumb.DragCompleted += Thumb_DragCompleted;
_minThumb.DragDelta += MinThumb_DragDelta;
_minThumb.DragStarted += MinThumb_DragStarted;
_minThumb.KeyDown += MinThumb_KeyDown;
_minThumb.KeyUp += Thumb_KeyUp;
}
if (_maxThumb != null)
{
_maxThumb.DragCompleted += Thumb_DragCompleted;
_maxThumb.DragDelta += MaxThumb_DragDelta;
_maxThumb.DragStarted += MaxThumb_DragStarted;
_maxThumb.KeyDown += MaxThumb_KeyDown;
_maxThumb.KeyUp += Thumb_KeyUp;
}
if (_containerCanvas != null)
{
_containerCanvas.SizeChanged += ContainerCanvas_SizeChanged;
_containerCanvas.PointerEntered += ContainerCanvas_PointerEntered;
_containerCanvas.PointerPressed += ContainerCanvas_PointerPressed;
_containerCanvas.PointerMoved += ContainerCanvas_PointerMoved;
_containerCanvas.PointerReleased += ContainerCanvas_PointerReleased;
_containerCanvas.PointerExited += ContainerCanvas_PointerExited;
}
VisualStateManager.GoToState(this, IsEnabled ? "Normal" : "Disabled", false);
IsEnabledChanged += RangeSelector_IsEnabledChanged;
// Measure our min/max text longest value so we can avoid the length of the scrolling reason shifting in size during use.
var tb = new TextBlock { Text = Maximum.ToString() };
tb.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
base.OnApplyTemplate();
}
private static void UpdateToolTipText(RangeSelector rangeSelector, TextBlock toolTip, double newValue)
{
if (toolTip != null)
{
toolTip.Text = string.Format("{0:0.##}", newValue);
}
}
private void ContainerCanvas_SizeChanged(object sender, SizeChangedEventArgs e)
{
SyncThumbs();
}
private void VerifyValues()
{
if (Minimum > Maximum)
{
Minimum = Maximum;
Maximum = Maximum;
}
if (Minimum == Maximum)
{
Maximum += Epsilon;
}
if (!_maxSet)
{
RangeEnd = Maximum;
}
if (!_minSet)
{
RangeStart = Minimum;
}
if (RangeStart < Minimum)
{
RangeStart = Minimum;
}
if (RangeEnd < Minimum)
{
RangeEnd = Minimum;
}
if (RangeStart > Maximum)
{
RangeStart = Maximum;
}
if (RangeEnd > Maximum)
{
RangeEnd = Maximum;
}
if (RangeEnd < RangeStart)
{
RangeStart = RangeEnd;
}
}
private void RangeMinToStepFrequency()
{
RangeStart = MoveToStepFrequency(RangeStart);
}
private void RangeMaxToStepFrequency()
{
RangeEnd = MoveToStepFrequency(RangeEnd);
}
private double MoveToStepFrequency(double rangeValue)
{
double newValue = Minimum + (((int)Math.Round((rangeValue - Minimum) / StepFrequency)) * StepFrequency);
if (newValue < Minimum)
{
return Minimum;
}
else if (newValue > Maximum || Maximum - newValue < StepFrequency)
{
return Maximum;
}
else
{
return newValue;
}
}
private void SyncThumbs(bool fromMinKeyDown = false, bool fromMaxKeyDown = false)
{
if (_containerCanvas == null)
{
return;
}
var relativeLeft = ((RangeStart - Minimum) / (Maximum - Minimum)) * DragWidth();
var relativeRight = ((RangeEnd - Minimum) / (Maximum - Minimum)) * DragWidth();
Canvas.SetLeft(_minThumb, relativeLeft);
Canvas.SetLeft(_maxThumb, relativeRight);
if (fromMinKeyDown || fromMaxKeyDown)
{
DragThumb(
fromMinKeyDown ? _minThumb : _maxThumb,
fromMinKeyDown ? 0 : Canvas.GetLeft(_minThumb),
fromMinKeyDown ? Canvas.GetLeft(_maxThumb) : DragWidth(),
fromMinKeyDown ? relativeLeft : relativeRight);
if (_toolTipText != null)
{
UpdateToolTipText(this, _toolTipText, fromMinKeyDown ? RangeStart : RangeEnd);
}
}
SyncActiveRectangle();
}
private void SyncActiveRectangle()
{
if (_containerCanvas == null)
{
return;
}
if (_minThumb == null)
{
return;
}
if (_maxThumb == null)
{
return;
}
var relativeLeft = Canvas.GetLeft(_minThumb);
Canvas.SetLeft(_activeRectangle, relativeLeft);
Canvas.SetTop(_activeRectangle, (_containerCanvas.ActualHeight - _activeRectangle.ActualHeight) / 2);
_activeRectangle.Width = Math.Max(0, Canvas.GetLeft(_maxThumb) - Canvas.GetLeft(_minThumb));
}
private void RangeSelector_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
{
VisualStateManager.GoToState(this, IsEnabled ? "Normal" : "Disabled", true);
}
}
}