DocumentTimeline.cpp 3.3 KB

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