-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRCarousel.cs
More file actions
607 lines (531 loc) · 23.8 KB
/
RCarousel.cs
File metadata and controls
607 lines (531 loc) · 23.8 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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
using System;
using System.Collections;
using System.Diagnostics;
using Windows.ApplicationModel;
using Windows.Devices.Input;
using Windows.System;
using Windows.System.Profile;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media.Animation;
#pragma warning disable 169
namespace Ratio.UWP.Controls
{
[TemplatePart(Name = "loopingStackPanel", Type = typeof(RLoopingStackPanel))]
[TemplatePart(Name = "leftButton", Type = typeof(Button))]
[TemplatePart(Name = "rightButton", Type = typeof(Button))]
[TemplatePart(Name = "leftPresenter", Type = typeof(ContentPresenter))]
[TemplatePart(Name = "rightPresenter", Type = typeof(ContentPresenter))]
[TemplatePart(Name = "positionMarkers", Type = typeof(RPositionMarkers))]
public sealed class RCarousel : Control
{
#region Fields
private RCarouselSaveState _pendingStateRestoration;
private Grid _rootGrid;
private RLoopingStackPanel _loopingStackPanel;
private RPositionMarkers _positionMarkers;
private Border _focusVisual;
private ScrollViewer _carouselScrollViewer;
private Button _leftButton;
private Button _rightButton;
private DispatcherTimer _timer = new DispatcherTimer();
private bool _isAutoRotationInitialized;
private bool _isNavigatedByKeyboard;
private bool _showFocusVisualOnResizeCompleted;
private Storyboard _focusVisualEntrance;
private Storyboard _focusVisualExit;
#endregion
#region Properties
private Storyboard FocusVisualEntrance => _focusVisualEntrance ?? (_focusVisualEntrance = _rootGrid.Resources["FocusVisualEntrance"] as Storyboard);
private Storyboard FocusVisualExit => _focusVisualExit ?? (_focusVisualExit = _rootGrid.Resources["FocusVisualExit"] as Storyboard);
#region Dependency Properties
public static readonly DependencyProperty AutoRotationEnabledProperty = DependencyProperty.Register(
"AutoRotationEnabled", typeof(bool), typeof(RCarousel), new PropertyMetadata(default(bool)));
public bool AutoRotationEnabled
{
get => (bool)GetValue(AutoRotationEnabledProperty);
set
{
SetValue(AutoRotationEnabledProperty, value);
UpdateAutoRotationConfiguration();
}
}
public static readonly DependencyProperty AutoRotationIntervalSecondsProperty = DependencyProperty.Register(
"AutoRotationIntervalSeconds", typeof(int), typeof(RCarousel), new PropertyMetadata(default(int)));
public int AutoRotationIntervalSeconds
{
get => (int)GetValue(AutoRotationIntervalSecondsProperty);
set
{
SetValue(AutoRotationIntervalSecondsProperty, value);
UpdateAutoRotationConfiguration();
}
}
public static readonly DependencyProperty FocusedItemProperty = DependencyProperty.Register(
"FocusedItem", typeof(object), typeof(RCarousel), new PropertyMetadata(default(object)));
public object FocusedItem
{
get => GetValue(FocusedItemProperty);
set => SetValue(FocusedItemProperty, value);
}
public static readonly DependencyProperty ItemHeightProperty = DependencyProperty.Register(
"ItemHeight", typeof(int), typeof(RCarousel), new PropertyMetadata(default(int), OnItemHeightChanged));
private static void OnItemHeightChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var carousel = d as RCarousel;
if (carousel?._focusVisual != null)
{
carousel._focusVisual.Height = (int) e.NewValue;
}
}
/// <summary>
/// The height of each item. If ItemWidth is not an absolute value, this property is ignored.
/// </summary>
public int ItemHeight
{
get => (int)GetValue(ItemHeightProperty);
set => SetValue(ItemHeightProperty, value);
}
public static readonly DependencyProperty ItemWidthProperty = DependencyProperty.Register(
"ItemWidth", typeof(double), typeof(RCarousel), new PropertyMetadata(default(double), OnItemWidthChanged));
private static void OnItemWidthChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var carousel = d as RCarousel;
if (carousel?._focusVisual != null)
{
carousel._focusVisual.Width = (double) e.NewValue;
}
}
/// <summary>
/// The width of each item in the carousel. The width may be specified as an absolute value or as relative to
/// the carousel's width. If the value is greater than 1, it is treated as an absolute value. If it is 1
/// or less, it is treated as a relative value; it is not possible to set an absolute item width of 1.
/// Note: When ItemWidth is treated as relative, ItemHeight is ignored and the actual height is calculated
/// using ItemAspectRatio.
/// </summary>
public double ItemWidth
{
get => (double)GetValue(ItemWidthProperty);
set => SetValue(ItemWidthProperty, value);
}
public static readonly DependencyProperty ItemAspectRatioProperty = DependencyProperty.Register(
"ItemAspectRatio", typeof(double), typeof(RCarousel), new PropertyMetadata(default(double)));
/// <summary>
/// The aspect ratio to apply to calculate the item height. This value is only used when ItemWidth is treated
/// as a fraction of the carousel's width, and ItemHeight would be ignored.
/// </summary>
public double ItemAspectRatio
{
get => (double)GetValue(ItemAspectRatioProperty);
set => SetValue(ItemAspectRatioProperty, value);
}
public static readonly DependencyProperty ItemTemplateProperty = DependencyProperty.Register(
"ItemTemplate", typeof(DataTemplate), typeof(RCarousel), new PropertyMetadata(default(DataTemplate)));
public DataTemplate ItemTemplate
{
get => (DataTemplate)GetValue(ItemTemplateProperty);
set => SetValue(ItemTemplateProperty, value);
}
public static readonly DependencyProperty ItemContainerStyleProperty = DependencyProperty.Register(
"ItemContainerStyle", typeof(Style), typeof(RCarousel), new PropertyMetadata(default(Style), OnItemContainerStyleChanged));
private static void OnItemContainerStyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var carousel = d as RCarousel;
carousel?.UpdateFocusVisualSize();
}
public Style ItemContainerStyle
{
get => (Style)GetValue(ItemContainerStyleProperty);
set => SetValue(ItemContainerStyleProperty, value);
}
public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register(
"ItemsSource", typeof(IList), typeof(RCarousel), new PropertyMetadata(default(IList)));
public IList ItemsSource
{
get => (IList)GetValue(ItemsSourceProperty);
set => SetValue(ItemsSourceProperty, value);
}
public static readonly DependencyProperty LeftButtonContainerWidthProperty = DependencyProperty.Register(
"LeftButtonContainerWidth", typeof(GridLength), typeof(RCarousel), new PropertyMetadata(new GridLength(40, GridUnitType.Pixel)));
public GridLength LeftButtonContainerWidth
{
get => (GridLength)GetValue(LeftButtonContainerWidthProperty);
set => SetValue(LeftButtonContainerWidthProperty, value);
}
public static readonly DependencyProperty RightButtonContainerWidthProperty = DependencyProperty.Register(
"RightButtonContainerWidth", typeof(GridLength), typeof(RCarousel), new PropertyMetadata(new GridLength(40, GridUnitType.Pixel)));
public GridLength RightButtonContainerWidth
{
get => (GridLength)GetValue(RightButtonContainerWidthProperty);
set => SetValue(RightButtonContainerWidthProperty, value);
}
public static readonly DependencyProperty LeftButtonStyleProperty = DependencyProperty.Register(
"LeftButtonStyle", typeof(Style), typeof(RCarousel), new PropertyMetadata(default(Style)));
public Style LeftButtonStyle
{
get => (Style)GetValue(LeftButtonStyleProperty);
set => SetValue(LeftButtonStyleProperty, value);
}
public static readonly DependencyProperty RightButtonStyleProperty = DependencyProperty.Register(
"RightButtonStyle", typeof(Style), typeof(RCarousel), new PropertyMetadata(default(Style)));
public Style RightButtonStyle
{
get => (Style)GetValue(RightButtonStyleProperty);
set => SetValue(RightButtonStyleProperty, value);
}
public static readonly DependencyProperty LeftButtonContentProperty = DependencyProperty.Register(
"LeftButtonContent", typeof(object), typeof(RCarousel), new PropertyMetadata(""));
public object LeftButtonContent
{
get => GetValue(LeftButtonContentProperty);
set => SetValue(LeftButtonContentProperty, value);
}
public static readonly DependencyProperty RightButtonContentProperty = DependencyProperty.Register(
"RightButtonContent", typeof(object), typeof(RCarousel), new PropertyMetadata(""));
public object RightButtonContent
{
get => GetValue(RightButtonContentProperty);
set => SetValue(RightButtonContentProperty, value);
}
public static readonly DependencyProperty ShiftStepsProperty = DependencyProperty.Register(
"ShiftSteps", typeof(int), typeof(RCarousel), new PropertyMetadata(1));
public int ShiftSteps
{
get => (int)GetValue(ShiftStepsProperty);
set => SetValue(ShiftStepsProperty, value);
}
public static readonly DependencyProperty SelectedCommandNameProperty = DependencyProperty.Register(
"SelectedCommandName", typeof(string), typeof(RCarousel), new PropertyMetadata(default(string)));
public string SelectedCommandName
{
get => (string) GetValue(SelectedCommandNameProperty);
set => SetValue(SelectedCommandNameProperty, value);
}
public static readonly DependencyProperty DisplayMarkersProperty = DependencyProperty.Register(
"DisplayMarkers", typeof(bool), typeof(RCarousel), new PropertyMetadata(true));
public bool DisplayMarkers
{
get => (bool) GetValue(DisplayMarkersProperty);
set => SetValue(DisplayMarkersProperty, value);
}
public static readonly DependencyProperty DisplayScrollButtonsProperty = DependencyProperty.Register(
"DisplayScrollButtons", typeof(bool), typeof(RCarousel), new PropertyMetadata(default(bool)));
public bool DisplayScrollButtons
{
get => (bool) GetValue(DisplayScrollButtonsProperty);
set => SetValue(DisplayScrollButtonsProperty, value);
}
#endregion
#endregion
#region Public Methods
public RCarousel()
{
if(DesignMode.DesignModeEnabled) return;
TabFocusNavigation = KeyboardNavigationMode.Once;
DefaultStyleKey = typeof(RCarousel);
Unloaded += OnUnloaded;
}
private void OnUnloaded(object sender, RoutedEventArgs routedEventArgs)
{
Unloaded -= OnUnloaded;
PointerEntered -= OnPointerEntered;
PointerExited -= OnPointerExited;
_loopingStackPanel.DirectManipulationStarted -= LoopingStackPanelOnDirectManipulationStarted;
_loopingStackPanel.FocusedItemChanged -= LoopingStackPanelOnFocusedItemChanged;
_loopingStackPanel.ItemsPopulated -= LoopingStackPanelOnItemsPopulated;
_loopingStackPanel.ResizingStarted -= LoopingStackPanelOnResizingStarted;
_loopingStackPanel.ResizingCompleted -= LoopingStackPanelOnResizingCompleted;
_loopingStackPanel.ScrollViewChanged -= LoopingStackPanelOnScrollViewChanged;
_leftButton.Click -= LeftButtonOnClick;
_rightButton.Click -= RightButtonOnClick;
_positionMarkers.OnMarkerClicked -= PositionMarkersOnMarkerClicked;
if(_timer != null)
{
_timer.Tick -= AutoRotation_Tick;
_timer = null;
}
}
public RCarouselSaveState SaveState()
{
return new RCarouselSaveState(_loopingStackPanel.SaveState());
}
public void RestoreState(RCarouselSaveState saveState)
{
if (saveState != null)
{
if (_loopingStackPanel != null)
{
_loopingStackPanel.RestoreState(new RLoopingStackPanelSaveState(saveState));
}
else
{
_pendingStateRestoration = saveState;
}
}
}
#endregion
#region Overrides
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();
UpdateAutoRotationConfiguration();
_rootGrid = GetTemplateChild("rootGrid") as Grid;
_loopingStackPanel = GetTemplateChild("loopingStackPanel") as RLoopingStackPanel;
_leftButton = GetTemplateChild("leftButton") as Button;
_rightButton = GetTemplateChild("rightButton") as Button;
_positionMarkers = GetTemplateChild("positionMarkers") as RPositionMarkers;
_focusVisual = GetTemplateChild("focusVisual") as Border;
if (_loopingStackPanel != null)
{
_loopingStackPanel.DirectManipulationStarted += LoopingStackPanelOnDirectManipulationStarted;
_loopingStackPanel.FocusedItemChanged += LoopingStackPanelOnFocusedItemChanged;
_loopingStackPanel.ItemsPopulated += LoopingStackPanelOnItemsPopulated;
_loopingStackPanel.ResizingStarted += LoopingStackPanelOnResizingStarted;
_loopingStackPanel.ResizingCompleted += LoopingStackPanelOnResizingCompleted;
_loopingStackPanel.ScrollViewChanged += LoopingStackPanelOnScrollViewChanged;
if (_pendingStateRestoration != null)
{
_loopingStackPanel.RestoreState(new RLoopingStackPanelSaveState(_pendingStateRestoration));
_pendingStateRestoration = null;
}
}
if (_leftButton != null)
{
if (DisplayScrollButtons)
{
_leftButton.Click += LeftButtonOnClick;
}
_leftButton.Visibility = Visibility.Collapsed;
}
if (_rightButton != null)
{
if (DisplayScrollButtons)
{
_rightButton.Click += RightButtonOnClick;
}
else
{
_rightButton.Visibility = Visibility.Collapsed;
}
}
if (_positionMarkers != null)
{
if (DisplayMarkers)
{
_positionMarkers.OnMarkerClicked += PositionMarkersOnMarkerClicked;
}
else
{
_positionMarkers.Visibility = Visibility.Collapsed;
}
}
// Subscribe to events so that the left/right buttons can be shown or hidden accordingly.
PointerEntered += OnPointerEntered;
PointerExited += OnPointerExited;
UpdateFocusVisualSize();
}
protected override void OnGotFocus(RoutedEventArgs e)
{
Debug.WriteLine($"Focus obtained by RCarousel.");
DisableAutoRotation();
base.OnGotFocus(e);
VisualStateManager.GoToState(this, "ButtonsShowing", true);
ShowFocusVisual();
StartBringIntoView(new BringIntoViewOptions() {AnimationDesired = true});
}
protected override void OnLostFocus(RoutedEventArgs e)
{
base.OnLostFocus(e);
VisualStateManager.GoToState(this, "ButtonsHidden", true);
HideFocusVisual();
}
protected override void OnKeyDown(KeyRoutedEventArgs e)
{
switch (e.Key)
{
case VirtualKey.Left:
_isNavigatedByKeyboard = true;
ScrollLeft();
DisableAutoRotation();
e.Handled = true;
break;
case VirtualKey.Right:
_isNavigatedByKeyboard = true;
DisableAutoRotation();
ScrollRight();
e.Handled = true;
break;
case VirtualKey.Up:
if (XYFocusUp != null && XYFocusUp is Control control1)
{
control1.Focus(FocusState.Keyboard);
e.Handled = true;
}
break;
case VirtualKey.Down:
if (XYFocusDown != null && XYFocusDown is Control control2)
{
control2.Focus(FocusState.Keyboard);
e.Handled = true;
}
break;
default:
base.OnKeyDown(e);
break;
}
}
#endregion
#region Event Handlers
private void OnPointerEntered(object sender, PointerRoutedEventArgs e)
{
if (e.GetCurrentPoint(null).PointerDevice.PointerDeviceType == PointerDeviceType.Mouse)
{
VisualStateManager.GoToState(this, "ButtonsShowing", true);
}
}
private void OnPointerExited(object sender, PointerRoutedEventArgs e)
{
VisualStateManager.GoToState(this, "ButtonsHidden", true);
}
private void LoopingStackPanelOnDirectManipulationStarted(object sender, EventArgs e)
{
HideFocusVisual();
}
private void LoopingStackPanelOnFocusedItemChanged(object sender, EventArgs e)
{
if(DisplayMarkers)
_positionMarkers.SelectedItem = _loopingStackPanel.FocusedItem;
// For all the reasons that caused the focus item to change, reset the auto-rotation so a full period is
// waited before the next change.
ResetAutoRotationTimer();
}
private void LoopingStackPanelOnItemsPopulated(object sender, EventArgs e)
{
// When there are actual items, it's now possible to fully determine the focus visual's size (namely margin).
UpdateFocusVisualSize();
}
private void LoopingStackPanelOnResizingCompleted(object sender, EventArgs e)
{
if (_showFocusVisualOnResizeCompleted)
{
_showFocusVisualOnResizeCompleted = false;
ShowFocusVisual();
}
}
private void LoopingStackPanelOnResizingStarted(object sender, EventArgs e)
{
_showFocusVisualOnResizeCompleted = _focusVisual.Opacity > 0;
HideFocusVisual();
}
private void LoopingStackPanelOnScrollViewChanged(object sender, ScrollViewer scrollViewer)
{
if (DisplayScrollButtons)
{
_leftButton.Visibility = scrollViewer.HorizontalOffset < 1 ? Visibility.Collapsed : Visibility.Visible;
_rightButton.Visibility = scrollViewer.HorizontalOffset > (scrollViewer.ExtentWidth - scrollViewer.ViewportWidth - 2) ? Visibility.Collapsed : Visibility.Visible;
}
// At the end of a scroll, show the focus visual if not shown already (it may have been hidden by a direct
// manipulation) and the scroll was triggered by keyboard navigation.
if (_isNavigatedByKeyboard)
{
_isNavigatedByKeyboard = false;
if(FocusState == FocusState.Unfocused) return;
ShowFocusVisual();
}
}
private void LeftButtonOnClick(object sender, RoutedEventArgs e)
{
HideFocusVisual();
ScrollLeft();
}
private void PositionMarkersOnMarkerClicked(object sender, object clickedItem)
{
if(!ControlHelpers.IsXBoxFamily())
_loopingStackPanel.JumpToItem(clickedItem);
}
private void AutoRotation_Tick(object sender, object e)
{
_loopingStackPanel?.AutoScrollBySteps(ShiftSteps);
}
private void RightButtonOnClick(object sender, RoutedEventArgs e)
{
HideFocusVisual();
ScrollRight();
}
#endregion
#region Support Methods
private void ScrollLeft()
{
if (double.IsNaN(ItemWidth)) return;
_loopingStackPanel.ScrollBySteps(-1 * ShiftSteps);
}
private void ScrollRight()
{
if (double.IsNaN(ItemWidth)) return;
_loopingStackPanel.ScrollBySteps(ShiftSteps);
}
private void UpdateFocusVisualSize()
{
if (_focusVisual != null && ItemsSource != null)
{
var carouselItem = FocusedItem as CarouselItem;
var horzOffset = 0.0;
var vertOffset = 0.0;
if (carouselItem != null)
{
horzOffset = carouselItem.Margin.Left + carouselItem.Margin.Right + carouselItem.Padding.Left + carouselItem.Padding.Right;
vertOffset = carouselItem.Margin.Top + carouselItem.Margin.Bottom + carouselItem.Padding.Top + carouselItem.Padding.Bottom;
}
_focusVisual.Width = ItemWidth - horzOffset;
_focusVisual.Height = ItemHeight - vertOffset;
}
}
private void ShowFocusVisual()
{
FocusVisualExit.SkipToFill();
if (_focusVisual != null && (int)Math.Round(_focusVisual.Opacity) == 0)
{
FocusVisualEntrance.Begin();
DisableAutoRotation();
}
}
private void HideFocusVisual()
{
Debug.WriteLine("Hide Focus border attempted.");
FocusVisualEntrance.SkipToFill();
if (_focusVisual != null && _focusVisual.Opacity > 0)
{
FocusVisualExit.Begin();
if (AutoRotationEnabled) UpdateAutoRotationConfiguration();
}
}
private void InitializeAutoRotation()
{
_timer.Tick += AutoRotation_Tick;
_timer.Interval = new TimeSpan(0, 0, AutoRotationIntervalSeconds);
if (AutoRotationEnabled) _timer.Start();
_isAutoRotationInitialized = true;
}
private void DisableAutoRotation()
{
if (_timer.IsEnabled) _timer.Stop();
_timer.Tick -= AutoRotation_Tick;
_isAutoRotationInitialized = false;
}
private void UpdateAutoRotationConfiguration()
{
if (_isAutoRotationInitialized) DisableAutoRotation();
InitializeAutoRotation();
}
private void ResetAutoRotationTimer()
{
if (_isAutoRotationInitialized)
{
if (_timer.IsEnabled) _timer.Stop();
if (AutoRotationEnabled) _timer.Start();
}
}
#endregion
}
}