-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRVerticalScroller.cs
164 lines (137 loc) · 6.33 KB
/
RVerticalScroller.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
using System;
using Windows.System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Markup;
namespace Ratio.UWP.Controls
{
[TemplatePart(Name = "ScrollViewer", Type = typeof(ScrollViewer))]
[ContentProperty(Name = "Content")]
public sealed class RVerticalScroller : Control
{
#region Properties
private ScrollViewer _scrollViewer;
#region Attached Properties
public static readonly DependencyProperty SuppressHorizontalScrollWheelProperty = DependencyProperty.RegisterAttached(
"SuppressHorizontalScrollWheel", typeof(bool), typeof(RVerticalScroller), new PropertyMetadata(default(bool)));
public static void SetSuppressHorizontalScrollWheel(DependencyObject target, bool value)
{
target.SetValue(SuppressHorizontalScrollWheelProperty,value);
}
public static bool GetSuppressHorizontalScrollWheel(DependencyObject target)
{
return (bool) target.GetValue(SuppressHorizontalScrollWheelProperty);
}
#endregion
#region Dependency Properties
public static readonly DependencyProperty ContentProperty = DependencyProperty.Register(
"Content", typeof(object), typeof(RVerticalScroller), new PropertyMetadata(default(object)));
public object Content
{
get => GetValue(ContentProperty);
set => SetValue(ContentProperty, value);
}
public static readonly DependencyProperty VerticalScrollStepSizeProperty = DependencyProperty.Register(
"VerticalScrollStepSize", typeof(double), typeof(RRowlist), new PropertyMetadata(50));
public double VerticalScrollStepSize
{
get => (double) GetValue(VerticalScrollStepSizeProperty);
set => SetValue(VerticalScrollStepSizeProperty, value);
}
public static readonly DependencyProperty DisableThumbstickScrollingProperty = DependencyProperty.Register(
"DisableThumbstickScrolling", typeof(bool), typeof(RVerticalScroller), new PropertyMetadata(default(bool)));
public bool DisableThumbstickScrolling
{
get => (bool) GetValue(DisableThumbstickScrollingProperty);
set => SetValue(DisableThumbstickScrollingProperty, value);
}
#endregion
public double? VerticalOffset => _scrollViewer?.VerticalOffset;
#endregion
public RVerticalScroller()
{
DefaultStyleKey = typeof(RVerticalScroller);
}
public void WireupSuppressedControls()
{
var wireupAction = new Action<DependencyObject>(depObject =>
{
if (!GetSuppressHorizontalScrollWheel(depObject)) return;
WireUpScrollWheel(depObject);
});
ControlUtilities.TraverseAndApply(this, wireupAction);
}
public void UnwireSuppressedControls()
{
var unwireAction = new Action<DependencyObject>(depObject =>
{
if (!GetSuppressHorizontalScrollWheel(depObject)) return;
UnwireScrollWheel(depObject);
});
ControlUtilities.TraverseAndApply(this, unwireAction);
}
public void ScrollToVerticalOffset(double? offset)
{
_scrollViewer?.ChangeView(0, offset, 1, true);
}
#region Overrides
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();
_scrollViewer = GetTemplateChild("ScrollViewer") as ScrollViewer;
if (_scrollViewer != null) _scrollViewer.BringIntoViewOnFocusChange = true;
}
#endregion
#region Event Handlers
private void ScrollContentPresenterOnPointerWheelChanged(object sender, PointerRoutedEventArgs pointerRoutedEventArgs)
{
if (_scrollViewer == null) return;
var pointer = pointerRoutedEventArgs.GetCurrentPoint(_scrollViewer);
double delta = pointer.Properties.MouseWheelDelta > 0 ? (-1 * VerticalScrollStepSize) : VerticalScrollStepSize;
_scrollViewer.ChangeView(0, _scrollViewer.VerticalOffset + delta, 1);
pointerRoutedEventArgs.Handled = true;
}
protected override void OnKeyUp(KeyRoutedEventArgs e)
{
if (DisableThumbstickScrolling)
{
base.OnKeyUp(e);
return;
}
if (e.OriginalKey == VirtualKey.GamepadLeftThumbstickUp)
{
ScrollToVerticalOffset(_scrollViewer.VerticalOffset - VerticalScrollStepSize);
}
else if (e.OriginalKey == VirtualKey.GamepadLeftThumbstickDown)
{
ScrollToVerticalOffset(_scrollViewer.VerticalOffset + VerticalScrollStepSize);
}
base.OnKeyUp(e);
}
#endregion
#region Support Methods
private void UnwireScrollWheel(DependencyObject control)
{
var scrollContentPresenter = ControlUtilities.RecursiveFindByType(control, typeof(ScrollContentPresenter)) as ScrollContentPresenter;
if (scrollContentPresenter != null) scrollContentPresenter.PointerWheelChanged -= ScrollContentPresenterOnPointerWheelChanged;
var horizontalScrollbar = ControlUtilities.RecursiveFindByType(control, typeof(ScrollBar), "HorizontalScrollBar") as ScrollBar;
if (horizontalScrollbar != null) horizontalScrollbar.PointerWheelChanged -= ScrollContentPresenterOnPointerWheelChanged;
}
private void WireUpScrollWheel(DependencyObject control)
{
var scrollContentPresenter = ControlUtilities.RecursiveFindByType(control, typeof(ScrollContentPresenter)) as ScrollContentPresenter;
if (scrollContentPresenter != null)
{
scrollContentPresenter.PointerWheelChanged += ScrollContentPresenterOnPointerWheelChanged;
}
var horizontalScrollbar = ControlUtilities.RecursiveFindByType(control, typeof(ScrollBar), "HorizontalScrollBar") as ScrollBar;
if (horizontalScrollbar != null)
{
horizontalScrollbar.PointerWheelChanged += ScrollContentPresenterOnPointerWheelChanged;
}
}
#endregion
}
}