DocumentTimeline.cpp 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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/Bindings/DocumentTimelinePrototype.h>
  10. #include <LibWeb/DOM/Document.h>
  11. #include <LibWeb/HTML/Window.h>
  12. #include <LibWeb/HighResolutionTime/Performance.h>
  13. #include <LibWeb/WebIDL/ExceptionOr.h>
  14. namespace Web::Animations {
  15. JS_DEFINE_ALLOCATOR(DocumentTimeline);
  16. JS::NonnullGCPtr<DocumentTimeline> DocumentTimeline::create(JS::Realm& realm, DOM::Document& document, HighResolutionTime::DOMHighResTimeStamp origin_time)
  17. {
  18. auto timeline = realm.heap().allocate<DocumentTimeline>(realm, realm, document, origin_time);
  19. auto current_time = document.last_animation_frame_timestamp();
  20. if (!current_time.has_value()) {
  21. // The document hasn't processed an animation frame yet, so just use the exact current time
  22. auto* window_or_worker = dynamic_cast<HTML::WindowOrWorkerGlobalScopeMixin*>(&realm.global_object());
  23. VERIFY(window_or_worker);
  24. current_time = window_or_worker->performance()->now();
  25. }
  26. timeline->set_current_time(current_time);
  27. return timeline;
  28. }
  29. // https://www.w3.org/TR/web-animations-1/#dom-documenttimeline-documenttimeline
  30. WebIDL::ExceptionOr<JS::NonnullGCPtr<DocumentTimeline>> DocumentTimeline::construct_impl(JS::Realm& realm, DocumentTimelineOptions options)
  31. {
  32. // Creates a new DocumentTimeline. The Document with which the timeline is associated is the Document associated
  33. // with the Window that is the current global object.
  34. auto& window = verify_cast<HTML::Window>(realm.global_object());
  35. return create(realm, window.associated_document(), options.origin_time);
  36. }
  37. // https://www.w3.org/TR/web-animations-1/#ref-for-timeline-time-to-origin-relative-time
  38. Optional<double> DocumentTimeline::convert_a_timeline_time_to_an_origin_relative_time(Optional<double> timeline_time)
  39. {
  40. // To convert a timeline time, timeline time, to an origin-relative time for a document timeline, timeline, return
  41. // the sum of the timeline time and timeline’s origin time. If timeline is inactive, return an unresolved time
  42. // value.
  43. if (is_inactive() || !timeline_time.has_value())
  44. return {};
  45. return timeline_time.value() + m_origin_time;
  46. }
  47. // https://www.w3.org/TR/web-animations-1/#origin-time
  48. void DocumentTimeline::set_current_time(Optional<double> current_time)
  49. {
  50. // A document timeline is a type of timeline that is associated with a document and whose current time is calculated
  51. // as a fixed offset from the now timestamp provided each time the update animations and send events procedure is
  52. // run. This fixed offset is referred to as the document timeline’s origin time.
  53. if (!current_time.has_value())
  54. Base::set_current_time({});
  55. else
  56. Base::set_current_time(current_time.value() - m_origin_time);
  57. // After a document timeline becomes active, it is monotonically increasing.
  58. if (!is_inactive())
  59. VERIFY(is_monotonically_increasing());
  60. }
  61. // https://www.w3.org/TR/web-animations-1/#document-timelines
  62. bool DocumentTimeline::is_inactive() const
  63. {
  64. // A document timeline that is associated with a Document which is not an active document is also considered to be
  65. // inactive.
  66. return Base::is_inactive() || !associated_document()->is_active();
  67. }
  68. DocumentTimeline::DocumentTimeline(JS::Realm& realm, DOM::Document& document, HighResolutionTime::DOMHighResTimeStamp origin_time)
  69. : AnimationTimeline(realm)
  70. , m_origin_time(origin_time)
  71. {
  72. set_associated_document(document);
  73. }
  74. void DocumentTimeline::initialize(JS::Realm& realm)
  75. {
  76. Base::initialize(realm);
  77. WEB_SET_PROTOTYPE_FOR_INTERFACE(DocumentTimeline);
  78. }
  79. }