On this page

Widgets and state

Widgets keep no model of their own. They read and write plain state your app already owns, so nothing needs syncing.

Passing state to the view

isolen_app_desc::user is handed back to the view as its second argument every frame. Point it at a struct that outlives the app. Widgets take pointers into that struct: a button returns true on the frame it is clicked, a toggle flips a bool, and a slider writes a float back in place.

main.cpp
#include "isolenui/app/app.h"
#include "isolenui/kit/kit.h"

// state the view closes over, isolen_app_desc::user hands the same pointer
// back to the view callback every frame
struct demo_state {
    float volume = 0.6f;
    bool muted = false;
    uint32_t clicks = 0;
};

static void draw(isolen_frame* f, void* user)
{
    demo_state* s = static_cast<demo_state*>(user);
    ui::ctx* u = f->ui;

    ui_box(u, ui::col().grow().pad(24.0f).gap(12.0f))
    {
        ui::text(u, "settings", ui::head(u));

        if (ui::button(u, "click me")) {
            ++s->clicks;
        }
        ui::label_valuef(u, "clicks", "%u", s->clicks);

        ui::toggle(u, "mute", &s->muted);
        if (!s->muted) {
            ui::slider(u, "volume", &s->volume, 0.0f, 1.0f, "%.2f");
        }
    }
}

int main(int argc, char** argv)
{
    demo_state state;

    isolen_app_desc d = isolen_app_defaults();
    d.title = "isolen ui, widgets and state";
    d.view = draw;
    d.user = &state;
    return isolen_run(&d, argc, argv);
}

Why this works

Every element gets an id from where it sits in the tree, and the context remembers each id from one frame to the next. A widget reads last frame's rectangle for its id to decide whether it was hovered or clicked, then draws this frame's state. Declaring the same widgets in the same order keeps their ids stable. Inside loops, ui::key pins them. See how widgets work.

Last updated 6 October 2026