Skip to content
Development

Widget initialization, input, and notifications ​

For callback syntax, custom event payloads, routing, and capacity configuration, see Events and callbacks.

#[view]
recipes.rs133 lines
886static APERTURE_BLADE: Path = path!(M 50 6 L 94 50 L 50 94 L 6 50 Z);
887static INNER_RING: Path = path!(M 50 24 L 76 50 L 50 76 L 24 50 Z);
888
889const ACCENTS: [Color; 3] = [
890 Color::rgb(52, 82, 255),
891 Color::rgb(207, 67, 43),
892 Color::rgb(42, 157, 143),
893];
894
895#[component]
896#[derive(Clone, Copy)]
897pub struct VectorGlyph {
898 pub accent: i32,
899 pub stroke: i32,
900}
901
902impl Default for VectorGlyph {
903 fn default() -> Self {
904 Self {
905 accent: 0,
906 stroke: 2,
907 }
908 }
909}
910
911impl VectorGlyph {
912 fn cycle(self) -> Self {
913 Self {
914 accent: (self.accent + 1) % 3,
915 stroke: if self.stroke >= 4 { 2 } else { self.stroke + 1 },
916 }
917 }
918}
919
920#[view(component = VectorGlyph, name = "VectorGlyph", priority = 60)]
921fn paint_glyph(glyph: component!(), paint: paint!()) {
922 let rect = paint.rect();
923 let transform = paint
924 .transform()
925 .compose(&Transform::translate(rect.x, rect.y))
926 .compose(&Transform::scale(
927 rect.w / Fixed::from_int(100),
928 rect.h / Fixed::from_int(100),
929 ));
930 let color = ACCENTS[(glyph.accent.rem_euclid(3)) as usize];
931 let stroke_paint = Paint::Color(color.into());
932 let dash: [Fixed; 0] = [];
933 paint.draw(&DrawCommand::StrokePath {
934 path: &APERTURE_BLADE,
935 transform,
936 paint: &stroke_paint,
937 width: Fixed::from_int(glyph.stroke),
938 opa: 255,
939 line_cap: LineCap::Round,
940 line_join: LineJoin::Round,
941 miter_limit: Fixed::from_int(4),
942 dash: &dash,
943 });
944 paint.draw(&DrawCommand::StrokePath {
945 path: &INNER_RING,
946 transform,
947 paint: &stroke_paint,
948 width: Fixed::from_int(1),
949 opa: 180,
950 line_cap: LineCap::Round,
951 line_join: LineJoin::Round,
952 miter_limit: Fixed::from_int(4),
953 dash: &dash,
954 });
955}
956
957#[compose]
958fn vector_scene() {
959 ui! {
960 Column (
961 grow: 1.0,
962 padding: Padding::all(18),
963 justify: JustifyContent::SpaceBetween,
964 text_color: ColorToken::OnSurface
965 ) {
966 Text (
967 "Custom #[view] — tap any tile to cycle stroke & pigment",
968 height: 20,
969 font_size: 13,
970 text_color: ColorToken::OnSurfaceVariant,
971 paragraph: ParagraphStyle::label().with_align(TextAlign::Start)
972 )
973 Row (
974 grow: 1.0,
975 justify: JustifyContent::SpaceEvenly,
976 align: AlignItems::Center,
977 column_gap: 12
978 ) {
979 VectorGlyph (
980 accent: 0,
981 stroke: 2,
982 width: 72,
983 height: 72,
984 bg_color: ColorToken::SurfaceVariant,
985 border_radius: 16
986 ) on Tap(|ev: event!(Tap), q: query!(VectorGlyph), cmd: commands!()| {
987 if let Some(next) = q.get(ev.target).copied().map(VectorGlyph::cycle) {
988 cmd.insert(ev.target, next);
989 }
990 })
991 VectorGlyph (
992 accent: 1,
993 stroke: 3,
994 width: 72,
995 height: 72,
996 bg_color: ColorToken::SurfaceVariant,
997 border_radius: 16
998 ) on Tap(|ev: event!(Tap), q: query!(VectorGlyph), cmd: commands!()| {
999 if let Some(next) = q.get(ev.target).copied().map(VectorGlyph::cycle) {
1000 cmd.insert(ev.target, next);
1001 }
1002 })
1003 VectorGlyph (
1004 accent: 2,
1005 stroke: 4,
1006 width: 72,
1007 height: 72,
1008 bg_color: ColorToken::SurfaceVariant,
1009 border_radius: 16
1010 ) on Tap(|ev: event!(Tap), q: query!(VectorGlyph), cmd: commands!()| {
1011 if let Some(next) = q.get(ev.target).copied().map(VectorGlyph::cycle) {
1012 cmd.insert(ev.target, next);
1013 }
1014 })
1015 }
1016 }
1017 };
1018}
Loading preview…

