srctree

Robin Linden parent d02a6fa7 502bc5fb
layout: Add %-unit-support to UnresolvedValue

Whatever % is resolved relative to still has to be added to theproperties we want it to work for.

inlinesplit
layout/layout_box.cpp added: 40, removed: 11, total 29
@@ -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;
@@ -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);
}
@@ -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
 
layout/layout_box.h added: 40, removed: 11, total 29
@@ -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
 
 
layout/unresolved_value.cpp added: 40, removed: 11, total 29
@@ -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
 
layout/unresolved_value.h added: 40, removed: 11, total 29
@@ -5,6 +5,7 @@
#ifndef LAYOUT_UNRESOLVED_VALUE_H_
#define LAYOUT_UNRESOLVED_VALUE_H_
 
#include <optional>
#include <string_view>
 
namespace layout {
@@ -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
 
layout/unresolved_value_test.cpp added: 40, removed: 11, total 29
@@ -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();
}