1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 17:37:37 +00:00

LibWeb: Refactor GridTrackSize classes

Refactor various classes in the GridTrackSize file for the incoming
named_tracks feature.

Previously the ExplicitTrackSizing had mixed responsiblities with the
newly-named GridRepeat class. This made it so it was not possible to
have multiple repeats within a single 'GridTrackSizeList' definition.

The MetaGridTrackSize class had both the responsibilities of being a
container for minmax values as well as for simple GridSizes. By uniting
the different possible values (repeat, minmax, default) into the
ExplicitGridTrack class are able to be more expressive as to the
different grid size modalities.

The GridTrackSizeList will be useful as compared to a
Vector<ExplicitGridTrack> since this way can keep track of the declared
line names. These same line names are able to be declared within the
values of a repeat function, hence the presence of a GridTrackSizeList
inside the GridRepeat class.
This commit is contained in:
martinfalisse 2022-10-30 13:27:57 +01:00 committed by Andreas Kling
parent 9441515312
commit b2b677e984
12 changed files with 639 additions and 380 deletions

View file

@ -12,7 +12,7 @@
namespace Web::CSS {
class GridTrackSize {
class GridSize {
public:
enum class Type {
Length,
@ -21,12 +21,13 @@ public:
// TODO: Max-Content
};
GridTrackSize(Length);
GridTrackSize(Percentage);
GridTrackSize(float);
~GridTrackSize();
GridSize(Length);
GridSize(Percentage);
GridSize(float);
GridSize();
~GridSize();
static GridTrackSize make_auto();
static GridSize make_auto();
Type type() const { return m_type; }
@ -52,7 +53,7 @@ public:
}
String to_string() const;
bool operator==(GridTrackSize const& other) const
bool operator==(GridSize const& other) const
{
return m_type == other.type()
&& m_length == other.length()
@ -69,63 +70,133 @@ private:
float m_flexible_length { 0 };
};
class MetaGridTrackSize {
class GridMinMax {
public:
MetaGridTrackSize(CSS::GridTrackSize);
MetaGridTrackSize(CSS::GridTrackSize min_grid_track_size, CSS::GridTrackSize max_grid_track_size);
GridMinMax(CSS::GridSize min_grid_size, CSS::GridSize max_grid_size);
GridMinMax() = default;
bool is_min_max() const { return m_is_min_max; }
GridTrackSize grid_track_size() const& { return m_min_grid_track_size; }
GridTrackSize min_grid_track_size() const& { return m_min_grid_track_size; }
GridTrackSize max_grid_track_size() const& { return m_max_grid_track_size; }
GridSize min_grid_size() const& { return m_min_grid_size; }
GridSize max_grid_size() const& { return m_max_grid_size; }
String to_string() const;
bool operator==(MetaGridTrackSize const& other) const
bool operator==(GridMinMax const& other) const
{
return m_min_grid_track_size == other.min_grid_track_size()
&& m_max_grid_track_size == other.max_grid_track_size();
return m_min_grid_size == other.min_grid_size()
&& m_max_grid_size == other.max_grid_size();
}
private:
GridTrackSize m_min_grid_track_size;
GridTrackSize m_max_grid_track_size;
bool m_is_min_max { false };
GridSize m_min_grid_size;
GridSize m_max_grid_size;
};
class ExplicitTrackSizing {
class GridTrackSizeList {
public:
GridTrackSizeList(Vector<CSS::ExplicitGridTrack> track_list);
GridTrackSizeList();
static GridTrackSizeList make_auto();
Vector<CSS::ExplicitGridTrack> track_list() const { return m_track_list; }
String to_string() const;
bool operator==(GridTrackSizeList const& other) const
{
return m_track_list == other.track_list();
}
private:
Vector<CSS::ExplicitGridTrack> m_track_list;
};
class GridRepeat {
public:
enum class Type {
AutoFit,
AutoFill,
Default,
};
GridRepeat(GridTrackSizeList, int repeat_count);
GridRepeat(GridTrackSizeList, Type);
GridRepeat();
ExplicitTrackSizing();
ExplicitTrackSizing(Vector<CSS::MetaGridTrackSize>);
ExplicitTrackSizing(Vector<CSS::MetaGridTrackSize>, int repeat_count);
ExplicitTrackSizing(Vector<CSS::MetaGridTrackSize>, Type);
static ExplicitTrackSizing make_auto() { return ExplicitTrackSizing(); };
bool is_repeat() const { return m_is_repeat; }
bool is_auto_fill() const { return m_is_auto_fill; }
bool is_auto_fit() const { return m_is_auto_fit; }
int repeat_count() const { return m_repeat_count; }
Vector<CSS::MetaGridTrackSize> meta_grid_track_sizes() const& { return m_meta_grid_track_sizes; }
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; }
String to_string() const;
bool operator==(ExplicitTrackSizing const& other) const
bool operator==(GridRepeat const& other) const
{
return m_meta_grid_track_sizes == other.meta_grid_track_sizes();
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:
Vector<CSS::MetaGridTrackSize> m_meta_grid_track_sizes;
bool m_is_repeat { false };
bool m_is_auto_fill { false };
bool m_is_auto_fit { false };
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; }
String 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;
};
}