Learn: a widget’s request is not its final size
An element can say “I’d like to be 8 pixels wide.” Its parent can say “You have at most 5.” Measuring answers how much space, placing answers where, and painting consumes the resulting box. Keep these phases separate so changing the window size does not require changing the element declaration.
Run it: place one strip at the same origin
./build/examples/core-lab fit-strip
void fit_strip() {
constexpr int desired = 8;
constexpr int origin = 2;
for (int available : {10, 5}) {
const int width = std::clamp(desired, 0, available);
std::cout << '[' << origin << ", " << origin + width << ")\n";
}
}
Expect [2, 10) then [2, 7). Only the available width changes. Try it: supply zero and observe the empty interval [2,2). This is one axis and has no tree or draw calls.
Build it: let one leaf resolve its box
Your challenge: Represent a leaf element with desired width, desired height, and a paint color. Measure within finite min/max constraints, then place the measured size at a supplied origin. Feed the resulting bounds and effective clip to the scene, not to a second independent set of drawing coordinates.
Give the measure operation constraints and a desired size; it should return a size, not mutate the surface. Defend against invalid constraints (min > max, negative sizes). Give placement that result and one origin. Clip against the actual viewport/parent separately; being permitted to request a size does not mean all of it is visible at a given origin.
Check it
For desired (8,3) and origin (2,1), with minimum zero and maximum (10,4), expect bounds [2,10) × [1,4). With maximum (5,2), expect [2,7) × [1,3). Independently clip the latter to a 6 × 3 viewport; only x=2..5 and y=1..2 are painted. Include min width 4, desired 1, max 8: result 4, not 1.
python3 course/check_core.py layout-box
Hints and troubleshooting
Hint: the fill is right but starts at (0,0)
Print the measured size independently from the placed bounds. Check that the origin is added once, after measurement. The rectangle rasterizer is not responsible for choosing the origin.