Skip to main content

Ui

Struct Ui 

Source
pub struct Ui { /* private fields */ }
Expand description

A tree of widgets: their layout, painting and events.

The Ui owns its widgets and lays them out with flexbox when their styles, contents or the viewport change. Every frame, paint draws them onto a Canvas, and the window layer feeds input through pointer, key and ime. Painting, hit-testing and delivering events do not allocate. Coordinates are physical pixels; Style lengths are logical pixels, multiplied by scale.

Implementations§

Source§

impl Ui

Source

pub fn access_id(&self, id: WidgetId) -> Option<AccessId>

The accessibility node id of widget id, if it lives.

Source

pub fn access_tree(&mut self) -> AccessTree

The accessibility tree of the shown widgets, laid out first if needed. It allocates: build it only while assistive technology is listening.

Source

pub fn access_action( &mut self, target: AccessId, action: AccessAction, value: Option<&str>, ) -> bool

Carries out assistive technology’s request to do action to node target, with value for AccessAction::SetValue; false if nothing did it: the widget is gone or not shown (a hidden widget is never acted on), or neither it nor, for scrolling, a parent handles it.

Source§

impl Ui

Source

pub fn pointer(&mut self, event: PointerEvent) -> Handled

Delivers a pointer event.

A press goes to the topmost widget under the pointer, then up its parents until one handles it; that widget then holds the pointer: it alone gets the moves, any other press and the release, which ends the hold. A release no widget holds goes nowhere, so a press elsewhere never ends in another widget’s click. Moves and scrolls without a holder go to the widget under the pointer, then up. A press first focuses the nearest widget (from the one under the pointer up) that accepts focus. Hover follows the pointer, except while a widget holds it: Enter and Leave go to the topmost widget the pointer comes over and leaves (its parents see PaintCx::is_hovered).

Open popups are over everything. A press or a scroll outside all of them closes them and goes no further, unless it is on the widget that opened them: that widget gets it with the popups still open (a menu bar closing its menu, or switching to another), and they close only if it does not handle it. A secondary press also reports ActionKind::ContextMenu, except inside a popup (outside them it closes them and reports it for the widget under the pointer).

Source

pub fn pointer_left(&mut self, position: Point)

The pointer left the window: nothing is hovered any more (unless a widget holds the pointer, which keeps it until the release).

Source

pub fn cancel_pointer(&mut self)

The press in progress was cancelled, say because the window lost focus: the widget holding the pointer gets PointerKind::Cancel and lets it go, and nothing is hovered.

Source

