Animation.h 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. public:
  15. static JS::NonnullGCPtr<Animation> create(JS::Realm&, JS::GCPtr<AnimationEffect>, JS::GCPtr<AnimationTimeline>);
  16. static WebIDL::ExceptionOr<JS::NonnullGCPtr<Animation>> construct_impl(JS::Realm&, JS::GCPtr<AnimationEffect>, JS::GCPtr<AnimationTimeline>);
  17. FlyString const& id() const { return m_id; }
  18. void set_id(FlyString value) { m_id = move(value); }
  19. JS::GCPtr<AnimationEffect> effect() const { return m_effect; }
  20. void set_effect(JS::GCPtr<AnimationEffect>);
  21. JS::GCPtr<AnimationTimeline> timeline() const { return m_timeline; }
  22. void set_timeline(JS::GCPtr<AnimationTimeline>);
  23. Optional<double> const& start_time() const { return m_start_time; }
  24. void set_start_time(Optional<double> const&);
  25. Optional<double> current_time() const;
  26. WebIDL::ExceptionOr<void> set_current_time(Optional<double> const&);
  27. double playback_rate() const { return m_playback_rate; }
  28. WebIDL::ExceptionOr<void> set_playback_rate(double value);
  29. Bindings::AnimationPlayState play_state() const;
  30. Bindings::AnimationReplaceState replace_state() const { return m_replace_state; }
  31. // https://www.w3.org/TR/web-animations-1/#dom-animation-ready
  32. JS::NonnullGCPtr<JS::Object> ready() const { return *current_ready_promise()->promise(); }
  33. // https://www.w3.org/TR/web-animations-1/#dom-animation-finished
  34. JS::NonnullGCPtr<JS::Object> finished() const { return *current_finished_promise()->promise(); }
  35. protected:
  36. Animation(JS::Realm&);
  37. virtual void initialize(JS::Realm&) override;
  38. virtual void visit_edges(Cell::Visitor&) override;
  39. private:
  40. void apply_any_pending_playback_rate();
  41. JS::NonnullGCPtr<WebIDL::Promise> current_ready_promise() const;
  42. JS::NonnullGCPtr<WebIDL::Promise> current_finished_promise() const;
  43. // https://www.w3.org/TR/web-animations-1/#dom-animation-id
  44. FlyString m_id;
  45. // https://www.w3.org/TR/web-animations-1/#dom-animation-effect
  46. JS::GCPtr<AnimationEffect> m_effect;
  47. // https://www.w3.org/TR/web-animations-1/#dom-animation-timeline
  48. JS::GCPtr<AnimationTimeline> m_timeline;
  49. // https://www.w3.org/TR/web-animations-1/#animation-start-time
  50. Optional<double> m_start_time {};
  51. // https://www.w3.org/TR/web-animations-1/#animation-hold-time
  52. Optional<double> m_hold_time {};
  53. // https://www.w3.org/TR/web-animations-1/#playback-rate
  54. double m_playback_rate { 1.0 };
  55. // https://www.w3.org/TR/web-animations-1/#pending-playback-rate
  56. Optional<double> m_pending_playback_rate {};
  57. // https://www.w3.org/TR/web-animations-1/#dom-animation-replacestate
  58. Bindings::AnimationReplaceState m_replace_state { Bindings::AnimationReplaceState::Active };
  59. // Note: The following promises are initialized lazily to avoid constructing them outside of an execution context
  60. // https://www.w3.org/TR/web-animations-1/#current-ready-promise
  61. mutable JS::GCPtr<WebIDL::Promise> m_current_ready_promise;
  62. // https://www.w3.org/TR/web-animations-1/#current-finished-promise
  63. mutable JS::GCPtr<WebIDL::Promise> m_current_finished_promise;
  64. };
  65. }