Animation.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. // FIXME: Implement
  69. (void)new_timeline;
  70. }
  71. // https://www.w3.org/TR/web-animations-1/#dom-animation-starttime
  72. // https://www.w3.org/TR/web-animations-1/#set-the-start-time
  73. void Animation::set_start_time(Optional<double> const& new_start_time)
  74. {
  75. // FIXME: Implement
  76. (void)new_start_time;
  77. }
  78. // https://www.w3.org/TR/web-animations-1/#animation-current-time
  79. Optional<double> Animation::current_time() const
  80. {
  81. // FIXME: Implement
  82. return {};
  83. }
  84. // https://www.w3.org/TR/web-animations-1/#animation-set-the-current-time
  85. WebIDL::ExceptionOr<void> Animation::set_current_time(Optional<double> const& seek_time)
  86. {
  87. // FIXME: Implement
  88. (void)seek_time;
  89. return {};
  90. }
  91. // https://www.w3.org/TR/web-animations-1/#dom-animation-playbackrate
  92. // https://www.w3.org/TR/web-animations-1/#set-the-playback-rate
  93. WebIDL::ExceptionOr<void> Animation::set_playback_rate(double new_playback_rate)
  94. {
  95. // FIXME: Implement
  96. (void)new_playback_rate;
  97. return {};
  98. }
  99. // https://www.w3.org/TR/web-animations-1/#animation-play-state
  100. Bindings::AnimationPlayState Animation::play_state() const
  101. {
  102. // FIXME: Implement
  103. return Bindings::AnimationPlayState::Idle;
  104. }
  105. JS::NonnullGCPtr<WebIDL::Promise> Animation::current_ready_promise() const
  106. {
  107. if (!m_current_ready_promise) {
  108. // The current ready promise is initially a resolved Promise created using the procedure to create a new
  109. // resolved Promise with the animation itself as its value and created in the relevant Realm of the animation.
  110. m_current_ready_promise = WebIDL::create_resolved_promise(realm(), this);
  111. }
  112. return *m_current_ready_promise;
  113. }
  114. JS::NonnullGCPtr<WebIDL::Promise> Animation::current_finished_promise() const
  115. {
  116. if (!m_current_finished_promise) {
  117. // The current finished promise is initially a pending Promise object.
  118. m_current_finished_promise = WebIDL::create_promise(realm());
  119. }
  120. return *m_current_finished_promise;
  121. }
  122. Animation::Animation(JS::Realm& realm)
  123. : DOM::EventTarget(realm)
  124. {
  125. }
  126. void Animation::initialize(JS::Realm& realm)
  127. {
  128. Base::initialize(realm);
  129. set_prototype(&Bindings::ensure_web_prototype<Bindings::AnimationPrototype>(realm, "Animation"));
  130. }
  131. void Animation::visit_edges(Cell::Visitor& visitor)
  132. {
  133. Base::visit_edges(visitor);
  134. visitor.visit(m_effect);
  135. visitor.visit(m_timeline);
  136. visitor.visit(m_current_ready_promise);
  137. visitor.visit(m_current_finished_promise);
  138. }
  139. }