LibWeb/CSS: Use Size in GridSize

Reduces a bunch of duplicate logic here for holding different sizing
functions, and also removes a user of `auto` Length.
This commit is contained in:
Sam Atkins 2025-09-04 11:38:27 +01:00
commit ae40c7ed95
Notes: github-actions[bot] 2025-09-04 12:34:21 +00:00
5 changed files with 92 additions and 102 deletions

View file

@ -11,41 +11,26 @@
namespace Web::CSS {
GridSize::GridSize(Type type, LengthPercentage length_percentage)
: m_value(move(length_percentage))
{
VERIFY(type == Type::FitContent);
m_type = type;
}
GridSize::GridSize(LengthPercentage length_percentage)
: m_type(Type::LengthPercentage)
, m_value(move(length_percentage))
GridSize::GridSize(Size size)
: m_value(move(size))
{
}
GridSize::GridSize(Flex flex_factor)
: m_type(Type::FlexibleLength)
, m_value(move(flex_factor))
: m_value(move(flex_factor))
{
}
GridSize::GridSize(Type type)
: m_value { Empty() }
{
VERIFY(type == Type::MinContent || type == Type::MaxContent);
m_type = type;
}
GridSize::~GridSize() = default;
bool GridSize::is_auto(Layout::AvailableSize const& available_size) const
{
if (m_type == Type::LengthPercentage) {
auto& length_percentage = m_value.get<LengthPercentage>();
if (length_percentage.contains_percentage())
if (auto const* size = m_value.get_pointer<Size>()) {
if (size->is_auto())
return true;
if (size->contains_percentage())
return !available_size.is_definite();
return length_percentage.is_auto();
return false;
}
return false;
@ -53,59 +38,83 @@ 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) {
auto& length_percentage = m_value.get<LengthPercentage>();
if (length_percentage.contains_percentage())
if (auto const* size = m_value.get_pointer<Size>()) {
if (!size->is_length_percentage())
return false;
if (size->contains_percentage())
return available_size.is_definite();
return !length_percentage.is_auto();
return true;
}
return false;
}
bool GridSize::is_flexible_length() const
{
return m_value.has<Flex>();
}
bool GridSize::is_fit_content() const
{
if (auto const* size = m_value.get_pointer<Size>())
return size->is_fit_content();
return false;
}
bool GridSize::is_max_content() const
{
if (auto const* size = m_value.get_pointer<Size>())
return size->is_max_content();
return false;
}
bool GridSize::is_min_content() const
{
if (auto const* size = m_value.get_pointer<Size>())
return size->is_min_content();
return false;
}
bool GridSize::is_intrinsic(Layout::AvailableSize const& available_size) const
{
return is_auto(available_size) || is_max_content() || is_min_content() || is_fit_content();
return m_value.visit(
[&available_size](Size const& size) {
return size.is_auto()
|| size.is_max_content()
|| size.is_min_content()
|| size.is_fit_content()
|| (size.contains_percentage() && !available_size.is_definite());
},
[](Flex const&) {
return false;
});
}
bool GridSize::is_definite() const
{
return m_value.visit(
[](Size const& size) {
return (size.is_length() && !size.length().is_auto()) || size.is_percentage() || size.is_calculated();
},
[](Flex const&) { return false; });
}
GridSize GridSize::make_auto()
{
return GridSize(CSS::Length::make_auto());
}
Size GridSize::css_size() const
{
VERIFY(m_type == Type::LengthPercentage || m_type == Type::FitContent);
auto& length_percentage = m_value.get<LengthPercentage>();
if (length_percentage.is_auto())
return CSS::Size::make_auto();
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());
return GridSize(Size::make_auto());
}
String GridSize::to_string(SerializationMode mode) const
{
switch (m_type) {
case Type::LengthPercentage:
return m_value.get<LengthPercentage>().to_string(mode);
case Type::FitContent:
return MUST(String::formatted("fit-content({})", m_value.get<LengthPercentage>().to_string(mode)));
case Type::FlexibleLength:
return m_value.get<Flex>().to_string();
case Type::MaxContent:
return "max-content"_string;
case Type::MinContent:
return "min-content"_string;
}
VERIFY_NOT_REACHED();
return m_value.visit([mode](auto const& it) { return it.to_string(mode); });
}
GridMinMax::GridMinMax(GridSize min_grid_size, GridSize max_grid_size)
: m_min_grid_size(min_grid_size)
, m_max_grid_size(max_grid_size)
: m_min_grid_size(move(min_grid_size))
, m_max_grid_size(move(max_grid_size))
{
}