DocumentTimeline.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright (c) 2023, Matthew Olsson <mattco@serenityos.org>.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Heap/Heap.h>
  7. #include <LibJS/Runtime/Realm.h>
  8. #include <LibWeb/Animations/DocumentTimeline.h>
  9. #include <LibWeb/DOM/Document.h>
  10. #include <LibWeb/HTML/Window.h>
  11. #include <LibWeb/WebIDL/ExceptionOr.h>
  12. namespace Web::Animations {
  13. JS_DEFINE_ALLOCATOR(DocumentTimeline);
  14. JS::NonnullGCPtr<DocumentTimeline> DocumentTimeline::create(JS::Realm& realm, DOM::Document& document, HighResolutionTime::DOMHighResTimeStamp origin_time)
  15. {
  16. return realm.heap().allocate<DocumentTimeline>(realm, realm, document, origin_time);
  17. }
  18. // https://www.w3.org/TR/web-animations-1/#dom-documenttimeline-documenttimeline
  19. WebIDL::ExceptionOr<JS::NonnullGCPtr<DocumentTimeline>> DocumentTimeline::construct_impl(JS::Realm& realm, DocumentTimelineOptions options)
  20. {
  21. // Creates a new DocumentTimeline. The Document with which the timeline is associated is the Document associated
  22. // with the Window that is the current global object.
  23. auto& window = verify_cast<HTML::Window>(realm.global_object());
  24. return create(realm, window.associated_document(), options.origin_time);
  25. }
  26. // https://www.w3.org/TR/web-animations-1/#ref-for-timeline-time-to-origin-relative-time
  27. Optional<double> DocumentTimeline::convert_a_timeline_time_to_an_origin_relative_time(Optional<double> timeline_time)
  28. {
  29. // To convert a timeline time, timeline time, to an origin-relative time for a document timeline, timeline, return
  30. // the sum of the timeline time and timeline’s origin time. If timeline is inactive, return an unresolved time
  31. // value.
  32. if (is_inactive() || !timeline_time.has_value())
  33. return {};
  34. return timeline_time.value() + m_origin_time;
  35. }
  36. // https://www.w3.org/TR/web-animations-1/#origin-time
  37. void DocumentTimeline::set_current_time(Optional<double> current_time)
  38. {
  39. // A document timeline is a type of timeline that is associated with a document and whose current time is calculated
  40. // as a fixed offset from the now timestamp provided each time the update animations and send events procedure is
  41. // run. This fixed offset is referred to as the document timeline’s origin time.
  42. if (!current_time.has_value())
  43. Base::set_current_time({});
  44. else
  45. Base::set_current_time(current_time.value() - m_origin_time);
  46. // After a document timeline becomes active, it is monotonically increasing.
  47. if (!is_inactive())
  48. VERIFY(is_monotonically_increasing());
  49. }
  50. // https://www.w3.org/TR/web-animations-1/#document-timelines
  51. bool DocumentTimeline::is_inactive() const
  52. {
  53. // A document timeline that is associated with a Document which is not an active document is also considered to be
  54. // inactive.
  55. return Base::is_inactive() || !associated_document()->is_active();
  56. }
  57. DocumentTimeline::DocumentTimeline(JS::Realm& realm, DOM::Document& document, HighResolutionTime::DOMHighResTimeStamp origin_time)
  58. : AnimationTimeline(realm)
  59. , m_origin_time(origin_time)
  60. {
  61. set_associated_document(document);
  62. }
  63. void DocumentTimeline::initialize(JS::Realm& realm)
  64. {
  65. Base::initialize(realm);
  66. set_prototype(&Bindings::ensure_web_prototype<Bindings::DocumentTimelinePrototype>(realm, "DocumentTimeline"_fly_string));
  67. }
  68. }