summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/CSS/GridTrackSize.h
blob: f30d96490f304affb35a2bf32c071338772ee6ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 * Copyright (c) 2022, Martin Falisse <mfalisse@outlook.com>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Vector.h>
#include <LibWeb/CSS/Length.h>
#include <LibWeb/CSS/Percentage.h>

namespace Web::CSS {

class GridSize {
public:
    enum class Type {
        Length,
        Percentage,
        FlexibleLength,
        // TODO: Max-Content
    };

    GridSize(Length);
    GridSize(Percentage);
    GridSize(float);
    GridSize();
    ~GridSize();

    static GridSize make_auto();

    Type type() const { return m_type; }

    bool is_length() const { return m_type == Type::Length; }
    bool is_percentage() const { return m_type == Type::Percentage; }
    bool is_flexible_length() const { return m_type == Type::FlexibleLength; }

    Length length() const;
    Percentage percentage() const { return m_percentage; }
    float flexible_length() const { return m_flexible_length; }

    // https://drafts.csswg.org/css-grid/#layout-algorithm
    // Intrinsic sizing function - min-content, max-content, auto, fit-content()
    // FIXME: Add missing properties once implemented.
    bool is_intrinsic_track_sizing() const
    {
        return (m_type == Type::Length && m_length.is_auto());
    }

    bool is_definite() const
    {
        return (m_type == Type::Length && !m_length.is_auto()) || is_percentage();
    }

    DeprecatedString to_string() const;
    bool operator==(GridSize const& other) const
    {
        return m_type == other.type()
            && m_length == other.length()
            && m_percentage == other.percentage()
            && m_flexible_length == other.flexible_length();
    }

private:
    Type m_type;
    // Length includes a RefPtr<CalculatedStyleValue> member, but we can't include the header StyleValue.h as it includes
    // this file already. To break the cyclic dependency, we must initialize m_length in the constructor.
    Length m_length;
    Percentage m_percentage { Percentage(0) };
    float m_flexible_length { 0 };
};

class GridMinMax {
public:
    GridMinMax(CSS::GridSize min_grid_size, CSS::GridSize max_grid_size);
    GridMinMax() = default;

    GridSize min_grid_size() const& { return m_min_grid_size; }
    GridSize max_grid_size() const& { return m_max_grid_size; }

    DeprecatedString to_string() const;
    bool operator==(GridMinMax const& other) const
    {
        return m_min_grid_size == other.min_grid_size()
            && m_max_grid_size == other.max_grid_size();
    }

private:
    GridSize m_min_grid_size;
    GridSize m_max_grid_size;
};

class GridTrackSizeList {
public:
    GridTrackSizeList(Vector<CSS::ExplicitGridTrack> track_list, Vector<Vector<DeprecatedString>> line_names);
    GridTrackSizeList();

    static GridTrackSizeList make_auto();

    Vector<CSS::ExplicitGridTrack> track_list() const { return m_track_list; }
    Vector<Vector<DeprecatedString>> line_names() const { return m_line_names; }

    DeprecatedString to_string() const;
    bool operator==(GridTrackSizeList const& other) const
    {
        return m_line_names == other.line_names() && m_track_list == other.track_list();
    }

private:
    Vector<CSS::ExplicitGridTrack> m_track_list;
    Vector<Vector<DeprecatedString>> m_line_names;
};

class GridRepeat {
public:
    enum class Type {
        AutoFit,
        AutoFill,
        Default,
    };
    GridRepeat(GridTrackSizeList, int repeat_count);
    GridRepeat(GridTrackSizeList, Type);
    GridRepeat();

    bool is_auto_fill() const { return m_type == Type::AutoFill; }
    bool is_auto_fit() const { return m_type == Type::AutoFit; }
    bool is_default() const { return m_type == Type::Default; }
    int repeat_count() const
    {
        VERIFY(is_default());
        return m_repeat_count;
    }
    GridTrackSizeList grid_track_size_list() const& { return m_grid_track_size_list; }
    Type type() const& { return m_type; }

    DeprecatedString to_string() const;
    bool operator==(GridRepeat const& other) const
    {
        if (m_type != other.type())
            return false;
        if (m_type == Type::Default && m_repeat_count != other.repeat_count())
            return false;
        return m_grid_track_size_list == other.grid_track_size_list();
    }

private:
    Type m_type;
    GridTrackSizeList m_grid_track_size_list;
    int m_repeat_count { 0 };
};

class ExplicitGridTrack {
public:
    enum class Type {
        MinMax,
        Repeat,
        Default,
    };
    ExplicitGridTrack(CSS::GridRepeat);
    ExplicitGridTrack(CSS::GridMinMax);
    ExplicitGridTrack(CSS::GridSize);

    bool is_repeat() const { return m_type == Type::Repeat; }
    GridRepeat repeat() const
    {
        VERIFY(is_repeat());
        return m_grid_repeat;
    }

    bool is_minmax() const { return m_type == Type::MinMax; }
    GridMinMax minmax() const
    {
        VERIFY(is_minmax());
        return m_grid_minmax;
    }

    bool is_default() const { return m_type == Type::Default; }
    GridSize grid_size() const
    {
        VERIFY(is_default());
        return m_grid_size;
    }

    Type type() const { return m_type; }

    DeprecatedString to_string() const;
    bool operator==(ExplicitGridTrack const& other) const
    {
        if (is_repeat() && other.is_repeat())
            return m_grid_repeat == other.repeat();
        if (is_minmax() && other.is_minmax())
            return m_grid_minmax == other.minmax();
        if (is_default() && other.is_default())
            return m_grid_size == other.grid_size();
        return false;
    }

private:
    Type m_type;
    GridRepeat m_grid_repeat;
    GridMinMax m_grid_minmax;
    GridSize m_grid_size;
};

}