KeyframeEffect.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (c) 2023-2024, Matthew Olsson <mattco@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Optional.h>
  8. #include <LibWeb/Animations/AnimationEffect.h>
  9. #include <LibWeb/Bindings/KeyframeEffectPrototype.h>
  10. #include <LibWeb/Bindings/PlatformObject.h>
  11. #include <LibWeb/CSS/PropertyID.h>
  12. #include <LibWeb/CSS/StyleValue.h>
  13. #include <LibWeb/DOM/Element.h>
  14. namespace Web::Animations {
  15. using EasingValue = Variant<String, NonnullRefPtr<CSS::StyleValue const>>;
  16. // https://www.w3.org/TR/web-animations-1/#the-keyframeeffectoptions-dictionary
  17. struct KeyframeEffectOptions : public EffectTiming {
  18. Bindings::CompositeOperation composite { Bindings::CompositeOperation::Replace };
  19. Optional<String> pseudo_element {};
  20. };
  21. // https://www.w3.org/TR/web-animations-1/#dictdef-basepropertyindexedkeyframe
  22. // Note: This is an intermediate structure used only when parsing Keyframes provided by the caller in a slightly
  23. // different format. It is converted to BaseKeyframe, which is why it doesn't need to store the parsed properties
  24. struct BasePropertyIndexedKeyframe {
  25. Variant<Optional<double>, Vector<Optional<double>>> offset { Vector<Optional<double>> {} };
  26. Variant<EasingValue, Vector<EasingValue>> easing { Vector<EasingValue> {} };
  27. Variant<Bindings::CompositeOperationOrAuto, Vector<Bindings::CompositeOperationOrAuto>> composite { Vector<Bindings::CompositeOperationOrAuto> {} };
  28. HashMap<String, Vector<String>> properties {};
  29. };
  30. // https://www.w3.org/TR/web-animations-1/#dictdef-basekeyframe
  31. struct BaseKeyframe {
  32. using UnparsedProperties = HashMap<String, String>;
  33. using ParsedProperties = HashMap<CSS::PropertyID, NonnullRefPtr<CSS::StyleValue const>>;
  34. Optional<double> offset {};
  35. EasingValue easing { "linear"_string };
  36. Bindings::CompositeOperationOrAuto composite { Bindings::CompositeOperationOrAuto::Auto };
  37. Optional<double> computed_offset {};
  38. Variant<UnparsedProperties, ParsedProperties> properties { UnparsedProperties {} };
  39. UnparsedProperties& unparsed_properties() { return properties.get<UnparsedProperties>(); }
  40. ParsedProperties& parsed_properties() { return properties.get<ParsedProperties>(); }
  41. };
  42. // https://www.w3.org/TR/web-animations-1/#the-keyframeeffect-interface
  43. class KeyframeEffect : public AnimationEffect {
  44. WEB_PLATFORM_OBJECT(KeyframeEffect, AnimationEffect);
  45. JS_DECLARE_ALLOCATOR(KeyframeEffect);
  46. public:
  47. static JS::NonnullGCPtr<KeyframeEffect> create(JS::Realm&);
  48. static WebIDL::ExceptionOr<JS::NonnullGCPtr<KeyframeEffect>> construct_impl(
  49. JS::Realm&,
  50. JS::Handle<DOM::Element> const& target,
  51. Optional<JS::Handle<JS::Object>> const& keyframes,
  52. Variant<double, KeyframeEffectOptions> options = KeyframeEffectOptions {});
  53. static WebIDL::ExceptionOr<JS::NonnullGCPtr<KeyframeEffect>> construct_impl(JS::Realm&, JS::NonnullGCPtr<KeyframeEffect> source);
  54. DOM::Element* target() const override { return m_target_element; }
  55. void set_target(DOM::Element* target) { m_target_element = target; }
  56. Optional<String> pseudo_element() const { return m_target_pseudo_selector; }
  57. void set_pseudo_element(Optional<String>);
  58. Bindings::CompositeOperation composite() const { return m_composite; }
  59. void set_composite(Bindings::CompositeOperation value) { m_composite = value; }
  60. WebIDL::ExceptionOr<Vector<JS::Object*>> get_keyframes();
  61. WebIDL::ExceptionOr<void> set_keyframes(Optional<JS::Handle<JS::Object>> const&);
  62. private:
  63. KeyframeEffect(JS::Realm&);
  64. virtual void initialize(JS::Realm&) override;
  65. virtual void visit_edges(Cell::Visitor&) override;
  66. // https://www.w3.org/TR/web-animations-1/#effect-target-target-element
  67. JS::GCPtr<DOM::Element> m_target_element {};
  68. // https://www.w3.org/TR/web-animations-1/#dom-keyframeeffect-pseudoelement
  69. Optional<String> m_target_pseudo_selector {};
  70. // https://www.w3.org/TR/web-animations-1/#dom-keyframeeffect-composite
  71. Bindings::CompositeOperation m_composite { Bindings::CompositeOperation::Replace };
  72. // https://www.w3.org/TR/web-animations-1/#keyframe
  73. Vector<BaseKeyframe> m_keyframes {};
  74. // A cached version of m_keyframes suitable for returning from get_keyframes()
  75. Vector<JS::Object*> m_keyframe_objects {};
  76. };
  77. }