Animation.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (c) 2023, Matthew Olsson <mattco@serenityos.org>.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibJS/Runtime/PromiseCapability.h>
  8. #include <LibWeb/Bindings/AnimationPrototype.h>
  9. #include <LibWeb/DOM/EventTarget.h>
  10. namespace Web::Animations {
  11. // https://www.w3.org/TR/web-animations-1/#the-animation-interface
  12. class Animation : public DOM::EventTarget {
  13. WEB_PLATFORM_OBJECT(Animation, DOM::EventTarget);
  14. JS_DECLARE_ALLOCATOR(Animation);
  15. public:
  16. static JS::NonnullGCPtr<Animation> create(JS::Realm&, JS::GCPtr<AnimationEffect>, JS::GCPtr<AnimationTimeline>);
  17. static WebIDL::ExceptionOr<JS::NonnullGCPtr<Animation>> construct_impl(JS::Realm&, JS::GCPtr<AnimationEffect>, JS::GCPtr<AnimationTimeline>);
  18. FlyString const& id() const { return m_id; }
  19. void set_id(FlyString value) { m_id = move(value); }
  20. JS::GCPtr<AnimationEffect> effect() const { return m_effect; }
  21. void set_effect(JS::GCPtr<AnimationEffect>);
  22. JS::GCPtr<AnimationTimeline> timeline() const { return m_timeline; }
  23. void set_timeline(JS::GCPtr<AnimationTimeline>);
  24. Optional<double> const& start_time() const { return m_start_time; }
  25. void set_start_time(Optional<double> const&);
  26. Optional<double> current_time() const;
  27. WebIDL::ExceptionOr<void> set_current_time(Optional<double> const&);
  28. double playback_rate() const { return m_playback_rate; }
  29. WebIDL::ExceptionOr<void> set_playback_rate(double value);
  30. Bindings::AnimationPlayState play_state() const;
  31. Bindings::AnimationReplaceState replace_state() const { return m_replace_state; }
  32. // https://www.w3.org/TR/web-animations-1/#dom-animation-pending
  33. bool pending() const { return m_pending_pause_task != TaskState::None || m_pending_play_task != TaskState::None; }
  34. // https://www.w3.org/TR/web-animations-1/#dom-animation-ready
  35. JS::NonnullGCPtr<JS::Object> ready() const { return *current_ready_promise()->promise(); }
  36. // https://www.w3.org/TR/web-animations-1/#dom-animation-finished
  37. JS::NonnullGCPtr<JS::Object> finished() const { return *current_finished_promise()->promise(); }
  38. Optional<double> convert_an_animation_time_to_timeline_time(Optional<double>) const;
  39. Optional<double> convert_a_timeline_time_to_an_origin_relative_time(Optional<double>) const;
  40. JS::GCPtr<DOM::Document> document_for_timing() const;
  41. protected:
  42. Animation(JS::Realm&);
  43. virtual void initialize(JS::Realm&) override;
  44. virtual void visit_edges(Cell::Visitor&) override;
  45. private:
  46. enum class TaskState {
  47. None,
  48. Pending,
  49. RunAsSoonAsReady,
  50. };
  51. enum class DidSeek {
  52. Yes,
  53. No,
  54. };
  55. enum class SynchronouslyNotify {
  56. Yes,
  57. No,
  58. };
  59. double associated_effect_end() const;
  60. double effective_playback_rate() const;
  61. void apply_any_pending_playback_rate();
  62. WebIDL::ExceptionOr<void> silently_set_current_time(Optional<double>);
  63. void update_finished_state(DidSeek, SynchronouslyNotify);
  64. JS::NonnullGCPtr<WebIDL::Promise> current_ready_promise() const;
  65. JS::NonnullGCPtr<WebIDL::Promise> current_finished_promise() const;
  66. // https://www.w3.org/TR/web-animations-1/#dom-animation-id
  67. FlyString m_id;
  68. // https://www.w3.org/TR/web-animations-1/#dom-animation-effect
  69. JS::GCPtr<AnimationEffect> m_effect;
  70. // https://www.w3.org/TR/web-animations-1/#dom-animation-timeline
  71. JS::GCPtr<AnimationTimeline> m_timeline;
  72. // https://www.w3.org/TR/web-animations-1/#animation-start-time
  73. Optional<double> m_start_time {};
  74. // https://www.w3.org/TR/web-animations-1/#animation-hold-time
  75. Optional<double> m_hold_time {};
  76. // https://www.w3.org/TR/web-animations-1/#previous-current-time
  77. Optional<double> m_previous_current_time {};
  78. // https://www.w3.org/TR/web-animations-1/#playback-rate
  79. double m_playback_rate { 1.0 };
  80. // https://www.w3.org/TR/web-animations-1/#pending-playback-rate
  81. Optional<double> m_pending_playback_rate {};
  82. // https://www.w3.org/TR/web-animations-1/#dom-animation-replacestate
  83. Bindings::AnimationReplaceState m_replace_state { Bindings::AnimationReplaceState::Active };
  84. // Note: The following promises are initialized lazily to avoid constructing them outside of an execution context
  85. // https://www.w3.org/TR/web-animations-1/#current-ready-promise
  86. mutable JS::GCPtr<WebIDL::Promise> m_current_ready_promise;
  87. // https://www.w3.org/TR/web-animations-1/#current-finished-promise
  88. mutable JS::GCPtr<WebIDL::Promise> m_current_finished_promise;
  89. bool m_current_finished_promise_resolved { false };
  90. // https://www.w3.org/TR/web-animations-1/#pending-play-task
  91. TaskState m_pending_play_task { TaskState::None };
  92. // https://www.w3.org/TR/web-animations-1/#pending-pause-task
  93. TaskState m_pending_pause_task { TaskState::None };
  94. // Flags used to manage the finish notification microtask and ultimately prevent more than one finish notification
  95. // microtask from being queued at any given time
  96. bool m_should_abort_finish_notification_microtask { false };
  97. bool m_has_finish_notification_microtask_scheduled { false };
  98. };
  99. }