From 015e054f097623cbb23af62e1fd3c206378ca3d6 Mon Sep 17 00:00:00 2001 From: Stephen Peters Date: Wed, 24 Mar 2021 15:31:14 -0700 Subject: [PATCH] Fix foreground brush color property changing not affecting animated icon's visuals. --- dev/AnimatedIcon/AnimatedIcon.cpp | 34 +++++--- dev/AnimatedIcon/AnimatedIcon.h | 5 +- dev/AnimatedIcon/TestUI/AnimatedIconPage.xaml | 79 +++++++++++++------ .../TestUI/AnimatedIcon_TestUI.projitems | 1 + .../TestUI/ColorToSolidColorBrushConverter.cs | 22 ++++++ 5 files changed, 104 insertions(+), 37 deletions(-) create mode 100644 dev/AnimatedIcon/TestUI/ColorToSolidColorBrushConverter.cs diff --git a/dev/AnimatedIcon/AnimatedIcon.cpp b/dev/AnimatedIcon/AnimatedIcon.cpp index 7adde6ffda..50a9dd9208 100644 --- a/dev/AnimatedIcon/AnimatedIcon.cpp +++ b/dev/AnimatedIcon/AnimatedIcon.cpp @@ -51,10 +51,8 @@ void AnimatedIcon::OnApplyTemplate() { winrt::ElementCompositionPreview::SetElementChildVisual(panel, visual.RootVisual()); } - if (auto const source = Source()) - { - TrySetForegroundProperty(source); - } + + TrySetForegroundProperty(); } } @@ -496,17 +494,33 @@ void AnimatedIcon::SetRootPanelChildToFallbackIcon() void AnimatedIcon::OnForegroundPropertyChanged(const winrt::DependencyObject& sender, const winrt::DependencyProperty& args) { - TrySetForegroundProperty(Source()); + m_foregroundColorPropertyChangedRevoker.revoke(); + if (auto const foregroundSolidColorBrush = Foreground().try_as()) + { + m_foregroundColorPropertyChangedRevoker = RegisterPropertyChanged(foregroundSolidColorBrush, winrt::SolidColorBrush::ColorProperty(), { this, &AnimatedIcon::OnForegroundBrushColorPropertyChanged }); + TrySetForegroundProperty(foregroundSolidColorBrush.Color()); + } +} + +void AnimatedIcon::OnForegroundBrushColorPropertyChanged(const winrt::DependencyObject& sender, const winrt::DependencyProperty& args) +{ + TrySetForegroundProperty(sender.GetValue(args).as()); } void AnimatedIcon::TrySetForegroundProperty(winrt::IAnimatedVisualSource2 const& source) { - if (source) + if (auto const foregroundSolidColorBrush = Foreground().try_as()) { - if (auto const ForegroundSolidColorBrush = Foreground().try_as()) - { - source.SetColorProperty(s_foregroundPropertyName, ForegroundSolidColorBrush.Color()); - } + TrySetForegroundProperty(foregroundSolidColorBrush.Color(), source); + } +} + +void AnimatedIcon::TrySetForegroundProperty(winrt::Color color, winrt::IAnimatedVisualSource2 const& source) +{ + auto const localSource = source ? source : Source(); + if (localSource) + { + localSource.SetColorProperty(s_foregroundPropertyName, color); } } diff --git a/dev/AnimatedIcon/AnimatedIcon.h b/dev/AnimatedIcon/AnimatedIcon.h index 907edbb82b..e0a12e35d3 100644 --- a/dev/AnimatedIcon/AnimatedIcon.h +++ b/dev/AnimatedIcon/AnimatedIcon.h @@ -47,9 +47,11 @@ class AnimatedIcon : void TransitionAndUpdateStates(const winrt::hstring& fromState, const winrt::hstring& toState, float playbackMultiplier = 1.0f); void TransitionStates(const winrt::hstring& fromState, const winrt::hstring& toState, float playtbackMultiplier = 1.0f); void PlaySegment(float from, float to, float playbackMultiplier = 1.0f); - void TrySetForegroundProperty(winrt::IAnimatedVisualSource2 const& source); + void TrySetForegroundProperty(winrt::Color color, winrt::IAnimatedVisualSource2 const& source = nullptr); + void TrySetForegroundProperty(winrt::IAnimatedVisualSource2 const& source = nullptr); void OnAnimationCompleted(winrt::IInspectable const&, winrt::CompositionBatchCompletedEventArgs const&); void OnForegroundPropertyChanged(const winrt::DependencyObject& sender, const winrt::DependencyProperty& args); + void OnForegroundBrushColorPropertyChanged(const winrt::DependencyObject& sender, const winrt::DependencyProperty& args); void SetRootPanelChildToFallbackIcon(); tracker_ref m_animatedVisual{ this }; @@ -74,6 +76,7 @@ class AnimatedIcon : ScopedBatchCompleted_revoker m_batchCompletedRevoker{ }; winrt::FrameworkElement::LayoutUpdated_revoker m_layoutUpdatedRevoker{}; + PropertyChanged_revoker m_foregroundColorPropertyChangedRevoker{}; winrt::AnimatedIconAnimationQueueBehavior m_queueBehavior{ winrt::AnimatedIconAnimationQueueBehavior::SpeedUpQueueOne }; }; diff --git a/dev/AnimatedIcon/TestUI/AnimatedIconPage.xaml b/dev/AnimatedIcon/TestUI/AnimatedIconPage.xaml index 5dddf1b4d3..d7e05be799 100644 --- a/dev/AnimatedIcon/TestUI/AnimatedIconPage.xaml +++ b/dev/AnimatedIcon/TestUI/AnimatedIconPage.xaml @@ -15,7 +15,8 @@ xmlns:myAnimatedvisuals="using:AnimatedVisuals" mc:Ignorable="d"> - + + #80808080 + + @@ -144,17 +147,32 @@ + + - + + + - + @@ -172,7 +190,7 @@ - + @@ -190,7 +208,7 @@ - + @@ -211,7 +229,7 @@ - + @@ -229,7 +247,7 @@ - + @@ -247,7 +265,7 @@ - + @@ -268,7 +286,7 @@ - + @@ -286,7 +304,7 @@ - + @@ -304,7 +322,7 @@ - + @@ -325,7 +343,7 @@ - + @@ -340,7 +358,7 @@ - + @@ -355,7 +373,7 @@ - + @@ -373,7 +391,7 @@ - + @@ -388,7 +406,7 @@ - + @@ -403,7 +421,7 @@ - + @@ -421,7 +439,7 @@ - + @@ -438,7 +456,7 @@ - + @@ -455,7 +473,7 @@ - + @@ -475,7 +493,7 @@ - + @@ -490,7 +508,7 @@ - + @@ -505,7 +523,7 @@ - + @@ -646,8 +664,11 @@ - + + + + + + + diff --git a/dev/AnimatedIcon/TestUI/AnimatedIcon_TestUI.projitems b/dev/AnimatedIcon/TestUI/AnimatedIcon_TestUI.projitems index 8011a3996b..47125870a6 100644 --- a/dev/AnimatedIcon/TestUI/AnimatedIcon_TestUI.projitems +++ b/dev/AnimatedIcon/TestUI/AnimatedIcon_TestUI.projitems @@ -21,6 +21,7 @@ AnimatedIconPage.xaml + diff --git a/dev/AnimatedIcon/TestUI/ColorToSolidColorBrushConverter.cs b/dev/AnimatedIcon/TestUI/ColorToSolidColorBrushConverter.cs new file mode 100644 index 0000000000..8250b441c6 --- /dev/null +++ b/dev/AnimatedIcon/TestUI/ColorToSolidColorBrushConverter.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Windows.UI; +using Windows.UI.Xaml.Data; +using Windows.UI.Xaml.Media; + +namespace MUXControlsTestApp +{ + class ColorToSolidColorBrushConverter : IValueConverter + { + public object Convert(object value, Type targetType, object parameter, string language) + { + return new SolidColorBrush((Color)value); + } + + public object ConvertBack(object value, Type targetType, object parameter, string language) + { + return ((SolidColorBrush)value).Color; + } + } +}