Handle input
When a switch changes, its status label should change too. Convert the input into a message, store the new value in App::update, and read it back in view.
The composition and forms guide covers lazy children, semantic callbacks, slots and typed bindings in 0.7.0.
Try the notification switch
Click the switch: Disabled becomes Enabled, and the count increases. Select Reset to restore both values. The complete application is on the left.
Loading the interactive example…
This example works immediately. Replace Notifications with your own title and select Run to see the edited screen. Each successful run starts from the state returned by app().
Store the input in application state
NotificationSettingsAction is the notification settings component's own action enum. Enabled(bool) and Reset describe inputs; enabled and changes are state fields, and the update method implements their changes. type Action = NotificationSettingsAction connects this type to the App interface; the component chooses the name on the right.
The switch interaction has three steps:
switch(self.enabled)describes the current value.on_value_changedconverts the native1or0intoNotificationSettingsAction::Enabled(value != 0).updatereceives the message, storesenabledand incrementschanges.viewreads both fields again to update the switch, label and count.
A button needs no value payload: button("Reset").on_click(NotificationSettingsAction::Reset) sends the reset message directly. Messages implement Clone + 'static because a click handler stores its message and clones it for each click.
Follow a switch's native 1/0 value through the queue, Ui::drain, state update and new View. The callback does not directly update App.
Open full-size diagram ↗The native callback copies input data, runs the handler and queues the message. Ui::drain in the platform loop then calls update and view. Browser previews provide that loop; see Mount and update for device setup. app, view and update must return promptly, without starting another run loop.
What happens if I only set the initial value?

