From 145ae54718072434c0c1f3f60a4bb4fdde8f8ab7 Mon Sep 17 00:00:00 2001 From: Matthew Olsson Date: Sat, 3 Feb 2024 18:54:49 -0700 Subject: [PATCH] LibWeb: Add a few Animation/AnimationEffect getters --- .../Libraries/LibWeb/Animations/Animation.cpp | 9 ++++ .../Libraries/LibWeb/Animations/Animation.h | 2 + .../LibWeb/Animations/AnimationEffect.cpp | 41 +++++++++++++++++++ .../LibWeb/Animations/AnimationEffect.h | 3 ++ .../LibWeb/Animations/AnimationTimeline.cpp | 1 - 5 files changed, 55 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibWeb/Animations/Animation.cpp b/Userland/Libraries/LibWeb/Animations/Animation.cpp index 223802197b..3bac7d96a1 100644 --- a/Userland/Libraries/LibWeb/Animations/Animation.cpp +++ b/Userland/Libraries/LibWeb/Animations/Animation.cpp @@ -298,6 +298,15 @@ Bindings::AnimationPlayState Animation::play_state() const return Bindings::AnimationPlayState::Running; } +// https://www.w3.org/TR/web-animations-1/#animation-relevant +bool Animation::is_relevant() const +{ + // An animation is relevant if: + // - Its associated effect is current or in effect, and + // - Its replace state is not removed. + return (m_effect->is_current() || m_effect->is_in_effect()) && replace_state() != Bindings::AnimationReplaceState::Removed; +} + // https://www.w3.org/TR/web-animations-1/#replaceable-animation bool Animation::is_replaceable() const { diff --git a/Userland/Libraries/LibWeb/Animations/Animation.h b/Userland/Libraries/LibWeb/Animations/Animation.h index 5c5b999656..350a95953b 100644 --- a/Userland/Libraries/LibWeb/Animations/Animation.h +++ b/Userland/Libraries/LibWeb/Animations/Animation.h @@ -50,6 +50,8 @@ public: Bindings::AnimationPlayState play_state() const; + bool is_relevant() const; + bool is_replaceable() const; Bindings::AnimationReplaceState replace_state() const { return m_replace_state; } diff --git a/Userland/Libraries/LibWeb/Animations/AnimationEffect.cpp b/Userland/Libraries/LibWeb/Animations/AnimationEffect.cpp index e8192630c5..c2cbad8cff 100644 --- a/Userland/Libraries/LibWeb/Animations/AnimationEffect.cpp +++ b/Userland/Libraries/LibWeb/Animations/AnimationEffect.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -264,6 +265,46 @@ Optional AnimationEffect::active_time_using_fill(Bindings::FillMode fill return {}; } +// https://www.w3.org/TR/web-animations-1/#in-play +bool AnimationEffect::is_in_play() const +{ + // An animation effect is in play if all of the following conditions are met: + // - the animation effect is in the active phase, and + // - the animation effect is associated with an animation that is not finished. + return is_in_the_active_phase() && m_associated_animation && !m_associated_animation->is_finished(); +} + +// https://www.w3.org/TR/web-animations-1/#current +bool AnimationEffect::is_current() const +{ + // An animation effect is current if any of the following conditions are true: + + // - the animation effect is in play, or + if (is_in_play()) + return true; + + if (auto animation = m_associated_animation) { + auto playback_rate = animation->playback_rate(); + + // - the animation effect is associated with an animation with a playback rate > 0 and the animation effect is + // in the before phase, or + if (playback_rate > 0.0 && is_in_the_before_phase()) + return true; + + // - the animation effect is associated with an animation with a playback rate < 0 and the animation effect is + // in the after phase, or + if (playback_rate < 0.0 && is_in_the_after_phase()) + return true; + + // - the animation effect is associated with an animation not in the idle play state with a non-null associated + // timeline that is not monotonically increasing. + if (animation->play_state() != Bindings::AnimationPlayState::Idle && animation->timeline() && !animation->timeline()->is_monotonically_increasing()) + return true; + } + + return false; +} + // https://www.w3.org/TR/web-animations-1/#in-effect bool AnimationEffect::is_in_effect() const { diff --git a/Userland/Libraries/LibWeb/Animations/AnimationEffect.h b/Userland/Libraries/LibWeb/Animations/AnimationEffect.h index d74af2557b..7713e2d239 100644 --- a/Userland/Libraries/LibWeb/Animations/AnimationEffect.h +++ b/Userland/Libraries/LibWeb/Animations/AnimationEffect.h @@ -104,6 +104,9 @@ public: double active_duration() const; Optional active_time() const; Optional active_time_using_fill(Bindings::FillMode) const; + + bool is_in_play() const; + bool is_current() const; bool is_in_effect() const; double before_active_boundary_time() const; diff --git a/Userland/Libraries/LibWeb/Animations/AnimationTimeline.cpp b/Userland/Libraries/LibWeb/Animations/AnimationTimeline.cpp index 54ee26022b..7d29b9b8bd 100644 --- a/Userland/Libraries/LibWeb/Animations/AnimationTimeline.cpp +++ b/Userland/Libraries/LibWeb/Animations/AnimationTimeline.cpp @@ -23,7 +23,6 @@ void AnimationTimeline::set_current_time(Optional value) } m_current_time = value; - for (auto& animation : m_associated_animations) animation->notify_timeline_time_did_change(); }