| 
									
										
										
										
											2022-09-25 19:12:09 +02:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2024-10-04 13:19:50 +02:00
										 |  |  |  * Copyright (c) 2022, Andreas Kling <andreas@ladybird.org> | 
					
						
							| 
									
										
										
										
											2022-09-25 19:12:09 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <AK/Format.h>
 | 
					
						
							| 
									
										
										
										
											2023-11-20 23:17:38 +13:00
										 |  |  | #include <AK/String.h>
 | 
					
						
							| 
									
										
										
										
											2022-09-25 19:12:09 +02:00
										 |  |  | #include <LibWeb/Forward.h>
 | 
					
						
							| 
									
										
										
										
											2022-11-23 17:46:10 +00:00
										 |  |  | #include <LibWeb/PixelUnits.h>
 | 
					
						
							| 
									
										
										
										
											2022-09-25 19:12:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace Web::Layout { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-27 15:29:17 +02:00
										 |  |  | class AvailableSize { | 
					
						
							| 
									
										
										
										
											2022-09-25 19:12:09 +02:00
										 |  |  | public: | 
					
						
							|  |  |  |     enum class Type { | 
					
						
							|  |  |  |         Definite, | 
					
						
							|  |  |  |         Indefinite, | 
					
						
							|  |  |  |         MinContent, | 
					
						
							|  |  |  |         MaxContent, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-23 17:46:10 +00:00
										 |  |  |     static AvailableSize make_definite(CSSPixels); | 
					
						
							| 
									
										
										
										
											2022-09-27 15:29:17 +02:00
										 |  |  |     static AvailableSize make_indefinite(); | 
					
						
							|  |  |  |     static AvailableSize make_min_content(); | 
					
						
							|  |  |  |     static AvailableSize make_max_content(); | 
					
						
							| 
									
										
										
										
											2022-09-25 19:12:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     bool is_definite() const { return m_type == Type::Definite; } | 
					
						
							|  |  |  |     bool is_indefinite() const { return m_type == Type::Indefinite; } | 
					
						
							|  |  |  |     bool is_min_content() const { return m_type == Type::MinContent; } | 
					
						
							|  |  |  |     bool is_max_content() const { return m_type == Type::MaxContent; } | 
					
						
							|  |  |  |     bool is_intrinsic_sizing_constraint() const { return is_min_content() || is_max_content(); } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-23 17:46:10 +00:00
										 |  |  |     CSSPixels to_px_or_zero() const | 
					
						
							| 
									
										
										
										
											2022-09-25 19:12:09 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-27 15:29:17 +02:00
										 |  |  |         if (!is_definite()) | 
					
						
							| 
									
										
										
										
											2023-08-26 15:03:04 +01:00
										 |  |  |             return 0; | 
					
						
							| 
									
										
										
										
											2022-09-25 19:12:09 +02:00
										 |  |  |         return m_value; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-20 23:17:38 +13:00
										 |  |  |     String to_string() const; | 
					
						
							| 
									
										
										
										
											2022-09-25 19:12:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-27 14:06:11 +02:00
										 |  |  |     bool operator==(AvailableSize const& other) const = default; | 
					
						
							| 
									
										
										
										
											2023-08-12 18:21:17 +02:00
										 |  |  |     bool operator<(AvailableSize const& other) const { return m_value < other.m_value; } | 
					
						
							| 
									
										
										
										
											2023-03-27 14:06:11 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-25 19:12:09 +02:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2022-11-23 17:46:10 +00:00
										 |  |  |     AvailableSize(Type type, CSSPixels); | 
					
						
							| 
									
										
										
										
											2022-09-25 19:12:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     Type m_type {}; | 
					
						
							| 
									
										
										
										
											2022-11-23 17:46:10 +00:00
										 |  |  |     CSSPixels m_value {}; | 
					
						
							| 
									
										
										
										
											2022-09-25 19:12:09 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-12 18:22:12 +02:00
										 |  |  | inline bool operator>(CSSPixels left, AvailableSize const& right) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (right.is_max_content() || right.is_indefinite()) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     if (right.is_min_content()) | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     return left > right.to_px_or_zero(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | inline bool operator<(CSSPixels left, AvailableSize const& right) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (right.is_max_content() || right.is_indefinite()) | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     if (right.is_min_content()) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     return left < right.to_px_or_zero(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | inline bool operator<(AvailableSize const& left, CSSPixels right) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (left.is_min_content()) | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     if (left.is_max_content() || left.is_indefinite()) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     return left.to_px_or_zero() < right; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-27 15:29:17 +02:00
										 |  |  | class AvailableSpace { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     AvailableSpace(AvailableSize w, AvailableSize h) | 
					
						
							|  |  |  |         : width(move(w)) | 
					
						
							|  |  |  |         , height(move(h)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-27 14:06:11 +02:00
										 |  |  |     bool operator==(AvailableSpace const& other) const = default; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-27 15:29:17 +02:00
										 |  |  |     AvailableSize width; | 
					
						
							|  |  |  |     AvailableSize height; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-20 23:17:38 +13:00
										 |  |  |     String to_string() const; | 
					
						
							| 
									
										
										
										
											2022-09-27 15:29:17 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-25 19:12:09 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-27 15:29:17 +02:00
										 |  |  | template<> | 
					
						
							|  |  |  | struct AK::Formatter<Web::Layout::AvailableSize> : Formatter<StringView> { | 
					
						
							|  |  |  |     ErrorOr<void> format(FormatBuilder& builder, Web::Layout::AvailableSize const& available_size) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-11-20 23:17:38 +13:00
										 |  |  |         return Formatter<StringView>::format(builder, available_size.to_string()); | 
					
						
							| 
									
										
										
										
											2022-09-27 15:29:17 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-25 19:12:09 +02:00
										 |  |  | template<> | 
					
						
							|  |  |  | struct AK::Formatter<Web::Layout::AvailableSpace> : Formatter<StringView> { | 
					
						
							|  |  |  |     ErrorOr<void> format(FormatBuilder& builder, Web::Layout::AvailableSpace const& available_space) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-11-20 23:17:38 +13:00
										 |  |  |         return Formatter<StringView>::format(builder, available_space.to_string()); | 
					
						
							| 
									
										
										
										
											2022-09-25 19:12:09 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | }; |