Loading the interactive example…
These controls accept clicks and drags, but update does not store any state. Changing their native appearance does not update the adjacent labels. To connect them, store the message value as in the switch example, then pass it back to slider(...), switch(...) or a label.
Process messages in the loop
Ui owns one bounded queue shared by the view, the overlay and every handler slot. Ui::new uses DEFAULT_QUEUE_CAPACITY (64) and Ui::with_capacity sets another size. When the queue is full, post removes the oldest message and increments the dropped count.
Ui::post(message) adds a message from platform code. Native callbacks use the same queue internally. Neither path calls App::update; that happens only when the application drains:
The snippet is a frame-loop fragment; the input example supplies the display, keypad and loop.
drain(&mut app, max_messages) processes at most the requested number of messages, calls App::update once per message, then calls App::view and reconciles once for the whole batch. It returns DrainStats:
processed: messages applied in this call.dropped: messages dropped during this call because the queue was full.reconciled: whether this call produced a render.
A drain that processes no message skips the view and reconciliation unless mark_dirty or set_theme requested a render, or no tree is mounted. State changed outside the message path, such as a clock or a sensor read in view, must call Ui::update(&app) or Ui::mark_dirty(). Ui::stats reports cumulative messages_processed, messages_dropped and reconciled_updates together with node counters.
Programmatic writes to controlled widgets suppress the next native VALUE_CHANGED event, so rendering a slider value does not feed the application its own write. User changes still enqueue messages. Replacing a handler keeps the node's native callback slot and drops the captures of the old closure.
Choose an event handler
Direct handlers respond to their own node. Descendant events require a bubbled handler and native bubbling flags. Each builder installs one independent handler. Calling the same builder again replaces only that handler; different handlers on one node combine.
on_value_changed reads the value that the native widget holds after the change: the value of a slider, bar or arc; 1 or 0 for a switch or checkbox; the selected index of a dropdown, roller or button matrix; a spinbox value; the active tab or tile index; a calendar date packed by CalendarDate::to_value; a table cell packed by (row << 16) | column; or a message-box footer index. A tileview reports -1 before a tile is active, and a calendar reports -1 for day names and adjacent-month days.
on_text_changed is valid only on a textarea. The string is borrowed from LVGL for the duration of the callback; build an owned String if the message must keep it. Native text that is not valid UTF-8 is skipped.
on_event receives the payload-free Event enum, which covers press, click, focus, hover, key, rotary, ready, cancel, scroll and gesture codes. Events bubbled from descendants are ignored by on_event. on_bubbled_event receives the copied InputEvent (Key, Rotary, Gesture, Scroll, Pointer or Other(Event)) and is the way to observe a child's input from an ancestor. Every intervening child must enable .event_bubble(true); gesture propagation follows LVGL's separate target rules and .gesture_bubble(bool).
The payload handlers are available with every widget selection; no optional feature gates on_key, on_pointer or the queue. Missing native key or rotary parameters and pointer events without a pointer device do not produce invented payloads.
Focus, keypads and encoders
The input-keypad feature is off by default. It forwards rs_lvgl/input-keypad and adds the focus API to Ui; the rs_lvgl keypad driver and input_device::key constants are available under the same feature. Enabling it does not activate focus by itself, and a pointer-only screen keeps its previous behavior.
An explicit per-UI group is required:
Ui::bind_keypad(&Keypad)binds a live typed keypad on the same display and activates the group.unsafe Ui::bind_input(NonNull<lv_indev_t>)binds a platform keypad or encoder. The device must be live on the UI thread and belong to the same display; keypads and encoders only.Ui::enable_focus()activates a group without a bound device.Ui::focus(Key)focuses the unique non-Autokey across the view and overlay. It returnsfalsefor a missing or ineligible node andUiError::InvalidPropertyfor an ambiguous key orKey::Auto.Ui::set_editing(bool)selects native encoder editing or navigation mode.
The fragment assumes keypad is a live Keypad on the same display; the input example covers display and device setup.
NEXT and PREV move group focus. ENTER activates the focused widget through the same native click path as a pointer, so on_click receives the same message. Arrow keys operate the focused widget. A label or other non-focusable class needs .focusable(true) before it can join the group, and .focusable(false) removes a node that the class would otherwise include. Hidden and disabled nodes, including descendants of hidden or disabled parents, are excluded.
State::FOCUSED and State::FOCUS_KEY style the focus ring and are switched by LVGL without a render. on_event reports Event::Focused and Event::Defocused when the group changes focus.
View::modal(true) restricts focus to a subtree while it is visible and enabled; the last modal in view/overlay pre-order wins. Closing it restores the previous mounted node when that node is still eligible, including an auto-keyed node. The Dialog component sets this on its backdrop when input-keypad is enabled. Modal membership scopes keyboard focus; blocking pointer input requires the dialog's full-size backdrop.
input-gridnav includes input-keypad and adds arrow navigation among the direct children of an object, row, column or grid. GridNavigation carries rollover, scroll_first and axis (NavigationAxis::Both, Horizontal or Vertical); View::grid_navigation enables it. The container occupies one Tab position, and arrows select a clickable eligible child. This feature also changes nothing until focus is activated.
Callback execution and borrowing
Handlers run synchronously inside the native event callback on the UI thread. They must not call Ui methods, run App::update, reconcile, delete native objects, block, or panic. A panic in an extern "C" callback cannot unwind into C and aborts instead. The library-side callbacks copy input data and post messages; application logic belongs in update.
The borrow rules follow from that synchronous call:
on_text_changedreceives a&strtied to LVGL's buffer for that call only. Copy it before storing.PointerEvent,ScrollOffset,GestureDirectionand the key/rotary values are copied.InputEventcontains only copied data; no native pointer or borrowed event escapes C.on_clickandon_long_pressstore their message and clone it per event; the message type must beClone.- Handler closures are
FnMut + 'staticand are owned by the mounted node. When a handler is replaced or the node is deleted, the old closure is dropped.
The busy guard rejects nested UI mutation with UiError::Reentrant, and a nested timer_handler returns 0. Those checks do not make callbacks a safe place for application work; they only prevent a reentrant call from corrupting the tree.
Ui is neither Send nor Sync, and the queue is not a cross-thread channel. All LVGL access, message posting and draining stay on one UI thread. The crate makes no unwinding, thread-safety or concurrent-access promise for handlers or native callbacks.
Lifetime and teardown
Input binding belongs to the Ui, not to the descriptor tree. A native group belongs to one Ui; it is separate from LVGL's global default group. Ui::unmount deletes the trees, drops the focus group and detaches its devices while the native display is still alive. A later mount creates a fresh tree and requires bind_keypad/bind_input again. Dropping a Ui also deletes its root and its focus group; the native display must outlive the Ui, and Declarative UI describes the teardown order.
Keypad owns its native input device: dropping it deletes the device unless native deletion already released it, after which Keypad::raw() returns None. Never delete a device from inside one of its callbacks, and do not keep a raw device pointer past native deletion.
The queue and boxed handlers allocate. rs_lvgl_ui is no_std outside tests and requires alloc, so the target must provide an allocator; input-keypad does not remove that requirement. Native allocation failures surface as UiError::Allocation during creation and update, not as a silently dropped input event.
Runnable example
input.rs creates an SDL window, mouse and keyboard, mounts two buttons and a scrollable column, binds the keyboard to the UI, focuses Key::Named("add"), and drains pointer clicks, key codes and scroll offsets in its frame loop.
The example target requires simulator and input-keypad. It needs SDL2 and a visible interactive window; INPUT_FRAMES=<n> bounds the run without replacing OS input. A compile check or a headless command does not exercise the keyboard path.