Callbacks and Lifetime

Most PixelUI callbacks use ETL inline storage:

1
2
3
using InputCallback = etl::inplace_function<bool(InputEvent), CALLBACK_STORAGE_SIZE>;
using VoidCallback = etl::inplace_function<void(), CALLBACK_STORAGE_SIZE>;
using ValueCallback = etl::inplace_function<void(int32_t), CALLBACK_STORAGE_SIZE>;

Each callback uses fixed inline storage. Captured objects retain their original ownership and lifetime.

1
2
3
4
5
6
7
8
void onEnter(ExitCallback cb) override {
IApplication::onEnter(cb);
button_.setCallback([this]() {
value_++;
ui_.markDirty();
});
ui_.addWidgetToFocusManager(&button_);
}

The Widget callback and its captured App share the App lifetime. See App lifecycle and ViewManager for the unified cleanup order before destruction.

Common mistake

1
2
3
4
5
6
void configure() {
int temporary = 42;
button_.setCallback([&temporary]() {
use(temporary); // dangling after configure() returns
});
}

Widget and Popup buffers, queued numeric references, and pointers returned by getCurrentApp() require storage that covers their complete use. Function-local storage ends when its function returns, and App pointers expire when the App is popped.

Numeric formatters, editor bindings, and edit sessions add the same kind of boundary. Formatter contexts, suffix strings and percentage ranges; binding value/changed contexts; and an injected ValueEditSession& are all non-owning. For queued Popups they must survive both the pending and active phases. Call clearPopups() before destroying a shorter-lived owner.

A request owns its ValueCallback object and moves it into the active Popup. Captured Apps, buffers, and state remain non-owning references with their original lifetimes.

Inline capacity

A large lambda may exceed etl::inplace_function capacity at compile time. Capturing this, a pointer, or a small ID keeps storage bounded. See Resource limits for the configured size.

Member declaration order

C++ destroys members in reverse declaration order. If a Widget holds a non-owning reference to a buffer or state object, declare that object before the Widget. See Widgets and Focus for a Chart example.

avatar
Link0327
汪🐱me0w, but furry wolf. 尝试变得毛茸茸
Link's Github