pub trait Model: Sized {
type Message: From<Event> + Send + 'static;
// Required methods
fn update(&mut self, msg: Self::Message) -> Cmd<Self::Message>;
fn view(&self, frame: &mut Frame<'_>);
// Provided methods
fn init(&mut self) -> Cmd<Self::Message> { ... }
fn subscriptions(&self) -> Vec<Box<dyn Subscription<Self::Message>>> { ... }
fn on_gesture(&mut self, gesture: SemanticEvent) -> Cmd<Self::Message> { ... }
fn on_accessibility(
&mut self,
a11y: AccessibilityFrame<'_>,
) -> Cmd<Self::Message> { ... }
fn as_screen_tick_dispatch(&mut self) -> Option<&mut dyn ScreenTickDispatch> { ... }
fn on_shutdown(&mut self) -> Cmd<Self::Message> { ... }
fn on_error(&mut self, _error: &str) -> Cmd<Self::Message> { ... }
}Expand description
The Model trait defines application state and behavior.
Implementations define how the application responds to events and renders its current state.
Required Associated Types§
Required Methods§
Provided Methods§
Sourcefn init(&mut self) -> Cmd<Self::Message>
fn init(&mut self) -> Cmd<Self::Message>
Initialize the model with startup commands.
Called once when the program starts. Return commands to execute initial side effects like loading data.
Sourcefn subscriptions(&self) -> Vec<Box<dyn Subscription<Self::Message>>>
fn subscriptions(&self) -> Vec<Box<dyn Subscription<Self::Message>>>
Declare active subscriptions.
Called after each update(). The runtime compares the returned set
(by SubId) against currently running subscriptions and starts/stops
as needed. Returning an empty vec stops all subscriptions.
§Default
Returns an empty vec (no subscriptions).
Sourcefn on_gesture(&mut self, gesture: SemanticEvent) -> Cmd<Self::Message>
fn on_gesture(&mut self, gesture: SemanticEvent) -> Cmd<Self::Message>
React to a recognized gesture: click, double/triple click, long
press, drag start/move/end/cancel, or key chord (see
SemanticEvent).
Only called when ProgramConfig::gestures is set. The runtime then
feeds every input event through a
GestureRecognizer after the
ordinary update() call and delivers each recognized gesture here;
long presses are checked once per tick. Raw events keep flowing to
update() unchanged, so enabling gestures never alters existing
behavior. Default: ignore gestures.
Sourcefn on_accessibility(
&mut self,
a11y: AccessibilityFrame<'_>,
) -> Cmd<Self::Message>
fn on_accessibility( &mut self, a11y: AccessibilityFrame<'_>, ) -> Cmd<Self::Message>
React to a changed accessibility tree: inspect the tree the last
frame produced and the screen-reader announcements derived from its
diff (see AccessibilityFrame). Typical uses: mirror the
announcements into an on-screen live region or a screen-reader
bridge, or expose the tree in a debug panel.
Only called when ProgramConfig::accessibility is set, and only
for frames whose tree differs from the previous one; it runs after
the frame was presented and the runtime schedules one more frame
so state changed here is rendered. Default: ignore.
Sourcefn as_screen_tick_dispatch(&mut self) -> Option<&mut dyn ScreenTickDispatch>
fn as_screen_tick_dispatch(&mut self) -> Option<&mut dyn ScreenTickDispatch>
Downcast to ScreenTickDispatch
for per-screen tick control.
Override this to return Some(self) in multi-screen Models. The runtime
will then consult the active TickStrategy
for each inactive screen instead of ticking monolithically.
Default: None (all screens tick every frame, backwards-compatible).
Sourcefn on_shutdown(&mut self) -> Cmd<Self::Message>
fn on_shutdown(&mut self) -> Cmd<Self::Message>
Called before the runtime exits, whether via Cmd::Quit or signal.
Return cleanup commands (e.g., saving state, closing connections). The runtime executes these before teardown.
§Migration rationale
Source frameworks use componentWillUnmount, useEffect cleanup, or
beforeDestroy hooks. This provides an equivalent lifecycle point.
Sourcefn on_error(&mut self, _error: &str) -> Cmd<Self::Message>
fn on_error(&mut self, _error: &str) -> Cmd<Self::Message>
Called when a runtime, command, or background subscription error occurs.
Return commands for error recovery or graceful degradation. The
error string contains the error description. Fatal runtime and
command errors still terminate after this hook runs; isolated
subscription failures may recover and keep the program running.
§Migration rationale
Source frameworks use componentDidCatch, error boundaries, or
onError hooks. This provides an equivalent error recovery point.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".