Animation.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. // FIXME: Implement
  40. (void)new_effect;
  41. }
  42. // https://www.w3.org/TR/web-animations-1/#animation-set-the-timeline-of-an-animation
  43. void Animation::set_timeline(JS::GCPtr<AnimationTimeline> new_timeline)
  44. {
  45. // FIXME: Implement
  46. (void)new_timeline;
  47. }
  48. // https://www.w3.org/TR/web-animations-1/#dom-animation-starttime
  49. // https://www.w3.org/TR/web-animations-1/#set-the-start-time
  50. void Animation::set_start_time(Optional<double> const& new_start_time)
  51. {
  52. // FIXME: Implement
  53. (void)new_start_time;
  54. }
  55. // https://www.w3.org/TR/web-animations-1/#animation-current-time
  56. Optional<double> Animation::current_time() const
  57. {
  58. // FIXME: Implement
  59. return {};
  60. }
  61. // https://www.w3.org/TR/web-animations-1/#animation-set-the-current-time
  62. WebIDL::ExceptionOr<void> Animation::set_current_time(Optional<double> const& seek_time)
  63. {
  64. // FIXME: Implement
  65. (void)seek_time;
  66. return {};
  67. }
  68. // https://www.w3.org/TR/web-animations-1/#dom-animation-playbackrate
  69. // https://www.w3.org/TR/web-animations-1/#set-the-playback-rate
  70. WebIDL::ExceptionOr<void> Animation::set_playback_rate(double new_playback_rate)
  71. {
  72. // FIXME: Implement
  73. (void)new_playback_rate;
  74. return {};
  75. }
  76. // https://www.w3.org/TR/web-animations-1/#animation-play-state
  77. Bindings::AnimationPlayState Animation::play_state() const
  78. {
  79. // FIXME: Implement
  80. return Bindings::AnimationPlayState::Idle;
  81. }
  82. JS::NonnullGCPtr<WebIDL::Promise> Animation::current_ready_promise() const
  83. {
  84. if (!m_current_ready_promise) {
  85. // The current ready promise is initially a resolved Promise created using the procedure to create a new
  86. // resolved Promise with the animation itself as its value and created in the relevant Realm of the animation.
  87. m_current_ready_promise = WebIDL::create_resolved_promise(realm(), this);
  88. }
  89. return *m_current_ready_promise;
  90. }
  91. JS::NonnullGCPtr<WebIDL::Promise> Animation::current_finished_promise() const
  92. {
  93. if (!m_current_finished_promise) {
  94. // The current finished promise is initially a pending Promise object.
  95. m_current_finished_promise = WebIDL::create_promise(realm());
  96. }
  97. return *m_current_finished_promise;
  98. }
  99. Animation::Animation(JS::Realm& realm)
  100. : DOM::EventTarget(realm)
  101. {
  102. }
  103. void Animation::initialize(JS::Realm& realm)
  104. {
  105. Base::initialize(realm);
  106. set_prototype(&Bindings::ensure_web_prototype<Bindings::AnimationPrototype>(realm, "Animation"));
  107. }
  108. void Animation::visit_edges(Cell::Visitor& visitor)
  109. {
  110. Base::visit_edges(visitor);
  111. visitor.visit(m_effect);
  112. visitor.visit(m_timeline);
  113. visitor.visit(m_current_ready_promise);
  114. visitor.visit(m_current_finished_promise);
  115. }
  116. }