Skip to content

Commit

Permalink
vaev-layout: Split some headers down.
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-monax committed Jul 30, 2024
1 parent 02e77c1 commit 0d9ef84
Show file tree
Hide file tree
Showing 8 changed files with 293 additions and 245 deletions.
73 changes: 73 additions & 0 deletions src/web/vaev-layout/block.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include "block.h"

#include "sizing.h"

namespace Vaev::Layout {

void BlockFlow::placeChildren(Context &ctx, Box box) {
Frag::placeChildren(ctx, box);

Axis mainAxis = Axis::VERTICAL;

Px res = box.contentBox().start();

for (auto &c : _frags) {
auto childcontext = ctx.subContext(
*c,
mainAxis,
box.contentBox()
);

auto blockSize = computePreferredOuterSize(
childcontext,
mainAxis,
max(Px{0}, box.contentBox().height - res)
);

auto inlineSize = computePreferredBorderSize(
childcontext,
mainAxis.cross(),
box.contentBox().width
);

RectPx borderBox = RectPx{
box.contentBox().start(),
res,
inlineSize,
blockSize,
};

auto box = computeBox(childcontext, borderBox);
c->placeChildren(childcontext, box);

res += blockSize;
}
}

Px BlockFlow::computeIntrinsicSize(Context &ctx, Axis axis, IntrinsicSize intrinsic, Px) {
Px res = Px{};

for (auto &c : _frags) {
auto childCtx = ctx.subContext(
*c,
axis,
Vec2Px::ZERO
);

if (axis == Axis::VERTICAL) {
auto size = computePreferredOuterSize(childCtx, axis);
if (intrinsic == IntrinsicSize::MAX_CONTENT) {
res += size;
} else {
res = max(res, size);
}
} else {
auto size = computePreferredOuterSize(childCtx, axis);
res = max(res, size);
}
}

return res;
}

} // namespace Vaev::Layout
67 changes: 2 additions & 65 deletions src/web/vaev-layout/block.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#include "flow.h"
#include "sizing.h"

namespace Vaev::Layout {

Expand All @@ -14,71 +13,9 @@ struct BlockFlow : public Flow {
return TYPE;
}

void placeChildren(Context &ctx, Box box) override {
Frag::placeChildren(ctx, box);
void placeChildren(Context &ctx, Box box) override;

Axis mainAxis = Axis::VERTICAL;

Px res = box.contentBox().start();

for (auto &c : _frags) {
auto childcontext = ctx.subContext(
*c,
mainAxis,
box.contentBox()
);

auto blockSize = computePreferredOuterSize(
childcontext,
mainAxis,
max(Px{0}, box.contentBox().height - res)
);

auto inlineSize = computePreferredBorderSize(
childcontext,
mainAxis.cross(),
box.contentBox().width
);

RectPx borderBox = RectPx{
box.contentBox().start(),
res,
inlineSize,
blockSize,
};

auto box = computeBox(childcontext, borderBox);
c->placeChildren(childcontext, box);

res += blockSize;
}
}

Px computeIntrinsicSize(Context &ctx, Axis axis, IntrinsicSize intrinsic, Px) override {
Px res = Px{};

for (auto &c : _frags) {
auto childCtx = ctx.subContext(
*c,
axis,
Vec2Px::ZERO
);

if (axis == Axis::VERTICAL) {
auto size = computePreferredOuterSize(childCtx, axis);
if (intrinsic == IntrinsicSize::MAX_CONTENT) {
res += size;
} else {
res = max(res, size);
}
} else {
auto size = computePreferredOuterSize(childCtx, axis);
res = max(res, size);
}
}

return res;
}
Px computeIntrinsicSize(Context &ctx, Axis axis, IntrinsicSize intrinsic, Px) override;
};

} // namespace Vaev::Layout
91 changes: 91 additions & 0 deletions src/web/vaev-layout/flex.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include "flex.h"

#include "sizing.h"

