| 
									
										
										
										
											2022-02-21 19:29:43 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2023-03-30 15:55:02 +01:00
										 |  |  |  * Copyright (c) 2022-2023, Sam Atkins <atkinssj@serenityos.org> | 
					
						
							| 
									
										
										
										
											2022-02-21 19:29:43 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-06 19:02:26 +01:00
										 |  |  | #include <AK/String.h>
 | 
					
						
							| 
									
										
										
										
											2022-02-21 19:29:43 +00:00
										 |  |  | #include <LibWeb/Forward.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Web::CSS { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Time { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     enum class Type { | 
					
						
							|  |  |  |         S, | 
					
						
							|  |  |  |         Ms, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     static Optional<Type> unit_from_name(StringView); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-27 21:10:21 +02:00
										 |  |  |     Time(double value, Type type); | 
					
						
							|  |  |  |     static Time make_seconds(double); | 
					
						
							| 
									
										
										
										
											2022-02-21 19:29:43 +00:00
										 |  |  |     Time percentage_of(Percentage const&) const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-22 12:25:30 +01:00
										 |  |  |     String to_string() const; | 
					
						
							| 
									
										
										
										
											2023-05-26 23:30:11 +03:30
										 |  |  |     double to_milliseconds() const; | 
					
						
							| 
									
										
										
										
											2023-05-27 21:10:21 +02:00
										 |  |  |     double to_seconds() const; | 
					
						
							| 
									
										
										
										
											2022-02-21 19:29:43 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-11 12:57:32 +01:00
										 |  |  |     Type type() const { return m_type; } | 
					
						
							| 
									
										
										
										
											2023-05-27 21:10:21 +02:00
										 |  |  |     double raw_value() const { return m_value; } | 
					
						
							| 
									
										
										
										
											2024-08-14 16:25:48 +01:00
										 |  |  |     StringView unit_name() const; | 
					
						
							| 
									
										
										
										
											2023-04-11 12:57:32 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-21 19:29:43 +00:00
										 |  |  |     bool operator==(Time const& other) const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return m_type == other.m_type && m_value == other.m_value; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-31 14:55:18 +01:00
										 |  |  |     int operator<=>(Time const& other) const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         auto this_seconds = to_seconds(); | 
					
						
							|  |  |  |         auto other_seconds = other.to_seconds(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (this_seconds < other_seconds) | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         if (this_seconds > other_seconds) | 
					
						
							|  |  |  |             return 1; | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-18 17:27:47 +01:00
										 |  |  |     static Time resolve_calculated(NonnullRefPtr<CSSMathValue> const&, Layout::Node const&, Time const& reference_value); | 
					
						
							| 
									
										
										
										
											2024-08-02 14:28:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-21 19:29:43 +00:00
										 |  |  | private: | 
					
						
							|  |  |  |     Type m_type; | 
					
						
							| 
									
										
										
										
											2023-05-27 21:10:21 +02:00
										 |  |  |     double m_value { 0 }; | 
					
						
							| 
									
										
										
										
											2022-02-21 19:29:43 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-07-27 11:41:31 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | template<> | 
					
						
							|  |  |  | struct AK::Formatter<Web::CSS::Time> : Formatter<StringView> { | 
					
						
							|  |  |  |     ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Time const& time) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-08-22 12:25:30 +01:00
										 |  |  |         return Formatter<StringView>::format(builder, time.to_string()); | 
					
						
							| 
									
										
										
										
											2022-07-27 11:41:31 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | }; |