Skip to content

ui::Callback

Header: winui/core/Callback.hpp.

template <typename Sig> class Callback

The one handler type: a std::move_only_function, so a handler may capture a std::unique_ptr. Spell the signature at the use site (Callback<void(bool)>, Callback<void(std::size_t)>), so a reader sees the payload without chasing a typedef.

A handler with a payload also takes a lambda that takes nothing, for when only the event matters: .on_selection_changed = [&] { refresh(); }. A handler that returns a ui::Handling or a ui::Decision also takes a lambda that returns nothing, which gets the event's default.

Example:

auto dialog = std::make_unique<std::string>("owned state"); // any move-only capture works
ui::Callback<void(bool)> on_checked_changed = [owned = std::move(dialog)](bool on) {
    if (on) { std::puts(owned->c_str()); }
};
auto toggle = std::make_unique<ui::ToggleSwitchControl>();
toggle->on_checked_changed(std::move(on_checked_changed));
toggle->on_checked_changed([] { std::puts("toggled"); }); // the payload ignored