gpui-cc Course handbook
CHAPTER 04 / STAGE 10COURSE

Hit-test the visible target

Use the painted geometry to locate a control.

hit-target
On this page

Learn: a visible box tells you where a click can land

A pointer event brings window-relative coordinates. It does not name a button. Use the most recent applicable layout to answer: which eligible box contains this point, after clipping? If two overlap, ask in reverse paint order so the uppermost eligible box wins. A button needs a stable identifier such as increase, not a pointer to one transient node.

Run it: inspect a clipped interval

Terminal
./build/examples/core-lab visible-edge
C++ · core-lab.cpp
void visible_edge() {
    const int left = std::max(1, 2);  // intersection of [1, 6) and [2, 5)
    const int right = std::min(6, 5);
    for (int position : {1, 2, 4, 5}) {
        std::cout << position << ' ' << (left <= position && position < right ? "hit" : "miss") << '\n';
    }
}

Expect 1 miss, 2 hit, 4 hit, 5 miss. The point 1 lies within the original box but outside its clip. Try it: use <= for the right edge and observe the unwanted hit at 5.

Build it: index the same bounds you paint

Your challenge: During frame preparation record interactive IDs and clipped bounds in paint order. Hit testing returns the ID of the top eligible target or no target; no callback runs yet.

Register only visible, eligible controls and reject duplicate IDs in the same render tree. Store an owned ID (or a value ID), not a string view into discarded elements. Before handling a point after a resize, prepare the new geometry; reading a stale record even once can activate a control that is no longer under the pointer. A frame snapshot can bundle the geometry, paint commands, and hit records without coupling the pure rasterizer to input.

Check it

Record A on [0,4)×[0,3) and B on [1,4)×[0,2) with clip [2,3)×[0,2); B is painted last. (2,1) selects B, (1,1) selects A, (3,1) selects A, (4,1) selects nothing. Then prepare a resized frame with B fully clipped and compare the hit at (2,1) again. Test a blank location, zero visible area, and duplicate ID.

Terminal
python3 course/check_core.py hit-target

Hints and troubleshooting

Hint: the underlying button wins

Print recorded order, scan order, and visible bounds, independently of the callback. Confirm you are hit-testing the viewport and frame that produced the current target geometry.