Skip to content

Commit 4112efc

Browse files
committed
Reorg RangeSelector code
into file based on type of input
1 parent 740491d commit 4112efc

File tree

5 files changed

+348
-295
lines changed

5 files changed

+348
-295
lines changed

Microsoft.Toolkit.Uwp.UI.Controls.Input/RangeSelector/RangeSelector.Events.cs

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6-
using Windows.Foundation;
7-
using Windows.UI.Xaml;
86
using Windows.UI.Xaml.Controls;
9-
using Windows.UI.Xaml.Controls.Primitives;
10-
using Windows.UI.Xaml.Shapes;
117

128
namespace Microsoft.Toolkit.Uwp.UI.Controls
139
{
@@ -21,29 +17,9 @@ public partial class RangeSelector : Control
2117
/// </summary>
2218
public event EventHandler<RangeChangedEventArgs> ValueChanged;
2319

24-
/// <summary>
25-
/// Event raised when lower or upper range thumbs start being dragged.
26-
/// </summary>
27-
public event DragStartedEventHandler ThumbDragStarted;
28-
29-
/// <summary>
30-
/// Event raised when lower or upper range thumbs end being dragged.
31-
/// </summary>
32-
public event DragCompletedEventHandler ThumbDragCompleted;
33-
3420
private void OnValueChanged(RangeChangedEventArgs e)
3521
{
3622
ValueChanged?.Invoke(this, e);
3723
}
38-
39-
private void OnThumbDragStarted(DragStartedEventArgs e)
40-
{
41-
ThumbDragStarted?.Invoke(this, e);
42-
}
43-
44-
private void OnThumbDragCompleted(DragCompletedEventArgs e)
45-
{
46-
ThumbDragCompleted?.Invoke(this, e);
47-
}
4824
}
4925
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using Windows.Foundation;
7+
using Windows.UI.Xaml;
8+
using Windows.UI.Xaml.Controls;
9+
using Windows.UI.Xaml.Controls.Primitives;
10+
11+
namespace Microsoft.Toolkit.Uwp.UI.Controls
12+
{
13+
/// <summary>
14+
/// RangeSelector is a "double slider" control for range values.
15+
/// </summary>
16+
public partial class RangeSelector : Control
17+
{
18+
/// <summary>
19+
/// Event raised when lower or upper range thumbs start being dragged.
20+
/// </summary>
21+
public event DragStartedEventHandler ThumbDragStarted;
22+
23+
/// <summary>
24+
/// Event raised when lower or upper range thumbs end being dragged.
25+
/// </summary>
26+
public event DragCompletedEventHandler ThumbDragCompleted;
27+
28+
private void OnThumbDragStarted(DragStartedEventArgs e)
29+
{
30+
ThumbDragStarted?.Invoke(this, e);
31+
}
32+
33+
private void OnThumbDragCompleted(DragCompletedEventArgs e)
34+
{
35+
ThumbDragCompleted?.Invoke(this, e);
36+
}
37+
38+
private void MinThumb_DragDelta(object sender, DragDeltaEventArgs e)
39+
{
40+
_absolutePosition += e.HorizontalChange;
41+
42+
RangeMin = DragThumb(_minThumb, 0, Canvas.GetLeft(_maxThumb), _absolutePosition);
43+
44+
if (_toolTipText != null)
45+
{
46+
UpdateToolTipText(this, _toolTipText, RangeMin);
47+
}
48+
}
49+
50+
private void MaxThumb_DragDelta(object sender, DragDeltaEventArgs e)
51+
{
52+
_absolutePosition += e.HorizontalChange;
53+
54+
RangeMax = DragThumb(_maxThumb, Canvas.GetLeft(_minThumb), DragWidth(), _absolutePosition);
55+
56+
if (_toolTipText != null)
57+
{
58+
UpdateToolTipText(this, _toolTipText, RangeMax);
59+
}
60+
}
61+
62+
private void MinThumb_DragStarted(object sender, DragStartedEventArgs e)
63+
{
64+
OnThumbDragStarted(e);
65+
Thumb_DragStarted(_minThumb);
66+
}
67+
68+
private void MaxThumb_DragStarted(object sender, DragStartedEventArgs e)
69+
{
70+
OnThumbDragStarted(e);
71+
Thumb_DragStarted(_maxThumb);
72+
}
73+
74+
private void Thumb_DragCompleted(object sender, DragCompletedEventArgs e)
75+
{
76+
OnThumbDragCompleted(e);
77+
OnValueChanged(sender.Equals(_minThumb) ? new RangeChangedEventArgs(_oldValue, RangeMin, RangeSelectorProperty.MinimumValue) : new RangeChangedEventArgs(_oldValue, RangeMax, RangeSelectorProperty.MaximumValue));
78+
SyncThumbs();
79+
80+
if (_toolTip != null)
81+
{
82+
_toolTip.Visibility = Visibility.Collapsed;
83+
}
84+
85+
VisualStateManager.GoToState(this, "Normal", true);
86+
}
87+
88+
private double DragWidth()
89+
{
90+
return _containerCanvas.ActualWidth - _maxThumb.Width;
91+
}
92+
93+
94+
private double DragThumb(Thumb thumb, double min, double max, double nextPos)
95+
{
96+
nextPos = Math.Max(min, nextPos);
97+
nextPos = Math.Min(max, nextPos);
98+
99+
Canvas.SetLeft(thumb, nextPos);
100+
101+
if (_toolTipText != null && _toolTip != null)
102+
{
103+
var thumbCenter = nextPos + (thumb.Width / 2);
104+
_toolTip.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
105+
var ttWidth = _toolTip.ActualWidth / 2;
106+
107+
Canvas.SetLeft(_toolTip, thumbCenter - ttWidth);
108+
}
109+
110+
return Minimum + ((nextPos / DragWidth()) * (Maximum - Minimum));
111+
}
112+
113+
private void Thumb_DragStarted(Thumb thumb)
114+
{
115+
var useMin = thumb == _minThumb;
116+
var otherThumb = useMin ? _maxThumb : _minThumb;
117+
118+
_absolutePosition = Canvas.GetLeft(thumb);
119+
Canvas.SetZIndex(thumb, 10);
120+
Canvas.SetZIndex(otherThumb, 0);
121+
_oldValue = RangeMin;
122+
123+
if (_toolTipText != null && _toolTip != null)
124+
{
125+
_toolTip.Visibility = Visibility.Visible;
126+
var thumbCenter = _absolutePosition + (thumb.Width / 2);
127+
_toolTip.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
128+
var ttWidth = _toolTip.ActualWidth / 2;
129+
Canvas.SetLeft(_toolTip, thumbCenter - ttWidth);
130+
131+
UpdateToolTipText(this, _toolTipText, useMin ? RangeMin : RangeMax);
132+
}
133+
134+
VisualStateManager.GoToState(this, useMin ? "MinPressed" : "MaxPressed", true);
135+
}
136+
}
137+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using Windows.System;
6+
using Windows.UI.Xaml;
7+
using Windows.UI.Xaml.Controls;
8+
using Windows.UI.Xaml.Input;
9+
10+
namespace Microsoft.Toolkit.Uwp.UI.Controls
11+
{
12+
/// <summary>
13+
/// RangeSelector is a "double slider" control for range values.
14+
/// </summary>
15+
16+
public partial class RangeSelector : Control
17+
{
18+
private readonly DispatcherQueueTimer keyDebounceTimer = DispatcherQueue.GetForCurrentThread().CreateTimer();
19+
20+
private void MinThumb_KeyDown(object sender, KeyRoutedEventArgs e)
21+
{
22+
switch (e.Key)
23+
{
24+
case VirtualKey.Left:
25+
RangeMin -= StepFrequency;
26+
SyncThumbs(fromMinKeyDown: true);
27+
if (_toolTip != null)
28+
{
29+
_toolTip.Visibility = Visibility.Visible;
30+
}
31+
32+
e.Handled = true;
33+
break;
34+
case VirtualKey.Right:
35+
RangeMin += StepFrequency;
36+
SyncThumbs(fromMinKeyDown: true);
37+
if (_toolTip != null)
38+
{
39+
_toolTip.Visibility = Visibility.Visible;
40+
}
41+
42+
e.Handled = true;
43+
break;
44+
}
45+
}
46+
47+
private void MaxThumb_KeyDown(object sender, KeyRoutedEventArgs e)
48+
{
49+
switch (e.Key)
50+
{
51+
case VirtualKey.Left:
52+
RangeMax -= StepFrequency;
53+
SyncThumbs(fromMaxKeyDown: true);
54+
if (_toolTip != null)
55+
{
56+
_toolTip.Visibility = Visibility.Visible;
57+
}
58+
59+
e.Handled = true;
60+
break;
61+
case VirtualKey.Right:
62+
RangeMax += StepFrequency;
63+
SyncThumbs(fromMaxKeyDown: true);
64+
if (_toolTip != null)
65+
{
66+
_toolTip.Visibility = Visibility.Visible;
67+
}
68+
69+
e.Handled = true;
70+
break;
71+
}
72+
}
73+
74+
private void Thumb_KeyUp(object sender, KeyRoutedEventArgs e)
75+
{
76+
switch (e.Key)
77+
{
78+
case VirtualKey.Left:
79+
case VirtualKey.Right:
80+
if (_toolTip != null)
81+
{
82+
keyDebounceTimer.Debounce(
83+
() => _toolTip.Visibility = Visibility.Collapsed,
84+
TimeToHideToolTipOnKeyUp);
85+
}
86+
87+
e.Handled = true;
88+
break;
89+
}
90+
}
91+
}
92+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using Windows.Foundation;
7+
using Windows.System;
8+
using Windows.UI.Xaml;
9+
using Windows.UI.Xaml.Controls;
10+
using Windows.UI.Xaml.Controls.Primitives;
11+
using Windows.UI.Xaml.Input;
12+
using Windows.UI.Xaml.Shapes;
13+
14+
namespace Microsoft.Toolkit.Uwp.UI.Controls
15+
{
16+
/// <summary>
17+
/// RangeSelector is a "double slider" control for range values.
18+
/// </summary>
19+
20+
public partial class RangeSelector : Control
21+
{
22+
private void ContainerCanvas_PointerEntered(object sender, PointerRoutedEventArgs e)
23+
{
24+
VisualStateManager.GoToState(this, "PointerOver", false);
25+
}
26+
27+
private void ContainerCanvas_PointerExited(object sender, PointerRoutedEventArgs e)
28+
{
29+
var position = e.GetCurrentPoint(_containerCanvas).Position.X;
30+
var normalizedPosition = ((position / DragWidth()) * (Maximum - Minimum)) + Minimum;
31+
32+
if (_pointerManipulatingMin)
33+
{
34+
_pointerManipulatingMin = false;
35+
_containerCanvas.IsHitTestVisible = true;
36+
OnValueChanged(new RangeChangedEventArgs(RangeMin, normalizedPosition, RangeSelectorProperty.MinimumValue));
37+
}
38+
else if (_pointerManipulatingMax)
39+
{
40+
_pointerManipulatingMax = false;
41+
_containerCanvas.IsHitTestVisible = true;
42+
OnValueChanged(new RangeChangedEventArgs(RangeMax, normalizedPosition, RangeSelectorProperty.MaximumValue));
43+
}
44+
45+
if (_toolTip != null)
46+
{
47+
_toolTip.Visibility = Visibility.Collapsed;
48+
}
49+
50+
VisualStateManager.GoToState(this, "Normal", false);
51+
}
52+
53+
private void ContainerCanvas_PointerReleased(object sender, PointerRoutedEventArgs e)
54+
{
55+
var position = e.GetCurrentPoint(_containerCanvas).Position.X;
56+
var normalizedPosition = ((position / DragWidth()) * (Maximum - Minimum)) + Minimum;
57+
58+
if (_pointerManipulatingMin)
59+
{
60+
_pointerManipulatingMin = false;
61+
_containerCanvas.IsHitTestVisible = true;
62+
OnValueChanged(new RangeChangedEventArgs(RangeMin, normalizedPosition, RangeSelectorProperty.MinimumValue));
63+
}
64+
else if (_pointerManipulatingMax)
65+
{
66+
_pointerManipulatingMax = false;
67+
_containerCanvas.IsHitTestVisible = true;
68+
OnValueChanged(new RangeChangedEventArgs(RangeMax, normalizedPosition, RangeSelectorProperty.MaximumValue));
69+
}
70+
71+
SyncThumbs();
72+
73+
if (_toolTip != null)
74+
{
75+
_toolTip.Visibility = Visibility.Collapsed;
76+
}
77+
}
78+
79+
private void ContainerCanvas_PointerMoved(object sender, PointerRoutedEventArgs e)
80+
{
81+
var position = e.GetCurrentPoint(_containerCanvas).Position.X;
82+
var normalizedPosition = ((position / DragWidth()) * (Maximum - Minimum)) + Minimum;
83+
84+
if (_pointerManipulatingMin && normalizedPosition < RangeMax)
85+
{
86+
RangeMin = DragThumb(_minThumb, 0, Canvas.GetLeft(_maxThumb), position);
87+
UpdateToolTipText(this, _toolTipText, RangeMin);
88+
}
89+
else if (_pointerManipulatingMax && normalizedPosition > RangeMin)
90+
{
91+
RangeMax = DragThumb(_maxThumb, Canvas.GetLeft(_minThumb), DragWidth(), position);
92+
UpdateToolTipText(this, _toolTipText, RangeMax);
93+
}
94+
}
95+
96+
private void ContainerCanvas_PointerPressed(object sender, PointerRoutedEventArgs e)
97+
{
98+
var position = e.GetCurrentPoint(_containerCanvas).Position.X;
99+
var normalizedPosition = position * Math.Abs(Maximum - Minimum) / DragWidth();
100+
double upperValueDiff = Math.Abs(RangeMax - normalizedPosition);
101+
double lowerValueDiff = Math.Abs(RangeMin - normalizedPosition);
102+
103+
if (upperValueDiff < lowerValueDiff)
104+
{
105+
RangeMax = normalizedPosition;
106+
_pointerManipulatingMax = true;
107+
Thumb_DragStarted(_maxThumb);
108+
}
109+
else
110+
{
111+
RangeMin = normalizedPosition;
112+
_pointerManipulatingMin = true;
113+
Thumb_DragStarted(_minThumb);
114+
}
115+
116+
SyncThumbs();
117+
}
118+
}
119+
}

0 commit comments

Comments
 (0)