DocumentTimeline.cpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. WebIDL::ExceptionOr<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. TRY(Base::set_current_time({}));
  44. else
  45. TRY(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. return {};
  50. }
  51. // https://www.w3.org/TR/web-animations-1/#document-timelines
  52. bool DocumentTimeline::is_inactive() const
  53. {
  54. // A document timeline that is associated with a Document which is not an active document is also considered to be
  55. // inactive.
  56. return Base::is_inactive() || !associated_document()->is_active();
  57. }
  58. DocumentTimeline::DocumentTimeline(JS::Realm& realm, DOM::Document& document, HighResolutionTime::DOMHighResTimeStamp origin_time)
  59. : AnimationTimeline(realm)
  60. , m_origin_time(origin_time)
  61. {
  62. set_associated_document(document);
  63. }
  64. void DocumentTimeline::initialize(JS::Realm& realm)
  65. {
  66. Base::initialize(realm);
  67. set_prototype(&Bindings::ensure_web_prototype<Bindings::DocumentTimelinePrototype>(realm, "DocumentTimeline"_fly_string));
  68. }
  69. }