From 5eea53f27a75f48b5def7add2b437e444316d0b9 Mon Sep 17 00:00:00 2001 From: Matthew Olsson Date: Sat, 3 Feb 2024 12:10:44 -0700 Subject: [PATCH] LibWeb: Keep track of associated AnimationEffects in Animatable --- .../Libraries/LibWeb/Animations/Animatable.cpp | 10 ++++++++++ Userland/Libraries/LibWeb/Animations/Animatable.h | 6 ++++++ .../LibWeb/Animations/KeyframeEffect.cpp | 15 +++++++++++++++ .../Libraries/LibWeb/Animations/KeyframeEffect.h | 3 ++- 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibWeb/Animations/Animatable.cpp b/Userland/Libraries/LibWeb/Animations/Animatable.cpp index 99d0fc8ed03..f3df72e1566 100644 --- a/Userland/Libraries/LibWeb/Animations/Animatable.cpp +++ b/Userland/Libraries/LibWeb/Animations/Animatable.cpp @@ -61,4 +61,14 @@ Vector> Animatable::get_animations(Web::Animations:: return {}; } +void Animatable::associate_with_effect(JS::NonnullGCPtr effect) +{ + m_associated_effects.append(effect); +} + +void Animatable::disassociate_with_effect(JS::NonnullGCPtr effect) +{ + m_associated_effects.remove_first_matching([&](auto element) { return effect == element; }); +} + } diff --git a/Userland/Libraries/LibWeb/Animations/Animatable.h b/Userland/Libraries/LibWeb/Animations/Animatable.h index 805b9918fd3..cb335e5bcea 100644 --- a/Userland/Libraries/LibWeb/Animations/Animatable.h +++ b/Userland/Libraries/LibWeb/Animations/Animatable.h @@ -29,6 +29,12 @@ public: WebIDL::ExceptionOr> animate(Optional> keyframes, Variant options = {}); Vector> get_animations(GetAnimationsOptions options = {}); + + void associate_with_effect(JS::NonnullGCPtr effect); + void disassociate_with_effect(JS::NonnullGCPtr effect); + +private: + Vector> m_associated_effects; }; } diff --git a/Userland/Libraries/LibWeb/Animations/KeyframeEffect.cpp b/Userland/Libraries/LibWeb/Animations/KeyframeEffect.cpp index 68440ee6290..3a776a857da 100644 --- a/Userland/Libraries/LibWeb/Animations/KeyframeEffect.cpp +++ b/Userland/Libraries/LibWeb/Animations/KeyframeEffect.cpp @@ -732,6 +732,15 @@ WebIDL::ExceptionOr> KeyframeEffect::construct_ return effect; } +void KeyframeEffect::set_target(DOM::Element* target) +{ + if (m_target_element) + m_target_element->disassociate_with_effect(*this); + m_target_element = target; + if (m_target_element) + m_target_element->associate_with_effect(*this); +} + void KeyframeEffect::set_pseudo_element(Optional pseudo_element) { // On setting, sets the target pseudo-selector of the animation effect to the provided value after applying the @@ -834,6 +843,12 @@ KeyframeEffect::KeyframeEffect(JS::Realm& realm) { } +KeyframeEffect::~KeyframeEffect() +{ + if (m_target_element) + m_target_element->disassociate_with_effect(*this); +} + void KeyframeEffect::initialize(JS::Realm& realm) { Base::initialize(realm); diff --git a/Userland/Libraries/LibWeb/Animations/KeyframeEffect.h b/Userland/Libraries/LibWeb/Animations/KeyframeEffect.h index 1c35c1f6e9a..1e10ed9efa0 100644 --- a/Userland/Libraries/LibWeb/Animations/KeyframeEffect.h +++ b/Userland/Libraries/LibWeb/Animations/KeyframeEffect.h @@ -82,7 +82,7 @@ public: static WebIDL::ExceptionOr> construct_impl(JS::Realm&, JS::NonnullGCPtr source); DOM::Element* target() const override { return m_target_element; } - void set_target(DOM::Element* target) { m_target_element = target; } + void set_target(DOM::Element* target); Optional pseudo_element() const { return m_target_pseudo_selector; } void set_pseudo_element(Optional); @@ -98,6 +98,7 @@ public: private: KeyframeEffect(JS::Realm&); + virtual ~KeyframeEffect() override; virtual void initialize(JS::Realm&) override; virtual void visit_edges(Cell::Visitor&) override;