| 
									
										
										
										
											2023-11-04 08:16:37 -07:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2024-02-02 15:01:30 -07:00
										 |  |  |  * Copyright (c) 2023-2024, Matthew Olsson <mattco@serenityos.org>. | 
					
						
							| 
									
										
										
										
											2023-11-04 08:16:37 -07:00
										 |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-04 11:58:09 -07:00
										 |  |  | #include <LibWeb/Animations/Animation.h>
 | 
					
						
							| 
									
										
										
										
											2023-11-04 08:16:37 -07:00
										 |  |  | #include <LibWeb/Animations/AnimationTimeline.h>
 | 
					
						
							| 
									
										
										
										
											2024-04-27 12:09:58 +12:00
										 |  |  | #include <LibWeb/Bindings/AnimationTimelinePrototype.h>
 | 
					
						
							| 
									
										
										
										
											2023-11-04 08:16:37 -07:00
										 |  |  | #include <LibWeb/DOM/Document.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Web::Animations { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  | GC_DEFINE_ALLOCATOR(AnimationTimeline); | 
					
						
							| 
									
										
										
										
											2023-11-19 19:47:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-02 15:01:30 -07:00
										 |  |  | void AnimationTimeline::set_current_time(Optional<double> value) | 
					
						
							| 
									
										
										
										
											2023-11-04 08:16:37 -07:00
										 |  |  | { | 
					
						
							|  |  |  |     if (value == m_current_time) | 
					
						
							| 
									
										
										
										
											2024-02-02 15:01:30 -07:00
										 |  |  |         return; | 
					
						
							| 
									
										
										
										
											2023-11-04 08:16:37 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (m_is_monotonically_increasing && m_current_time.has_value()) { | 
					
						
							|  |  |  |         if (!value.has_value() || value.value() < m_current_time.value()) | 
					
						
							|  |  |  |             m_is_monotonically_increasing = false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     m_current_time = value; | 
					
						
							| 
									
										
										
										
											2024-12-27 16:10:18 -05:00
										 |  |  |     // The loop might modify the content of m_associated_animations, so let's iterate over a copy.
 | 
					
						
							|  |  |  |     auto temporary_copy = GC::RootVector<GC::Ref<Animation>>(vm().heap()); | 
					
						
							|  |  |  |     temporary_copy.extend(m_associated_animations.values()); | 
					
						
							|  |  |  |     for (auto& animation : temporary_copy) { | 
					
						
							| 
									
										
										
										
											2024-02-02 15:01:30 -07:00
										 |  |  |         animation->notify_timeline_time_did_change(); | 
					
						
							| 
									
										
										
										
											2024-12-27 16:10:18 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-11-04 08:16:37 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  | void AnimationTimeline::set_associated_document(GC::Ptr<DOM::Document> document) | 
					
						
							| 
									
										
										
										
											2023-11-04 08:16:37 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-11-04 08:43:10 -07:00
										 |  |  |     if (document) | 
					
						
							|  |  |  |         document->associate_with_timeline(*this); | 
					
						
							|  |  |  |     if (m_associated_document) | 
					
						
							|  |  |  |         m_associated_document->disassociate_with_timeline(*this); | 
					
						
							| 
									
										
										
										
											2023-11-04 08:16:37 -07:00
										 |  |  |     m_associated_document = document; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://www.w3.org/TR/web-animations-1/#inactive-timeline
 | 
					
						
							|  |  |  | bool AnimationTimeline::is_inactive() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // A timeline is considered to be inactive when its time value is unresolved.
 | 
					
						
							|  |  |  |     return !m_current_time.has_value(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | AnimationTimeline::AnimationTimeline(JS::Realm& realm) | 
					
						
							|  |  |  |     : Bindings::PlatformObject(realm) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-11 15:17:17 +01:00
										 |  |  | void AnimationTimeline::finalize() | 
					
						
							| 
									
										
										
										
											2023-11-04 08:43:10 -07:00
										 |  |  | { | 
					
						
							|  |  |  |     if (m_associated_document) | 
					
						
							|  |  |  |         m_associated_document->disassociate_with_timeline(*this); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-04 08:16:37 -07:00
										 |  |  | void AnimationTimeline::initialize(JS::Realm& realm) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Base::initialize(realm); | 
					
						
							| 
									
										
										
										
											2024-03-16 13:13:08 +01:00
										 |  |  |     WEB_SET_PROTOTYPE_FOR_INTERFACE(AnimationTimeline); | 
					
						
							| 
									
										
										
										
											2023-11-04 08:16:37 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void AnimationTimeline::visit_edges(Cell::Visitor& visitor) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Base::visit_edges(visitor); | 
					
						
							|  |  |  |     visitor.visit(m_associated_document); | 
					
						
							| 
									
										
										
										
											2024-04-15 13:58:21 +02:00
										 |  |  |     visitor.visit(m_associated_animations); | 
					
						
							| 
									
										
										
										
											2023-11-04 08:16:37 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |