Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

layout: Handle relative max/min-width properties #772

Merged
merged 1 commit into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions layout/layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,16 +191,18 @@ void calculate_width_and_margin(
box.dimensions.content.width = parent.width - box.dimensions.margin_box().width;
}

if (auto min = box.get_property<css::PropertyId::MinWidth>()) {
if (box.dimensions.content.width < *min) {
box.dimensions.content.width = *min;
if (auto min = box.get_property<css::PropertyId::MinWidth>(); !min.is_auto()) {
auto resolved = min.resolve(font_size, root_font_size, parent.width);
if (box.dimensions.content.width < resolved) {
box.dimensions.content.width = resolved;
calculate_left_and_right_margin(box, parent, margin_left, margin_right, font_size, root_font_size);
}
}

if (auto max = box.get_property<css::PropertyId::MaxWidth>()) {
if (box.dimensions.content.width > *max) {
box.dimensions.content.width = *max;
if (auto max = box.get_property<css::PropertyId::MaxWidth>(); !max.is_none()) {
auto resolved = max.resolve(font_size, root_font_size, parent.width);
if (box.dimensions.content.width > resolved) {
box.dimensions.content.width = resolved;
calculate_left_and_right_margin(box, parent, margin_left, margin_right, font_size, root_font_size);
}
}
Expand Down
22 changes: 0 additions & 22 deletions layout/layout_box.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,28 +100,6 @@ std::pair<int, int> LayoutBox::get_border_radius_property(css::PropertyId id) co
return {to_px(horizontal, font_size, root_font_size), to_px(vertical, font_size, root_font_size)};
}

std::optional<int> LayoutBox::get_min_width_property() const {
auto raw = node->get_raw_property(css::PropertyId::MinWidth);
if (raw == "auto") {
return std::nullopt;
}

int font_size = node->get_property<css::PropertyId::FontSize>();
int root_font_size = get_root_font_size(*node);
return to_px(raw, font_size, root_font_size);
}

std::optional<int> LayoutBox::get_max_width_property() const {
auto raw = node->get_raw_property(css::PropertyId::MaxWidth);
if (raw == "none") {
return std::nullopt;
}

int font_size = node->get_property<css::PropertyId::FontSize>();
int root_font_size = get_root_font_size(*node);
return to_px(raw, font_size, root_font_size);
}

LayoutBox const *box_at_position(LayoutBox const &box, geom::Position p) {
if (!box.dimensions.contains(p)) {
return nullptr;
Expand Down
7 changes: 2 additions & 5 deletions layout/layout_box.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,9 @@ struct LayoutBox {
if constexpr (T == css::PropertyId::BorderBottomLeftRadius || T == css::PropertyId::BorderBottomRightRadius
|| T == css::PropertyId::BorderTopLeftRadius || T == css::PropertyId::BorderTopRightRadius) {
return get_border_radius_property(T);
} else if constexpr (T == css::PropertyId::MinWidth) {
return get_min_width_property();
} else if constexpr (T == css::PropertyId::Width) {
} else if constexpr (T == css::PropertyId::MinWidth || T == css::PropertyId::Width
|| T == css::PropertyId::MaxWidth) {
return UnresolvedValue{node->get_raw_property(T)};
} else if constexpr (T == css::PropertyId::MaxWidth) {
return get_max_width_property();
} else {
return node->get_property<T>();
}
Expand Down
8 changes: 4 additions & 4 deletions layout/layout_property_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ int main() {
});

etest::test("width", [] {
expect_property_eq<MinWidth>("13px", 13);
expect_property_eq<MinWidth>("auto", std::nullopt);
expect_property_eq<MinWidth>("13px", layout::UnresolvedValue{"13px"});
expect_property_eq<MinWidth>("auto", layout::UnresolvedValue{"auto"});

expect_property_eq<Width>("42px", layout::UnresolvedValue{"42px"});
expect_property_eq<Width>("auto", layout::UnresolvedValue{"auto"});

expect_property_eq<MaxWidth>("420px", 420);
expect_property_eq<MaxWidth>("none", std::nullopt);
expect_property_eq<MaxWidth>("420px", layout::UnresolvedValue{"420px"});
expect_property_eq<MaxWidth>("none", layout::UnresolvedValue{"none"});
});

return etest::run_all_tests();
Expand Down
12 changes: 6 additions & 6 deletions layout/layout_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ int main() {
.node = dom_root,
.properties = {{css::PropertyId::MinWidth, "100px"}, {css::PropertyId::Display, "block"}},
.children = {
{children[0], {{css::PropertyId::MinWidth, "50px"}, {css::PropertyId::Display, "block"}}, {
{children[0], {{css::PropertyId::MinWidth, "50%"}, {css::PropertyId::Display, "block"}}, {
{std::get<dom::Element>(children[0]).children[0], {{css::PropertyId::Display, "block"}}, {}},
}},
},
Expand Down Expand Up @@ -582,9 +582,9 @@ int main() {
auto const &children = std::get<dom::Element>(dom_root).children;
auto style_root = style::StyledNode{
.node = dom_root,
.properties = {{css::PropertyId::MaxWidth, "100px"}, {css::PropertyId::Display, "block"}},
.properties = {{css::PropertyId::MaxWidth, "200px"}, {css::PropertyId::Display, "block"}},
.children = {
{children[0], {{css::PropertyId::MaxWidth, "50px"}, {css::PropertyId::Display, "block"}}, {
{children[0], {{css::PropertyId::MaxWidth, "50%"}, {css::PropertyId::Display, "block"}}, {
{std::get<dom::Element>(children[0]).children[0], {{css::PropertyId::Display, "block"}}, {}},
}},
},
Expand All @@ -593,10 +593,10 @@ int main() {
auto expected_layout = layout::LayoutBox{
.node = &style_root,
.type = LayoutType::Block,
.dimensions = {{0, 0, 100, 0}},
.dimensions = {{0, 0, 200, 0}},
.children = {
{&style_root.children[0], LayoutType::Block, {{0, 0, 50, 0}}, {
{&style_root.children[0].children[0], LayoutType::Block, {{0, 0, 50, 0}}, {}},
{&style_root.children[0], LayoutType::Block, {{0, 0, 100, 0}}, {
{&style_root.children[0].children[0], LayoutType::Block, {{0, 0, 100, 0}}, {}},
}},
}
};
Expand Down
1 change: 1 addition & 0 deletions layout/unresolved_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ struct UnresolvedValue {
[[nodiscard]] bool operator==(UnresolvedValue const &) const = default;

constexpr bool is_auto() const { return raw == "auto"; }
constexpr bool is_none() const { return raw == "none"; }
int resolve(int font_size, int root_font_size, std::optional<int> percent_relative_to = std::nullopt) const;
};

Expand Down