DocumentTimeline.cpp 3.4 KB

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