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 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_window_size(&self, window: NodeId, size: Size)
pub fn set_window_size(&self, window: NodeId, size: Size)
Asks for a window’s content size, in points, as the app would resize
it. The platform may refuse it (a window in full screen keeps the
screen’s), and gives it no smaller than the window’s MinSize; the
content is laid out at the size it reports (WindowResized). A
WindowSize::FollowHeight window takes only the width: its content
sets its height. Other windows stop fitting their height.
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 update_a11y(&self, id: NodeId, f: impl FnOnce(&mut A11yProps))
pub fn update_a11y(&self, id: NodeId, f: impl FnOnce(&mut A11yProps))
Changes part of a node’s semantics.
pub fn set_test_id(&self, id: NodeId, test_id: impl Into<String>)
Source§impl Ui
impl Ui
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.
Sourcepub fn present(&self, id: NodeId)
pub fn present(&self, id: NodeId)
Brings the window id is in (or the window id is) forward, at
the next commit.
Sourcepub fn select_text(&self, id: NodeId, range: Range<usize>)
pub fn select_text(&self, id: NodeId, range: Range<usize>)
Focuses a text field or text area and selects these grapheme clusters of its text, at the next commit; a range past its end is cut to it. Other controls are only focused.
Source§impl Ui
impl Ui
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) or new sizes views watch, 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.
Source§impl Ui
impl Ui
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. A list’s rows are where the platform put them, at the size sent.
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 viewport(&self, id: NodeId) -> Option<Rect>
pub fn viewport(&self, id: NodeId) -> Option<Rect>
Where a ScrollView shows its content, in window coordinates: its
frame less the room its scroll bars take, if the platform’s do.
Sourcepub fn viewport_insets(&self, id: NodeId) -> Option<Insets>
pub fn viewport_insets(&self, id: NodeId) -> Option<Insets>
Where a ScrollView’s viewport is in its frame, as the platform
last reported it.
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’s viewport and by the window (or, in the toolbar,
by its toolbar item; the sidebar, by itself). None if nothing is.
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>
Source§impl Ui
impl Ui
Sourcepub fn scroll_offset(&self, id: NodeId) -> Option<Point>
pub fn scroll_offset(&self, id: NodeId) -> Option<Point>
Current scroll offset of a ScrollView, List or Table.
Sourcepub fn scroll_to(&self, id: NodeId, offset: Point)
pub fn scroll_to(&self, id: NodeId, offset: Point)
Scrolls a ScrollView, List or Table. A scroll view’s offset is
clamped to its content; a list’s or table’s isn’t, since its rows
are the platform’s: the backend clamps it.
Sourcepub fn scroll_into_view(&self, id: NodeId)
pub fn scroll_into_view(&self, id: NodeId)
Scrolls every enclosing ScrollView, List and Table just enough to show
the node.
Source§impl Ui
impl Ui
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<>
Sourcepub fn trash(
&self,
parent: Option<NodeId>,
paths: Vec<PathBuf>,
) -> impl Future<Output = Result<(), ServiceError>> + use<>
pub fn trash( &self, parent: Option<NodeId>, paths: Vec<PathBuf>, ) -> impl Future<Output = Result<(), ServiceError>> + use<>
Moves files and folders to the trash; see Services::trash.
Sourcepub fn launch(
&self,
parent: Option<NodeId>,
target: Launch,
) -> impl Future<Output = Result<(), ServiceError>> + use<>
pub fn launch( &self, parent: Option<NodeId>, target: Launch, ) -> impl Future<Output = Result<(), ServiceError>> + use<>
Opens a file, folder or URL in another app; see Services::launch.
Sourcepub fn notify(
&self,
notification: Notification,
) -> impl Future<Output = Result<(), ServiceError>> + use<>
pub fn notify( &self, notification: Notification, ) -> impl Future<Output = Result<(), ServiceError>> + use<>
Shows a notification outside the app’s windows; see
Services::notify. Resolves once the platform has it.
Sourcepub fn withdraw_notification(&self, id: &str)
pub fn withdraw_notification(&self, id: &str)
Takes away the notification with this id, if it’s still there.
Sourcepub fn on_notification_activated(&self, handler: impl Fn(&str) + 'static)
pub fn on_notification_activated(&self, handler: impl Fn(&str) + 'static)
Runs handler with a notification’s id when the user clicks it,
until the current scope ends. The platform has brought the app
forward by then; the app shows what the notification was about.
Sourcepub fn on_reopen(&self, handler: impl Fn(&Reopen) + 'static)
pub fn on_reopen(&self, handler: impl Fn(&Reopen) + 'static)
Runs handler each time a single-instance app is launched again,
with what the launch passed on, until the current scope ends. The
backend has brought the last active window forward by then, with
the platform’s activation token; an app with none opens one here,
and the token is still there for it. Without a handler, a launch
only brings the window forward. On macOS a click on the Dock icon
comes here too: it’s how macOS apps reopen a closed window.
Sourcepub fn reopen(&self, reopen: Reopen)
pub fn reopen(&self, reopen: Reopen)
What backends call, on the main thread, when the app is launched
again, once they’ve brought its window forward: the on_reopen
handlers run in the next turn.
Installs the app’s menus, replacing the ones installed before. Their reactive parts (titles, enabled and checked states, which items there are) are sent to the platform again when they change, for as long as the current scope lives.
Installs a window’s own menus, shown with the app’s, until the
current scope ends. A MenuBar in the window’s content does this.
Sourcepub fn set_tray(&self, tray: TrayIcon)
pub fn set_tray(&self, tray: TrayIcon)
Shows the app’s icon in the platform’s tray until the current scope
ends, replacing the one shown before. Its reactive parts (title,
icon, tooltip, the menu’s items) are sent to the platform again
when they change. Where there’s no tray to show it in (plain GNOME,
or a Linux tray host that isn’t running yet), it shows once one
comes: see tray_shown.
Sourcepub fn tray_shown(&self) -> Signal<bool>
pub fn tray_shown(&self) -> Signal<bool>
Whether the tray icon is showing right now: false before the app sets one, where the platform has no tray (plain GNOME), and while a Linux tray host is away. An app that hides its window on close only when there’s a tray to bring it back watches it. The signal lives as long as the Ui.
Sourcepub fn set_runs_without_windows(&self, runs: bool)
pub fn set_runs_without_windows(&self, runs: bool)
Keeps the app running while no window is open, until Ui::quit:
for an app that waits before it shows one, as for a notification’s
click or a request from another process. Without, an app ends when
its last window closes.
Sourcepub fn quit(&self)
pub fn quit(&self)
Ends the app once this turn of its run loop is done, with any
windows still open, without asking them first (unlike
Ui::request_quit).
Sourcepub fn should_end(&self) -> bool
pub fn should_end(&self) -> bool
Whether the run loop should end now: Ui::quit was called, or no
window is open and the app doesn’t run without them. Backends ask
after each turn, and before running at all.
Sourcepub fn request_quit(&self)
pub fn request_quit(&self)
Quitting as the platform asks for it from outside the app’s menus (macOS’s Dock, logging out, the session ending): the app’s Quit item, as if chosen, if it has one; otherwise every window is asked to close, as by its close button. Either way the app decides, and it ends when its last window closes. Backends call it, then see whether windows are left.
Source§impl Ui
impl Ui
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.
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_app_info(&self, info: AppInfo)
pub fn set_app_info(&self, info: AppInfo)
The app’s id, name and icon, for the platform to show. Set it
before the app’s first window; App does.
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_locales(&self, locales: Locales)
pub fn set_locales(&self, locales: Locales)
The app’s translations (locales!). Set them before the app’s
first window; App does. The language is chosen from them again.
Sourcepub fn set_languages(&self, languages: Option<Vec<String>>)
pub fn set_languages(&self, languages: Option<Vec<String>>)
The languages to choose the app’s from, most wanted first, in place
of the user’s: an app’s own language setting, or a test’s. None:
the user’s, as the platform lists them.
Sourcepub fn language(&self) -> LanguageIdentifier
pub fn language(&self) -> LanguageIdentifier
The app’s language: the first of the user’s it has translations for, else its fallback. Tracked.
Sourcepub fn take_l10n_errors(&self) -> Vec<String>
pub fn take_l10n_errors(&self) -> Vec<String>
Messages missing from every language, and messages that failed to format, since the last call. Tests fail on them.
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.