Tap any VectorGlyph tile to mutate its component in an on Tap handler and re-stroke the static vector path.

Text documents ​

mirui::ui::widgets::TextDocument stores UTF-8 content independently of an editor's selection, composition, and appearance. It can be registered with app.add_model(document).finish() or nested using model!(owned TextDocument). Its default byte capacity is 32; set_capacity prepares larger buffers explicitly. Capacity changes are observable independently of the content revision.

TextEditing::default() owns a local inline document; TextEditing::new(document.clone()) binds a registered document without copying its content. Each component retains its own TextSelection. read borrows content for a callback, and set_selection validates both UTF-8 byte endpoints atomically. Selection reads resolve external edits by clamping to the document's end and preceding UTF-8 boundaries; they do not transform positions using edit history. Register text_editing::view() to observe shared content and capacity changes through normal layout invalidation. This descriptor does not paint.

Use with_capacity and with_text to prepare a component before insertion. For a shared source, these constructors modify the bound document, while selection changes remain local. Registered document bindings retain their App ownership and lifetime rules; they do not fall back to an empty local document when the source expires.

The TextEditing View installs focus and hit-testing components and handles committed fragments, deletion, navigation, Shift selection, and Return independently of appearance. Its Changed, SelectionChanged, and Submitted notifications use the existing event callbacks. External selection corrections are retained at input and pre-render boundaries. Input rejects unavailable document bindings; shared edits release component borrows before Model effects and validate the source and revision before updating selection.

rust
let document = app.add_model(TextDocument::new()).finish();
document.set_capacity(256)?;
document.set_text("Search")?;
document.replace(0..6, "Find")?;
document.read(|text| assert_eq!(text, "Find"));

replace uses UTF-8 byte ranges and rejects invalid boundaries, reversed ranges, and insufficient capacity before changing content. Successful content changes advance the observable revision; identical replacements and failures do not. read borrows text only during its callback and does not copy it; subscribe to revision when observing content changes. Documents accept multiline text; single-line restrictions belong to the editor. Prepared replacements do not allocate.

Input delivery ​

Native and simulated input use the same delivery order. Recognized gestures, their deferred commands, business notifications, and reactive updates complete before the next raw input is handled. A Tap followed by a Key in one input batch therefore delivers Tap first. Callback changes that hide, remove, or rebind a node affect subsequent inputs. Systems and painting retain their frame schedule.

Spatial input refreshes stale layout or transformed hit geometry before dispatch. Unchanged geometry is reused; pending paint damage remains for the frame renderer. A layout failure skips spatial dispatch and cancels active input instead of hitting stale coordinates.

Key carries code, pressed, modifiers: Option<KeyModifiers>, and repeat: Option<bool>. Modifiers expose shift, control, alt, and meta. Some(false) identifies a non-repeated key event; None means the platform did not provide that information. SDL and Web supply both snapshots; WGPU modifiers remain unknown until a native modifier update and are cleared on focus loss or suspension. Linux uses the keyboard device state and native repeat flag. NuttX keyboard records do not provide modifiers or repeat metadata. Simulated input supplies the fields explicitly.

Text editing ​

TextCommitted delivers one complete UTF-8 fragment through the focused node, its ancestors, and App fallback. Read it with event.text(). Inline on TextCommitted, closures, and #[event] callbacks share this route. Returning false lets the TextEditing handler apply the fragment; consuming it prevents default editing.

Construct simulated or custom input with TextCommitted::new("你好").into() or move an existing String into TextCommitted::new(text). Static strings are borrowed and owned strings are moved. Callback execution and deferred editing borrow the event until the current invocation completes; retaining business data requires its own storage. Cloning owned event text copies it. Platform text conversion and input-queue growth are separate from prepared callback and editing storage.

