| 
									
										
										
										
											2023-11-04 08:32:30 -07:00
										 |  |  |  | /*
 | 
					
						
							|  |  |  |  |  * Copyright (c) 2023, Matthew Olsson <mattco@serenityos.org>. | 
					
						
							|  |  |  |  |  * | 
					
						
							|  |  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  |  */ | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |  | #include <LibGC/Heap.h>
 | 
					
						
							| 
									
										
										
										
											2023-11-04 08:32:30 -07:00
										 |  |  |  | #include <LibJS/Runtime/Realm.h>
 | 
					
						
							|  |  |  |  | #include <LibWeb/Animations/DocumentTimeline.h>
 | 
					
						
							| 
									
										
										
										
											2024-04-27 12:09:58 +12:00
										 |  |  |  | #include <LibWeb/Bindings/DocumentTimelinePrototype.h>
 | 
					
						
							| 
									
										
										
										
											2023-11-04 08:32:30 -07:00
										 |  |  |  | #include <LibWeb/DOM/Document.h>
 | 
					
						
							|  |  |  |  | #include <LibWeb/HTML/Window.h>
 | 
					
						
							| 
									
										
										
										
											2024-03-09 14:58:10 -07:00
										 |  |  |  | #include <LibWeb/HighResolutionTime/Performance.h>
 | 
					
						
							| 
									
										
										
										
											2023-11-04 08:32:30 -07:00
										 |  |  |  | #include <LibWeb/WebIDL/ExceptionOr.h>
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | namespace Web::Animations { | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |  | GC_DEFINE_ALLOCATOR(DocumentTimeline); | 
					
						
							| 
									
										
										
										
											2023-11-19 19:47:52 +01:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |  | GC::Ref<DocumentTimeline> DocumentTimeline::create(JS::Realm& realm, DOM::Document& document, HighResolutionTime::DOMHighResTimeStamp origin_time) | 
					
						
							| 
									
										
										
										
											2023-11-04 08:32:30 -07:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-11-14 05:50:17 +13:00
										 |  |  |  |     auto timeline = realm.create<DocumentTimeline>(realm, document, origin_time); | 
					
						
							| 
									
										
										
										
											2024-06-02 06:48:36 -07:00
										 |  |  |  |     auto current_time = document.last_animation_frame_timestamp(); | 
					
						
							|  |  |  |  |     if (!current_time.has_value()) { | 
					
						
							|  |  |  |  |         // The document hasn't processed an animation frame yet, so just use the exact current time
 | 
					
						
							|  |  |  |  |         auto* window_or_worker = dynamic_cast<HTML::WindowOrWorkerGlobalScopeMixin*>(&realm.global_object()); | 
					
						
							|  |  |  |  |         VERIFY(window_or_worker); | 
					
						
							|  |  |  |  |         current_time = window_or_worker->performance()->now(); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  |     timeline->set_current_time(current_time); | 
					
						
							| 
									
										
										
										
											2024-03-09 14:58:10 -07:00
										 |  |  |  |     return timeline; | 
					
						
							| 
									
										
										
										
											2023-11-04 08:32:30 -07:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // https://www.w3.org/TR/web-animations-1/#dom-documenttimeline-documenttimeline
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |  | WebIDL::ExceptionOr<GC::Ref<DocumentTimeline>> DocumentTimeline::construct_impl(JS::Realm& realm, DocumentTimelineOptions options) | 
					
						
							| 
									
										
										
										
											2023-11-04 08:32:30 -07:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     // Creates a new DocumentTimeline. The Document with which the timeline is associated is the Document associated
 | 
					
						
							|  |  |  |  |     // with the Window that is the current global object.
 | 
					
						
							| 
									
										
										
										
											2025-01-21 09:12:05 -05:00
										 |  |  |  |     auto& window = as<HTML::Window>(realm.global_object()); | 
					
						
							| 
									
										
										
										
											2023-11-04 08:32:30 -07:00
										 |  |  |  |     return create(realm, window.associated_document(), options.origin_time); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // https://www.w3.org/TR/web-animations-1/#ref-for-timeline-time-to-origin-relative-time
 | 
					
						
							| 
									
										
										
										
											2024-02-03 18:22:44 -07:00
										 |  |  |  | Optional<double> DocumentTimeline::convert_a_timeline_time_to_an_origin_relative_time(Optional<double> timeline_time) | 
					
						
							| 
									
										
										
										
											2023-11-04 08:32:30 -07:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     // To convert a timeline time, timeline time, to an origin-relative time for a document timeline, timeline, return
 | 
					
						
							|  |  |  |  |     // the sum of the timeline time and timeline’s origin time. If timeline is inactive, return an unresolved time
 | 
					
						
							|  |  |  |  |     // value.
 | 
					
						
							|  |  |  |  |     if (is_inactive() || !timeline_time.has_value()) | 
					
						
							|  |  |  |  |         return {}; | 
					
						
							|  |  |  |  |     return timeline_time.value() + m_origin_time; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // https://www.w3.org/TR/web-animations-1/#origin-time
 | 
					
						
							| 
									
										
										
										
											2024-02-02 15:01:30 -07:00
										 |  |  |  | void DocumentTimeline::set_current_time(Optional<double> current_time) | 
					
						
							| 
									
										
										
										
											2023-11-04 08:32:30 -07:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     // A document timeline is a type of timeline that is associated with a document and whose current time is calculated
 | 
					
						
							|  |  |  |  |     // as a fixed offset from the now timestamp provided each time the update animations and send events procedure is
 | 
					
						
							|  |  |  |  |     // run. This fixed offset is referred to as the document timeline’s origin time.
 | 
					
						
							|  |  |  |  |     if (!current_time.has_value()) | 
					
						
							| 
									
										
										
										
											2024-02-02 15:01:30 -07:00
										 |  |  |  |         Base::set_current_time({}); | 
					
						
							| 
									
										
										
										
											2023-11-04 08:32:30 -07:00
										 |  |  |  |     else | 
					
						
							| 
									
										
										
										
											2024-02-02 15:01:30 -07:00
										 |  |  |  |         Base::set_current_time(current_time.value() - m_origin_time); | 
					
						
							| 
									
										
										
										
											2023-11-04 08:32:30 -07:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     // After a document timeline becomes active, it is monotonically increasing.
 | 
					
						
							|  |  |  |  |     if (!is_inactive()) | 
					
						
							|  |  |  |  |         VERIFY(is_monotonically_increasing()); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // https://www.w3.org/TR/web-animations-1/#document-timelines
 | 
					
						
							|  |  |  |  | bool DocumentTimeline::is_inactive() const | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     // A document timeline that is associated with a Document which is not an active document is also considered to be
 | 
					
						
							|  |  |  |  |     // inactive.
 | 
					
						
							|  |  |  |  |     return Base::is_inactive() || !associated_document()->is_active(); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | DocumentTimeline::DocumentTimeline(JS::Realm& realm, DOM::Document& document, HighResolutionTime::DOMHighResTimeStamp origin_time) | 
					
						
							|  |  |  |  |     : AnimationTimeline(realm) | 
					
						
							|  |  |  |  |     , m_origin_time(origin_time) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     set_associated_document(document); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void DocumentTimeline::initialize(JS::Realm& realm) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     Base::initialize(realm); | 
					
						
							| 
									
										
										
										
											2024-03-16 13:13:08 +01:00
										 |  |  |  |     WEB_SET_PROTOTYPE_FOR_INTERFACE(DocumentTimeline); | 
					
						
							| 
									
										
										
										
											2023-11-04 08:32:30 -07:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | } |