gpui-cc Course handbook
CHAPTER 05 / STAGE 18COURSE

Run a counter on the framework

Bring input, state, text, layout, and rendering together.

counter-app
On this page

Learn: app decisions stay on the app side

The app owns the number, chooses a label and controls, and handles named actions. The framework owns the native connection, event loop, focus traversal, layout, and paint. Those boundaries matter more than the exact spelling of the C++ types.

Run it: describe state without changing it

Terminal
./build/examples/core-lab model-consumer
C++ · core-lab.cpp
void model_consumer() {
    enum class Action { Start, Finish };
    struct Job { bool finished = false; };
    const auto describe = [](Job job) { return job.finished ? "Done" : "Waiting"; };
    const auto handle = [](Job& job, Action action) {
        if (action != Action::Finish || job.finished) return false;
        job.finished = true;
        return true;
    };
    Job job;
    std::cout << describe(job) << '\n';
    if (handle(job, Action::Finish)) std::cout << describe(job) << '\n';
    std::cout << "duplicate changed state: " << std::boolalpha << handle(job, Action::Finish) << '\n';
}

Expect Waiting, Done, then duplicate changed state: false. Try it: send Start instead of Finish. The action can return “unchanged,” so the caller knows not to request a new frame. In the capstone the description is an element tree, not a console string.

Build it: make a separate consumer

Your challenge: Link a standalone counter executable against the UI library. Start at zero; show the count and -/+ buttons. Mouse and keyboard use the same actions. Preserve state and eligible focus across resize.

Create a small app-facing header and an apps/counter.cpp consumer (example path; keep src/main.cpp as the earlier demo if helpful). Expose window options, a way to build panel/row/column/overlay/label/button elements, stable control IDs, and a way to start a session with a builder and action handler or equivalent interface. Put native headers in private implementation files. Move the existing native loop into the host and the portable render/layout/input logic into the core; don’t rewrite a desktop event loop in the counter. Link the app to the library through CMake, not by including .cpp files.

Describe one column with a label and a row of buttons. Use distinct stable IDs for the controls; let the handler mutate only the number and report a change. Specify an overflow policy (a bounded count with a no-op at either limit is fine) rather than invoking signed overflow. Render focus through the library. Keep a test entry point using the same model and UI builder with a recording presenter; avoid a second test-only counter.

Check it

Replay this trace headlessly, checking model, focus, and frame effects. Then run the counter on a supported desktop and perform the same actions visually.

Event Count Focus
Start 0 none
Click + 1 +
Tab (given button order -, +) 1 -
Space 0 -
Resize while both controls remain visible 0 -
Click blank space 0 - may remain
Close no further frames exited

Add unrelated keys and a held Space in the native smoke. Compare the updated label, not only a debug print. Use your own desktop manually if an isolated test environment does not provide a window manager. Log which native behaviors you actually observed.

Terminal
python3 course/check_core.py counter-app

Hints and troubleshooting

Hint: the counter works only through one input path or one app

Trace both inputs to the same action name. If counter.cpp needs XCB, raw pixels, or the layout engine’s internal vector to achieve the UI, the library boundary has not been exercised yet.