Bläddra i källkod

LibWeb: Keep track of associated AnimationEffects in Animatable

Matthew Olsson 1 år sedan
förälder
incheckning
5eea53f27a

+ 10 - 0
Userland/Libraries/LibWeb/Animations/Animatable.cpp

@@ -61,4 +61,14 @@ Vector<JS::NonnullGCPtr<Animation>> Animatable::get_animations(Web::Animations::
     return {};
     return {};
 }
 }
 
 
+void Animatable::associate_with_effect(JS::NonnullGCPtr<AnimationEffect> effect)
+{
+    m_associated_effects.append(effect);
+}
+
+void Animatable::disassociate_with_effect(JS::NonnullGCPtr<AnimationEffect> effect)
+{
+    m_associated_effects.remove_first_matching([&](auto element) { return effect == element; });
+}
+
 }
 }

+ 6 - 0
Userland/Libraries/LibWeb/Animations/Animatable.h

@@ -29,6 +29,12 @@ public:
 
 
     WebIDL::ExceptionOr<JS::NonnullGCPtr<Animation>> animate(Optional<JS::Handle<JS::Object>> keyframes, Variant<Empty, double, KeyframeAnimationOptions> options = {});
     WebIDL::ExceptionOr<JS::NonnullGCPtr<Animation>> animate(Optional<JS::Handle<JS::Object>> keyframes, Variant<Empty, double, KeyframeAnimationOptions> options = {});
     Vector<JS::NonnullGCPtr<Animation>> get_animations(GetAnimationsOptions options = {});
     Vector<JS::NonnullGCPtr<Animation>> get_animations(GetAnimationsOptions options = {});
+
+    void associate_with_effect(JS::NonnullGCPtr<AnimationEffect> effect);
+    void disassociate_with_effect(JS::NonnullGCPtr<AnimationEffect> effect);
+
+private:
+    Vector<JS::NonnullGCPtr<AnimationEffect>> m_associated_effects;
 };
 };
 
 
 }
 }

+ 15 - 0
Userland/Libraries/LibWeb/Animations/KeyframeEffect.cpp

@@ -732,6 +732,15 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<KeyframeEffect>> KeyframeEffect::construct_
     return effect;
     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<String> pseudo_element)
 void KeyframeEffect::set_pseudo_element(Optional<String> pseudo_element)
 {
 {
     // On setting, sets the target pseudo-selector of the animation effect to the provided value after applying the
     // 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)
 void KeyframeEffect::initialize(JS::Realm& realm)
 {
 {
     Base::initialize(realm);
     Base::initialize(realm);

+ 2 - 1
Userland/Libraries/LibWeb/Animations/KeyframeEffect.h

@@ -82,7 +82,7 @@ public:
     static WebIDL::ExceptionOr<JS::NonnullGCPtr<KeyframeEffect>> construct_impl(JS::Realm&, JS::NonnullGCPtr<KeyframeEffect> source);
     static WebIDL::ExceptionOr<JS::NonnullGCPtr<KeyframeEffect>> construct_impl(JS::Realm&, JS::NonnullGCPtr<KeyframeEffect> source);
 
 
     DOM::Element* target() const override { return m_target_element; }
     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<String> pseudo_element() const { return m_target_pseudo_selector; }
     Optional<String> pseudo_element() const { return m_target_pseudo_selector; }
     void set_pseudo_element(Optional<String>);
     void set_pseudo_element(Optional<String>);
@@ -98,6 +98,7 @@ public:
 
 
 private:
 private:
     KeyframeEffect(JS::Realm&);
     KeyframeEffect(JS::Realm&);
+    virtual ~KeyframeEffect() override;
 
 
     virtual void initialize(JS::Realm&) override;
     virtual void initialize(JS::Realm&) override;
     virtual void visit_edges(Cell::Visitor&) override;
     virtual void visit_edges(Cell::Visitor&) override;