|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using Windows.UI.Xaml; |
| 7 | +using Windows.UI.Xaml.Controls; |
| 8 | + |
| 9 | +namespace Microsoft.Toolkit.Uwp.UI.Controls |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// A ContentControl that show an image repeated many times. |
| 13 | + /// The control can be synchronized with a ScrollViewer and animated easily. |
| 14 | + /// </summary> |
| 15 | + public partial class TileControl : ContentControl |
| 16 | + { |
| 17 | + /// <summary> |
| 18 | + /// Identifies the <see cref="ScrollViewerContainer"/> property. |
| 19 | + /// </summary> |
| 20 | + public static readonly DependencyProperty ScrollViewerContainerProperty = |
| 21 | + DependencyProperty.Register(nameof(ScrollViewerContainer), typeof(FrameworkElement), typeof(TileControl), new PropertyMetadata(null, OnScrollViewerContainerChange)); |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Identifies the <see cref="ImageAlignment"/> property. |
| 25 | + /// </summary> |
| 26 | + public static readonly DependencyProperty ImageAlignmentProperty = |
| 27 | + DependencyProperty.Register(nameof(ImageAlignment), typeof(ImageAlignment), typeof(TileControl), new PropertyMetadata(ImageAlignment.None, OnAlignmentChange)); |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Identifies the <see cref="ImageSource"/> property. |
| 31 | + /// </summary> |
| 32 | + public static readonly DependencyProperty ImageSourceProperty = |
| 33 | + DependencyProperty.Register(nameof(ImageSource), typeof(Uri), typeof(TileControl), new PropertyMetadata(null, OnImageSourceChanged)); |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Identifies the <see cref="ScrollOrientation"/> property. |
| 37 | + /// </summary> |
| 38 | + public static readonly DependencyProperty ScrollOrientationProperty = |
| 39 | + DependencyProperty.Register(nameof(ScrollOrientation), typeof(ScrollOrientation), typeof(TileControl), new PropertyMetadata(ScrollOrientation.Both, OnOrientationChanged)); |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Identifies the <see cref="OffsetX"/> property. |
| 43 | + /// </summary> |
| 44 | + public static readonly DependencyProperty OffsetXProperty = |
| 45 | + DependencyProperty.Register(nameof(OffsetX), typeof(double), typeof(TileControl), new PropertyMetadata(0.0, OnOffsetChange)); |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Identifies the <see cref="OffsetY"/> property. |
| 49 | + /// </summary> |
| 50 | + public static readonly DependencyProperty OffsetYProperty = |
| 51 | + DependencyProperty.Register(nameof(OffsetY), typeof(double), typeof(TileControl), new PropertyMetadata(0.0, OnOffsetChange)); |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Identifies the <see cref="ParallaxSpeedRatio"/> property. |
| 55 | + /// </summary> |
| 56 | + public static readonly DependencyProperty ParallaxSpeedRatioProperty = |
| 57 | + DependencyProperty.Register(nameof(ParallaxSpeedRatio), typeof(double), typeof(TileControl), new PropertyMetadata(1.0, OnScrollSpeedRatioChange)); |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// Identifies the <see cref="IsAnimated"/> property. |
| 61 | + /// </summary> |
| 62 | + public static readonly DependencyProperty IsAnimatedProperty = |
| 63 | + DependencyProperty.Register(nameof(IsAnimated), typeof(bool), typeof(TileControl), new PropertyMetadata(false, OnIsAnimatedChange)); |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// Identifies the <see cref="AnimationStepX"/> property. |
| 67 | + /// </summary> |
| 68 | + public static readonly DependencyProperty AnimationStepXProperty = |
| 69 | + DependencyProperty.Register(nameof(AnimationStepX), typeof(double), typeof(TileControl), new PropertyMetadata(1.0)); |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Identifies the <see cref="AnimationStepY"/> property. |
| 73 | + /// </summary> |
| 74 | + public static readonly DependencyProperty AnimationStepYProperty = |
| 75 | + DependencyProperty.Register(nameof(AnimationStepY), typeof(double), typeof(TileControl), new PropertyMetadata(1.0)); |
| 76 | + |
| 77 | + /// <summary> |
| 78 | + /// Identifies the <see cref="AnimationDuration"/> property. |
| 79 | + /// </summary> |
| 80 | + public static readonly DependencyProperty AnimationDurationProperty = |
| 81 | + DependencyProperty.Register(nameof(AnimationDuration), typeof(double), typeof(TileControl), new PropertyMetadata(30.0, OnAnimationDuration)); |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// Gets or sets a ScrollViewer or a frameworkElement containing a ScrollViewer. |
| 85 | + /// The tile control is synchronized with the offset of the scrollViewer |
| 86 | + /// </summary> |
| 87 | + public FrameworkElement ScrollViewerContainer |
| 88 | + { |
| 89 | + get { return (FrameworkElement)GetValue(ScrollViewerContainerProperty); } |
| 90 | + set { SetValue(ScrollViewerContainerProperty, value); } |
| 91 | + } |
| 92 | + |
| 93 | + private static async void OnScrollViewerContainerChange(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 94 | + { |
| 95 | + var control = d as TileControl; |
| 96 | + await control.InitializeScrollViewerContainer(e.OldValue as FrameworkElement, e.NewValue as FrameworkElement); |
| 97 | + } |
| 98 | + |
| 99 | + /// <summary> |
| 100 | + /// Gets or sets the alignment of the tile when the <see cref="ScrollOrientation"/> is set to Vertical or Horizontal. |
| 101 | + /// Valid values are Left or Right for <see cref="ScrollOrientation"/> set to Horizontal and Top or Bottom for <see cref="ScrollOrientation"/> set to Vertical. |
| 102 | + /// </summary> |
| 103 | + public ImageAlignment ImageAlignment |
| 104 | + { |
| 105 | + get { return (ImageAlignment)GetValue(ImageAlignmentProperty); } |
| 106 | + set { SetValue(ImageAlignmentProperty, value); } |
| 107 | + } |
| 108 | + |
| 109 | + private static async void OnAlignmentChange(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 110 | + { |
| 111 | + var control = d as TileControl; |
| 112 | + await control.RefreshContainerTileLocked(); |
| 113 | + } |
| 114 | + |
| 115 | + /// <summary> |
| 116 | + /// Gets or sets the uri of the image to load |
| 117 | + /// </summary> |
| 118 | + public Uri ImageSource |
| 119 | + { |
| 120 | + get { return (Uri)GetValue(ImageSourceProperty); } |
| 121 | + set { SetValue(ImageSourceProperty, value); } |
| 122 | + } |
| 123 | + |
| 124 | + private static async void OnImageSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 125 | + { |
| 126 | + var control = d as TileControl; |
| 127 | + await control.LoadImageBrushAsync(e.NewValue as Uri); |
| 128 | + } |
| 129 | + |
| 130 | + /// <summary> |
| 131 | + /// Gets or sets the scroll orientation of the tile. |
| 132 | + /// Less images are drawn when you choose the Horizontal or Vertical value. |
| 133 | + /// </summary> |
| 134 | + public ScrollOrientation ScrollOrientation |
| 135 | + { |
| 136 | + get { return (ScrollOrientation)GetValue(ScrollOrientationProperty); } |
| 137 | + set { SetValue(ScrollOrientationProperty, value); } |
| 138 | + } |
| 139 | + |
| 140 | + private static async void OnOrientationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 141 | + { |
| 142 | + var control = d as TileControl; |
| 143 | + await control.RefreshContainerTileLocked(); |
| 144 | + await control.CreateModuloExpression(control._scrollViewer); |
| 145 | + } |
| 146 | + |
| 147 | + /// <summary> |
| 148 | + /// Gets or sets an X offset of the image |
| 149 | + /// </summary> |
| 150 | + public double OffsetX |
| 151 | + { |
| 152 | + get { return (double)GetValue(OffsetXProperty); } |
| 153 | + set { SetValue(OffsetXProperty, value); } |
| 154 | + } |
| 155 | + |
| 156 | + private static void OnOffsetChange(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 157 | + { |
| 158 | + var c = d as TileControl; |
| 159 | + |
| 160 | + c.RefreshMove(); |
| 161 | + } |
| 162 | + |
| 163 | + /// <summary> |
| 164 | + /// Gets or sets an Y offset of the image |
| 165 | + /// </summary> |
| 166 | + public double OffsetY |
| 167 | + { |
| 168 | + get { return (double)GetValue(OffsetYProperty); } |
| 169 | + set { SetValue(OffsetYProperty, value); } |
| 170 | + } |
| 171 | + |
| 172 | + /// <summary> |
| 173 | + /// Gets or sets the speed ratio of the parallax effect with the <see cref="ScrollViewerContainer"/> |
| 174 | + /// </summary> |
| 175 | + public double ParallaxSpeedRatio |
| 176 | + { |
| 177 | + get { return (double)GetValue(ParallaxSpeedRatioProperty); } |
| 178 | + set { SetValue(ParallaxSpeedRatioProperty, value); } |
| 179 | + } |
| 180 | + |
| 181 | + private static void OnScrollSpeedRatioChange(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 182 | + { |
| 183 | + var c = d as TileControl; |
| 184 | + c.RefreshScrollSpeedRatio((double)e.NewValue); |
| 185 | + } |
| 186 | + |
| 187 | + /// <summary> |
| 188 | + /// Gets or sets a value indicating whether the tile is animated or not |
| 189 | + /// </summary> |
| 190 | + public bool IsAnimated |
| 191 | + { |
| 192 | + get { return (bool)GetValue(IsAnimatedProperty); } |
| 193 | + set { SetValue(IsAnimatedProperty, value); } |
| 194 | + } |
| 195 | + |
| 196 | + private static void OnIsAnimatedChange(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 197 | + { |
| 198 | + var c = d as TileControl; |
| 199 | + |
| 200 | + if ((bool)e.NewValue) |
| 201 | + { |
| 202 | + c._timerAnimation.Start(); |
| 203 | + } |
| 204 | + else |
| 205 | + { |
| 206 | + c._timerAnimation.Stop(); |
| 207 | + c._animationX = 0; |
| 208 | + c._animationY = 0; |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + /// <summary> |
| 213 | + /// Gets or sets the animation step of the OffsetX |
| 214 | + /// </summary> |
| 215 | + public double AnimationStepX |
| 216 | + { |
| 217 | + get { return (double)GetValue(AnimationStepXProperty); } |
| 218 | + set { SetValue(AnimationStepXProperty, value); } |
| 219 | + } |
| 220 | + |
| 221 | + /// <summary> |
| 222 | + /// Gets or sets the animation step of the OffsetY |
| 223 | + /// </summary> |
| 224 | + public double AnimationStepY |
| 225 | + { |
| 226 | + get { return (double)GetValue(AnimationStepYProperty); } |
| 227 | + set { SetValue(AnimationStepYProperty, value); } |
| 228 | + } |
| 229 | + |
| 230 | + /// <summary> |
| 231 | + /// Gets or sets a duration for the animation of the tile |
| 232 | + /// </summary> |
| 233 | + public double AnimationDuration |
| 234 | + { |
| 235 | + get { return (double)GetValue(AnimationDurationProperty); } |
| 236 | + set { SetValue(AnimationDurationProperty, value); } |
| 237 | + } |
| 238 | + |
| 239 | + private static void OnAnimationDuration(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 240 | + { |
| 241 | + var c = d as TileControl; |
| 242 | + |
| 243 | + c._timerAnimation.Interval = TimeSpan.FromMilliseconds(c.AnimationDuration); |
| 244 | + } |
| 245 | + } |
| 246 | +} |
0 commit comments