From ceb9887ac554e65bf968e7550c3737fdca990597 Mon Sep 17 00:00:00 2001 From: Martin Zikmund Date: Wed, 23 Jun 2021 23:57:22 +0200 Subject: [PATCH] feat: AnimatedIconSource --- .../Controls/IconSource/AnimatedIconSource.cs | 74 +++++++++++++++++++ .../UI/Xaml/Controls/IconSource/IconSource.cs | 2 + 2 files changed, 76 insertions(+) create mode 100644 src/Uno.UI/Microsoft/UI/Xaml/Controls/IconSource/AnimatedIconSource.cs diff --git a/src/Uno.UI/Microsoft/UI/Xaml/Controls/IconSource/AnimatedIconSource.cs b/src/Uno.UI/Microsoft/UI/Xaml/Controls/IconSource/AnimatedIconSource.cs new file mode 100644 index 000000000000..1b8ffc90ca0c --- /dev/null +++ b/src/Uno.UI/Microsoft/UI/Xaml/Controls/IconSource/AnimatedIconSource.cs @@ -0,0 +1,74 @@ +using Windows.UI.Xaml; +using Windows.UI.Xaml.Controls; + +namespace Microsoft.UI.Xaml.Controls +{ + public class AnimatedIconSource : IconSource + { + public IconSource FallbackIconSource + { + get => (IconSource)GetValue(FallbackIconSourceProperty); + set => SetValue(FallbackIconSourceProperty, value); + } + + public static DependencyProperty FallbackIconSourceProperty { get; } = + DependencyProperty.Register(nameof(FallbackIconSource), typeof(IconSource), typeof(AnimatedIconSource), new PropertyMetadata(null, OnFallbackIconSourcePropertyChanged)); + + public bool MirroredWhenRightToLeft + { + get => (bool)GetValue(MirroredWhenRightToLeftProperty); + set => SetValue(MirroredWhenRightToLeftProperty, value); + } + + public static DependencyProperty MirroredWhenRightToLeftProperty { get; } = + DependencyProperty.Register(nameof(MirroredWhenRightToLeft), typeof(bool), typeof(AnimatedIconSource), new PropertyMetadata(false, OnMirroredWhenRightToLeftPropertyChanged)); + + public IAnimatedVisualSource2 Source + { + get => (IAnimatedVisualSource2)GetValue(SourceProperty); + set => SetValue(SourceProperty, value); + } + + public static DependencyProperty SourceProperty { get; } = + DependencyProperty.Register(nameof(Source), typeof(IAnimatedVisualSource2), typeof(AnimatedIconSource), new PropertyMetadata(null, OnSourcePropertyChanged)); + + public override IconElement CreateIconElement() + { + AnimatedIcon animatedIcon = new AnimatedIcon(); + if (Source is { } source) + { + animatedIcon.Source = source; + } + if (FallbackIconSource is { } fallbackIconSource) + { + animatedIcon.FallbackIconSource = fallbackIconSource; + } + if (Foreground is { } newForeground) + { + animatedIcon.Foreground = newForeground; + } + animatedIcon.MirroredWhenRightToLeft = MirroredWhenRightToLeft; + + return animatedIcon; + } + + protected override DependencyProperty GetIconElementProperty(DependencyProperty sourceProperty) + { + if (sourceProperty == SourceProperty) + { + return AnimatedIcon.SourceProperty; + } + else if (sourceProperty == FallbackIconSourceProperty) + { + return AnimatedIcon.FallbackIconSourceProperty; + } + else if (sourceProperty == MirroredWhenRightToLeftProperty) + { + return AnimatedIcon.MirroredWhenRightToLeftProperty; + } + + return base.GetIconElementProperty(sourceProperty); + } + + } +} diff --git a/src/Uno.UI/Microsoft/UI/Xaml/Controls/IconSource/IconSource.cs b/src/Uno.UI/Microsoft/UI/Xaml/Controls/IconSource/IconSource.cs index 304494f10cd6..019be320fd89 100644 --- a/src/Uno.UI/Microsoft/UI/Xaml/Controls/IconSource/IconSource.cs +++ b/src/Uno.UI/Microsoft/UI/Xaml/Controls/IconSource/IconSource.cs @@ -22,5 +22,7 @@ public Brush Foreground #nullable enable public virtual IconElement? CreateIconElement() => default; + + protected virtual DependencyProperty? GetIconElementProperty(DependencyProperty sourceProperty) => default; } }