SDL preserves each native text event as one fragment; WGPU preserves each text or IME commit fragment rather than splitting it into characters. Native fragments are not guaranteed to be entire composition sessions or grapheme clusters. The Web keyboard source leaves composing key events to the input method, including Enter, deletion, and navigation keys; it neither cancels their browser default nor dispatches them to editors or shortcuts. Composition lifecycle and candidate-window handling are not supplied by that keyboard source.

TextInput stores appearance only. Its attachment installs TextEditing when no capability was supplied on the node. Register both Views, or use App::with_default_widgets. TextEditing owns the document binding, directed selection, focus display state, and cursor animation. TextInput reads those components and the prepared layout when drawing; it does not keep a second copy of the text.

Read content with TextEditing::read, and positions with selection() and cursor(). All lengths and positions are UTF-8 byte offsets. set_selection(TextSelection { anchor, cursor }) validates both endpoints atomically and preserves reversed selections. The default is 32 inline bytes. TextInput(capacity: 256), TextInput::build().capacity(256)?, and TextEditing::default().with_capacity(256)? prepare larger buffers before input. An explicit TextEditing attachment replaces the constructor's default capability.

To change content from application logic, bind a TextDocument and use its methods. Changes publish to all bound editors; selections remain independent. Input notifications describe accepted user actions, not programmatic document changes.

rust
#[compose]
fn search(document: model!(TextDocument)) {
    ui! {
        TextInput(width: 240, height: 36) [
            TextEditing::new(document),
            Placeholder("Search"),
        ] on TextEditing::Submitted {
            // Handle the accepted Return press.
        }
    };
}

let document = app.add_model(TextDocument::new()).finish();
document.set_capacity(256)?;
document.set_text("Hello Mirui")?;
app.compose(root, search(document.clone()));
document.replace(6..11, "world")?;

Committed text replaces the current selection as one operation. An accepted fragment produces at most one TextEditing::Changed { len: u32 }; invalid or oversized fragments are rejected in full with a warning. Backspace and Delete remove a nonempty selection first; otherwise they remove one Unicode scalar. Left and Right collapse a selection to its beginning or end, and otherwise move by one scalar, not a full grapheme cluster. Changed reports content accepted by input handlers, not caret-only changes. Calling component methods directly updates component state without synthesizing input notifications.

Shift+Left, Shift+Right, Shift+Home, and Shift+End extend the selection while retaining its anchor. Without Shift metadata, navigation moves or collapses the selection normally. Movement at an unchanged boundary does not dirty the input or notify.

Tapping an editor moves the caret to the nearest prepared visual position and collapses its selection. Hit testing shares the text's horizontal scrolling and ancestor transforms. Missing or outdated text layouts leave the selection unchanged; pointer handling does not perform text layout. A changed caret emits SelectionChanged, not Changed.

TextInput paints the focused selection behind the text using its focus border color. Selection geometry uses the prepared visual runs and shares the text's scrolling, transforms, and clipping; placeholder text is not selected.

TextEditing::SelectionChanged { anchor: u32, cursor: u32 } reports the resulting directed UTF-8 byte offsets when an input handler changes the selection. An edit that changes both content and selection reports Changed first. Replacing selected text with identical text can collapse the selection without reporting a content change. Rejected and unchanged operations do not notify.

TextEditing::Submitted reports a Return press accepted by the default key handler, including repeated presses. It does not insert a newline, clear content, move the selection, or change focus. An authored key handler can consume Return before the default handler. Both notifications support inline bodies, closures, typed callbacks, and widget builders. Notifications without listeners are skipped; queue failure does not roll back an edit or replay it later.

Custom editor examples ​

See Composing text editors for a complete custom Component, attachment callback, nested document Model, and View registration example.

