Procházet zdrojové kódy

LibWeb: Add AnimationTimeline IDL object

Matthew Olsson před 1 rokem
rodič
revize
521f8bd5f2

+ 5 - 0
Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp

@@ -3319,6 +3319,7 @@ void generate_namespace_implementation(IDL::Interface const& interface, StringBu
 
     generator.append(R"~~~(
 // FIXME: This is a total hack until we can figure out the namespace for a given type somehow.
+using namespace Web::Animations;
 using namespace Web::CSS;
 using namespace Web::DOM;
 using namespace Web::DOMParsing;
@@ -3537,6 +3538,7 @@ void generate_constructor_implementation(IDL::Interface const& interface, String
 
     generator.append(R"~~~(
 // FIXME: This is a total hack until we can figure out the namespace for a given type somehow.
+using namespace Web::Animations;
 using namespace Web::CSS;
 using namespace Web::DOM;
 using namespace Web::DOMParsing;
@@ -3922,6 +3924,7 @@ void generate_prototype_implementation(IDL::Interface const& interface, StringBu
     generator.append(R"~~~(
 
 // FIXME: This is a total hack until we can figure out the namespace for a given type somehow.
+using namespace Web::Animations;
 using namespace Web::Crypto;
 using namespace Web::CSS;
 using namespace Web::DOM;
@@ -4071,6 +4074,7 @@ void generate_iterator_prototype_implementation(IDL::Interface const& interface,
 
     generator.append(R"~~~(
 // FIXME: This is a total hack until we can figure out the namespace for a given type somehow.
+using namespace Web::Animations;
 using namespace Web::CSS;
 using namespace Web::DOM;
 using namespace Web::DOMParsing;
@@ -4203,6 +4207,7 @@ void generate_global_mixin_implementation(IDL::Interface const& interface, Strin
     generator.append(R"~~~(
 
 // FIXME: This is a total hack until we can figure out the namespace for a given type somehow.
+using namespace Web::Animations;
 using namespace Web::Crypto;
 using namespace Web::CSS;
 using namespace Web::DOM;

+ 56 - 0
Userland/Libraries/LibWeb/Animations/AnimationTimeline.cpp

@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2023, Matthew Olsson <mattco@serenityos.org>.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibWeb/Animations/AnimationTimeline.h>
+#include <LibWeb/DOM/Document.h>
+
+namespace Web::Animations {
+
+WebIDL::ExceptionOr<void> AnimationTimeline::set_current_time(Optional<double> value)
+{
+    if (value == m_current_time)
+        return {};
+
+    if (m_is_monotonically_increasing && m_current_time.has_value()) {
+        if (!value.has_value() || value.value() < m_current_time.value())
+            m_is_monotonically_increasing = false;
+    }
+
+    m_current_time = value;
+
+    return {};
+}
+
+void AnimationTimeline::set_associated_document(JS::GCPtr<DOM::Document> document)
+{
+    m_associated_document = document;
+}
+
+// https://www.w3.org/TR/web-animations-1/#inactive-timeline
+bool AnimationTimeline::is_inactive() const
+{
+    // A timeline is considered to be inactive when its time value is unresolved.
+    return !m_current_time.has_value();
+}
+
+AnimationTimeline::AnimationTimeline(JS::Realm& realm)
+    : Bindings::PlatformObject(realm)
+{
+}
+
+void AnimationTimeline::initialize(JS::Realm& realm)
+{
+    Base::initialize(realm);
+    set_prototype(&Bindings::ensure_web_prototype<Bindings::AnimationTimelinePrototype>(realm, "AnimationTimeline"));
+}
+
+void AnimationTimeline::visit_edges(Cell::Visitor& visitor)
+{
+    Base::visit_edges(visitor);
+    visitor.visit(m_associated_document);
+}
+
+}

+ 47 - 0
Userland/Libraries/LibWeb/Animations/AnimationTimeline.h

@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2023, Matthew Olsson <mattco@serenityos.org>.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <LibWeb/Bindings/PlatformObject.h>
+
+namespace Web::Animations {
+
+// https://www.w3.org/TR/web-animations-1/#animationtimeline
+class AnimationTimeline : public Bindings::PlatformObject {
+    WEB_PLATFORM_OBJECT(AnimationTimeline, Bindings::PlatformObject);
+
+public:
+    Optional<double> current_time() const { return m_current_time; }
+    virtual WebIDL::ExceptionOr<void> set_current_time(Optional<double>);
+
+    JS::GCPtr<DOM::Document> associated_document() const { return m_associated_document; }
+    void set_associated_document(JS::GCPtr<DOM::Document>);
+
+    virtual bool is_inactive() const;
+    bool is_monotonically_increasing() const { return m_is_monotonically_increasing; }
+
+    // https://www.w3.org/TR/web-animations-1/#timeline-time-to-origin-relative-time
+    virtual Optional<double> convert_a_timeline_time_to_an_original_relative_time(Optional<double>) { VERIFY_NOT_REACHED(); }
+    virtual bool can_convert_a_timeline_time_to_an_original_relative_time() const { return false; }
+
+protected:
+    AnimationTimeline(JS::Realm&);
+
+    virtual void initialize(JS::Realm&) override;
+    virtual void visit_edges(Cell::Visitor&) override;
+
+    // https://www.w3.org/TR/web-animations-1/#dom-animationtimeline-currenttime
+    Optional<double> m_current_time {};
+
+    // https://www.w3.org/TR/web-animations-1/#monotonically-increasing-timeline
+    bool m_is_monotonically_increasing { true };
+
+    // https://www.w3.org/TR/web-animations-1/#timeline-associated-with-a-document
+    JS::GCPtr<DOM::Document> m_associated_document {};
+};
+
+}

+ 5 - 0
Userland/Libraries/LibWeb/Animations/AnimationTimeline.idl

@@ -0,0 +1,5 @@
+// https://www.w3.org/TR/web-animations-1/#animationtimeline
+[Exposed=Window]
+interface AnimationTimeline {
+    readonly attribute double? currentTime;
+};

+ 1 - 0
Userland/Libraries/LibWeb/CMakeLists.txt

@@ -2,6 +2,7 @@ include(libweb_generators)
 include(accelerated_graphics)
 
 set(SOURCES
+    Animations/AnimationTimeline.cpp
     ARIA/AriaData.cpp
     ARIA/ARIAMixin.cpp
     ARIA/Roles.cpp

+ 4 - 0
Userland/Libraries/LibWeb/Forward.h

@@ -26,6 +26,10 @@ namespace Web::Painting {
 class RecordingPainter;
 }
 
+namespace Web::Animations {
+class AnimationTimeline;
+}
+
 namespace Web::ARIA {
 class AriaData;
 class ARIAMixin;

+ 1 - 0
Userland/Libraries/LibWeb/idl_files.cmake

@@ -1,6 +1,7 @@
 # This file is included from "Meta/CMake/libweb_data.cmake"
 # It is defined here so that there is no need to go to the Meta directory when adding new idl files
 
+libweb_js_bindings(Animations/AnimationTimeline)
 libweb_js_bindings(Crypto/Crypto)
 libweb_js_bindings(Crypto/SubtleCrypto)
 libweb_js_bindings(CSS/CSSConditionRule)