Skip to main content

Widget

Trait Widget 

Source
pub trait Widget<M>: Any {
    // Required methods
    fn as_any(&self) -> &dyn Any;
    fn as_any_mut(&mut self) -> &mut dyn Any;

    // Provided methods
    fn set_attribute(&mut self, name: &str, value: &str) -> bool { ... }
    fn axis(&self) -> Option<Axis> { ... }
    fn measure(&self, fonts: &dyn Fonts, style: &ComputedStyle) -> Size { ... }
    fn state(&self) -> ElementState { ... }
    fn focusable(&self) -> bool { ... }
    fn event(&mut self, cx: &mut EventCx<'_, M>, event: &Event) -> bool { ... }
    fn paint(
        &self,
        renderer: &mut dyn Renderer,
        cx: &PaintCx<'_>,
        content: Rect,
    ) { ... }
}
Expand description

What an element does: how it measures, reacts to input and paints.

Composition is declarative (the ui! macro builds a tree of elements) but behaviour is imperative: a widget is a plain struct with methods, and you can reach it through Ui::widget_mut to change it.

The framework draws backgrounds and borders from the computed style and lays children out; a widget only paints its own content and handles events aimed at it. Every method has a default, so a container is an empty impl plus axis.

Required Methods§

Source

fn as_any(&self) -> &dyn Any

Source

fn as_any_mut(&mut self) -> &mut dyn Any

Provided Methods§

Source

fn set_attribute(&mut self, name: &str, value: &str) -> bool

Takes an attribute from the element description, returning false for one the widget does not know. id, class, style, grow, align, disabled, shortcut and state never reach here.

Source

fn axis(&self) -> Option<Axis>

Some for a container: the direction it lays its children out in.

Source

fn measure(&self, fonts: &dyn Fonts, style: &ComputedStyle) -> Size

The natural size of the widget’s own content, without padding or border. Containers are measured from their children instead.

Source

fn state(&self) -> ElementState

State the widget owns, such as CHECKED, merged into the element’s state for styling.

Source

fn focusable(&self) -> bool

Whether the widget can take keyboard focus.

Source

fn event(&mut self, cx: &mut EventCx<'_, M>, event: &Event) -> bool

Handles an event aimed at this element. Returning true stops the event from bubbling to the parent.

Source

fn paint(&self, renderer: &mut dyn Renderer, cx: &PaintCx<'_>, content: Rect)

Paints the widget’s own content inside content, after the framework has painted the background and border and before it paints the children.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<M: 'static> Widget<M> for Blank

Source§

impl<M: 'static> Widget<M> for Button

Source§

impl<M: 'static> Widget<M> for Checkbox

Source§

impl<M: 'static> Widget<M> for Container

Source§

impl<M: 'static> Widget<M> for Label

Source§

impl<M: 'static> Widget<M> for TextInput