srctree

Robin Linden parent 004ce252 f33c4b40
layout: Add a helper that lets us move resolving values out of LayoutBox

This saves us from having to give LayoutBox knowledge of its parent inorder to resolve %-values, and as an added bonus, lets us easily reuseresolved font-size values.

inlinesplit
filename was Deleted added: 76, removed: 3, total 73
@@ -0,0 +1,14 @@
// SPDX-FileCopyrightText: 2023 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: BSD-2-Clause
 
#include "layout/unresolved_value.h"
 
#include "layout/layout_box.h"
 
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);
}
} // namespace layout
 
filename was Deleted added: 76, removed: 3, total 73
@@ -0,0 +1,22 @@
// SPDX-FileCopyrightText: 2023 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: BSD-2-Clause
 
#ifndef LAYOUT_UNRESOLVED_VALUE_H_
#define LAYOUT_UNRESOLVED_VALUE_H_
 
#include <string_view>
 
namespace layout {
 
struct UnresolvedValue {
std::string_view raw{};
[[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;
};
 
} // namespace layout
 
#endif
 
filename was Deleted added: 76, removed: 3, total 73
@@ -0,0 +1,37 @@
// SPDX-FileCopyrightText: 2023 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: BSD-2-Clause
 
#include "layout/unresolved_value.h"
 
#include "etest/etest2.h"
 
int main() {
etest::Suite s{"UnresolvedValue"};
 
s.add_test("unit/px", [](etest::IActions &a) {
// Just a raw numeric value.
auto const uv = layout::UnresolvedValue{.raw = "37px"};
a.expect_eq(uv.resolve(100, 100), 37);
a.expect_eq(uv.resolve(123, 456), 37);
a.expect_eq(uv.resolve(0, 0), 37);
});
 
s.add_test("unit/em", [](etest::IActions &a) {
// Based on the first argument, the current element's font-size.
auto const uv = layout::UnresolvedValue{.raw = "2em"};
a.expect_eq(uv.resolve(100, 100), 200);
a.expect_eq(uv.resolve(123, 456), 246);
a.expect_eq(uv.resolve(0, 0), 0);
});
 
s.add_test("unit/rem", [](etest::IActions &a) {
// Based on the second argument, the root element's font-size.
auto const uv = layout::UnresolvedValue{.raw = "2rem"};
a.expect_eq(uv.resolve(100, 100), 200);
a.expect_eq(uv.resolve(123, 456), 912);
a.expect_eq(uv.resolve(0, 0), 0);
});
 
return s.run();
}