SearchEditor & TextDocument
recipes.rs130 lines
1709#[model]
1710pub struct SearchForm {
1711 query: model!(owned TextDocument),
1712 #[observe]
1713 pub submissions: u32,
1714}
1715
1716impl Default for SearchForm {
1717 fn default() -> Self {
1718 Self {
1719 query: TextDocument::new().into(),
1720 submissions: 0,
1721 }
1722 }
1723}
1724
1725#[model]
1726impl SearchForm {
1727 pub fn submit(&mut self) {
1728 self.submissions += 1;
1729 }
1730
1731 pub fn preset(&mut self, value: &str) {
1732 let _ = self.query.set_text(value);
1733 }
1734}
1735
1736#[component]
1737#[derive(Default)]
1738struct SearchEditor {
1739 document: model!(optional TextDocument),
1740}
1741
1742#[event]
1743fn attach_search(
1744 node: entity!(),
1745 search: query!(SearchEditor),
1746 editing: query!(TextEditing),
1747 appearance: query!(TextInput),
1748 commands: commands!(),
1749) {
1750 let Some(search) = search.get(node) else {
1751 return;
1752 };
1753 if editing.get(node).is_none() {
1754 let editor = search
1755 .document
1756 .clone()
1757 .map(TextEditing::new)
1758 .unwrap_or_default();
1759 commands.insert(node, editor);
1760 }
1761 if appearance.get(node).is_none() {
1762 commands.insert(node, TextInput::new());
1763 }
1764}
1765
1766#[view(component = SearchEditor, attach = attach_search::callback())]
1767fn search_view() {}
1768
1769#[compose]
1770fn editor_shared_scene(form: model!(SearchForm)) {
1771 ui! {
1772 Column (
1773 grow: 1.0,
1774 padding: Padding::all(16),
1775 row_gap: 10,
1776 justify: JustifyContent::SpaceBetween,
1777 text_color: ColorToken::OnSurface
1778 ) {
1779 Row (height: 30, column_gap: 6, align: AlignItems::Center) {
1780 Text (
1781 text: ${ format_args!("Submits: {}", form.submissions()) },
1782 capacity: 24,
1783 grow: 1.0,
1784 height: 20,
1785 font_size: 12,
1786 text_color: ColorToken::OnSurfaceVariant,
1787 paragraph: ParagraphStyle::label().with_align(TextAlign::Start)
1788 )
1789 Button (
1790 "Set 'SPI'",
1791 width: 68,
1792 height: 28,
1793 font_size: 12,
1794 border_radius: 7,
1795 normal_color: ColorToken::SurfaceVariant,
1796 pressed_color: ColorToken::Outline,
1797 text_color: ColorToken::OnSurface
1798 ) on Tap { form.preset("SPI 128x160"); }
1799 Button (
1800 "Submit",
1801 width: 64,
1802 height: 28,
1803 font_size: 12,
1804 border_radius: 7,
1805 normal_color: ColorToken::Primary,
1806 pressed_color: ColorToken::Secondary,
1807 text_color: ColorToken::OnPrimary
1808 ) on Tap { form.submit(); }
1809 }
1810 SearchEditor (
1811 document: Some(form.query()),
1812 width: Dimension::percent(100),
1813 height: 38,
1814 padding: Padding::all(9),
1815 font_size: 14,
1816 bg_color: ColorToken::SurfaceVariant,
1817 border_radius: 8,
1818 border_width: 1,
1819 border_color: ColorToken::Outline
1820 ) [Placeholder("Primary SearchEditor")] on TextEditing::Submitted {
1821 form.submit();
1822 }
1823 TextInput (
1824 width: Dimension::percent(100),
1825 height: 38,
1826 padding: Padding::all(9),
1827 font_size: 14,
1828 bg_color: ColorToken::SurfaceVariant,
1829 border_radius: 8,
1830 border_width: 1,
1831 border_color: ColorToken::Outline
1832 ) [
1833 TextEditing::new(form.query()),
1834 Placeholder("Mirror TextInput (independent caret)"),
1835 ]
1836 }
1837 };
1838}
Loading preview…

Type into either input box or click Set 'SPI' to see SearchEditor and TextInput share one TextDocument with independent carets.

Gallery's editor_lab contains SearchEditor, CommandEditor, and SharedEditor. Each is a Component with a nonpainting View whose attach callback inserts TextEditing on the same node and reuses TextInput for appearance. Search observes document revisions to filter a fixed catalog; commands handle TextEditing::Submitted; two shared editors bind one owned TextDocument with independent selections. The source is split into components, Models, and composition.

Pointer identity ​

Raw InputEvent pointer records carry kind: PointerKind (Mouse, Touch, Pen, or Unknown) and id. Gesture and scroll capture match both fields; movement or release from another pointer cannot end that capture. Backends report the device category rather than inferring it from the slot number. Simulated cursor actions use Mouse; simulated pinch and rotation contacts use Touch.

