Repository navigation
Expand file tree
/
Copy pathScrollViewerExtensions.cs
More file actions
executable file
·535 lines (503 loc) · 26.9 KB
/
Copy pathScrollViewerExtensions.cs
File metadata and controls
executable file
·535 lines (503 loc) · 26.9 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
//
// This is the source code of Telegram for Windows Phone v. 3.x.x.
// It is licensed under GNU GPL v. 2 or later.
// You should have received a copy of the license in this archive (see LICENSE).
//
// Copyright Evgeny Nadymov, 2013-present.
//
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Animation;
namespace Telegram.Controls.Extensions
{
/// <summary>
/// Provides useful extensions to ScrollViewer instances.
///
/// </summary>
/// <QualityBand>Experimental</QualityBand>
public static class ScrollViewerExtensions
{
/// <summary>
/// The amount to scroll a ScrollViewer for a line change.
///
/// </summary>
private const double LineChange = 16.0;
/// <summary>
/// Identifies the IsMouseWheelScrollingEnabled dependency property.
///
/// </summary>
public static readonly DependencyProperty IsMouseWheelScrollingEnabledProperty;
/// <summary>
/// Identifies the VerticalOffset dependency property.
///
/// </summary>
private static readonly DependencyProperty VerticalOffsetProperty;
/// <summary>
/// Identifies the HorizontalOffset dependency property.
///
/// </summary>
private static readonly DependencyProperty HorizontalOffsetProperty;
static ScrollViewerExtensions()
{
// ISSUE: method pointer
ScrollViewerExtensions.IsMouseWheelScrollingEnabledProperty = DependencyProperty.RegisterAttached("IsMouseWheelScrollingEnabled", typeof(bool), typeof(ScrollViewerExtensions), new PropertyMetadata((object)false, new PropertyChangedCallback(OnIsMouseWheelScrollingEnabledPropertyChanged)));
// ISSUE: method pointer
ScrollViewerExtensions.VerticalOffsetProperty = DependencyProperty.RegisterAttached("VerticalOffset", typeof(double), typeof(ScrollViewerExtensions), new PropertyMetadata(new PropertyChangedCallback(OnVerticalOffsetPropertyChanged)));
// ISSUE: method pointer
ScrollViewerExtensions.HorizontalOffsetProperty = DependencyProperty.RegisterAttached("HorizontalOffset", typeof(double), typeof(ScrollViewerExtensions), new PropertyMetadata(new PropertyChangedCallback(OnHorizontalOffsetPropertyChanged)));
}
/// <summary>
/// Gets a value indicating whether the ScrollViewer has enabled
/// scrolling via the mouse wheel.
///
/// </summary>
/// <param name="viewer">The ScrollViewer.</param>
/// <returns>
/// A value indicating whether the ScrollViewer has enabled scrolling
/// via the mouse wheel.
///
/// </returns>
public static bool GetIsMouseWheelScrollingEnabled(this ScrollViewer viewer)
{
if (viewer == null)
throw new ArgumentNullException("viewer");
else
return (bool)viewer.GetValue(ScrollViewerExtensions.IsMouseWheelScrollingEnabledProperty);
}
/// <summary>
/// Sets a value indicating whether the ScrollViewer will enable
/// scrolling via the mouse wheel.
///
/// </summary>
/// <param name="viewer">The ScrollViewer.</param><param name="value">A value indicating whether the ScrollViewer will enable scrolling
/// via the mouse wheel.
/// </param>
public static void SetIsMouseWheelScrollingEnabled(this ScrollViewer viewer, bool value)
{
if (viewer == null)
throw new ArgumentNullException("viewer");
viewer.SetValue(ScrollViewerExtensions.IsMouseWheelScrollingEnabledProperty, (value ? 1 : 0));
}
/// <summary>
/// IsMouseWheelScrollingEnabledProperty property changed handler.
///
/// </summary>
/// <param name="d">ScrollViewerExtensions that changed its IsMouseWheelScrollingEnabled.</param><param name="e">Event arguments.</param>
private static void OnIsMouseWheelScrollingEnabledPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ScrollViewer scrollViewer = d as ScrollViewer;
if ((bool)e.NewValue)
{
// ISSUE: method pointer
scrollViewer.MouseWheel += new MouseWheelEventHandler(OnMouseWheel);
}
else
{
// ISSUE: method pointer
scrollViewer.MouseWheel -= new MouseWheelEventHandler(OnMouseWheel);
}
}
/// <summary>
/// Handles the mouse wheel event.
///
/// </summary>
/// <param name="sender">The ScrollViewer.</param><param name="e">Event arguments.</param>
private static void OnMouseWheel(object sender, MouseWheelEventArgs e)
{
ScrollViewer viewer = sender as ScrollViewer;
Debug.Assert(viewer != null, "sender should be a non-null ScrollViewer!");
Debug.Assert(e != null, "e should not be null!");
if (e.Handled)
return;
double offset = ScrollViewerExtensions.CoerceVerticalOffset(viewer, viewer.VerticalOffset - (double)e.Delta);
viewer.ScrollToVerticalOffset(offset);
e.Handled = true;
}
/// <summary>
/// Gets the value of the VerticalOffset attached property for a specified ScrollViewer.
///
/// </summary>
/// <param name="element">The ScrollViewer from which the property value is read.</param>
/// <returns>
/// The VerticalOffset property value for the ScrollViewer.
/// </returns>
private static double GetVerticalOffset(ScrollViewer element)
{
if (element == null)
throw new ArgumentNullException("element");
else
return (double)element.GetValue(ScrollViewerExtensions.VerticalOffsetProperty);
}
/// <summary>
/// Sets the value of the VerticalOffset attached property to a specified ScrollViewer.
///
/// </summary>
/// <param name="element">The ScrollViewer to which the attached property is written.</param><param name="value">The needed VerticalOffset value.</param>
private static void SetVerticalOffset(ScrollViewer element, double value)
{
if (element == null)
throw new ArgumentNullException("element");
element.SetValue(ScrollViewerExtensions.VerticalOffsetProperty, (object)value);
}
/// <summary>
/// VerticalOffsetProperty property changed handler.
///
/// </summary>
/// <param name="dependencyObject">ScrollViewer that changed its VerticalOffset.</param><param name="eventArgs">Event arguments.</param>
private static void OnVerticalOffsetPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs eventArgs)
{
ScrollViewer scrollViewer = dependencyObject as ScrollViewer;
if (scrollViewer == null)
throw new ArgumentNullException("dependencyObject");
scrollViewer.ScrollToVerticalOffset((double)eventArgs.NewValue);
}
/// <summary>
/// Gets the value of the HorizontalOffset attached property for a specified ScrollViewer.
///
/// </summary>
/// <param name="element">The ScrollViewer from which the property value is read.</param>
/// <returns>
/// The HorizontalOffset property value for the ScrollViewer.
/// </returns>
private static double GetHorizontalOffset(ScrollViewer element)
{
if (element == null)
throw new ArgumentNullException("element");
else
return (double)element.GetValue(ScrollViewerExtensions.HorizontalOffsetProperty);
}
/// <summary>
/// Sets the value of the HorizontalOffset attached property to a specified ScrollViewer.
///
/// </summary>
/// <param name="element">The ScrollViewer to which the attached property is written.</param><param name="value">The needed HorizontalOffset value.</param>
private static void SetHorizontalOffset(ScrollViewer element, double value)
{
if (element == null)
throw new ArgumentNullException("element");
element.SetValue(ScrollViewerExtensions.HorizontalOffsetProperty, (object)value);
}
/// <summary>
/// HorizontalOffsetProperty property changed handler.
///
/// </summary>
/// <param name="dependencyObject">ScrollViewer that changed its HorizontalOffset.</param><param name="eventArgs">Event arguments.</param>
private static void OnHorizontalOffsetPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs eventArgs)
{
ScrollViewer scrollViewer = dependencyObject as ScrollViewer;
if (scrollViewer == null)
throw new ArgumentNullException("dependencyObject");
scrollViewer.ScrollToHorizontalOffset((double)eventArgs.NewValue);
}
/// <summary>
/// Coerce a vertical offset to fall within the vertical bounds of a
/// ScrollViewer.
///
/// </summary>
/// <param name="viewer">The ScrollViewer.</param><param name="offset">The vertical offset to coerce.</param>
/// <returns>
/// The coerced vertical offset that falls within the ScrollViewer's
/// vertical bounds.
///
/// </returns>
private static double CoerceVerticalOffset(ScrollViewer viewer, double offset)
{
Debug.Assert(viewer != null, "viewer should not be null!");
return Math.Max(Math.Min(offset, viewer.ExtentHeight), 0.0);
}
/// <summary>
/// Coerce a horizontal offset to fall within the horizontal bounds of a
/// ScrollViewer.
///
/// </summary>
/// <param name="viewer">The ScrollViewer.</param><param name="offset">The horizontal offset to coerce.</param>
/// <returns>
/// The coerced horizontal offset that falls within the ScrollViewer's
/// horizontal bounds.
///
/// </returns>
private static double CoerceHorizontalOffset(ScrollViewer viewer, double offset)
{
Debug.Assert(viewer != null, "viewer should not be null!");
return Math.Max(Math.Min(offset, viewer.ExtentWidth), 0.0);
}
/// <summary>
/// Scroll a ScrollViewer vertically by a given offset.
///
/// </summary>
/// <param name="viewer">The ScrollViewer.</param><param name="offset">The vertical offset to scroll.</param>
private static void ScrollByVerticalOffset(ScrollViewer viewer, double offset)
{
Debug.Assert(viewer != null, "viewer should not be null!");
offset += viewer.VerticalOffset;
offset = ScrollViewerExtensions.CoerceVerticalOffset(viewer, offset);
viewer.ScrollToVerticalOffset(offset);
}
/// <summary>
/// Scroll a ScrollViewer horizontally by a given offset.
///
/// </summary>
/// <param name="viewer">The ScrollViewer.</param><param name="offset">The horizontal offset to scroll.</param>
private static void ScrollByHorizontalOffset(ScrollViewer viewer, double offset)
{
Debug.Assert(viewer != null, "viewer should not be null!");
offset += viewer.HorizontalOffset;
offset = ScrollViewerExtensions.CoerceHorizontalOffset(viewer, offset);
viewer.ScrollToHorizontalOffset(offset);
}
/// <summary>
/// Scroll the ScrollViewer up by a line.
///
/// </summary>
/// <param name="viewer">The ScrollViewer.</param><exception cref="T:System.ArgumentNullException"><paramref name="viewer"/> is null.
/// </exception>
public static void LineUp(this ScrollViewer viewer)
{
if (viewer == null)
throw new ArgumentNullException("viewer");
ScrollViewerExtensions.ScrollByVerticalOffset(viewer, -16.0);
}
/// <summary>
/// Scroll the ScrollViewer down by a line.
///
/// </summary>
/// <param name="viewer">The ScrollViewer.</param><exception cref="T:System.ArgumentNullException"><paramref name="viewer"/> is null.
/// </exception>
public static void LineDown(this ScrollViewer viewer)
{
if (viewer == null)
throw new ArgumentNullException("viewer");
ScrollViewerExtensions.ScrollByVerticalOffset(viewer, 16.0);
}
/// <summary>
/// Scroll the ScrollViewer left by a line.
///
/// </summary>
/// <param name="viewer">The ScrollViewer.</param><exception cref="T:System.ArgumentNullException"><paramref name="viewer"/> is null.
/// </exception>
public static void LineLeft(this ScrollViewer viewer)
{
if (viewer == null)
throw new ArgumentNullException("viewer");
ScrollViewerExtensions.ScrollByHorizontalOffset(viewer, -16.0);
}
/// <summary>
/// Scroll the ScrollViewer right by a line.
///
/// </summary>
/// <param name="viewer">The ScrollViewer.</param><exception cref="T:System.ArgumentNullException"><paramref name="viewer"/> is null.
/// </exception>
public static void LineRight(this ScrollViewer viewer)
{
if (viewer == null)
throw new ArgumentNullException("viewer");
ScrollViewerExtensions.ScrollByHorizontalOffset(viewer, 16.0);
}
/// <summary>
/// Scroll the ScrollViewer up by a page.
///
/// </summary>
/// <param name="viewer">The ScrollViewer.</param><exception cref="T:System.ArgumentNullException"><paramref name="viewer"/> is null.
/// </exception>
public static void PageUp(this ScrollViewer viewer)
{
if (viewer == null)
throw new ArgumentNullException("viewer");
ScrollViewerExtensions.ScrollByVerticalOffset(viewer, -viewer.ViewportHeight);
}
/// <summary>
/// Scroll the ScrollViewer down by a page.
///
/// </summary>
/// <param name="viewer">The ScrollViewer.</param><exception cref="T:System.ArgumentNullException"><paramref name="viewer"/> is null.
/// </exception>
public static void PageDown(this ScrollViewer viewer)
{
if (viewer == null)
throw new ArgumentNullException("viewer");
ScrollViewerExtensions.ScrollByVerticalOffset(viewer, viewer.ViewportHeight);
}
/// <summary>
/// Scroll the ScrollViewer left by a page.
///
/// </summary>
/// <param name="viewer">The ScrollViewer.</param><exception cref="T:System.ArgumentNullException"><paramref name="viewer"/> is null.
/// </exception>
public static void PageLeft(this ScrollViewer viewer)
{
if (viewer == null)
throw new ArgumentNullException("viewer");
ScrollViewerExtensions.ScrollByHorizontalOffset(viewer, -viewer.ViewportWidth);
}
/// <summary>
/// Scroll the ScrollViewer right by a page.
///
/// </summary>
/// <param name="viewer">The ScrollViewer.</param><exception cref="T:System.ArgumentNullException"><paramref name="viewer"/> is null.
/// </exception>
public static void PageRight(this ScrollViewer viewer)
{
if (viewer == null)
throw new ArgumentNullException("viewer");
ScrollViewerExtensions.ScrollByHorizontalOffset(viewer, viewer.ViewportWidth);
}
/// <summary>
/// Scroll the ScrollViewer to the top.
///
/// </summary>
/// <param name="viewer">The ScrollViewer.</param><exception cref="T:System.ArgumentNullException"><paramref name="viewer"/> is null.
/// </exception>
public static void ScrollToTop(this ScrollViewer viewer)
{
if (viewer == null)
throw new ArgumentNullException("viewer");
viewer.ScrollToVerticalOffset(0.0);
}
/// <summary>
/// Scroll the ScrollViewer to the bottom.
///
/// </summary>
/// <param name="viewer">The ScrollViewer.</param><exception cref="T:System.ArgumentNullException"><paramref name="viewer"/> is null.
/// </exception>
public static void ScrollToBottom(this ScrollViewer viewer)
{
if (viewer == null)
throw new ArgumentNullException("viewer");
viewer.ScrollToVerticalOffset(viewer.ExtentHeight);
}
/// <summary>
/// Scroll the ScrollViewer to the left.
///
/// </summary>
/// <param name="viewer">The ScrollViewer.</param><exception cref="T:System.ArgumentNullException"><paramref name="viewer"/> is null.
/// </exception>
public static void ScrollToLeft(this ScrollViewer viewer)
{
if (viewer == null)
throw new ArgumentNullException("viewer");
viewer.ScrollToHorizontalOffset(0.0);
}
/// <summary>
/// Scroll the ScrollViewer to the right.
///
/// </summary>
/// <param name="viewer">The ScrollViewer.</param><exception cref="T:System.ArgumentNullException"><paramref name="viewer"/> is null.
/// </exception>
public static void ScrollToRight(this ScrollViewer viewer)
{
if (viewer == null)
throw new ArgumentNullException("viewer");
viewer.ScrollToHorizontalOffset(viewer.ExtentWidth);
}
/// <summary>
/// Scroll the desired element into the ScrollViewer's viewport.
///
/// </summary>
/// <param name="viewer">The ScrollViewer.</param><param name="element">The element to scroll into view.</param><exception cref="T:System.ArgumentNullException"><paramref name="viewer"/> is null.
/// </exception><exception cref="T:System.ArgumentNullException"><paramref name="element"/> is null.
/// </exception>
public static void ScrollIntoView(this ScrollViewer viewer, FrameworkElement element)
{
if (viewer == null)
throw new ArgumentNullException("viewer");
if (element == null)
throw new ArgumentNullException("element");
ScrollViewerExtensions.ScrollIntoView(viewer, element, 0.0, 112.0, TimeSpan.FromSeconds(0.25));
}
public static void ScrollToBeginnig(this ScrollViewer viewer, Duration duration)
{
if (viewer == null)
throw new ArgumentNullException("viewer");
Storyboard storyboard = new Storyboard();
ScrollViewerExtensions.SetVerticalOffset(viewer, viewer.VerticalOffset);
ScrollViewerExtensions.SetHorizontalOffset(viewer, viewer.HorizontalOffset);
DoubleAnimation doubleAnimation1 = new DoubleAnimation();
doubleAnimation1.To = new double?(0.0);
doubleAnimation1.Duration = duration;
DoubleAnimation doubleAnimation2 = doubleAnimation1;
DoubleAnimation doubleAnimation3 = new DoubleAnimation();
doubleAnimation3.To = new double?(0.0);
doubleAnimation3.Duration = duration;
DoubleAnimation doubleAnimation4 = doubleAnimation3;
Storyboard.SetTarget((Timeline)doubleAnimation2, (DependencyObject)viewer);
Storyboard.SetTarget((Timeline)doubleAnimation4, (DependencyObject)viewer);
Storyboard.SetTargetProperty((Timeline)doubleAnimation4, new PropertyPath((object)ScrollViewerExtensions.HorizontalOffsetProperty));
Storyboard.SetTargetProperty((Timeline)doubleAnimation2, new PropertyPath((object)ScrollViewerExtensions.VerticalOffsetProperty));
storyboard.Children.Add((Timeline)doubleAnimation2);
storyboard.Children.Add((Timeline)doubleAnimation4);
storyboard.Begin();
}
/// <summary>
/// Scroll the desired element into the ScrollViewer's viewport.
///
/// </summary>
/// <param name="viewer">The ScrollViewer.</param><param name="element">The element to scroll into view.</param><param name="horizontalMargin">The margin to add on the left or right.
/// </param><param name="verticalMargin">The margin to add on the top or bottom.
/// </param><param name="duration">The duration of the animation.</param><exception cref="T:System.ArgumentNullException"><paramref name="viewer"/> is null.
/// </exception><exception cref="T:System.ArgumentNullException"><paramref name="element"/> is null.
/// </exception>
public static void ScrollIntoView(this ScrollViewer viewer, FrameworkElement element, double horizontalMargin, double verticalMargin, Duration duration)
{
if (viewer == null)
throw new ArgumentNullException("viewer");
if (element == null)
throw new ArgumentNullException("element");
Rect? boundsRelativeTo = VisualTreeExtensions.GetBoundsRelativeTo(element, (UIElement)viewer);
if (!boundsRelativeTo.HasValue)
return;
double verticalOffset = viewer.VerticalOffset;
double num1 = 0.0;
double viewportHeight = viewer.ViewportHeight;
double num2 = boundsRelativeTo.Value.Bottom + verticalMargin;
if (viewportHeight < num2)
{
num1 = num2 - viewportHeight;
verticalOffset += num1;
}
double num3 = boundsRelativeTo.Value.Top - verticalMargin;
if (num3 - num1 < 0.0)
verticalOffset -= num1 - num3;
double horizontalOffset = viewer.HorizontalOffset;
double num4 = 0.0;
double viewportWidth = viewer.ViewportWidth;
double num5 = boundsRelativeTo.Value.Right + horizontalMargin;
if (viewportWidth < num5)
{
num4 = num5 - viewportWidth;
horizontalOffset += num4;
}
double num6 = boundsRelativeTo.Value.Left - horizontalMargin;
if (num6 - num4 < 0.0)
horizontalOffset -= num4 - num6;
if (duration == (Duration)TimeSpan.Zero)
{
viewer.ScrollToVerticalOffset(verticalOffset);
viewer.ScrollToHorizontalOffset(horizontalOffset);
}
else
{
Storyboard storyboard = new Storyboard();
ScrollViewerExtensions.SetVerticalOffset(viewer, viewer.VerticalOffset);
ScrollViewerExtensions.SetHorizontalOffset(viewer, viewer.HorizontalOffset);
DoubleAnimation doubleAnimation1 = new DoubleAnimation();
doubleAnimation1.To = new double?(verticalOffset);
doubleAnimation1.Duration = duration;
DoubleAnimation doubleAnimation2 = doubleAnimation1;
DoubleAnimation doubleAnimation3 = new DoubleAnimation();
doubleAnimation3.To = new double?(verticalOffset);
doubleAnimation3.Duration = duration;
DoubleAnimation doubleAnimation4 = doubleAnimation3;
Storyboard.SetTarget((Timeline)doubleAnimation2, (DependencyObject)viewer);
Storyboard.SetTarget((Timeline)doubleAnimation4, (DependencyObject)viewer);
Storyboard.SetTargetProperty((Timeline)doubleAnimation4, new PropertyPath((object)ScrollViewerExtensions.HorizontalOffsetProperty));
Storyboard.SetTargetProperty((Timeline)doubleAnimation2, new PropertyPath((object)ScrollViewerExtensions.VerticalOffsetProperty));
storyboard.Children.Add((Timeline)doubleAnimation2);
storyboard.Children.Add((Timeline)doubleAnimation4);
storyboard.Begin();
}
}
}
}