Repository navigation
Expand file tree
/
Copy pathQuickReturnHeader.cs
More file actions
274 lines (232 loc) · 9.64 KB
/
Copy pathQuickReturnHeader.cs
File metadata and controls
274 lines (232 loc) · 9.64 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
// ******************************************************************
// Copyright (c) Patrick Dehne. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THE CODE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
// THE CODE OR THE USE OR OTHER DEALINGS IN THE CODE.
// ******************************************************************
using System;
using Windows.Foundation.Metadata;
using Windows.UI.Composition;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Hosting;
using Windows.UI.Xaml.Media;
namespace QuickReturnHeaderListView
{
/// <summary>
/// Quick return header control to be used with ListViews
/// </summary>
public class QuickReturnHeader : ContentControl
{
private ScrollViewer _scrollViewer;
private double _previousVerticalScrollOffset;
private CompositionPropertySet _scrollProperties;
private CompositionPropertySet _animationProperties;
private Visual _headerVisual;
public QuickReturnHeader()
{
HorizontalContentAlignment = HorizontalAlignment.Stretch;
}
/// <summary>
/// Identifies the <see cref="IsQuickReturnEnabled"/> property.
/// </summary>
public static readonly DependencyProperty IsQuickReturnEnabledProperty =
DependencyProperty.Register(nameof(IsQuickReturnEnabled), typeof(bool), typeof(QuickReturnHeader), new PropertyMetadata(true, OnIsQuickReturnEnabledChanged));
/// <summary>
/// Gets or sets a value indicating whether the quick return header is enabled.
/// If true the quick return behavior is used.
/// If false regular header behavior is used.
/// Default is true.
/// </summary>
public bool IsQuickReturnEnabled
{
get { return (bool)GetValue(IsQuickReturnEnabledProperty); }
set { SetValue(IsQuickReturnEnabledProperty, value); }
}
/// <summary>
/// Identifies the <see cref="IsSticky"/> property.
/// </summary>
public static readonly DependencyProperty IsStickyProperty =
DependencyProperty.Register(nameof(IsSticky), typeof(bool), typeof(QuickReturnHeader), new PropertyMetadata(false, OnIsStickyChanged));
/// <summary>
/// Identifies the <see cref="TargetListViewBase"/> property.
/// </summary>
public static readonly DependencyProperty TargetListViewBaseProperty =
DependencyProperty.Register(nameof(TargetListViewBase), typeof(ListViewBase), typeof(QuickReturnHeader), new PropertyMetadata(null));
/// <summary>
/// Gets or sets a value indicating whether the quick return header should always be visible.
/// If true the header is always visible.
/// If false the header will move out of view when scrolling down.
/// Default is false.
/// </summary>
public bool IsSticky
{
get { return (bool)GetValue(IsStickyProperty); }
set { SetValue(IsStickyProperty, value); }
}
/// <summary>
/// Gets or sets the container this header belongs to
/// </summary>
public ListView TargetListViewBase
{
get { return (ListView)GetValue(TargetListViewBaseProperty); }
set { SetValue(TargetListViewBaseProperty, value); }
}
/// <summary>
/// Show the header
/// </summary>
public void Show()
{
if (_headerVisual != null && _scrollViewer != null)
{
_previousVerticalScrollOffset = _scrollViewer.VerticalOffset;
_animationProperties.InsertScalar("OffsetY", 0.0f);
}
}
protected override void OnApplyTemplate()
{
SizeChanged -= QuickReturnHeader_SizeChanged;
SizeChanged += QuickReturnHeader_SizeChanged;
if (TargetListViewBase != null)
{
_scrollViewer = GetScrollViewer(TargetListViewBase);
// Place items below header
var panel = TargetListViewBase.ItemsPanelRoot;
Canvas.SetZIndex(panel, -1);
}
if (_scrollViewer != null)
{
_scrollViewer.ViewChanged -= ScrollViewer_ViewChanged;
_scrollViewer.ViewChanged += ScrollViewer_ViewChanged;
}
if (IsQuickReturnEnabled)
{
StartAnimation();
}
}
private static ScrollViewer GetScrollViewer(DependencyObject o)
{
if (o is ScrollViewer)
{
return o as ScrollViewer;
}
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(o); i++)
{
var child = VisualTreeHelper.GetChild(o, i);
var result = GetScrollViewer(child);
if (result != null)
{
return result;
}
}
return null;
}
private static void OnIsQuickReturnEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var me = d as QuickReturnHeader;
if (me.TargetListViewBase != null)
{
if (me.IsQuickReturnEnabled)
{
me.StartAnimation();
}
else
{
me.StopAnimation();
}
}
}
private static void OnIsStickyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var me = d as QuickReturnHeader;
if (me.TargetListViewBase != null)
{
me.StopAnimation();
if (me.IsQuickReturnEnabled)
{
me.StartAnimation();
}
}
}
private void QuickReturnHeader_SizeChanged(object sender, SizeChangedEventArgs e)
{
if (TargetListViewBase != null)
{
StopAnimation();
if (IsQuickReturnEnabled)
{
StartAnimation();
}
}
}
private void ScrollViewer_ViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
{
if (_animationProperties != null)
{
if (!IsSticky)
{
float oldOffsetY = 0.0f;
_animationProperties.TryGetScalar("OffsetY", out oldOffsetY);
var delta = _scrollViewer.VerticalOffset - _previousVerticalScrollOffset;
_previousVerticalScrollOffset = _scrollViewer.VerticalOffset;
var newOffsetY = oldOffsetY - (float)delta;
// Keep values within negativ header size and 0
FrameworkElement header = (FrameworkElement)TargetListViewBase.Header;
newOffsetY = Math.Max((float)-header.ActualHeight, newOffsetY);
newOffsetY = Math.Min(0, newOffsetY);
if (oldOffsetY != newOffsetY)
{
_animationProperties.InsertScalar("OffsetY", newOffsetY);
}
}
}
}
private void StartAnimation()
{
// Windows.UI.Xaml.Hosting.ElementCompositionPreview is only available in Windows 10 10586 or later
if (!ApiInformation.IsMethodPresent("Windows.UI.Xaml.Hosting.ElementCompositionPreview", nameof(ElementCompositionPreview.GetScrollViewerManipulationPropertySet)))
{
return;
}
if (_scrollViewer == null)
{
return;
}
if (_scrollProperties == null)
{
_scrollProperties = ElementCompositionPreview.GetScrollViewerManipulationPropertySet(_scrollViewer);
}
var compositor = _scrollProperties.Compositor;
if (_animationProperties == null)
{
_animationProperties = compositor.CreatePropertySet();
}
_previousVerticalScrollOffset = _scrollViewer.VerticalOffset;
_headerVisual = ElementCompositionPreview.GetElementVisual((UIElement)TargetListViewBase.Header);
_animationProperties.InsertScalar("OffsetY", 0.0f);
ExpressionAnimation expressionAnimation = compositor.CreateExpressionAnimation($"Round(max(animationProperties.OffsetY - ScrollingProperties.Translation.Y, 0))");
expressionAnimation.SetReferenceParameter("ScrollingProperties", _scrollProperties);
expressionAnimation.SetReferenceParameter("animationProperties", _animationProperties);
if (_headerVisual != null && IsQuickReturnEnabled)
{
_headerVisual.StartAnimation("Offset.Y", expressionAnimation);
}
}
private void StopAnimation()
{
if (_headerVisual != null)
{
_headerVisual.StopAnimation("Offset.Y");
_animationProperties.InsertScalar("OffsetY", 0.0f);
var offset = _headerVisual.Offset;
offset.Y = 0.0f;
_headerVisual.Offset = offset;
}
}
}
}