srctree

Robin Linden parent d8176c3c 2de4e985
layout: Prepare for handling box model layouts

inlinesplit
layout/layout.cpp added: 42, removed: 27, total 15
@@ -99,20 +99,20 @@ void calculate_width(LayoutBox &box, Rect const &parent) {
width_px += underflow;
}
 
box.dimensions.width = static_cast<float>(width_px);
box.dimensions.content.width = static_cast<float>(width_px);
}
 
void calculate_position(LayoutBox &box, Rect const &parent) {
box.dimensions.x = parent.x;
box.dimensions.content.x = parent.x;
// Position below previous content in parent.
box.dimensions.y = parent.y + parent.height;
box.dimensions.content.y = parent.y + parent.height;
}
 
// The box should already have the correct height unless it's overridden in CSS.
void calculate_height(LayoutBox &box) {
assert(box.node != nullptr);
if (auto height = get_property(*box.node, "height"); height) {
box.dimensions.height = static_cast<float>(to_px(*height));
box.dimensions.content.height = static_cast<float>(to_px(*height));
}
}
 
@@ -122,8 +122,8 @@ void layout(LayoutBox &box, Rect const &bounds) {
calculate_width(box, bounds);
calculate_position(box, bounds);
for (auto &child : box.children) {
layout(child, box.dimensions);
box.dimensions.height += child.dimensions.height;
layout(child, box.dimensions.content);
box.dimensions.content.height += child.dimensions.content.height;
}
calculate_height(box);
return;
@@ -164,7 +164,7 @@ void print_box(LayoutBox const &box, std::ostream &os, uint8_t depth = 0) {
for (int8_t i = 0; i < depth; ++i) { os << " "; }
}
 
os << to_str(box.type) << " " << to_str(box.dimensions) << '\n';
os << to_str(box.type) << " " << to_str(box.dimensions.content) << '\n';
for (auto const &child : box.children) { print_box(child, os, depth + 1); }
}
 
 
layout/layout.h added: 42, removed: 27, total 15
@@ -13,6 +13,21 @@ struct Rect {
bool operator==(Rect const &) const = default;
};
 
struct EdgeSize {
float left{}, right{}, top{}, bottom{};
bool operator==(EdgeSize const &) const = default;
};
 
struct BoxModel {
Rect content{};
 
EdgeSize margin{};
EdgeSize border{};
EdgeSize padding{};
 
bool operator==(BoxModel const &) const = default;
};
 
enum class LayoutType {
Inline,
Block,
@@ -22,7 +37,7 @@ enum class LayoutType {
struct LayoutBox {
style::StyledNode const *node;
LayoutType type;
Rect dimensions;
BoxModel dimensions;
std::vector<LayoutBox> children;
bool operator==(LayoutBox const &) const = default;
};
 
layout/layout_test.cpp added: 42, removed: 27, total 15
@@ -174,10 +174,10 @@ int main() {
auto expected_layout = layout::LayoutBox{
.node = &style_root,
.type = LayoutType::Block,
.dimensions = {0, 0, 100, 0},
.dimensions = {{0, 0, 100, 0}},
.children = {
{&style_root.children[0], LayoutType::Block, {0, 0, 100, 0}, {
{&style_root.children[0].children[0], LayoutType::Block, {0, 0, 100, 0}, {}},
{&style_root.children[0], LayoutType::Block, {{0, 0, 100, 0}}, {
{&style_root.children[0].children[0], LayoutType::Block, {{0, 0, 100, 0}}, {}},
}},
}
};
@@ -205,10 +205,10 @@ int main() {
auto expected_layout = layout::LayoutBox{
.node = &style_root,
.type = LayoutType::Block,
.dimensions = {0, 0, 100, 0},
.dimensions = {{0, 0, 100, 0}},
.children = {
{&style_root.children[0], LayoutType::Block, {0, 0, 50, 0}, {
{&style_root.children[0].children[0], LayoutType::Block, {0, 0, 25, 0}, {}},
{&style_root.children[0], LayoutType::Block, {{0, 0, 50, 0}}, {
{&style_root.children[0].children[0], LayoutType::Block, {{0, 0, 25, 0}}, {}},
}},
}
};
@@ -236,10 +236,10 @@ int main() {
auto expected_layout = layout::LayoutBox{
.node = &style_root,
.type = LayoutType::Block,
.dimensions = {0, 0, 100, 0},
.dimensions = {{0, 0, 100, 0}},
.children = {
{&style_root.children[0], LayoutType::Block, {0, 0, 100, 0}, {
{&style_root.children[0].children[0], LayoutType::Block, {0, 0, 100, 0}, {}},
{&style_root.children[0], LayoutType::Block, {{0, 0, 100, 0}}, {
{&style_root.children[0].children[0], LayoutType::Block, {{0, 0, 100, 0}}, {}},
}},
}
};
@@ -267,10 +267,10 @@ int main() {
auto expected_layout = layout::LayoutBox{
.node = &style_root,
.type = LayoutType::Block,
.dimensions = {0, 0, 0, 100},
.dimensions = {{0, 0, 0, 100}},
.children = {
{&style_root.children[0], LayoutType::Block, {0, 0, 0, 0}, {
{&style_root.children[0].children[0], LayoutType::Block, {0, 0, 0, 0}, {}},
{&style_root.children[0], LayoutType::Block, {{0, 0, 0, 0}}, {
{&style_root.children[0].children[0], LayoutType::Block, {{0, 0, 0, 0}}, {}},
}},
}
};
@@ -300,11 +300,11 @@ int main() {
auto expected_layout = layout::LayoutBox{
.node = &style_root,
.type = LayoutType::Block,
.dimensions = {0, 0, 0, 25},
.dimensions = {{0, 0, 0, 25}},
.children = {
{&style_root.children[0], LayoutType::Block, {0, 0, 0, 25}, {
{&style_root.children[0].children[0], LayoutType::Block, {0, 0, 0, 25}, {}},
{&style_root.children[0].children[1], LayoutType::Block, {0, 25, 0, 0}, {}},
{&style_root.children[0], LayoutType::Block, {{0, 0, 0, 25}}, {
{&style_root.children[0].children[0], LayoutType::Block, {{0, 0, 0, 25}}, {}},
{&style_root.children[0].children[1], LayoutType::Block, {{0, 25, 0, 0}}, {}},
}},
}
};