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
|
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using Tango.Core.EventArguments;
using Tango.DragAndDrop;
namespace Tango.Touch.Controls
{
public class LightTouchScrollViewer : ContentControl
{
private Border _border_viewport;
private Grid _grid_content;
private Point _mouse_down_location;
private List<double> _last_manipulation_deltas;
private const double touch_inertia_coefficiant = 10;
private const double bounce_offset_max = 100;
private Thumb _thumb;
private Border _border_thumb;
private TouchDevice _moveTouchDevice;
#region Properties
/// <summary>
/// Gets or sets the scroll bar margins.
/// </summary>
public Thickness ScrollBarMargin
{
get { return (Thickness)GetValue(ScrollBarMarginProperty); }
set { SetValue(ScrollBarMarginProperty, value); }
}
public static readonly DependencyProperty ScrollBarMarginProperty =
DependencyProperty.Register("ScrollBarMargin", typeof(Thickness), typeof(LightTouchScrollViewer), new PropertyMetadata(new Thickness()));
/// <summary>
/// Gets or sets a value indicating whether the data grid is currently scrolling.
/// </summary>
public bool IsScrolling
{
get { return (bool)GetValue(IsScrollingProperty); }
set { SetValue(IsScrollingProperty, value); }
}
public static readonly DependencyProperty IsScrollingProperty =
DependencyProperty.Register("IsScrolling", typeof(bool), typeof(LightTouchScrollViewer), new PropertyMetadata(false));
/// <summary>
/// Gets or sets a value indicating whether this instance is mouse touch down.
/// </summary>
public bool IsMouseTouchDown
{
get { return (bool)GetValue(IsMouseTouchDownProperty); }
set { SetValue(IsMouseTouchDownProperty, value); }
}
public static readonly DependencyProperty IsMouseTouchDownProperty =
DependencyProperty.Register("IsMouseTouchDown", typeof(bool), typeof(LightTouchScrollViewer), new PropertyMetadata(false));
/// <summary>
/// Gets or sets the width of the scroll bar.
/// </summary>
/// <value>
/// The width of the scroll bar.
/// </value>
public double ScrollBarWidth
{
get { return (double)GetValue(ScrollBarWidthProperty); }
set { SetValue(ScrollBarWidthProperty, value); }
}
public static readonly DependencyProperty ScrollBarWidthProperty =
DependencyProperty.Register("ScrollBarWidth", typeof(double), typeof(LightTouchScrollViewer), new PropertyMetadata(5.0));
/// <summary>
/// Gets or sets the scroll bar background.
/// </summary>
public Brush ScrollBarBackground
{
get { return (Brush)GetValue(ScrollBarBackgroundProperty); }
set { SetValue(ScrollBarBackgroundProperty, value); }
}
public static readonly DependencyProperty ScrollBarBackgroundProperty =
DependencyProperty.Register("ScrollBarBackground", typeof(Brush), typeof(LightTouchScrollViewer), new PropertyMetadata(null));
/// <summary>
/// Gets or sets the scroll bar foreground.
/// </summary>
public Brush ScrollBarForeground
{
get { return (Brush)GetValue(ScrollBarForegroundProperty); }
set { SetValue(ScrollBarForegroundProperty, value); }
}
public static readonly DependencyProperty ScrollBarForegroundProperty =
DependencyProperty.Register("ScrollBarForeground", typeof(Brush), typeof(LightTouchScrollViewer), new PropertyMetadata(null));
/// <summary>
/// Gets or sets the scroll bar corner radius.
/// </summary>
public CornerRadius ScrollBarCornerRadius
{
get { return (CornerRadius)GetValue(ScrollBarCornerRadiusProperty); }
set { SetValue(ScrollBarCornerRadiusProperty, value); }
}
public static readonly DependencyProperty ScrollBarCornerRadiusProperty =
DependencyProperty.Register("ScrollBarCornerRadius", typeof(CornerRadius), typeof(LightTouchScrollViewer), new PropertyMetadata(default(CornerRadius)));
/// <summary>
/// Gets or sets the size of the top arc.
/// </summary>
public Size TopArcSize
{
get { return (Size)GetValue(TopArcSizeProperty); }
set { SetValue(TopArcSizeProperty, value); }
}
public static readonly DependencyProperty TopArcSizeProperty =
DependencyProperty.Register("TopArcSize", typeof(Size), typeof(LightTouchScrollViewer), new PropertyMetadata(default(Size)));
/// <summary>
/// Gets or sets the size of the bottom arc.
/// </summary>
public Size BottomArcSize
{
get { return (Size)GetValue(BottomArcSizeProperty); }
set { SetValue(BottomArcSizeProperty, value); }
}
public static readonly DependencyProperty BottomArcSizeProperty =
DependencyProperty.Register("BottomArcSize", typeof(Size), typeof(LightTouchScrollViewer), new PropertyMetadata(default(Size)));
#endregion
#region Constructors
public LightTouchScrollViewer()
{
_last_manipulation_deltas = new List<double>();
}
static LightTouchScrollViewer()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(LightTouchScrollViewer), new FrameworkPropertyMetadata(typeof(LightTouchScrollViewer)));
}
#endregion
#region Apply Template
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
_border_viewport = GetTemplateChild("PART_Border") as Border;
_grid_content = GetTemplateChild("PART_Grid_Content") as Grid;
_thumb = GetTemplateChild("PART_Thumb") as Thumb;
_border_thumb = GetTemplateChild("PART_Thumb_Border") as Border;
ContentPresenter presenter = GetTemplateChild("PART_Content_Presenter") as ContentPresenter;
_border_viewport.RegisterForMouseOrTouchDown(OnMouseTouchDown);
_border_viewport.RegisterForMouseOrTouchMove(OnMouseTouchMove);
_border_viewport.RegisterForPreviewMouseOrTouchUp(OnMouseTouchUp);
_border_viewport.ManipulationDelta += _grid_rows_ManipulationDelta;
_border_viewport.ManipulationCompleted += _grid_rows_ManipulationCompleted;
_thumb.DragDelta += _thumb_DragDelta;
_thumb.DragStarted += _thumb_DragStarted;
_thumb.DragCompleted += _thumb_DragCompleted;
}
private void _thumb_DragCompleted(object sender, DragCompletedEventArgs e)
{
IsScrolling = false;
}
private void _thumb_DragStarted(object sender, DragStartedEventArgs e)
{
IsScrolling = true;
}
private void _thumb_DragDelta(object sender, DragDeltaEventArgs e)
{
var step = CalculateScrollbarThumbStep();
double content_margin = _grid_content.Margin.Top + -(step * e.VerticalChange);
_grid_content.BeginAnimation(Grid.MarginProperty, null);
Canvas.SetTop(_border_thumb, Canvas.GetTop(_border_thumb) + e.VerticalChange);
_grid_content.Margin = new Thickness(0, content_margin, 0, 0);
if (content_margin > 0)
{
Canvas.SetTop(_border_thumb, 0);
_grid_content.Margin = new Thickness(0, 0, 0, 0);
}
else if (content_margin - _border_viewport.ActualHeight < -_grid_content.ActualHeight)
{
Canvas.SetTop(_border_thumb, _border_viewport.ActualHeight - _border_thumb.ActualHeight);
_grid_content.Margin = new Thickness(0, -(_grid_content.ActualHeight - _border_viewport.ActualHeight), 0, 0);
}
}
#endregion
#region Touch Manipulation Handlers
/// <summary>
/// Handles the ManipulationCompleted event of the _grid_rows control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="ManipulationCompletedEventArgs"/> instance containing the event data.</param>
private void _grid_rows_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
{
_border_viewport.IsManipulationEnabled = false;
ReleaseMouseTouchCapture();
e.Cancel();
if (_last_manipulation_deltas.Count == 0) return;
var last_manipulation_delta = _last_manipulation_deltas.Last();
if (last_manipulation_delta == 0 && _last_manipulation_deltas.Count > 1)
{
last_manipulation_delta = _last_manipulation_deltas[_last_manipulation_deltas.Count - 2];
}
if (last_manipulation_delta != 0)
{
var to = (_grid_content.Margin.Top + (last_manipulation_delta * touch_inertia_coefficiant));
if (to > 0)
{
to = Math.Min(to, bounce_offset_max);
}
else
{
to = Math.Max(to, bounce_offset_max + -(_grid_content.ActualHeight - _border_viewport.ActualHeight));
}
bool bounced = false;
ThicknessAnimation ani = new ThicknessAnimation();
ani.Duration = TimeSpan.FromSeconds(1);
ani.To = new Thickness(0, to, 0, 0);
ani.CurrentTimeInvalidated += (_, __) =>
{
if (!bounced)
{
if (_grid_content.Margin.Top > 0 || (_grid_content.Margin.Top - _border_viewport.ActualHeight < -_grid_content.ActualHeight))
{
bounced = true;
SnapContentToBounds();
}
}
SetThumbPosition();
};
ani.DecelerationRatio = 1.0;
_grid_content.BeginAnimation(Grid.MarginProperty, ani);
_last_manipulation_deltas.Clear();
}
}
/// <summary>
/// Handles the ManipulationDelta event of the _grid_rows control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="ManipulationDeltaEventArgs"/> instance containing the event data.</param>
private void _grid_rows_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
_last_manipulation_deltas.Add(e.DeltaManipulation.Translation.Y);
}
#endregion
#region Touch / Mouse Handlers
/// <summary>
/// Called when the mouse touch has been down
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="MouseOrTouchEventArgs"/> instance containing the event data.</param>
protected virtual void OnMouseTouchDown(object sender, MouseOrTouchEventArgs e)
{
if (e.OriginalSource.GetType() == typeof(DragThumb))
{
return;
}
_mouse_down_location = new Point(e.Location.X, e.Location.Y - _grid_content.Margin.Top);
IsMouseTouchDown = true;
}
/// <summary>
/// Called when the mouse touch has been move
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="MouseOrTouchEventArgs"/> instance containing the event data.</param>
protected virtual void OnMouseTouchMove(object sender, MouseOrTouchEventArgs e)
{
var a = _mouse_down_location.Y + _grid_content.Margin.Top;
if (IsMouseTouchDown && Math.Abs((e.Location.Y - a)) > 10)
{
if (!IsScrolling)
{
_border_viewport.IsManipulationEnabled = true;
IsScrolling = true;
Mouse.Capture(_border_viewport);
if (e.TouchDevice != null)
{
_moveTouchDevice = e.TouchDevice;
e.TouchDevice.Capture(_border_viewport);
}
}
_grid_content.Margin = new Thickness(0, e.Location.Y - _mouse_down_location.Y, 0, 0);
_grid_content.BeginAnimation(Grid.MarginProperty, null);
double bottom_offset = -(_grid_content.ActualHeight - (_border_viewport.ActualHeight + -_grid_content.Margin.Top));
if (_grid_content.Margin.Top > 0)
{
BeginAnimation(TopArcSizeProperty, null);
TopArcSize = new Size(250, Math.Min(_grid_content.Margin.Top / 15, 100));
}
else if (bottom_offset > 0)
{
BeginAnimation(BottomArcSizeProperty, null);
BottomArcSize = new Size(250, Math.Min(bottom_offset / 15, 100));
}
SetThumbPosition();
}
}
/// <summary>
/// Called when the mouse touch has been up
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="MouseOrTouchEventArgs"/> instance containing the event data.</param>
protected virtual void OnMouseTouchUp(object sender, MouseOrTouchEventArgs e)
{
if (IsMouseTouchDown)
{
IsMouseTouchDown = false;
if (IsScrolling)
{
ReleaseMouseTouchCapture();
e.Handled = true;
SnapContentToBounds();
}
IsScrolling = false;
}
}
#endregion
#region Private Methods
/// <summary>
/// Snaps the content if it's out of bounds.
/// </summary>
private void SnapContentToBounds()
{
ThicknessAnimation ani = new ThicknessAnimation();
ani.Duration = TimeSpan.FromSeconds(0.2);
ani.AccelerationRatio = 1;
SizeAnimation sizeAni = new SizeAnimation();
sizeAni.Duration = TimeSpan.FromSeconds(0.2);
sizeAni.AccelerationRatio = 1;
sizeAni.To = new Size(250, 0);
BeginAnimation(TopArcSizeProperty, sizeAni);
BeginAnimation(BottomArcSizeProperty, sizeAni);
if (_grid_content.Margin.Top > 0 || _grid_content.ActualHeight < _border_viewport.ActualHeight)
{
ani.To = new Thickness(0);
_grid_content.BeginAnimation(Grid.MarginProperty, ani);
}
else if (_grid_content.Margin.Top - _border_viewport.ActualHeight < -_grid_content.ActualHeight)
{
ani.To = new Thickness(0, -(_grid_content.ActualHeight - _border_viewport.ActualHeight), 0, 0);
_grid_content.BeginAnimation(Grid.MarginProperty, ani);
}
}
private double CalculateScrollbarThumbStep()
{
var scrollTrackSpace = _grid_content.ActualHeight - _border_viewport.ActualHeight;
var scrollThumbSpace = _border_viewport.ActualHeight - _thumb.ActualHeight;
var scrollJump = scrollTrackSpace / scrollThumbSpace;
return scrollJump;
}
private void SetThumbPosition()
{
double step = CalculateScrollbarThumbStep();
if (step != 0)
{
double content_margin = _grid_content.Margin.Top;
double thumb_top = -(content_margin / step);
Canvas.SetTop(_border_thumb, thumb_top);
if (thumb_top < 0)
{
Canvas.SetTop(_border_thumb, 0);
}
else if (thumb_top + _border_thumb.ActualHeight > _border_viewport.ActualHeight)
{
Canvas.SetTop(_border_thumb, _border_viewport.ActualHeight - _border_thumb.ActualHeight);
}
}
}
private void ReleaseMouseTouchCapture()
{
Mouse.Capture(null);
_border_viewport.ReleaseMouseCapture();
_border_viewport.ReleaseAllTouchCaptures();
if (_moveTouchDevice != null)
{
_border_viewport.ReleaseTouchCapture(_moveTouchDevice);
_moveTouchDevice = null;
}
}
#endregion
#region Public Methods
public void ScrollToElement(FrameworkElement element)
{
if (element != null)
{
var e = this.FindVisualChildren<FrameworkElement>().SingleOrDefault(x => x == element);
if (e != null)
{
var location = e.TranslatePoint(new Point(0, 0), _grid_content);
Rect elementRect = new Rect(location, new Size(e.ActualWidth, e.ActualHeight));
Rect viewPortRect = new Rect(0, 0, _border_viewport.ActualWidth, _border_viewport.ActualHeight);
ScrollToPosition((location.Y - (viewPortRect.Height / 2)) + (elementRect.Height / 2));
}
}
}
public void ScrollToTop()
{
_grid_content.Margin = new Thickness(0);
}
public void ScrollToPosition(double y)
{
if (_grid_content.ActualHeight > _border_viewport.ActualHeight)
{
if (y < 0)
{
y = 0;
}
if (-y - _border_viewport.ActualHeight < -_grid_content.ActualHeight)
{
y = (_grid_content.ActualHeight - _border_viewport.ActualHeight);
}
_grid_content.BeginAnimation(Grid.MarginProperty, null);
_grid_content.Margin = new Thickness(0, -y, 0, 0);
}
SnapContentToBounds();
}
#endregion
}
}
|