gpui-cc Course handbook
CHAPTER 04 / STAGE 11COURSE

Turn a press and release into one action

Route matching pointer events without double activation.

pointer-action
On this page

Learn: a click is a decision over two events

The server reports press and release. Our framework chooses to activate only if both refer to the same live control. A window losing OS focus is deactivated; this is not yet a statement about which control has keyboard focus. Deactivation and removal cancel a pending press.

Run it: cancel an armed state

Terminal
./build/examples/core-lab cancel-press
./build/examples/core-lab cancel-press no-cancel
C++ · core-lab.cpp
void cancel_press(bool cancel) {
    bool armed = false;
    const std::string_view events = cancel ? "PCU" : "PU";
    for (char event : events) {
        if (event == 'P') armed = true;
        if (event == 'C') armed = false;
        if (event == 'U') {
            std::cout << (armed ? "activate" : "ignore") << '\n';
            armed = false;
        }
    }
}

Expect ignore then activate. The toy remembers a boolean; the real dispatcher must remember which target is armed. Try it: append another U in the sample; a second release should not activate again.

Build it: normalize at the native boundary

Your challenge: Translate XCB left-button presses/releases into framework values and activate a semantic action only on a completed click. Never call native APIs from the action handler.

Select XCB_EVENT_MASK_BUTTON_PRESS, BUTTON_RELEASE, and FOCUS_CHANGE with the other window event masks. Verify response_type & 0x7f before reading the appropriate event struct. detail identifies the button; for the supported left button use detail 1. Read event_x/event_y rather than root_x/root_y so coordinates match layout. Convert to an owned, normalized event before freeing the XCB allocation.

Represent pending press as an optional ID. A new valid press replaces it; release consumes it whether or not it matches. Deactivation and removal invalidate it. Look up the current target and route a match to the associated action, e.g. Increase. Don’t capture a pointer to a transient node.

Check it

Replay down A, up A → one A action. up A alone → none. down A, up B and down A, up blank → none. down A, deactivate, activate, up A → none. down A, remove A, up at old position, non-left button, and duplicated release must not trigger. Test a resize between press and release to prove the target is resolved at each event.

Terminal
python3 course/check_core.py pointer-action

Hints and troubleshooting

Hint: the handler runs twice or on the wrong control

Log event kind → located ID → pending ID → emitted action for one trace. The action belongs to matching release, not press. Verify the release clears state even when it misses.