diff --git a/src/web/vaev-layout/block.cpp b/src/web/vaev-layout/block.cpp new file mode 100644 index 0000000000..b44cdeac3c --- /dev/null +++ b/src/web/vaev-layout/block.cpp @@ -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 diff --git a/src/web/vaev-layout/block.h b/src/web/vaev-layout/block.h index 78e4e43a2b..c6c2b04608 100644 --- a/src/web/vaev-layout/block.h +++ b/src/web/vaev-layout/block.h @@ -1,7 +1,6 @@ #pragma once #include "flow.h" -#include "sizing.h" namespace Vaev::Layout { @@ -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 diff --git a/src/web/vaev-layout/flex.cpp b/src/web/vaev-layout/flex.cpp new file mode 100644 index 0000000000..ee374a9a4a --- /dev/null +++ b/src/web/vaev-layout/flex.cpp @@ -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 diff --git a/src/web/vaev-layout/flex.h b/src/web/vaev-layout/flex.h index e556404e3a..42d065043a 100644 --- a/src/web/vaev-layout/flex.h +++ b/src/web/vaev-layout/flex.h @@ -1,7 +1,6 @@ #pragma once #include "flow.h" -#include "sizing.h" namespace Vaev::Layout { @@ -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 diff --git a/src/web/vaev-layout/flow.cpp b/src/web/vaev-layout/flow.cpp new file mode 100644 index 0000000000..715143f1b7 --- /dev/null +++ b/src/web/vaev-layout/flow.cpp @@ -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()); +} + +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 diff --git a/src/web/vaev-layout/flow.h b/src/web/vaev-layout/flow.h index a656845d31..c5afddda47 100644 --- a/src/web/vaev-layout/flow.h +++ b/src/web/vaev-layout/flow.h @@ -25,47 +25,13 @@ struct Flow : public Frag { _frags.pushBack(frag); } - void placeChildren(Context &ctx, Box box) override { - Frag::placeChildren(ctx, box); + void placeChildren(Context &ctx, Box box) override; - for (auto &c : _frags) { - c->placeChildren(ctx, box); - } - } - - void makePaintables(Paint::Stack &stack) override { - Frag::makePaintables(stack); - - for (auto &c : _frags) { - c->makePaintables(stack); - } - } + void makePaintables(Paint::Stack &stack) override; - void paintWireframe(Gfx::Context &g) override { - for (auto &c : _frags) { - c->paintWireframe(g); - } + void paintWireframe(Gfx::Context &g) override; - g.strokeStyle({ - .paint = Gfx::BLACK, - .width = 1, - .align = Gfx::INSIDE_ALIGN, - }); - g.stroke(_box.borderBox.cast()); - } - - void repr(Io::Emit &e) const override { - e("({} {}", type(), _box.borderBox); - if (_frags) { - e.indentNewline(); - for (auto &c : _frags) { - c->repr(e); - e.newline(); - } - e.deindent(); - } - e(")"); - } + void repr(Io::Emit &e) const override; }; } // namespace Vaev::Layout diff --git a/src/web/vaev-layout/inline.cpp b/src/web/vaev-layout/inline.cpp new file mode 100644 index 0000000000..757678147d --- /dev/null +++ b/src/web/vaev-layout/inline.cpp @@ -0,0 +1,69 @@ +#include "inline.h" + +namespace Vaev::Layout { + +void InlineFlow::placeChildren(Context &ctx, Box box) { + Frag::placeChildren(ctx, box); + + Axis mainAxis = Axis::INLINE; + + Px res = box.borderBox.start(); + auto blockSize = computePreferredBorderSize( + ctx, + mainAxis.cross(), + box.contentBox().height + ); + + for (auto &c : _frags) { + auto childCtx = ctx.subContext( + *c, + mainAxis, + box.contentBox() + ); + + auto inlineSize = computePreferredOuterSize( + childCtx, mainAxis, + box.contentBox().width - res + ); + + RectPx borderBox = RectPx{ + res, + box.contentBox().top(), + inlineSize, + blockSize, + }; + + auto box = computeBox(childCtx, borderBox); + c->placeChildren(childCtx, box); + + res += inlineSize; + } +} + +Px InlineFlow::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, Px{0}); + if (intrinsic == IntrinsicSize::MAX_CONTENT) { + res += size; + } else { + res = max(res, size); + } + } else { + auto size = computePreferredOuterSize(childCtx, axis, Px{0}); + res = max(res, size); + } + } + + return res; +} + +} // namespace Vaev::Layout diff --git a/src/web/vaev-layout/inline.h b/src/web/vaev-layout/inline.h index f119c6cb44..6687103370 100644 --- a/src/web/vaev-layout/inline.h +++ b/src/web/vaev-layout/inline.h @@ -24,69 +24,9 @@ struct InlineFlow : 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::INLINE; - - Px res = box.borderBox.start(); - auto blockSize = computePreferredBorderSize( - ctx, - mainAxis.cross(), - box.contentBox().height - ); - - for (auto &c : _frags) { - auto childCtx = ctx.subContext( - *c, - mainAxis, - box.contentBox() - ); - - auto inlineSize = computePreferredOuterSize( - childCtx, mainAxis, - box.contentBox().width - res - ); - - RectPx borderBox = RectPx{ - res, - box.contentBox().top(), - inlineSize, - blockSize, - }; - - auto box = computeBox(childCtx, borderBox); - c->placeChildren(childCtx, 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, Px{0}); - if (intrinsic == IntrinsicSize::MAX_CONTENT) { - res += size; - } else { - res = max(res, size); - } - } else { - auto size = computePreferredOuterSize(childCtx, axis, Px{0}); - res = max(res, size); - } - } - - return res; - } + Px computeIntrinsicSize(Context &ctx, Axis axis, IntrinsicSize intrinsic, Px) override; }; } // namespace Vaev::Layout