Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the animated icon foreground. #4646

Merged
merged 2 commits into from
May 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 24 additions & 10 deletions dev/AnimatedIcon/AnimatedIcon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,8 @@ void AnimatedIcon::OnApplyTemplate()
{
winrt::ElementCompositionPreview::SetElementChildVisual(panel, visual.RootVisual());
}
if (auto const source = Source())
{
TrySetForegroundProperty(source);
}

TrySetForegroundProperty();
}
}

Expand Down Expand Up @@ -515,17 +513,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 @@ -50,9 +50,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 @@ -78,6 +80,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