Skip to content

Commit

Permalink
layout: Add %-unit-support to UnresolvedValue
Browse files Browse the repository at this point in the history
Whatever % is resolved relative to still has to be added to the
properties we want it to work for.
  • Loading branch information
robinlinden committed Nov 14, 2023
1 parent d02a6fa commit 502bc5f
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
17 changes: 15 additions & 2 deletions layout/layout_box.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,11 @@ std::string to_string(LayoutBox const &box) {
return std::move(ss).str();
}

// NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
int to_px(std::string_view property, int const font_size, int const root_font_size) {
// NOLINTBEGIN(bugprone-easily-swappable-parameters)
int to_px(std::string_view property,
int const font_size,
int const root_font_size,
std::optional<int> parent_property_value) {
// Special case for 0 since it won't ever have a unit that needs to be handled.
if (property == "0") {
return 0;
Expand All @@ -163,6 +166,15 @@ int to_px(std::string_view property, int const font_size, int const root_font_si
auto const parsed_length = std::distance(property.data(), parse_result.ptr);
auto const unit = property.substr(parsed_length);

if (unit == "%") {
if (!parent_property_value.has_value()) {
spdlog::warn("Missing parent-value for property w/ '%' unit");
return 0;
}

return static_cast<int>(res / 100.f * (*parent_property_value));
}

if (unit == "px") {
return static_cast<int>(res);
}
Expand All @@ -180,5 +192,6 @@ int to_px(std::string_view property, int const font_size, int const root_font_si
spdlog::warn("Bad property '{}' w/ unit '{}' in to_px", property, unit);
return static_cast<int>(res);
}
// NOLINTEND(bugprone-easily-swappable-parameters)

} // namespace layout
5 changes: 4 additions & 1 deletion layout/layout_box.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ inline std::vector<LayoutBox const *> dom_children(LayoutBox const &node) {

// TODO(robinlinden): This should be internal.
// NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
int to_px(std::string_view property, int const font_size, int const root_font_size);
int to_px(std::string_view property,
int const font_size,
int const root_font_size,
std::optional<int> parent_property_value = std::nullopt);

} // namespace layout

Expand Down
4 changes: 2 additions & 2 deletions layout/unresolved_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace layout {
// NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
int UnresolvedValue::resolve(int font_size, int root_font_size) const {
return to_px(raw, font_size, root_font_size);
int UnresolvedValue::resolve(int font_size, int root_font_size, std::optional<int> percent_relative_to) const {
return to_px(raw, font_size, root_font_size, percent_relative_to);
}
} // namespace layout
3 changes: 2 additions & 1 deletion layout/unresolved_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#ifndef LAYOUT_UNRESOLVED_VALUE_H_
#define LAYOUT_UNRESOLVED_VALUE_H_

#include <optional>
#include <string_view>

namespace layout {
Expand All @@ -14,7 +15,7 @@ struct UnresolvedValue {
[[nodiscard]] bool operator==(UnresolvedValue const &) const = default;

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

} // namespace layout
Expand Down
12 changes: 12 additions & 0 deletions layout/unresolved_value_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,17 @@ int main() {
a.expect_eq(uv.resolve(0, 0), 0);
});

s.add_test("unit/%", [](etest::IActions &a) {
// Based on the third argument, whatever the spec wants the property
// this came from to be resolved against.
auto const uv = layout::UnresolvedValue{.raw = "50%"};
a.expect_eq(uv.resolve(100, 100, 100), 50);
a.expect_eq(uv.resolve(100, 100, 200), 100);
a.expect_eq(uv.resolve(0, 0, 1000), 500);

// If the third argument is not provided, you get nothing.
a.expect_eq(uv.resolve(123, 456), 0);
});

return s.run();
}

0 comments on commit 502bc5f

Please sign in to comment.