gpui-cc Course handbook
CHAPTER 03 / STAGE 09COURSE

Compose a tree of boxes

Lay out rows, columns, and overlays.

layout-tree
On this page

Learn: origin is relative until you place the child

A row advances its x cursor, a column advances y, and an overlay gives each child the same content origin. Padding reduces available content space before any children are placed. Only add an inter-child gap between children. Parent-local positions become window positions once.

Run it: translate a grandchild

Terminal
./build/examples/core-lab local-origin
C++ · core-lab.cpp
void local_origin() {
    const int parent_x = 10, child_x = 3, grandchild_x = 2;
    const int child_absolute = parent_x + child_x;
    std::cout << "child: " << child_absolute
              << "; grandchild: " << child_absolute + grandchild_x << '\n';
}

Expect child: 13; grandchild: 15. Try it: change only parent_x to 20. Both absolute coordinates shift by 10, not 20. This is coordinate composition, not a layout algorithm.

Build it: own children and resolve one frame

Your challenge: Add row, column, and overlay elements with owned child descriptions. Measure leaves and containers and produce absolute bounds plus clips. Replay the resulting scene to show a nested UI that responds to viewport changes.

Start with a simple owned structure (kind, style values, and a vector of children); don’t start a widget class hierarchy. Measure child requests within your finite bounds, combine extents (sum main-axis sizes plus gaps, max on cross-axis; overlay max on both), and cap the container’s reported size. At placement, advance based on each child’s measured extent plus the gap and intersect visible bounds with ancestor clips. Under this course’s policy, overflow clips instead of redistributing sizes; later children can be entirely invisible. Record paint order from the tree, not from sorted coordinates.

Check it

With a 10 × 6 viewport, put a column at (1,1) with padding 1 and gap 1. Use a (4,1) leaf, then a row with two (2,1) leaves and gap 1. Expect starts (2,2), (2,4), (5,4) respectively. In a 6 × 6 viewport, column content ends at x=5 exclusive, so the right-most leaf at x=5 has no visible columns. Derive that clip from the declared policy, not a hard-coded paint command. Add an empty container (padding can remain, no gap) and an overlay with two colors: the later child is on top.

Terminal
python3 course/check_core.py layout-tree

Hints and troubleshooting

Hint: nested children drift or paint outside the parent

Log a row with local origin, absolute origin, resolved size, and clip. Test gap separately from padding. Passing a window-relative origin to a painter that adds its parent again is a double translation.

GPUI-CE has separate layout, prepaint, and paint phases. This edition does not copy Taffy; it borrows the rule that resolved geometry serves more than one consumer. Next, input uses the same placed and clipped boxes.