PointerEnter and PointerLeave report surface boundaries without inventing coordinates. Entering waits for a position-bearing input before hit testing. Leaving clears hover and cursor feedback without releasing an active drag; captured movement still uses its actual coordinates. Touch does not produce hover feedback, and pen hover requires an explicit enter or non-contact movement.

Web and WGPU map active native pointers to stable slots: mouse uses 0, with up to 16 other pointers occupying separate slots. A full map ignores new contacts without replacing active ones. Touch slots retire on release or cancellation; Web hovering pointers retain their slots until leaving without an active press. Host focus/visibility loss clears the mapping. Web lost pointer capture cancels an active press; a capture-loss event following normal release does not cancel it again. Pointer slot numbers are not persistent device identifiers.

Node hover ​

HoverChanged { hovered, x, y } reports whether a mouse or hovering pen's hit path includes the node. It supports inline bodies, closures, and #[event] callbacks:

rust
#[event]
fn hovered(event: event!(HoverChanged), state: model!(CardState)) {
    state.set_hovered(event.hovered);
}

Column() {
    Button("Open") on Tap { open(); }
} on HoverChanged(hovered::callback(state))

Subscribing does not make an otherwise non-interactive node hit-testable or add visual feedback. Containers observe their existing interactive descendants. Touch and unknown pointer sources do not hover. Multiple hovering pointers share membership; a node leaves only when none remain. Pressing does not clear hover, and leaving the surface does not end drag capture.

Leaves are delivered from child to parent, followed by entries from parent to child. Shared ancestors do not toggle when moving between children. Coordinates are logical surface coordinates, not local node coordinates. Hidden, disabled, removed, and rebound nodes lose eligibility; published layout changes update stationary hover. Registering a listener does not replay prior transitions. Notifications share EventCapacity; failed enqueue attempts are logged and discarded without retry.

Press lifecycle ​

Pressed { pointer_id, x, y } starts a single-pointer press. Released carries the same fields for a normal release, including releases outside the original bounds; it does not imply Tap. PressCancelled adds reason: PressCancelReason: Scroll, Gesture, Platform, Suspended, or Unavailable.

rust
Button("Open")
    on Pressed { state.set_pressed(true); }
    on Released { state.set_pressed(false); }
    on PressCancelled { state.set_pressed(false); }

All three notifications also accept closures and #[event] callbacks. They do not consume input, add hit targets, or enable visual feedback. Coordinates are logical surface coordinates; pointer_id is a backend contact slot, not a persistent device identifier. Capture internally distinguishes pointer kinds as well as slots.

The hit node and its listening ancestors are captured at the initial down and notified from child to parent. Moving, leaving the surface, reparenting, or adding a listener does not redirect that path. Long press retains capture. Scroll takeover, drag or multi-pointer recognition, platform cancellation, suspension, and target invalidation cancel it instead of releasing it. Removed or rebound receivers do not receive notifications for their old identities. Each press has at most one termination; a full notification queue discards delivery without retrying or changing recognition.

Scroll geometry ​

mirui::input::event::scroll::ScrollBounds is a layout-produced component with viewport_width, viewport_height, content_width, content_height, max_x, and max_y. Read it through query!(ScrollBounds) in events, systems, or views. It is absent before the first successful layout; configuration edits take effect in this snapshot after layout succeeds. A failed layout leaves the previous value available.

rust
#[event]
fn inspect_scroll(node: entity!(), bounds: query!(ScrollBounds), state: model!(ScrollReadout)) {
    if let Some(bounds) = bounds.get(node) {
        state.set_max_offset(bounds.max_y);
    }
}

Configure scrollable content through ScrollConfig, not ScrollBounds. Retained descendants, clipping, and explicit extents determine the range; LazyList uses the entire logical item extent rather than the visible cell pool. A successful layout removes published bounds for nodes no longer in its tree or without ScrollOffset.

Scroll notifications ​

ScrollStarted { offset } marks the first actual displacement. ScrollChanged { old, new } reports content offsets, including fractional and elastic positions. ScrollEnded { offset, reason } follows completion of input, inertia, and rebound; reason is ScrollEndReason::Settled or Cancelled. Releasing a pointer does not end a segment while its content still moves.

rust
#[event]
fn track_scroll(change: event!(ScrollChanged), readout: model!(ScrollReadout)) {
    readout.set_position(change.new.y);
}

