Serenity Operating System
at master 83 lines 2.3 kB view raw
1/* 2 * Copyright (c) 2022, Andreas Kling <kling@serenityos.org> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#pragma once 8 9#include <LibWeb/CSS/Length.h> 10#include <LibWeb/CSS/Percentage.h> 11 12namespace Web::CSS { 13 14class Size { 15public: 16 enum class Type { 17 Auto, 18 Length, 19 Percentage, 20 MinContent, 21 MaxContent, 22 FitContent, 23 None, // NOTE: This is only valid for max-width and max-height. 24 }; 25 26 static Size make_auto(); 27 static Size make_px(CSSPixels); 28 static Size make_length(Length); 29 static Size make_percentage(Percentage); 30 static Size make_min_content(); 31 static Size make_max_content(); 32 static Size make_fit_content(Length available_space); 33 static Size make_none(); 34 35 bool is_auto() const { return m_type == Type::Auto; } 36 bool is_length() const { return m_type == Type::Length; } 37 bool is_percentage() const { return m_type == Type::Percentage; } 38 bool is_min_content() const { return m_type == Type::MinContent; } 39 bool is_max_content() const { return m_type == Type::MaxContent; } 40 bool is_fit_content() const { return m_type == Type::FitContent; } 41 bool is_none() const { return m_type == Type::None; } 42 43 // FIXME: This is a stopgap API that will go away once all layout code is aware of CSS::Size. 44 CSS::Length resolved(Layout::Node const&, Length const& reference_value) const; 45 46 bool contains_percentage() const; 47 48 CSS::Length const& length() const 49 { 50 VERIFY(is_length()); 51 return m_length_percentage.length(); 52 } 53 54 CSS::Percentage const& percentage() const 55 { 56 VERIFY(is_percentage()); 57 return m_length_percentage.percentage(); 58 } 59 60 CSS::Length const& fit_content_available_space() const 61 { 62 VERIFY(is_fit_content()); 63 return m_length_percentage.length(); 64 } 65 66 ErrorOr<String> to_string() const; 67 68private: 69 Size(Type type, LengthPercentage); 70 71 Type m_type {}; 72 CSS::LengthPercentage m_length_percentage; 73}; 74 75} 76 77template<> 78struct AK::Formatter<Web::CSS::Size> : Formatter<StringView> { 79 ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Size const& size) 80 { 81 return Formatter<StringView>::format(builder, TRY(size.to_string())); 82 } 83};