LibWeb: Store GridSize values as a Variant

A GridSize can't hold both a LengthPercentage and a Flex at the same
time, so let's limit that.
This commit is contained in:
Sam Atkins 2023-09-28 16:16:25 +01:00 committed by Sam Atkins
parent b66ff21379
commit ca16a1ed08
2 changed files with 26 additions and 25 deletions

View file

@ -12,17 +12,18 @@ namespace Web::CSS {
GridSize::GridSize(LengthPercentage length_percentage)
: m_type(Type::LengthPercentage)
, m_length_percentage(length_percentage) {};
, m_value(move(length_percentage))
{
}
GridSize::GridSize(Flex flex_factor)
: m_type(Type::FlexibleLength)
, m_length_percentage { Length::make_px(0) }
, m_flex_factor(flex_factor)
, m_value(move(flex_factor))
{
}
GridSize::GridSize(Type type)
: m_length_percentage { Length::make_auto() }
: m_value { Empty() }
{
VERIFY(type == Type::MinContent || type == Type::MaxContent);
m_type = type;
@ -30,7 +31,7 @@ GridSize::GridSize(Type type)
GridSize::GridSize()
: m_type(Type::LengthPercentage)
, m_length_percentage { Length::make_auto() }
, m_value { Length::make_auto() }
{
}
@ -39,9 +40,10 @@ GridSize::~GridSize() = default;
bool GridSize::is_auto(Layout::AvailableSize const& available_size) const
{
if (m_type == Type::LengthPercentage) {
if (m_length_percentage.contains_percentage())
auto& length_percentage = m_value.get<LengthPercentage>();
if (length_percentage.contains_percentage())
return !available_size.is_definite();
return m_length_percentage.is_auto();
return length_percentage.is_auto();
}
return false;
@ -50,9 +52,10 @@ bool GridSize::is_auto(Layout::AvailableSize const& available_size) const
bool GridSize::is_fixed(Layout::AvailableSize const& available_size) const
{
if (m_type == Type::LengthPercentage) {
if (m_length_percentage.contains_percentage())
auto& length_percentage = m_value.get<LengthPercentage>();
if (length_percentage.contains_percentage())
return available_size.is_definite();
return !m_length_percentage.is_auto();
return !length_percentage.is_auto();
}
return false;
@ -71,23 +74,23 @@ GridSize GridSize::make_auto()
Size GridSize::css_size() const
{
VERIFY(m_type == Type::LengthPercentage);
if (m_length_percentage.is_auto())
auto& length_percentage = m_value.get<LengthPercentage>();
if (length_percentage.is_auto())
return CSS::Size::make_auto();
if (m_length_percentage.is_length())
return CSS::Size::make_length(m_length_percentage.length());
if (m_length_percentage.is_calculated()) {
return CSS::Size::make_calculated(m_length_percentage.calculated());
}
return CSS::Size::make_percentage(m_length_percentage.percentage());
if (length_percentage.is_length())
return CSS::Size::make_length(length_percentage.length());
if (length_percentage.is_calculated())
return CSS::Size::make_calculated(length_percentage.calculated());
return CSS::Size::make_percentage(length_percentage.percentage());
}
String GridSize::to_string() const
{
switch (m_type) {
case Type::LengthPercentage:
return m_length_percentage.to_string();
return m_value.get<LengthPercentage>().to_string();
case Type::FlexibleLength:
return m_flex_factor.to_string();
return m_value.get<Flex>().to_string();
case Type::MaxContent:
return "max-content"_string;
case Type::MinContent:

View file

@ -37,8 +37,8 @@ public:
bool is_max_content() const { return m_type == Type::MaxContent; }
bool is_min_content() const { return m_type == Type::MinContent; }
LengthPercentage length_percentage() const { return m_length_percentage; }
double flex_factor() const { return m_flex_factor.to_fr(); }
LengthPercentage length_percentage() const { return m_value.get<LengthPercentage>(); }
double flex_factor() const { return m_value.get<Flex>().to_fr(); }
// https://www.w3.org/TR/css-grid-2/#layout-algorithm
// An intrinsic sizing function (min-content, max-content, auto, fit-content()).
@ -47,7 +47,7 @@ public:
bool is_definite() const
{
return type() == Type::LengthPercentage && !m_length_percentage.is_auto();
return type() == Type::LengthPercentage && !length_percentage().is_auto();
}
Size css_size() const;
@ -56,14 +56,12 @@ public:
bool operator==(GridSize const& other) const
{
return m_type == other.type()
&& m_length_percentage == other.length_percentage()
&& m_flex_factor == other.m_flex_factor;
&& m_value == other.m_value;
}
private:
Type m_type;
LengthPercentage m_length_percentage;
Flex m_flex_factor { 0, Flex::Type::Fr };
Variant<Empty, LengthPercentage, Flex> m_value;
};
class GridMinMax {