#[compose]
fn panel(readout: model!(ScrollReadout)) {
    ui! {
        Column(height: 240, clip_children: true) [
            ScrollOffset::default(), ScrollConfig::default(),
        ] on ScrollChanged(track_scroll::callback(readout)) {
            Text("Content", height: 480)
        }
    };
}

Inline bodies and closures use the same notifications. Nodes must have ScrollOffset; subscriptions do not add hit targets, input consumption, or visual feedback. Wheel, pointer, rotary, and programmatic writes share observation after callback completion. A discrete write without an active motion produces Started, Changed, then Ended; writes restored to their original offset within the same callback produce no notification.

Switching scroll targets or cancelling input ends the old segment with Cancelled. Removed, hidden, detached, or rebound sources discard their old observation. New listeners do not replay earlier movement, and a full notification budget drops delivery without retrying or rolling back the scroll position.

Node focus ​

Attach PointerFocus::Preserve to an editing toolbar or on-screen keyboard to keep keyboard focus while its buttons receive pointer input. The closest explicit policy on the hit node or its ancestors applies; PointerFocus::Automatic overrides a preserving ancestor. Without a policy, pointer activation focuses the closest focusable node or clears focus. Preservation does not restore hidden, removed, or otherwise cleared targets, and does not change keyboard or programmatic focus changes.

rust
Row() [PointerFocus::Preserve] {
    Button() [Text::label("Replace")] on Tap(replace_document::callback(document.clone()))
}

FocusChanged { focused, related } is a direct node notification. related identifies the other node in the transition and may no longer exist when read. Subscribe with an inline body, closure, or #[event] callback:

rust
#[event]
fn focus_changed(event: event!(FocusChanged), state: model!(EditorState)) {
    state.set_active(event.focused);
}

TextInput() on FocusChanged(focus_changed::callback(state))

FocusState.focused remains the focus resource. Writes within one callback publish the final value; separate Event, System, and Timer callbacks preserve separate transitions. The previous node loses focus before the new node gains it. Tap selects focus and delivers its notifications before authored Tap handlers. Changes made by focus listeners are queued, not delivered recursively. Hidden, disabled, removed, or rebound targets lose eligibility; notifications for destroyed or retired identities are discarded. Registering a listener does not replay existing focus or add pointer feedback. Host focus remains independent.

Focus notifications share EventCapacity storage and its flush limit. Failed enqueue attempts are logged and discarded without rolling back focus or retrying the notification.

Surface changes ​

Register ViewportChanged and SafeAreaChanged through App::on_event. Both carry old and new values. ViewportChanged includes changes to physical size or scale even when logical dimensions stay the same; safe-area insets are in logical pixels.

rust
app.on_event(ViewportChanged::EVENT, resized::callback());

Surface resources are updated before these callbacks and before subsequent input dispatch. Node layout may still be pending; use SizeChanged to observe a node's resolved size. Initial state and unchanged values do not emit notifications, and later subscribers receive no replay. App payload storage is prepared when listeners are installed, independently of the business notification budget.

Host state ​

HostFocusChanged { focused } and HostVisibilityChanged { visible } are App notifications. The first platform report establishes the state; repeated values are ignored and later listeners receive no replay. Platforms without a report leave that state unknown.

SDL reports window focus and shown/minimized state. WGPU reports window focus and occlusion. Web reports canvas focus while its document is focused, and document visibility; it does not infer CSS visibility or overlapping elements.

Losing host focus or visibility cancels active input before delivering the notification. Widget focus is retained. Neither notification suspends or resumes the App; AutoSuspendPlugin responds only to explicit platform suspend/resume requests.

Initialization ​

Use attach = initialize::callback() for initialization during widget assembly. The callback uses #[event] with the same entity, query, resource, Model, and command roles as other callbacks. It does not require an event payload.

rust
#[event]
fn initialize(node: entity!(), commands: commands!()) {
    commands.insert(node, TouchAction::PanY);
}

#[view(component = Slider, attach = initialize::callback())]
fn slider_behavior() {}

Static components, parent links, and IDs are installed before initialization runs. Callback borrows end and deferred commands commit before the next attachment executes. Reactive properties, authored event bindings, and children follow their normal assembly order. Initialization does not bubble, add pointer feedback, or consume business notification storage.