pub fn key(&mut self, event: &KeyEvent<'_>) -> Handled

Delivers a key event to the focused widget (the root if none), then up its parents until one handles it. An unhandled Tab moves focus to the next widget that accepts it (Shift+Tab to the previous), in tree order (within the topmost popup while popups are open). An unhandled context menu key (or Shift+F10) reports ActionKind::ContextMenu for the focused widget.

Source

pub fn ime(&mut self, ime: &Ime<'_>) -> Handled

Delivers an input method event to the focused widget, then up its parents.

Source

pub fn focus(&self) -> Option<WidgetId>

The widget with keyboard focus.

Source

pub fn set_focus(&mut self, id: Option<WidgetId>)

Gives keyboard focus to id (None: to nobody), sending FocusLost and FocusGained. A removed or hidden widget gets nothing.

Source

pub fn hovered(&self) -> Option<WidgetId>

The widget under the pointer, deepest first.

Source

pub fn hit_test(&mut self, at: Point) -> Option<WidgetId>

The topmost visible widget at at: later children are over earlier ones, and a widget that clips hides its children outside it.

Source§

impl Ui

Source

pub fn open_popup( &mut self, widget: impl Widget, anchor: Anchor, style: Style, ) -> Result<WidgetId, UiError>

Opens widget as a popup next to anchor, above every other widget, laid out by style (its position is made absolute: the anchor places it). It takes keyboard focus if it accepts it, and gives it back when it closes.

While popups are open, a press or a scroll outside all of them closes them all and goes no further; moves still reach the widgets under the pointer.

§Errors

UiError::TooManyWidgets.

Source

pub fn close_popup(&mut self, id: WidgetId) -> bool

Closes popup id and every popup opened after it. false if id is not open.

Source

pub fn close_popups(&mut self)

Closes every popup (the window lost focus, or was resized).

Source

pub fn popups(&self) -> impl Iterator<Item = WidgetId> + '_

The open popups, oldest first.

Source

pub fn is_popup_open(&self, id: WidgetId) -> bool

Whether id is an open popup.

Source§

impl Ui

Source

pub fn new(text: TextSystem) -> Self

An empty interface with the dark theme at scale 1, its root a column filling the viewport. text shapes every widget’s text.

Source

pub fn root(&self) -> WidgetId

The root: a column filling the viewport, painted with the theme’s background.

Source

pub fn add( &mut self, parent: WidgetId, widget: impl Widget, style: Style, ) -> Result<WidgetId, UiError>

Adds widget as the last child of parent, laid out by style.

§Errors

UiError::NoSuchWidget if parent was removed, UiError::TooManyWidgets.

Source

pub fn insert( &mut self, parent: WidgetId, index: usize, widget: impl Widget, style: Style, ) -> Result<WidgetId, UiError>

Adds widget as child number index of parent (past the end: the last), laid out by style.

§Errors

UiError::NoSuchWidget if parent was removed, UiError::TooManyWidgets.

Source

pub fn remove(&mut self, id: WidgetId) -> bool

Removes id and everything inside it; false if it was already gone, or is the root. An open popup is closed as close_popup closes it: with the popups opened after it, focus going back.

Source

pub fn set_style(&mut self, id: WidgetId, style: Style) -> bool

Changes id’s style; false if it was removed.

Source

pub fn style(&self, id: WidgetId) -> Option<&Style>

id’s style.

Source

pub fn parent(&self, id: WidgetId) -> Option<WidgetId>

id’s parent; None for the root or a removed widget.

Source

pub fn children(&self, id: WidgetId) -> &[WidgetId]

id’s children, first painted first; empty for a removed widget.

Source

pub fn rect(&self, id: WidgetId) -> Option<Rect>

Where id is, in physical pixels from the window’s top-left corner, as of the last layout.

Source

pub fn widget<T: Widget>(&self, id: WidgetId) -> Option<&T>

The widget id, if it is a T.

Source

pub fn update<T: Widget, R>( &mut self, id: WidgetId, update: impl FnOnce(&mut T, &mut EventCx<'_>) -> R, ) -> Option<R>

Runs update on the widget id, if it is a T, with a context to request layout, paint or focus as an event handler would.

Source

pub fn theme(&self) -> &Theme

The theme.

Source

pub fn set_theme(&mut self, theme: Theme)

Changes the theme; widgets are measured again (sizes may differ).

Source

pub fn clipboard(&mut self) -> &mut dyn Clipboard

The clipboard widgets copy to and paste from.

Source

pub fn set_clipboard(&mut self, clipboard: impl Clipboard + 'static)

Gives widgets clipboard (the system’s, from a window) in place of the MemoryClipboard a Ui starts with.

Source

pub fn scale(&self) -> f32

Physical pixels per logical pixel.

Source

pub fn set_scale(&mut self, scale: f32)

Changes the scale factor (the window moved to a screen of another density).

Source

pub fn viewport(&self) -> Size

The area the root fills, in physical pixels.

Source

pub fn resize(&mut self, viewport: Size)

Changes the area the root fills (the window was resized).

Source

pub fn text_system(&mut self) -> &mut TextSystem

The text system widgets shape their text with.

Source

pub fn needs_paint(&self) -> bool

Whether something changed since the last paint.

Source

pub fn cursor(&self) -> Cursor

The cursor to show: the one set by the widget under the pointer (or holding it), or its nearest parent that set one.

Source

pub fn ime_area(&self) -> Option<Rect>

Where the focused widget wants input method candidates, if it does.

Source

pub fn actions(&mut self) -> impl Iterator<Item = Action> + '_

The actions widgets reported and not yet read, oldest first, taken out.

Source

pub fn next_action(&mut self) -> Option<Action>

The oldest action not yet read, taken out: lets the application act on each with the Ui at hand.

Source

pub fn layout(&mut self)

Lays the widgets out if anything changed. paint and the event methods call it first; call it to read rects before painting.

Source

pub fn paint(&mut self, canvas: &mut Canvas<'_>)

Paints every visible widget onto canvas, laying them out first if needed.

Trait Implementations§

Source§

impl Debug for Ui

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !Freeze for Ui

§

impl !RefUnwindSafe for Ui

§

impl !Send for Ui

§

impl !Sync for Ui

§

impl !UnwindSafe for Ui

§

impl Unpin for Ui

§

impl UnsafeUnpin for Ui

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.