namespace Vaev::Layout {

void FlexFlow::_clear() {
_items.clear();
_lines.clear();
}

void FlexFlow::_createItems() {
for (usize i = 0; i < _frags.len(); i++)
_items.pushBack({i});
}

void FlexFlow::_sortByOrder() {
stableSort(_items, [&](auto a, auto b) {
return styleAt(a.frag).order < styleAt(b.frag).order;
});
}

void FlexFlow::placeChildren(Context &ctx, Box box) {
Frag::placeChildren(ctx, box);

_clear();
_createItems();
_sortByOrder();

Axis mainAxis = Axis::HORIZONTAL;

Px res = box.contentBox().start();
auto blockSize = computePreferredBorderSize(
ctx,
mainAxis.cross(),
box.contentBox().height
);

for (auto &c : _frags) {
auto childcontext = ctx.subContext(
*c,
mainAxis,
box.contentBox()
);

auto inlineSize = computePreferredOuterSize(
childcontext, mainAxis,
max(Px{0}, box.contentBox().width - res)
);

RectPx borderBox = RectPx{
res,
box.contentBox().top(),
inlineSize,
blockSize,
};

auto box = computeBox(childcontext, borderBox);
c->placeChildren(childcontext, box);

res += inlineSize;
}
}

Px FlexFlow::computeIntrinsicSize(Context &ctx, Axis axis, IntrinsicSize intrinsic, Px) {
Px res = Px{};

for (auto &c : _frags) {
auto childCtx = ctx.subContext(
*c,
axis,
Vec2Px::ZERO
);

if (axis == Axis::HORIZONTAL) {
auto size = computePreferredOuterSize(childCtx, axis);
if (intrinsic == IntrinsicSize::MAX_CONTENT) {
res += size;
} else {
res = max(res, size);
}
} else {
auto size = computePreferredOuterSize(childCtx, axis);
res = max(res, size);
}
}

return res;
}

} // namespace Vaev::Layout
85 changes: 5 additions & 80 deletions src/web/vaev-layout/flex.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#include "flow.h"
#include "sizing.h"

namespace Vaev::Layout {

Expand All @@ -25,89 +24,15 @@ struct FlexFlow : public Flow {
return TYPE;
}

void _clear() {
_items.clear();
_lines.clear();
}

void _createItems() {
for (usize i = 0; i < _frags.len(); i++)
_items.pushBack({i});
}

void _sortByOrder() {
stableSort(_items, [&](auto a, auto b) {
return styleAt(a.frag).order < styleAt(b.frag).order;
});
}

void placeChildren(Context &ctx, Box box) override {
Frag::placeChildren(ctx, box);

_clear();
_createItems();
_sortByOrder();

Axis mainAxis = Axis::HORIZONTAL;
void _clear();

Px res = box.contentBox().start();
auto blockSize = computePreferredBorderSize(
ctx,
mainAxis.cross(),
box.contentBox().height
);
void _createItems();

for (auto &c : _frags) {
auto childcontext = ctx.subContext(
*c,
mainAxis,
box.contentBox()
);
void _sortByOrder();

auto inlineSize = computePreferredOuterSize(
childcontext, mainAxis,
max(Px{0}, box.contentBox().width - res)
);
void placeChildren(Context &ctx, Box box) override;

RectPx borderBox = RectPx{
res,
box.contentBox().top(),
inlineSize,
blockSize,
};

auto box = computeBox(childcontext, borderBox);
c->placeChildren(childcontext, box);

res += inlineSize;
}
}

Px computeIntrinsicSize(Context &ctx, Axis axis, IntrinsicSize intrinsic, Px) override {
Px res = Px{};

for (auto &c : _frags) {
auto childCtx = ctx.subContext(
*c,
axis,
Vec2Px::ZERO
);

if (axis == Axis::HORIZONTAL) {
auto size = computePreferredOuterSize(childCtx, axis);
if (intrinsic == IntrinsicSize::MAX_CONTENT) {
res += size;
} else {
res = max(res, size);
}
} else {
auto size = computePreferredOuterSize(childCtx, axis);
res = max(res, size);
}
}

return res;
}
Px computeIntrinsicSize(Context &ctx, Axis axis, IntrinsicSize intrinsic, Px) override;
};

} // namespace Vaev::Layout
47 changes: 47 additions & 0 deletions src/web/vaev-layout/flow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "flow.h"

namespace Vaev::Layout {

void Flow::placeChildren(Context &ctx, Box box) {
Frag::placeChildren(ctx, box);

for (auto &c : _frags) {
c->placeChildren(ctx, box);
}
}

void Flow::makePaintables(Paint::Stack &stack) {
Frag::makePaintables(stack);

for (auto &c : _frags) {
c->makePaintables(stack);
}
}

void Flow::paintWireframe(Gfx::Context &g) {
for (auto &c : _frags) {
c->paintWireframe(g);
}

g.strokeStyle({
.paint = Gfx::BLACK,
.width = 1,
.align = Gfx::INSIDE_ALIGN,
});
g.stroke(_box.borderBox.cast<f64>());
}

void Flow::repr(Io::Emit &e) const {
e("({} {}", type(), _box.borderBox);
if (_frags) {
e.indentNewline();
for (auto &c : _frags) {
c->repr(e);
e.newline();
}
e.deindent();
}
e(")");
}

} // namespace Vaev::Layout
Loading

0 comments on commit 0d9ef84

Please sign in to comment.