Each registered View prepares its initializer on the first matching entity and reuses it for later instances in that App. Initialization runs once per component attachment: ordinary field writes retain the attachment, while component replacement or removal followed by insertion creates a new one. Components installed by an initializer receive their own registered initialization regardless of View registration order. Newly registered Views also initialize existing assembled widgets at the next input or render boundary.

UI delivery prepares pending attachments before freezing each input or notification route. A callback can install a component for the next event without adding its input hooks to the event already being delivered. Initialization also runs at render preparation when no input is pending.

Configure event capacity before constructing widgets. Preparation failure logs a warning and disables that initializer; execution failure leaves the instance unavailable for View input without rolling back completed mutations or retrying automatically. Repeated structural feedback is bounded and logged. Niche declarations retain their composition lifecycle and slot construction.

Input ​

Declare a widget's default input behavior with #[view(input = ...)]. Each handler uses the existing #[event] parameters; entity!() identifies the current widget and the gesture payload retains the original hit target.

rust
#[event]
fn toggle(
    node: entity!(),
    _tap: event!(Tap),
    boxes: query!(Checkbox),
    commands: commands!(),
) -> bool {
    let Some(checkbox) = boxes.get(node) else {
        return false;
    };
    let now = !checkbox.checked;
    commands.set::<mirui::ui::property::prop::CheckboxChecked>(node, now);
    commands.emit(node, mirui::ui::widgets::checkbox::Toggled { now });
    true
}

#[view(component = Checkbox, input = toggle::callback())]
fn checkbox_input() {}

Register this descriptor through App::with_widget. A custom descriptor for an existing component replaces its default input implementation only when the default descriptor is not also registered. An empty body supplies no painting. Input and painting can belong to the same descriptor.

Use a tuple for multiple input callbacks: input = (tap::callback(), drag::callback(), key::callback()). Handlers declare their payload with event!(Tap), event!(DragMove), event!(Key), or another supported input type. Supported gestures are Tap, LongPress, DragStart, DragMove, DragEnd, DragCancel, Pinch, and Rotate; keyboard inputs are Key and TextCommitted. Business notifications remain authored on bindings, not View input hooks.

Callbacks are prepared when the first matching widget is assembled, after the App's event capacity has been configured. Matching instances share prepared callback storage; they do not each allocate command queues. Each callback invocation receives its current entity and borrows parameters afresh. Stable input dispatch reuses prepared storage.

Gesture hooks run before authored listeners on the same node. Keyboard hooks run after authored listeners on the focused node and its ancestors, before App fallback. Within a View, matching hooks run in tuple order. Returning true or () consumes input; returning false continues the route. Keyboard-only hooks do not add pointer hit targets or interaction feedback. Entity lifetime and retained-cell guards apply to the frozen route. View hooks additionally require a successfully initialized, unchanged component attachment; replacing or removing that component invalidates its remaining frozen hooks even if a replacement is initialized before dispatch resumes.

Notification delivery ​

Slider, Switch & Checkbox
2480#[model]
2481pub struct ControlDeck {
2482 #[observe]
2483 pub gain: i32,
2484 #[observe]
2485 pub armed: bool,
2486 #[observe]
2487 pub limiter: bool,
2488}
2489
2490impl Default for ControlDeck {
2491 fn default() -> Self {
2492 Self {
2493 gain: 64,
2494 armed: true,
2495 limiter: true,
2496 }
2497 }
2498}
2499
2500#[model]
2501impl ControlDeck {
2502 pub fn set_gain(&mut self, value: i32) {
2503 self.gain = value.clamp(0, 100);
2504 }
2505
2506 pub fn set_armed(&mut self, on: bool) {
2507 self.armed = on;
2508 }
2509
2510 pub fn set_limiter(&mut self, checked: bool) {
2511 self.limiter = checked;
2512 }
2513}
2514
2515#[compose]
2516fn controls_gallery_scene(deck: model!(ControlDeck)) {
2517 ui! {
2518 Column (
2519 grow: 1.0,
2520 padding: Padding::all(16),
2521 justify: JustifyContent::SpaceBetween,
2522 text_color: ColorToken::OnSurface
2523 ) {
2524 Row (height: 30, column_gap: 10, align: AlignItems::Center) {
2525 Switch (width: 44, height: 24, on: ${ deck.armed() })
2526 on Toggled { deck.set_armed(event.now); }
2527 Checkbox (width: 22, height: 22, checked: ${ deck.limiter() })
2528 on Toggled { deck.set_limiter(event.now); }
2529 Text (
2530 text: ${ format_args!("Limiter · Gain: {}%", deck.gain()) },
2531 capacity: 28,
2532 grow: 1.0,
2533 height: 20,
2534 font_size: 13
2535 )
2536 }
2537 Slider (
2538 min: 0,
2539 max: 100,
2540 value: ${ Fixed::from_int(deck.gain()) },
2541 height: 28,
2542 fill_color: ColorToken::Primary
2543 ) on ValueChanged { deck.set_gain(event.new.round().to_int()); }
2544 ProgressBar (
2545 value: ${ if deck.armed() { deck.gain() as f32 / 100.0 } else { 0.0 } },
2546 height: 12,
2547 fill_color: ColorToken::Primary
2548 )
2549 Text (
2550 text: ${ format_args!("Bus: {} · Peak guard: {}", if deck.armed() { "ARMED" } else { "MUTED" }, if deck.limiter() { "ON" } else { "BYPASS" }) },
2551 capacity: 48,
2552 height: 18,
2553 font_size: 12,
2554 text_color: ColorToken::OnSurfaceVariant
2555 )
2556 }
2557 };
2558}
Loading preview…

