| 
									
										
										
										
											2023-09-28 15:18:14 +01:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2025-05-16 20:02:16 +01:00
										 |  |  |  * Copyright (c) 2023-2025, Sam Atkins <sam@ladybird.org> | 
					
						
							| 
									
										
										
										
											2023-09-28 15:18:14 +01:00
										 |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <AK/String.h>
 | 
					
						
							| 
									
										
										
										
											2025-05-16 20:02:16 +01:00
										 |  |  | #include <LibWeb/CSS/SerializationMode.h>
 | 
					
						
							| 
									
										
										
										
											2025-09-02 13:48:49 +01:00
										 |  |  | #include <LibWeb/CSS/Units.h>
 | 
					
						
							| 
									
										
										
										
											2023-09-28 15:18:14 +01:00
										 |  |  | #include <LibWeb/Forward.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Web::CSS { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://drafts.csswg.org/css-grid-2/#typedef-flex
 | 
					
						
							|  |  |  | class Flex { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2025-09-02 13:48:49 +01:00
										 |  |  |     Flex(double value, FlexUnit unit); | 
					
						
							| 
									
										
										
										
											2023-09-28 15:18:14 +01:00
										 |  |  |     static Flex make_fr(double); | 
					
						
							|  |  |  |     Flex percentage_of(Percentage const&) const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-16 20:02:16 +01:00
										 |  |  |     String to_string(SerializationMode = SerializationMode::Normal) const; | 
					
						
							| 
									
										
										
										
											2023-09-28 15:18:14 +01:00
										 |  |  |     double to_fr() const; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     double raw_value() const { return m_value; } | 
					
						
							| 
									
										
										
										
											2025-09-02 13:48:49 +01:00
										 |  |  |     FlexUnit unit() const { return m_unit; } | 
					
						
							| 
									
										
										
										
											2025-09-11 11:50:59 +01:00
										 |  |  |     FlyString unit_name() const { return CSS::to_string(m_unit); } | 
					
						
							| 
									
										
										
										
											2023-09-28 15:18:14 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     bool operator==(Flex const& other) const | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-09-02 13:48:49 +01:00
										 |  |  |         return m_unit == other.m_unit && m_value == other.m_value; | 
					
						
							| 
									
										
										
										
											2023-09-28 15:18:14 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     int operator<=>(Flex const& other) const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         auto this_fr = to_fr(); | 
					
						
							|  |  |  |         auto other_fr = other.to_fr(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (this_fr < other_fr) | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         if (this_fr > other_fr) | 
					
						
							|  |  |  |             return 1; | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							| 
									
										
										
										
											2025-09-02 13:48:49 +01:00
										 |  |  |     FlexUnit m_unit; | 
					
						
							| 
									
										
										
										
											2023-09-28 15:18:14 +01:00
										 |  |  |     double m_value { 0 }; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template<> | 
					
						
							|  |  |  | struct AK::Formatter<Web::CSS::Flex> : Formatter<StringView> { | 
					
						
							|  |  |  |     ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Flex const& flex) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return Formatter<StringView>::format(builder, flex.to_string()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | }; |