diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/TileControl/ImageAlignment.cs b/Microsoft.Toolkit.Uwp.UI.Controls/TileControl/ImageAlignment.cs new file mode 100644 index 00000000000..c4e0a813238 --- /dev/null +++ b/Microsoft.Toolkit.Uwp.UI.Controls/TileControl/ImageAlignment.cs @@ -0,0 +1,37 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace Microsoft.Toolkit.Uwp.UI.Controls +{ + /// + /// Image alignment + /// + public enum ImageAlignment + { + /// + /// No alignment needed + /// + None, + + /// + /// Align to Left when the property ScrollOrientation is Horizontal + /// + Left, + + /// + /// Align to Right when the property ScrollOrientation is Horizontal + /// + Right, + + /// + /// Align to Top when the property ScrollOrientation is Vertical + /// + Top, + + /// + /// Align to Bottom when the property ScrollOrientation is Vertical + /// + Bottom + } +} diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/TileControl/ScrollOrientation.cs b/Microsoft.Toolkit.Uwp.UI.Controls/TileControl/ScrollOrientation.cs new file mode 100644 index 00000000000..a1e8ec00e59 --- /dev/null +++ b/Microsoft.Toolkit.Uwp.UI.Controls/TileControl/ScrollOrientation.cs @@ -0,0 +1,27 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace Microsoft.Toolkit.Uwp.UI.Controls +{ + /// + /// Orientation of the scroll + /// + public enum ScrollOrientation + { + /// + /// Scroll only Horizontally (and optimize the number of image used) + /// + Horizontal, + + /// + /// Scroll only Vertically (and optimize the number of image used) + /// + Vertical, + + /// + /// Scroll both Horizontally and vertically + /// + Both + } +} diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/TileControl/TileControl.Properties.cs b/Microsoft.Toolkit.Uwp.UI.Controls/TileControl/TileControl.Properties.cs new file mode 100644 index 00000000000..bb8c1b45728 --- /dev/null +++ b/Microsoft.Toolkit.Uwp.UI.Controls/TileControl/TileControl.Properties.cs @@ -0,0 +1,246 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using Windows.UI.Xaml; +using Windows.UI.Xaml.Controls; + +namespace Microsoft.Toolkit.Uwp.UI.Controls +{ + /// + /// A ContentControl that show an image repeated many times. + /// The control can be synchronized with a ScrollViewer and animated easily. + /// + public partial class TileControl : ContentControl + { + /// + /// Identifies the property. + /// + public static readonly DependencyProperty ScrollViewerContainerProperty = + DependencyProperty.Register(nameof(ScrollViewerContainer), typeof(FrameworkElement), typeof(TileControl), new PropertyMetadata(null, OnScrollViewerContainerChange)); + + /// + /// Identifies the property. + /// + public static readonly DependencyProperty ImageAlignmentProperty = + DependencyProperty.Register(nameof(ImageAlignment), typeof(ImageAlignment), typeof(TileControl), new PropertyMetadata(ImageAlignment.None, OnAlignmentChange)); + + /// + /// Identifies the property. + /// + public static readonly DependencyProperty ImageSourceProperty = + DependencyProperty.Register(nameof(ImageSource), typeof(Uri), typeof(TileControl), new PropertyMetadata(null, OnImageSourceChanged)); + + /// + /// Identifies the property. + /// + public static readonly DependencyProperty ScrollOrientationProperty = + DependencyProperty.Register(nameof(ScrollOrientation), typeof(ScrollOrientation), typeof(TileControl), new PropertyMetadata(ScrollOrientation.Both, OnOrientationChanged)); + + /// + /// Identifies the property. + /// + public static readonly DependencyProperty OffsetXProperty = + DependencyProperty.Register(nameof(OffsetX), typeof(double), typeof(TileControl), new PropertyMetadata(0.0, OnOffsetChange)); + + /// + /// Identifies the property. + /// + public static readonly DependencyProperty OffsetYProperty = + DependencyProperty.Register(nameof(OffsetY), typeof(double), typeof(TileControl), new PropertyMetadata(0.0, OnOffsetChange)); + + /// + /// Identifies the property. + /// + public static readonly DependencyProperty ParallaxSpeedRatioProperty = + DependencyProperty.Register(nameof(ParallaxSpeedRatio), typeof(double), typeof(TileControl), new PropertyMetadata(1.0, OnScrollSpeedRatioChange)); + + /// + /// Identifies the property. + /// + public static readonly DependencyProperty IsAnimatedProperty = + DependencyProperty.Register(nameof(IsAnimated), typeof(bool), typeof(TileControl), new PropertyMetadata(false, OnIsAnimatedChange)); + + /// + /// Identifies the property. + /// + public static readonly DependencyProperty AnimationStepXProperty = + DependencyProperty.Register(nameof(AnimationStepX), typeof(double), typeof(TileControl), new PropertyMetadata(1.0)); + + /// + /// Identifies the property. + /// + public static readonly DependencyProperty AnimationStepYProperty = + DependencyProperty.Register(nameof(AnimationStepY), typeof(double), typeof(TileControl), new PropertyMetadata(1.0)); + + /// + /// Identifies the property. + /// + public static readonly DependencyProperty AnimationDurationProperty = + DependencyProperty.Register(nameof(AnimationDuration), typeof(double), typeof(TileControl), new PropertyMetadata(30.0, OnAnimationDuration)); + + /// + /// Gets or sets a ScrollViewer or a frameworkElement containing a ScrollViewer. + /// The tile control is synchronized with the offset of the scrollViewer + /// + public FrameworkElement ScrollViewerContainer + { + get { return (FrameworkElement)GetValue(ScrollViewerContainerProperty); } + set { SetValue(ScrollViewerContainerProperty, value); } + } + + private static async void OnScrollViewerContainerChange(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + var control = d as TileControl; + await control.InitializeScrollViewerContainer(e.OldValue as FrameworkElement, e.NewValue as FrameworkElement); + } + + /// + /// Gets or sets the alignment of the tile when the is set to Vertical or Horizontal. + /// Valid values are Left or Right for set to Horizontal and Top or Bottom for set to Vertical. + /// + public ImageAlignment ImageAlignment + { + get { return (ImageAlignment)GetValue(ImageAlignmentProperty); } + set { SetValue(ImageAlignmentProperty, value); } + } + + private static async void OnAlignmentChange(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + var control = d as TileControl; + await control.RefreshContainerTileLocked(); + } + + /// + /// Gets or sets the uri of the image to load + /// + public Uri ImageSource + { + get { return (Uri)GetValue(ImageSourceProperty); } + set { SetValue(ImageSourceProperty, value); } + } + + private static async void OnImageSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + var control = d as TileControl; + await control.LoadImageBrushAsync(e.NewValue as Uri); + } + + /// + /// Gets or sets the scroll orientation of the tile. + /// Less images are drawn when you choose the Horizontal or Vertical value. + /// + public ScrollOrientation ScrollOrientation + { + get { return (ScrollOrientation)GetValue(ScrollOrientationProperty); } + set { SetValue(ScrollOrientationProperty, value); } + } + + private static async void OnOrientationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + var control = d as TileControl; + await control.RefreshContainerTileLocked(); + await control.CreateModuloExpression(control._scrollViewer); + } + + /// + /// Gets or sets an X offset of the image + /// + public double OffsetX + { + get { return (double)GetValue(OffsetXProperty); } + set { SetValue(OffsetXProperty, value); } + } + + private static void OnOffsetChange(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + var c = d as TileControl; + + c.RefreshMove(); + } + + /// + /// Gets or sets an Y offset of the image + /// + public double OffsetY + { + get { return (double)GetValue(OffsetYProperty); } + set { SetValue(OffsetYProperty, value); } + } + + /// + /// Gets or sets the speed ratio of the parallax effect with the + /// + public double ParallaxSpeedRatio + { + get { return (double)GetValue(ParallaxSpeedRatioProperty); } + set { SetValue(ParallaxSpeedRatioProperty, value); } + } + + private static void OnScrollSpeedRatioChange(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + var c = d as TileControl; + c.RefreshScrollSpeedRatio((double)e.NewValue); + } + + /// + /// Gets or sets a value indicating whether the tile is animated or not + /// + public bool IsAnimated + { + get { return (bool)GetValue(IsAnimatedProperty); } + set { SetValue(IsAnimatedProperty, value); } + } + + private static void OnIsAnimatedChange(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + var c = d as TileControl; + + if ((bool)e.NewValue) + { + c._timerAnimation.Start(); + } + else + { + c._timerAnimation.Stop(); + c._animationX = 0; + c._animationY = 0; + } + } + + /// + /// Gets or sets the animation step of the OffsetX + /// + public double AnimationStepX + { + get { return (double)GetValue(AnimationStepXProperty); } + set { SetValue(AnimationStepXProperty, value); } + } + + /// + /// Gets or sets the animation step of the OffsetY + /// + public double AnimationStepY + { + get { return (double)GetValue(AnimationStepYProperty); } + set { SetValue(AnimationStepYProperty, value); } + } + + /// + /// Gets or sets a duration for the animation of the tile + /// + public double AnimationDuration + { + get { return (double)GetValue(AnimationDurationProperty); } + set { SetValue(AnimationDurationProperty, value); } + } + + private static void OnAnimationDuration(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + var c = d as TileControl; + + c._timerAnimation.Interval = TimeSpan.FromMilliseconds(c.AnimationDuration); + } + } +} diff --git a/Microsoft.Toolkit.Uwp.UI.Controls/TileControl/TileControl.cs b/Microsoft.Toolkit.Uwp.UI.Controls/TileControl/TileControl.cs index 00912da8913..dea441e074f 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls/TileControl/TileControl.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls/TileControl/TileControl.cs @@ -2,147 +2,29 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System; +using System.Collections.Generic; +using System.Linq; +using System.Numerics; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Toolkit.Uwp.UI.Animations.Expressions; +using Microsoft.Toolkit.Uwp.UI.Extensions; +using Windows.Foundation; +using Windows.UI.Composition; +using Windows.UI.Xaml; +using Windows.UI.Xaml.Controls; +using Windows.UI.Xaml.Hosting; +using Windows.UI.Xaml.Media; + namespace Microsoft.Toolkit.Uwp.UI.Controls { - using System; - using System.Collections.Generic; - using System.Linq; - using System.Numerics; - using System.Threading; - using System.Threading.Tasks; - using Microsoft.Toolkit.Uwp.UI.Animations.Expressions; - using Microsoft.Toolkit.Uwp.UI.Extensions; - using Windows.Foundation; - using Windows.UI.Composition; - using Windows.UI.Xaml; - using Windows.UI.Xaml.Controls; - using Windows.UI.Xaml.Hosting; - using Windows.UI.Xaml.Media; - - /// - /// Orientation of the scroll - /// - public enum ScrollOrientation - { - /// - /// Scroll only Horizontally (and optimize the number of image used) - /// - Horizontal, - - /// - /// Scroll only Vertically (and optimize the number of image used) - /// - Vertical, - - /// - /// Scroll both Horizontally and vertically - /// - Both - } - - /// - /// Image alignment - /// - public enum ImageAlignment - { - /// - /// No alignment needed - /// - None, - - /// - /// Align to Left when the property ScrollOrientation is Horizontal - /// - Left, - - /// - /// Align to Right when the property ScrollOrientation is Horizontal - /// - Right, - - /// - /// Align to Top when the property ScrollOrientation is Vertical - /// - Top, - - /// - /// Align to Bottom when the property ScrollOrientation is Vertical - /// - Bottom - } - /// /// A ContentControl that show an image repeated many times. /// The control can be synchronized with a ScrollViewer and animated easily. /// - public class TileControl : ContentControl + public partial class TileControl : ContentControl { - /// - /// Identifies the property. - /// - public static readonly DependencyProperty ScrollViewerContainerProperty = - DependencyProperty.Register(nameof(ScrollViewerContainer), typeof(FrameworkElement), typeof(TileControl), new PropertyMetadata(null, OnScrollViewerContainerChange)); - - /// - /// Identifies the property. - /// - public static readonly DependencyProperty ImageAlignmentProperty = - DependencyProperty.Register(nameof(ImageAlignment), typeof(ImageAlignment), typeof(TileControl), new PropertyMetadata(ImageAlignment.None, OnAlignmentChange)); - - /// - /// Identifies the property. - /// - public static readonly DependencyProperty ImageSourceProperty = - DependencyProperty.Register(nameof(ImageSource), typeof(Uri), typeof(TileControl), new PropertyMetadata(null, OnImageSourceChanged)); - - /// - /// Identifies the property. - /// - public static readonly DependencyProperty ScrollOrientationProperty = - DependencyProperty.Register(nameof(ScrollOrientation), typeof(ScrollOrientation), typeof(TileControl), new PropertyMetadata(ScrollOrientation.Both, OnOrientationChanged)); - - /// - /// Identifies the property. - /// - public static readonly DependencyProperty OffsetXProperty = - DependencyProperty.Register(nameof(OffsetX), typeof(double), typeof(TileControl), new PropertyMetadata(0.0, OnOffsetChange)); - - /// - /// Identifies the property. - /// - public static readonly DependencyProperty OffsetYProperty = - DependencyProperty.Register(nameof(OffsetY), typeof(double), typeof(TileControl), new PropertyMetadata(0.0, OnOffsetChange)); - - /// - /// Identifies the property. - /// - public static readonly DependencyProperty ParallaxSpeedRatioProperty = - DependencyProperty.Register(nameof(ParallaxSpeedRatio), typeof(double), typeof(TileControl), new PropertyMetadata(1.0, OnScrollSpeedRatioChange)); - - /// - /// Identifies the property. - /// - public static readonly DependencyProperty IsAnimatedProperty = - DependencyProperty.Register(nameof(IsAnimated), typeof(bool), typeof(TileControl), new PropertyMetadata(false, OnIsAnimatedChange)); - - /// - /// Identifies the property. - /// - public static readonly DependencyProperty AnimationStepXProperty = - DependencyProperty.Register(nameof(AnimationStepX), typeof(double), typeof(TileControl), new PropertyMetadata(1.0)); - - /// - /// Identifies the property. - /// - public static readonly DependencyProperty AnimationStepYProperty = - DependencyProperty.Register(nameof(AnimationStepY), typeof(double), typeof(TileControl), new PropertyMetadata(1.0)); - - /// - /// Identifies the property. - /// - public static readonly DependencyProperty AnimationDurationProperty = - DependencyProperty.Register(nameof(AnimationDuration), typeof(double), typeof(TileControl), new PropertyMetadata(30.0, OnAnimationDuration)); - /// /// a flag to lock shared method /// @@ -189,22 +71,6 @@ public TileControl() InitializeAnimation(); } - /// - /// Gets or sets a ScrollViewer or a frameworkElement containing a ScrollViewer. - /// The tile control is synchronized with the offset of the scrollViewer - /// - public FrameworkElement ScrollViewerContainer - { - get { return (FrameworkElement)GetValue(ScrollViewerContainerProperty); } - set { SetValue(ScrollViewerContainerProperty, value); } - } - - private static async void OnScrollViewerContainerChange(DependencyObject d, DependencyPropertyChangedEventArgs e) - { - var control = d as TileControl; - await control.InitializeScrollViewerContainer(e.OldValue as FrameworkElement, e.NewValue as FrameworkElement); - } - /// /// Initialize the new ScrollViewer /// @@ -239,22 +105,6 @@ private async void ScrollViewerContainer_Loaded(object sender, RoutedEventArgs e await AttachScrollViewer(sender as FrameworkElement); } - /// - /// Gets or sets the alignment of the tile when the is set to Vertical or Horizontal. - /// Valid values are Left or Right for set to Horizontal and Top or Bottom for set to Vertical. - /// - public ImageAlignment ImageAlignment - { - get { return (ImageAlignment)GetValue(ImageAlignmentProperty); } - set { SetValue(ImageAlignmentProperty, value); } - } - - private static async void OnAlignmentChange(DependencyObject d, DependencyPropertyChangedEventArgs e) - { - var control = d as TileControl; - await control.RefreshContainerTileLocked(); - } - /// /// Attach a ScrollViewer to the TileControl (parallax effect) /// @@ -278,21 +128,6 @@ private async Task AttachScrollViewer(DependencyObject scrollViewerContainer) } } - /// - /// Gets or sets the uri of the image to load - /// - public Uri ImageSource - { - get { return (Uri)GetValue(ImageSourceProperty); } - set { SetValue(ImageSourceProperty, value); } - } - - private static async void OnImageSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) - { - var control = d as TileControl; - await control.LoadImageBrushAsync(e.NewValue as Uri); - } - /// /// Load the image and transform it to a composition brush or a XAML brush (depends of the UIStrategy) /// @@ -376,23 +211,6 @@ private async Task LoadImageBrushAsync(Uri uri) return true; } - /// - /// Gets or sets the scroll orientation of the tile. - /// Less images are drawn when you choose the Horizontal or Vertical value. - /// - public ScrollOrientation ScrollOrientation - { - get { return (ScrollOrientation)GetValue(ScrollOrientationProperty); } - set { SetValue(ScrollOrientationProperty, value); } - } - - private static async void OnOrientationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) - { - var control = d as TileControl; - await control.RefreshContainerTileLocked(); - await control.CreateModuloExpression(control._scrollViewer); - } - /// protected override async void OnApplyTemplate() { @@ -585,46 +403,6 @@ private bool RefreshContainerTile(double width, double height, double imageWidth return true; } - /// - /// Gets or sets an X offset of the image - /// - public double OffsetX - { - get { return (double)GetValue(OffsetXProperty); } - set { SetValue(OffsetXProperty, value); } - } - - private static void OnOffsetChange(DependencyObject d, DependencyPropertyChangedEventArgs e) - { - var c = d as TileControl; - - c.RefreshMove(); - } - - /// - /// Gets or sets an Y offset of the image - /// - public double OffsetY - { - get { return (double)GetValue(OffsetYProperty); } - set { SetValue(OffsetYProperty, value); } - } - - /// - /// Gets or sets the speed ratio of the parallax effect with the - /// - public double ParallaxSpeedRatio - { - get { return (double)GetValue(ParallaxSpeedRatioProperty); } - set { SetValue(ParallaxSpeedRatioProperty, value); } - } - - private static void OnScrollSpeedRatioChange(DependencyObject d, DependencyPropertyChangedEventArgs e) - { - var c = d as TileControl; - c.RefreshScrollSpeedRatio((double)e.NewValue); - } - /// /// Create the modulo expression and apply it to the ContainerVisual element /// @@ -833,31 +611,6 @@ private void RefreshScrollSpeedRatio(double speedRatio) _propertySetModulo?.InsertScalar("speed", (float)speedRatio); } - /// - /// Gets or sets a value indicating whether the tile is animated or not - /// - public bool IsAnimated - { - get { return (bool)GetValue(IsAnimatedProperty); } - set { SetValue(IsAnimatedProperty, value); } - } - - private static void OnIsAnimatedChange(DependencyObject d, DependencyPropertyChangedEventArgs e) - { - var c = d as TileControl; - - if ((bool)e.NewValue) - { - c._timerAnimation.Start(); - } - else - { - c._timerAnimation.Stop(); - c._animationX = 0; - c._animationY = 0; - } - } - private void InitializeAnimation() { if (_timerAnimation == null) @@ -900,39 +653,5 @@ private void Timer_Tick(object sender, object e) RefreshMove(); } } - - /// - /// Gets or sets the animation step of the OffsetX - /// - public double AnimationStepX - { - get { return (double)GetValue(AnimationStepXProperty); } - set { SetValue(AnimationStepXProperty, value); } - } - - /// - /// Gets or sets the animation step of the OffsetY - /// - public double AnimationStepY - { - get { return (double)GetValue(AnimationStepYProperty); } - set { SetValue(AnimationStepYProperty, value); } - } - - /// - /// Gets or sets a duration for the animation of the tile - /// - public double AnimationDuration - { - get { return (double)GetValue(AnimationDurationProperty); } - set { SetValue(AnimationDurationProperty, value); } - } - - private static void OnAnimationDuration(DependencyObject d, DependencyPropertyChangedEventArgs e) - { - var c = d as TileControl; - - c._timerAnimation.Interval = TimeSpan.FromMilliseconds(c.AnimationDuration); - } } }