Toggle the Switch (on Toggled) or Checkbox (Limiter) and drag the Slider (on ValueChanged) to see built-in widget notifications update ControlDeck and drive the ProgressBar in real time.

Built-in WidgetNotification Payload(s)Emitted When
SliderValueChanged { new, old }, DragStarted, DragEnded, DragCancelledUser drags or taps the track; cancellation preserves the last value without emitting DragEnded
SwitchToggled { now: bool }User taps the switch (WidgetSource<Switch>)
CheckboxToggled { now: bool }User taps the checkbox (WidgetSource<Checkbox>)
ProgressBarValueChanged { new: f32, old: f32 }Interactive scrub changes normalized progress (0.0..=1.0)
TabBarSelectionChanged { index: usize }User selects a different tab index
TextEditingChanged { len }, SelectionChanged { anchor, cursor }, SubmittedUser commits/deletes text, moves caret/selection, or presses Return

Slider reports DragStarted, ValueChanged, and either DragEnded or DragCancelled. Cancellation preserves the last value and does not also report a normal end.

commands.emit(node, payload) records a deferred command. At command commit, a matching listener must exist for that exact entity and payload type; otherwise the payload is dropped. Skipped events do not occupy business notification storage, and later subscribers do not receive them. Payload expressions still execute normally when the command is recorded.

commands.emit_always(node, payload) records an unconditional enqueue request. A listener installed after enqueue but before dispatch can receive the notification. Dispatch without listeners discards the event; the queue does not preserve history. Invalid entities, missing source components, and insufficient notification capacity still fail through the normal callback error path.

Both methods use CommandCapacity::emit::<Payload>(count) for command records and payload storage. Notifications that are actually enqueued also use the App's business notification budget. Recording or submission errors are handled by the common executor; completed mutations are not rolled back. Commands run in order after callback borrows end, so a property update recorded before a notification is applied before that notification is dispatched.

Queued notifications retain the source cell's data-binding identity. Rebinding or deactivating that cell discards its older notifications, including unread callbacks of a notification already being dispatched. New notifications use the current binding. Discarded payloads are released without replay.

An explicit EventCapacity replaces the App's default preset. Its command budget also applies to registered widget initialization and input callbacks, including component insertion, property changes, animation commands, and emitted notifications; it is not limited to authored on handlers.

Native and software text-input sessions deliver CompositionStarted, CompositionUpdated, and CompositionEnded directly to the original editor. These callbacks return (), do not bubble, and cannot be emitted as business notifications. The editor's View input hook runs before authored listeners. CompositionUpdated::text() borrows the preview; selection() returns its optional UTF-8 byte range. CompositionEnded::reason distinguishes Committed, Cancelled, and Interrupted.

TextCommitted remains consumable: rejecting it prevents the default insertion but still ends the composition. Committing clears the preview before interception, publishes resulting document/selection notifications, then reports CompositionEnded. Committed describes delivery of the fragment, not successful insertion. Changing focus, replacing the editor or document, rebinding a retained cell, or externally editing the composing document invalidates older session requests.

Use a Model subscription when a later subscriber needs current state. A notification describes an occurrence; neither emission method stores past occurrences for future subscribers.