Skip to content

Commit

Permalink
Fix foreground brush color property changing not affecting animated i…
Browse files Browse the repository at this point in the history
…con's visuals. (#4646)
  • Loading branch information
StephenLPeters authored May 3, 2021
1 parent b853109 commit aca8c13
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 37 deletions.
34 changes: 24 additions & 10 deletions dev/AnimatedIcon/AnimatedIcon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,8 @@ void AnimatedIcon::OnApplyTemplate()
{
winrt::ElementCompositionPreview::SetElementChildVisual(panel, visual.RootVisual());
}
if (auto const source = Source())
{
TrySetForegroundProperty(source);
}

TrySetForegroundProperty();
}
}

Expand Down Expand Up @@ -530,17 +528,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<winrt::SolidColorBrush>())
{
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<winrt::Color>());
}

void AnimatedIcon::TrySetForegroundProperty(winrt::IAnimatedVisualSource2 const& source)
{
if (source)
if (auto const foregroundSolidColorBrush = Foreground().try_as<winrt::SolidColorBrush>())
{
if (auto const ForegroundSolidColorBrush = Foreground().try_as<winrt::SolidColorBrush>())
{
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);
}
}

Expand Down
5 changes: 4 additions & 1 deletion dev/AnimatedIcon/AnimatedIcon.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,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<winrt::IAnimatedVisual> m_animatedVisual{ this };
Expand All @@ -79,6 +81,7 @@ class AnimatedIcon :
ScopedBatchCompleted_revoker m_batchCompletedRevoker{ };
PropertyChanged_revoker m_ancestorStatePropertyChangedRevoker{};
winrt::FrameworkElement::LayoutUpdated_revoker m_layoutUpdatedRevoker{};
PropertyChanged_revoker m_foregroundColorPropertyChangedRevoker{};

winrt::AnimatedIconAnimationQueueBehavior m_queueBehavior{ winrt::AnimatedIconAnimationQueueBehavior::SpeedUpQueueOne };
};
Loading

0 comments on commit aca8c13

Please sign in to comment.