Animation.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Copyright (c) 2023, Matthew Olsson <mattco@serenityos.org>.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Animations/Animation.h>
  7. #include <LibWeb/Animations/AnimationEffect.h>
  8. #include <LibWeb/Animations/DocumentTimeline.h>
  9. #include <LibWeb/Bindings/Intrinsics.h>
  10. #include <LibWeb/DOM/Document.h>
  11. #include <LibWeb/HTML/Window.h>
  12. #include <LibWeb/WebIDL/ExceptionOr.h>
  13. #include <LibWeb/WebIDL/Promise.h>
  14. namespace Web::Animations {
  15. // https://www.w3.org/TR/web-animations-1/#dom-animation-animation
  16. JS::NonnullGCPtr<Animation> Animation::create(JS::Realm& realm, JS::GCPtr<AnimationEffect> effect, JS::GCPtr<AnimationTimeline> timeline)
  17. {
  18. // 1. Let animation be a new Animation object.
  19. auto animation = realm.heap().allocate<Animation>(realm, realm);
  20. // 2. Run the procedure to set the timeline of an animation on animation passing timeline as the new timeline or, if
  21. // a timeline argument is missing, passing the default document timeline of the Document associated with the
  22. // Window that is the current global object.
  23. if (!timeline) {
  24. auto& window = verify_cast<HTML::Window>(HTML::current_global_object());
  25. timeline = window.associated_document().timeline();
  26. }
  27. animation->set_timeline(timeline);
  28. // 3. Run the procedure to set the associated effect of an animation on animation passing source as the new effect.
  29. animation->set_effect(effect);
  30. return animation;
  31. }
  32. WebIDL::ExceptionOr<JS::NonnullGCPtr<Animation>> Animation::construct_impl(JS::Realm& realm, JS::GCPtr<AnimationEffect> effect, JS::GCPtr<AnimationTimeline> timeline)
  33. {
  34. return create(realm, effect, timeline);
  35. }
  36. // https://www.w3.org/TR/web-animations-1/#animation-set-the-associated-effect-of-an-animation
  37. void Animation::set_effect(JS::GCPtr<AnimationEffect> new_effect)
  38. {
  39. // Setting this attribute updates the object’s associated effect using the procedure to set the associated effect of
  40. // an animation.
  41. // 1. Let old effect be the current associated effect of animation, if any.
  42. auto old_effect = m_effect;
  43. // 2. If new effect is the same object as old effect, abort this procedure.
  44. if (new_effect == old_effect)
  45. return;
  46. // FIXME: 3. If animation has a pending pause task, reschedule that task to run as soon as animation is ready.
  47. // FIXME: 4. If animation has a pending play task, reschedule that task to run as soon as animation is ready to play
  48. // new effect.
  49. // 5. If new effect is not null and if new effect is the associated effect of another animation, previous animation,
  50. // run the procedure to set the associated effect of an animation (this procedure) on previous animation passing
  51. // null as new effect.
  52. if (new_effect && new_effect->associated_animation() != this) {
  53. if (auto animation = new_effect->associated_animation())
  54. animation->set_effect({});
  55. }
  56. // 6. Let the associated effect of animation be new effect.
  57. if (new_effect)
  58. new_effect->set_associated_animation(this);
  59. if (m_effect)
  60. m_effect->set_associated_animation({});
  61. m_effect = new_effect;
  62. // FIXME: 7. Run the procedure to update an animation’s finished state for animation with the did seek flag set to
  63. // false, and the synchronously notify flag set to false.
  64. }
  65. // https://www.w3.org/TR/web-animations-1/#animation-set-the-timeline-of-an-animation
  66. void Animation::set_timeline(JS::GCPtr<AnimationTimeline> new_timeline)
  67. {
  68. // Setting this attribute updates the object’s timeline using the procedure to set the timeline of an animation.
  69. // 1. Let old timeline be the current timeline of animation, if any.
  70. auto old_timeline = m_timeline;
  71. // 2. If new timeline is the same object as old timeline, abort this procedure.
  72. if (new_timeline == old_timeline)
  73. return;
  74. // 3. Let the timeline of animation be new timeline.
  75. if (m_timeline)
  76. m_timeline->disassociate_with_animation(*this);
  77. m_timeline = new_timeline;
  78. m_timeline->associate_with_animation(*this);
  79. // 4. If the start time of animation is resolved, make animation’s hold time unresolved.
  80. if (m_start_time.has_value())
  81. m_hold_time = {};
  82. // FIXME: 5. Run the procedure to update an animation’s finished state for animation with the did seek flag set to
  83. // false, and the synchronously notify flag set to false.
  84. }
  85. // https://www.w3.org/TR/web-animations-1/#dom-animation-starttime
  86. // https://www.w3.org/TR/web-animations-1/#set-the-start-time
  87. void Animation::set_start_time(Optional<double> const& new_start_time)
  88. {
  89. // Setting this attribute updates the start time using the procedure to set the start time of this object to the new
  90. // value.
  91. // 1. Let timeline time be the current time value of the timeline that animation is associated with. If there is no
  92. // timeline associated with animation or the associated timeline is inactive, let the timeline time be
  93. // unresolved.
  94. auto timeline_time = m_timeline && !m_timeline->is_inactive() ? m_timeline->current_time() : Optional<double> {};
  95. // 2. If timeline time is unresolved and new start time is resolved, make animation’s hold time unresolved.
  96. if (!timeline_time.has_value() && new_start_time.has_value())
  97. m_hold_time = {};
  98. // 3. Let previous current time be animation’s current time.
  99. auto previous_current_time = current_time();
  100. // 4. Apply any pending playback rate on animation.
  101. apply_any_pending_playback_rate();
  102. // 5. Set animation’s start time to new start time.
  103. m_start_time = new_start_time;
  104. // 6. Update animation’s hold time based on the first matching condition from the following,
  105. // -> If new start time is resolved,
  106. if (new_start_time.has_value()) {
  107. // If animation’s playback rate is not zero, make animation’s hold time unresolved.
  108. if (m_playback_rate != 0.0)
  109. m_hold_time = {};
  110. }
  111. // -> Otherwise (new start time is unresolved),
  112. else {
  113. // Set animation’s hold time to previous current time even if previous current time is unresolved.
  114. m_hold_time = previous_current_time;
  115. }
  116. // FIXME: 7. If animation has a pending play task or a pending pause task, cancel that task and resolve animation’s
  117. // current ready promise with animation.
  118. // FIXME: 8. Run the procedure to update an animation’s finished state for animation with the did seek flag set to
  119. // true, and the synchronously notify flag set to false.
  120. }
  121. // https://www.w3.org/TR/web-animations-1/#animation-current-time
  122. Optional<double> Animation::current_time() const
  123. {
  124. // FIXME: Implement
  125. return {};
  126. }
  127. // https://www.w3.org/TR/web-animations-1/#animation-set-the-current-time
  128. WebIDL::ExceptionOr<void> Animation::set_current_time(Optional<double> const& seek_time)
  129. {
  130. // FIXME: Implement
  131. (void)seek_time;
  132. return {};
  133. }
  134. // https://www.w3.org/TR/web-animations-1/#dom-animation-playbackrate
  135. // https://www.w3.org/TR/web-animations-1/#set-the-playback-rate
  136. WebIDL::ExceptionOr<void> Animation::set_playback_rate(double new_playback_rate)
  137. {
  138. // FIXME: Implement
  139. (void)new_playback_rate;
  140. return {};
  141. }
  142. // https://www.w3.org/TR/web-animations-1/#animation-play-state
  143. Bindings::AnimationPlayState Animation::play_state() const
  144. {
  145. // FIXME: Implement
  146. return Bindings::AnimationPlayState::Idle;
  147. }
  148. // https://www.w3.org/TR/web-animations-1/#apply-any-pending-playback-rate
  149. void Animation::apply_any_pending_playback_rate()
  150. {
  151. // 1. If animation does not have a pending playback rate, abort these steps.
  152. if (!m_pending_playback_rate.has_value())
  153. return;
  154. // 2. Set animation’s playback rate to its pending playback rate.
  155. m_playback_rate = m_pending_playback_rate.value();
  156. // 3. Clear animation’s pending playback rate.
  157. m_pending_playback_rate = {};
  158. }
  159. JS::NonnullGCPtr<WebIDL::Promise> Animation::current_ready_promise() const
  160. {
  161. if (!m_current_ready_promise) {
  162. // The current ready promise is initially a resolved Promise created using the procedure to create a new
  163. // resolved Promise with the animation itself as its value and created in the relevant Realm of the animation.
  164. m_current_ready_promise = WebIDL::create_resolved_promise(realm(), this);
  165. }
  166. return *m_current_ready_promise;
  167. }
  168. JS::NonnullGCPtr<WebIDL::Promise> Animation::current_finished_promise() const
  169. {
  170. if (!m_current_finished_promise) {
  171. // The current finished promise is initially a pending Promise object.
  172. m_current_finished_promise = WebIDL::create_promise(realm());
  173. }
  174. return *m_current_finished_promise;
  175. }
  176. Animation::Animation(JS::Realm& realm)
  177. : DOM::EventTarget(realm)
  178. {
  179. }
  180. void Animation::initialize(JS::Realm& realm)
  181. {
  182. Base::initialize(realm);
  183. set_prototype(&Bindings::ensure_web_prototype<Bindings::AnimationPrototype>(realm, "Animation"));
  184. }
  185. void Animation::visit_edges(Cell::Visitor& visitor)
  186. {
  187. Base::visit_edges(visitor);
  188. visitor.visit(m_effect);
  189. visitor.visit(m_timeline);
  190. visitor.visit(m_current_ready_promise);
  191. visitor.visit(m_current_finished_promise);
  192. }
  193. }