Adding agents.md, fixing long-broken initializer list constructor

This commit is contained in:
Dynamitos committed 2026-10-25 22:50:20 +01:00
1 parent 6a6294c67d
commit ff2e4e642d
12 files changed
+484 -526

No files matched your search

+33 -28
View File
@@ -40,12 +40,12 @@ namespace {
struct Flow {
std::vector<size_t> idx;
std::vector<int32> order;
static void build(Element* parent, Flow& f) {
static void build(PElement parent, Flow& f) {
f.idx.clear();
f.order.clear();
const auto& kids = parent->getChildren();
for (size_t i = 0; i < kids.size(); i++) {
Element* c = kids[i];
PElement c = kids[i];
if (c == nullptr || c->getStyle().display == Display::Hidden) {
continue;
}
@@ -92,7 +92,7 @@ static float resolvePx(DimensionType t, float v, float cont) {
// (px/%) size resolves against the parent's content box; an Auto (or nested-container)
// child contributes its intrinsic size (0 for a container, since its real size is
// finalized when the place phase recurses into it). Includes main-axis margins.
static float childMainFootprint(Element* c, const FlexLayout::Axis& axis, float parentContentMain) {
static float childMainFootprint(PElement c, const FlexLayout::Axis& axis, float parentContentMain) {
const Style& s = c->getStyle();
bool isX = axis.mainIsX;
float size;
@@ -120,7 +120,7 @@ static float childMainFootprint(Element* c, const FlexLayout::Axis& axis, float
// flow children of their footprints (resolved against the element's *content* box on
// that axis) + gaps, or the element's intrinsic size for a leaf. This is the Auto
// fallback for the element's own size.
static float contentAxis(bool isX, Element* el, const Flow& flow, const Array<Element*>& kids) {
static float contentAxis(bool isX, PElement el, const Flow& flow, const Array<OElement>& kids) {
if (flow.size() == 0) {
return isX ? el->getIntrinsicW() : el->getIntrinsicH();
}
@@ -130,7 +130,7 @@ static float contentAxis(bool isX, Element* el, const Flow& flow, const Array<El
// parent), the reference is 0 (percent sizes on a root with no containing box
// are degenerate; the caller should use px).
float parentContent = 0.0f;
Element* parent = el->getParent();
PElement parent = el->getParent();
if (parent != nullptr) {
if (isX) {
parentContent = std::max(0.0f, (float)parent->getLayoutDimensions().x - (float)(parent->getStyle().paddingLeft + parent->getStyle().paddingRight));
@@ -140,7 +140,7 @@ static float contentAxis(bool isX, Element* el, const Flow& flow, const Array<El
}
float m = 0.0f;
for (size_t k = 0; k < flow.size(); k++) {
Element* c = kids[flow[k]];
PElement c = kids[flow[k]];
m = std::max(m, childMainFootprint(c, isX ? Axis{true, false} : Axis{false, false}, parentContent));
}
float gap = (float)el->getStyle().gap;
@@ -152,7 +152,7 @@ static float contentAxis(bool isX, Element* el, const Flow& flow, const Array<El
// resolve one concrete axis of the element's own size: explicit wins; Auto falls back
// to the content measurement; min/max then clamp. Returns the final axis size.
static float resolveAxisSize(bool isX, Element* el, float content, const Style& s) {
static float resolveAxisSize(bool isX, PElement el, float content, const Style& s) {
// the percent reference for this element's own size is its *parent's* box (the
// containing block). the element's own dimensions are finalized before this is
// called (layoutElement sets them before recursing into placeFlowChildren), so
@@ -183,7 +183,7 @@ static float resolveAxisSize(bool isX, Element* el, float content, const Style&
// resolves against the parent's content box on the cross axis; Auto falls back to the
// element's intrinsic cross size. includes cross-axis margins. used for the parent's
// Auto cross measurement (max over children) and for non-stretch item sizing.
static float childCrossFootprint(Element* c, const FlexLayout::Axis& axis, float parentContentCross) {
static float childCrossFootprint(PElement c, const FlexLayout::Axis& axis, float parentContentCross) {
const Style& s = c->getStyle();
bool isX = axis.mainIsX; // cross axis is the other one
float size;
@@ -214,7 +214,7 @@ static float childCrossFootprint(Element* c, const FlexLayout::Axis& axis, float
// resolve the child's main-axis basis for flex distribution: explicit (px/%) resolves
// against the parent's content main; Auto falls back to the child's intrinsic main.
// does NOT include margins (those are handled separately in the layout loop).
static float childMainBasis(Element* c, const FlexLayout::Axis& axis, float parentContentMain) {
static float childMainBasis(PElement c, const FlexLayout::Axis& axis, float parentContentMain) {
const Style& s = c->getStyle();
bool isX = axis.mainIsX;
// explicit basis (flex-basis) wins over size
@@ -265,7 +265,7 @@ static float childMainBasis(Element* c, const FlexLayout::Axis& axis, float pare
// apply justify (main) and align (cross), write positions. this is the core of the
// flexbox algorithm. the element's own size is already resolved (by the caller); this
// function only positions the children.
static void placeFlowChildren(Element* el, const Axis& axis, const Flow& flow, const Array<Element*>& kids) {
static void placeFlowChildren(PElement el, const Axis& axis, const Flow& flow, const Array<OElement>& kids) {
if (flow.size() == 0) return;
const Style& s = el->getStyle();
// the element's content box (in the element's own coordinate space, before
@@ -307,7 +307,7 @@ static void placeFlowChildren(Element* el, const Axis& axis, const Flow& flow, c
std::vector<Placed> placed;
placed.reserve(flow.size());
for (size_t k = 0; k < flow.size(); k++) {
Element* c = kids[flow[k]];
PElement c = kids[flow[k]];
const Style& cs = c->getStyle();
Placed p{};
p.idx = flow[k];
@@ -482,7 +482,7 @@ static void placeFlowChildren(Element* el, const Axis& axis, const Flow& flow, c
for (size_t k = 0; k < placed.size(); k++) {
if (placed[k].line != ln) continue;
Placed& p = placed[k];
Element* c = kids[p.idx];
PElement c = kids[p.idx];
// main position: line start + item offset within line.
float itemMain;
if (firstOnLine) {
@@ -565,8 +565,8 @@ static void placeFlowChildren(Element* el, const Axis& axis, const Flow& flow, c
// layout one element: measure (resolve its own size + recurse to measure children),
// then place (position flow children in the content box, handle absolute children).
// writes `position` and `dimensions` in PARENT space (excluding margins).
void layoutElement(Element* el, const Axis& axis) {
const Array<Element*>& kids = el->getChildren();
void layoutElement(PElement el, const Axis& axis) {
const Array<OElement>& kids = el->getChildren();
if (el->getStyle().display == Display::Hidden) {
el->setLayout(Vector2(0), Vector2(0));
return;
@@ -585,7 +585,7 @@ void layoutElement(Element* el, const Axis& axis) {
// prior layout run). for a root element (no parent), the reference is 0.
float parentContentCross = 0.0f;
{
Element* parent = el->getParent();
PElement parent = el->getParent();
if (parent != nullptr) {
parentContentCross = axis.mainIsX
? std::max(0.0f, (float)parent->getLayoutDimensions().y - (float)(parent->getStyle().paddingTop + parent->getStyle().paddingBottom))
@@ -594,7 +594,7 @@ void layoutElement(Element* el, const Axis& axis) {
}
float crossContent = 0.0f;
for (size_t k = 0; k < flow.size(); k++) {
Element* c = kids[flow[k]];
PElement c = kids[flow[k]];
crossContent = std::max(crossContent, childCrossFootprint(c, axis, parentContentCross));
}
// if there are no flow children, the cross content is the element's intrinsic cross.
@@ -621,7 +621,7 @@ void layoutElement(Element* el, const Axis& axis) {
// direction to determine its axis (a Row child of a Column parent has mainIsX=true
// even though the parent has mainIsX=false).
for (size_t k = 0; k < flow.size(); k++) {
Element* c = kids[flow[k]];
PElement c = kids[flow[k]];
const Style& css = c->getStyle();
bool cIsX = (css.direction == FlexDirection::Row || css.direction == FlexDirection::RowReverse);
Axis childAxis{cIsX, cIsX ? (css.direction == FlexDirection::RowReverse) : (css.direction == FlexDirection::ColumnReverse)};
@@ -631,7 +631,7 @@ void layoutElement(Element* el, const Axis& axis) {
// content-box corner. measure them (so their own children are laid out), then
// position by left/top offsets (right/bottom are not supported).
for (size_t i = 0; i < kids.size(); i++) {
Element* c = kids[i];
PElement c = kids[i];
if (c == nullptr || c->getStyle().display == Display::Hidden) continue;
if (c->getStyle().position != Position::Absolute) continue;
const Style& css = c->getStyle();
@@ -655,7 +655,10 @@ void layoutElement(Element* el, const Axis& axis) {
for (size_t i = 0; i < kids.size(); i++) {
if (kids[i] == nullptr) continue;
if (kids[i]->getStyle().display == Display::Hidden) {
kids[i]->setLayout(Vector2(0), Vector2(0));
// raw pointers can be modified inside a const Array<>&, but RefPtr imposes const-ness also for the elements
// this side-steps the issue by creating a non-const RefPtr copy
PElement kid = kids[i];
kid->setLayout(Vector2(0), Vector2(0));
}
}
// place flow children in the content box: compute per-line packing, apply
@@ -666,7 +669,7 @@ void layoutElement(Element* el, const Axis& axis) {
// here, so its own grandchildren were laid out relative to a stale position.
// Re-run each container child's placement now that the parent position is final.
for (size_t k = 0; k < flow.size(); k++) {
Element* c = kids[flow[k]];
PElement c = kids[flow[k]];
if (c->getChildren().size() == 0) continue;
const Style& css = c->getStyle();
if (css.position == Position::Absolute) continue;
@@ -674,7 +677,7 @@ void layoutElement(Element* el, const Axis& axis) {
Axis childAxis{cIsX, cIsX ? (css.direction == FlexDirection::RowReverse) : (css.direction == FlexDirection::ColumnReverse)};
Flow cf;
Flow::build(c, cf);
const Array<Element*>& cKids = c->getChildren();
const Array<OElement>& cKids = c->getChildren();
placeFlowChildren(c, childAxis, cf, cKids);
}
}
@@ -682,7 +685,7 @@ void layoutElement(Element* el, const Axis& axis) {
// the public entry point: set the root's dimensions to the viewport, then run the
// full layout. the root has no parent, so its percent sizes are degenerate (0) —
// the root should use explicit (px) or Auto sizes.
void layoutRoot(Element* root, UVector2 viewport) {
void layoutRoot(PElement root, UVector2 viewport) {
if (root == nullptr) return;
// the root's dimensions are the viewport (the root IS the viewport's content).
// the root has no parent, so resolveAxisSize will treat its percent sizes as 0
@@ -739,7 +742,7 @@ void layoutRoot(Element* root, UVector2 viewport) {
// does *except* re-resolving the root's own size (which would use a null parent
// and a 0 percent reference, wrong for the root).
Axis axis{isX, isX ? (s.direction == FlexDirection::RowReverse) : (s.direction == FlexDirection::ColumnReverse)};
const Array<Element*>& kids = root->getChildren();
const Array<OElement>& kids = root->getChildren();
if (root->getStyle().display == Display::Hidden) {
root->setLayout(Vector2(0), Vector2(0));
return;
@@ -750,19 +753,21 @@ void layoutRoot(Element* root, UVector2 viewport) {
for (size_t i = 0; i < kids.size(); i++) {
if (kids[i] == nullptr) continue;
if (kids[i]->getStyle().display == Display::Hidden) {
kids[i]->setLayout(Vector2(0), Vector2(0));
// modifyable copyied refptr, something you can do with raw pointers natively
PElement kid = kids[i];
kid->setLayout(Vector2(0), Vector2(0));
}
}
// measure the flow children (recursively) and the absolute children.
for (size_t k = 0; k < flow.size(); k++) {
Element* c = kids[flow[k]];
PElement c = kids[flow[k]];
const Style& css = c->getStyle();
bool cIsX = (css.direction == FlexDirection::Row || css.direction == FlexDirection::RowReverse);
Axis childAxis{cIsX, cIsX ? (css.direction == FlexDirection::RowReverse) : (css.direction == FlexDirection::ColumnReverse)};
layoutElement(c, childAxis);
}
for (size_t i = 0; i < kids.size(); i++) {
Element* c = kids[i];
PElement c = kids[i];
if (c == nullptr || c->getStyle().display == Display::Hidden) continue;
if (c->getStyle().position != Position::Absolute) continue;
const Style& css = c->getStyle();
@@ -779,7 +784,7 @@ void layoutRoot(Element* root, UVector2 viewport) {
// Same fix as in layoutElement: container children were measured before their
// position was set, so re-run their placement with the now-final position.
for (size_t k = 0; k < flow.size(); k++) {
Element* c = kids[flow[k]];
PElement c = kids[flow[k]];
if (c->getChildren().size() == 0) continue;
const Style& css = c->getStyle();
if (css.position == Position::Absolute) continue;
@@ -787,7 +792,7 @@ void layoutRoot(Element* root, UVector2 viewport) {
Axis childAxis{cIsX, cIsX ? (css.direction == FlexDirection::RowReverse) : (css.direction == FlexDirection::ColumnReverse)};
Flow cf;
Flow::build(c, cf);
const Array<Element*>& cKids = c->getChildren();
const Array<OElement>& cKids = c->getChildren();
placeFlowChildren(c, childAxis, cf, cKids);
}
}