pub struct Ui { /* private fields */ }Expand description
Handle to a UI instance: one backend, its windows and their node trees. Cheap to clone; all clones share the same state.
Implementations§
Source§impl Ui
impl Ui
pub fn new(backend: impl Backend + 'static) -> Ui
pub fn downgrade(&self) -> WeakUi
Sourcepub fn spawn_local(
&self,
future: impl Future<Output = ()> + 'static,
) -> TaskHandle
pub fn spawn_local( &self, future: impl Future<Output = ()> + 'static, ) -> TaskHandle
Runs future on the UI thread, polled from Ui::tick. Unlike the
free spawn_local, it is not tied to a
reactive scope; cancel it with the returned handle.
Sourcepub fn sleep(&self, duration: Duration) -> Sleep ⓘ
pub fn sleep(&self, duration: Duration) -> Sleep ⓘ
A future that completes after duration on this Ui’s clock.
Sourcepub fn set_clock(&self, clock: Rc<dyn Clock>)
pub fn set_clock(&self, clock: Rc<dyn Clock>)
Replaces the clock timers use (tests install a ManualClock).
Sourcepub fn set_waker(&self, wake: Arc<dyn Fn() + Send + Sync>)
pub fn set_waker(&self, wake: Arc<dyn Fn() + Send + Sync>)
How to make the UI thread’s run loop turn from any thread; called when a task is woken (e.g. by background work finishing).
Sourcepub fn time_to_next_timer(&self) -> Option<Duration>
pub fn time_to_next_timer(&self) -> Option<Duration>
Time until the next timer fires (zero if overdue), for run loops to schedule a wake-up.
Sourcepub fn set_services(&self, services: Box<dyn Services>)
pub fn set_services(&self, services: Box<dyn Services>)
Replaces the platform services (tests install a scripted fake).
pub fn clipboard_text(&self) -> impl Future<Output = Option<String>> + use<>
Sourcepub fn set_clipboard_text(
&self,
text: &str,
) -> impl Future<Output = Result<(), ServiceError>> + use<>
pub fn set_clipboard_text( &self, text: &str, ) -> impl Future<Output = Result<(), ServiceError>> + use<>
Puts text on the clipboard. The write is issued right away; await the result to learn whether it worked.
Sourcepub fn alert(
&self,
parent: Option<NodeId>,
alert: Alert,
) -> impl Future<Output = usize> + use<>
pub fn alert( &self, parent: Option<NodeId>, alert: Alert, ) -> impl Future<Output = usize> + use<>
Shows an alert; resolves to the index of the chosen button.
Sourcepub fn open_file(
&self,
parent: Option<NodeId>,
request: OpenFile,
) -> impl Future<Output = Option<Vec<PathBuf>>> + use<>
pub fn open_file( &self, parent: Option<NodeId>, request: OpenFile, ) -> impl Future<Output = Option<Vec<PathBuf>>> + use<>
Resolves to the chosen paths, or None if cancelled.
pub fn save_file( &self, parent: Option<NodeId>, request: SaveFile, ) -> impl Future<Output = Option<PathBuf>> + use<>
Installs the app’s menus. Reactive enabled states are re-sent to
the platform when they change, for as long as the current scope lives.
Sourcepub fn pending_tasks(&self) -> usize
pub fn pending_tasks(&self) -> usize
Tasks that have not finished yet.
pub fn events(&self) -> EventSink
pub fn metrics(&self) -> PlatformMetrics
Sourcepub fn set_commit_scheduler(&self, schedule: impl Fn() + 'static)
pub fn set_commit_scheduler(&self, schedule: impl Fn() + 'static)
Native backends call this to learn when a commit is needed. The
callback should schedule Ui::commit on the run loop (e.g. before
the next frame). Without a scheduler, callers commit explicitly.
pub fn create(&self, kind: WidgetKind, props: Vec<Prop>) -> NodeId
Sourcepub fn create_window(
&self,
title: impl Into<String>,
size: impl Into<WindowSize>,
) -> NodeId
pub fn create_window( &self, title: impl Into<String>, size: impl Into<WindowSize>, ) -> NodeId
A window fitting its height is sized at its first layout, so give it its content before the next commit.
Sourcepub fn set_prop(&self, id: NodeId, prop: Prop)
pub fn set_prop(&self, id: NodeId, prop: Prop)
Sets a prop, sending it to the backend only if it changed.
pub fn set_style(&self, id: NodeId, style: Style)
Sourcepub fn update_style(&self, id: NodeId, f: impl FnOnce(&mut Style))
pub fn update_style(&self, id: NodeId, f: impl FnOnce(&mut Style))
Edits a node’s style in place (used by reactive style bindings).
pub fn set_a11y(&self, id: NodeId, a11y: A11yProps)
Sourcepub fn set_tab_index(&self, id: NodeId, index: Option<u32>)
pub fn set_tab_index(&self, id: NodeId, index: Option<u32>)
Moves a control ahead in the Tab order: controls with a tab index come first, lowest index first, then everything else in tree order.
Sourcepub fn focused(&self, window: NodeId) -> Option<NodeId>
pub fn focused(&self, window: NodeId) -> Option<NodeId>
The control that has keyboard focus in window, if any.
Sourcepub fn focus_order(&self, window: NodeId) -> Vec<NodeId>
pub fn focus_order(&self, window: NodeId) -> Vec<NodeId>
The keyboard order of a window’s focusable controls, as sent to the backend.
pub fn set_test_id(&self, id: NodeId, test_id: impl Into<String>)
Sourcepub fn on_event(&self, id: NodeId, handler: impl Fn(&UiEvent) + 'static)
pub fn on_event(&self, id: NodeId, handler: impl Fn(&UiEvent) + 'static)
Adds an event handler. It runs in the reactive scope that is current
now (usually the component being built), so inject, spawn_local
and friends work inside it.
pub fn focus(&self, id: NodeId)
Sourcepub fn insert_child(&self, parent: NodeId, child: NodeId, index: Option<usize>)
pub fn insert_child(&self, parent: NodeId, child: NodeId, index: Option<usize>)
Inserts child under parent at index (or at the end), detaching it
from its previous parent first.
pub fn append_child(&self, parent: NodeId, child: NodeId)
Sourcepub fn set_children(&self, parent: NodeId, children: Vec<NodeId>)
pub fn set_children(&self, parent: NodeId, children: Vec<NodeId>)
Replaces the children of parent, keeping nodes that stay.
Sourcepub fn process_events(&self)
pub fn process_events(&self)
Drains native events and dispatches them to handlers. Handlers run in one reactive batch per event.
Sourcepub fn tick(&self)
pub fn tick(&self)
One run-loop turn: dispatch events and commit, repeating while the commit itself produced new events (e.g. a window resize), until idle. Backends call this from their run loop, before it goes to sleep.
Sourcepub fn perform(
&self,
id: NodeId,
action: &A11yAction,
) -> Result<(), ActionError>
pub fn perform( &self, id: NodeId, action: &A11yAction, ) -> Result<(), ActionError>
Asks the backend to perform an accessibility action, then dispatches the events it produced.
Custom widgets the backend can’t act on get the event their shared
definition gives the action (e.g. Increment → a new value).
Sourcepub fn synthesize(
&self,
id: NodeId,
input: &SyntheticInput,
) -> Result<(), ActionError>
pub fn synthesize( &self, id: NodeId, input: &SyntheticInput, ) -> Result<(), ActionError>
Synthesizes raw input on a native control, then dispatches the events it produced.
pub fn windows(&self) -> Vec<NodeId>
pub fn exists(&self, id: NodeId) -> bool
pub fn kind(&self, id: NodeId) -> Option<WidgetKind>
pub fn props(&self, id: NodeId) -> Vec<Prop>
pub fn parent(&self, id: NodeId) -> Option<NodeId>
pub fn children(&self, id: NodeId) -> Vec<NodeId>
pub fn native_children(&self, id: NodeId) -> Vec<NodeId>
Sourcepub fn frame(&self, id: NodeId) -> Option<Rect>
pub fn frame(&self, id: NodeId) -> Option<Rect>
Frame relative to the native parent, as last sent to the backend.
Sourcepub fn window_frame(&self, id: NodeId) -> Option<Rect>
pub fn window_frame(&self, id: NodeId) -> Option<Rect>
Frame in the coordinates of the node’s window.
Sourcepub fn visible_rect(&self, id: NodeId) -> Option<Rect>
pub fn visible_rect(&self, id: NodeId) -> Option<Rect>
The part of a node that can be seen: its window frame, clipped by
every enclosing scroll view and by the window. None if nothing is.
Sourcepub fn scroll_offset(&self, id: NodeId) -> Option<Point>
pub fn scroll_offset(&self, id: NodeId) -> Option<Point>
Current scroll offset of a ScrollView.
Sourcepub fn scroll_to(&self, id: NodeId, offset: Point)
pub fn scroll_to(&self, id: NodeId, offset: Point)
Scrolls a ScrollView, clamping to its content.
Sourcepub fn scroll_into_view(&self, id: NodeId)
pub fn scroll_into_view(&self, id: NodeId)
Scrolls every enclosing ScrollView just enough to show the node.
Sourcepub fn window_of(&self, id: NodeId) -> Option<NodeId>
pub fn window_of(&self, id: NodeId) -> Option<NodeId>
The window a node is attached to, if any.
pub fn window_size(&self, window: NodeId) -> Option<Size>
pub fn native_state(&self, id: NodeId) -> Option<NativeState>
Sourcepub fn capture(
&self,
id: NodeId,
) -> impl Future<Output = Result<Image, CaptureError>> + use<>
pub fn capture( &self, id: NodeId, ) -> impl Future<Output = Result<Image, CaptureError>> + use<>
An offscreen screenshot of a window or node.