Skip to main content

Ui

Struct Ui 

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

The element tree, the stylesheets that style it, and the focus.

M is the application’s message type. Handlers produce messages, and handle returns them for the application to act on, which keeps every decision in explicit application code rather than in callbacks scattered through the tree.

A frame goes: feed events to handle, act on the messages, layout, paint.

Implementations§

Source§

impl<M: 'static> Ui<M>

Source

pub fn layout(&mut self, viewport: Size, fonts: &dyn Fonts)

Recomputes styles if anything changed, then sizes and places every element for a window of viewport pixels. Call before painting and before hit-testing events after a change.

Source§

impl<M: 'static> Ui<M>

Source

pub fn paint(&self, renderer: &mut dyn Renderer)

Draws the whole tree, as last laid out, through renderer.

For each element, in tree order: the background, the border, the widget’s own content, then its children. Nothing is clipped unless a widget asks for it, so an element that overflows its parent shows.

Source§

impl<M: 'static> Ui<M>

Source

pub fn new() -> Self

An empty tree styled by the default theme.

Source

pub fn unstyled() -> Self

An empty tree with no stylesheet at all.

Source

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

Replaces every stylesheet with this one.

Source

pub fn add_stylesheet(&mut self, sheet: Stylesheet)

Adds a stylesheet after the existing ones, so it wins ties.

Source

pub fn styler(&self) -> &Styler

Source

pub fn styler_mut(&mut self) -> &mut Styler

The style engine, for changing the root font size and the like.

Source

pub fn set_root(&mut self, element: Element<M>) -> NodeId

Replaces the whole tree with element.

Source

pub fn mount(&mut self, parent: NodeId, element: Element<M>) -> NodeId

Appends element as the last child of parent.

Source

pub fn remove(&mut self, id: NodeId)

Removes an element and everything under it.

Source

pub fn clear(&mut self)

Removes every element.

Source

pub fn root(&self) -> Option<NodeId>

Source

pub fn find(&self, id: &str) -> Option<NodeId>

The element with this id attribute, if any.

Source

pub fn contains(&self, id: NodeId) -> bool

Source

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

Source

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

Source

pub fn tag(&self, id: NodeId) -> &'static str

Source

pub fn rect(&self, id: NodeId) -> Rect

The element’s border box, as of the last layout.

Source

pub fn content_rect(&self, id: NodeId) -> Rect

The element’s content box, as of the last layout.

Source

pub fn style(&self, id: NodeId) -> &ComputedStyle

The element’s style, as of the last layout.

Source

pub fn state(&self, id: NodeId) -> ElementState

The element’s full state: what the framework tracks, what the widget owns, and DISABLED if it is disabled.

Source

pub fn widget<W: Widget<M>>(&self, id: NodeId) -> Option<&W>

The widget behind an element, if it is a W.

Source

pub fn widget_mut<W: Widget<M>>(&mut self, id: NodeId) -> Option<&mut W>

The widget behind an element, for changing it. Styles and layout are recomputed afterwards.

Source

pub fn hit_test(&self, point: Point) -> Option<NodeId>

The deepest element under point, as of the last layout.

Source

pub fn preorder(&self) -> Vec<NodeId>

Every element, parents before children, siblings in order.

Source

pub fn needs_layout(&self) -> bool

Whether something changed since the last layout.

Source

pub fn viewport(&self) -> Size

Source

pub fn has_class(&self, id: NodeId, class: &str) -> bool

Source

pub fn set_class(&mut self, id: NodeId, class: &str, on: bool)

Adds or removes a class.

Source

pub fn is_disabled(&self, id: NodeId) -> bool

Source

pub fn set_disabled(&mut self, id: NodeId, disabled: bool)

A disabled element matches :disabled, gets no events and cannot take focus.

Source

pub fn set_state(&mut self, id: NodeId, name: &str, on: bool)

Sets or clears a custom state, matched by :state(name).

Source

pub fn set_attr(&mut self, id: NodeId, name: &str, value: &str) -> bool

Sets an attribute at run time, the same ones Element::attr takes. Returns false for an attribute the element does not accept or a value it cannot parse, rather than panicking, since the value may come from anywhere.

Source

pub fn set_inline_style(&mut self, id: NodeId, css: &str) -> Result<(), Error>

Replaces the inline style.

Source

pub fn focused(&self) -> Option<NodeId>

Source

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

Source

pub fn focus(&mut self, id: NodeId) -> bool

Moves keyboard focus to id, if it can take it. Focus moved this way shows as :focus-visible, like focus moved with the keyboard.

Source

pub fn blur(&mut self)

Source

pub fn move_focus(&mut self, forward: bool)

Moves focus to the next (or previous) focusable element in tree order, wrapping around. What Tab and Shift+Tab do.

Source

pub fn bind(&mut self, shortcut: Shortcut, handler: impl FnMut() -> M + 'static)

Binds a shortcut for the whole window. Global shortcuts are checked before element shortcuts and before the focused element sees the key.

Source

pub fn unbind(&mut self, shortcut: Shortcut)

Source

pub fn shortcuts(&self) -> Vec<(Shortcut, Option<NodeId>)>

Every shortcut in effect: the global ones and those of elements that are not disabled, with the element’s id when it has one. For help screens and menus.

Source

pub fn handle(&mut self, event: &Event, fonts: &dyn Fonts) -> Vec<M>

Feeds one event through the tree and returns the messages it produced.

Pointer events go to the deepest element under the pointer and bubble up until a widget handles them; while a button is held, they go to the element it was pressed on. Key events go first to global shortcuts, then to element shortcuts, then to the focused element, and finally Tab and Shift+Tab move focus. Text goes to the focused element.

Hit-testing uses the last layout, so call layout after changes before feeding more pointer events.

Trait Implementations§

Source§

impl<M: 'static> Debug for Ui<M>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<M: 'static> Default for Ui<M>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<M> !RefUnwindSafe for Ui<M>

§

impl<M> !Send for Ui<M>

§

impl<M> !Sync for Ui<M>

§

impl<M> !UnwindSafe for Ui<M>

§

impl<M> Freeze for Ui<M>

§

impl<M> Unpin for Ui<M>

§

impl<M> UnsafeUnpin for Ui<M>

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> 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.