/src/serenity/Userland/Libraries/LibWeb/Layout/AvailableSpace.h
Line | Count | Source (jump to first uncovered line) |
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 <AK/Format.h> |
10 | | #include <AK/String.h> |
11 | | #include <LibWeb/Forward.h> |
12 | | #include <LibWeb/PixelUnits.h> |
13 | | |
14 | | namespace Web::Layout { |
15 | | |
16 | | class AvailableSize { |
17 | | public: |
18 | | enum class Type { |
19 | | Definite, |
20 | | Indefinite, |
21 | | MinContent, |
22 | | MaxContent, |
23 | | }; |
24 | | |
25 | | static AvailableSize make_definite(CSSPixels); |
26 | | static AvailableSize make_indefinite(); |
27 | | static AvailableSize make_min_content(); |
28 | | static AvailableSize make_max_content(); |
29 | | |
30 | 0 | bool is_definite() const { return m_type == Type::Definite; } |
31 | 0 | bool is_indefinite() const { return m_type == Type::Indefinite; } |
32 | 0 | bool is_min_content() const { return m_type == Type::MinContent; } |
33 | 0 | bool is_max_content() const { return m_type == Type::MaxContent; } |
34 | 0 | bool is_intrinsic_sizing_constraint() const { return is_min_content() || is_max_content(); } |
35 | | |
36 | | CSSPixels to_px_or_zero() const |
37 | 0 | { |
38 | 0 | if (!is_definite()) |
39 | 0 | return 0; |
40 | 0 | return m_value; |
41 | 0 | } |
42 | | |
43 | | String to_string() const; |
44 | | |
45 | | bool operator==(AvailableSize const& other) const = default; |
46 | 0 | bool operator<(AvailableSize const& other) const { return m_value < other.m_value; } |
47 | | |
48 | | private: |
49 | | AvailableSize(Type type, CSSPixels); |
50 | | |
51 | | Type m_type {}; |
52 | | CSSPixels m_value {}; |
53 | | }; |
54 | | |
55 | | inline bool operator>(CSSPixels left, AvailableSize const& right) |
56 | 0 | { |
57 | 0 | if (right.is_max_content() || right.is_indefinite()) |
58 | 0 | return false; |
59 | 0 | if (right.is_min_content()) |
60 | 0 | return true; |
61 | 0 | return left > right.to_px_or_zero(); |
62 | 0 | } |
63 | | |
64 | | inline bool operator<(CSSPixels left, AvailableSize const& right) |
65 | 0 | { |
66 | 0 | if (right.is_max_content() || right.is_indefinite()) |
67 | 0 | return true; |
68 | 0 | if (right.is_min_content()) |
69 | 0 | return false; |
70 | 0 | return left < right.to_px_or_zero(); |
71 | 0 | } |
72 | | |
73 | | inline bool operator<(AvailableSize const& left, CSSPixels right) |
74 | 0 | { |
75 | 0 | if (left.is_min_content()) |
76 | 0 | return true; |
77 | 0 | if (left.is_max_content() || left.is_indefinite()) |
78 | 0 | return false; |
79 | 0 | return left.to_px_or_zero() < right; |
80 | 0 | } |
81 | | |
82 | | class AvailableSpace { |
83 | | public: |
84 | | AvailableSpace(AvailableSize w, AvailableSize h) |
85 | 0 | : width(move(w)) |
86 | 0 | , height(move(h)) |
87 | 0 | { |
88 | 0 | } |
89 | | |
90 | | bool operator==(AvailableSpace const& other) const = default; |
91 | | |
92 | | AvailableSize width; |
93 | | AvailableSize height; |
94 | | |
95 | | String to_string() const; |
96 | | }; |
97 | | |
98 | | } |
99 | | |
100 | | template<> |
101 | | struct AK::Formatter<Web::Layout::AvailableSize> : Formatter<StringView> { |
102 | | ErrorOr<void> format(FormatBuilder& builder, Web::Layout::AvailableSize const& available_size) |
103 | 0 | { |
104 | 0 | return Formatter<StringView>::format(builder, available_size.to_string()); |
105 | 0 | } |
106 | | }; |
107 | | |
108 | | template<> |
109 | | struct AK::Formatter<Web::Layout::AvailableSpace> : Formatter<StringView> { |
110 | | ErrorOr<void> format(FormatBuilder& builder, Web::Layout::AvailableSpace const& available_space) |
111 | 0 | { |
112 | 0 | return Formatter<StringView>::format(builder, available_space.to_string()); |
113 | 0 | } |
114 | | }; |