Trait Widget

Source
pub trait Widget<'a, Message>: Send {
    type State: Any + Send + Sync;

    // Required methods
    fn mount(&self) -> Self::State;
    fn widget(&self) -> &'static str;
    fn len(&self) -> usize;
    fn visit_children(
        &mut self,
        visitor: &mut dyn FnMut(&mut dyn GenericNode<'a, Message>),
    );
    fn size(&self, state: &Self::State, style: &Stylesheet) -> (Size, Size);
    fn draw(
        &mut self,
        state: &mut Self::State,
        layout: Rectangle,
        clip: Rectangle,
        style: &Stylesheet,
    ) -> Vec<Primitive<'a>>;

    // Provided methods
    fn key(&self) -> u64 { ... }
    fn state(&self, _state: &Self::State) -> StateVec { ... }
    fn is_empty(&self) -> bool { ... }
    fn hit(
        &self,
        _state: &Self::State,
        layout: Rectangle,
        clip: Rectangle,
        _style: &Stylesheet,
        x: f32,
        y: f32,
    ) -> bool { ... }
    fn focused(&self, _state: &Self::State) -> bool { ... }
    fn event(
        &mut self,
        _state: &mut Self::State,
        _layout: Rectangle,
        _clip: Rectangle,
        _style: &Stylesheet,
        _event: Event,
        _context: &mut Context<Message>,
    ) { ... }
}
Expand description

A user interface widget.

Required Associated Types§

Source

type State: Any + Send + Sync

The type of state this widget keeps track of.

Required Methods§

Source

fn mount(&self) -> Self::State

Create a new state

Source

fn widget(&self) -> &'static str

The name of this widget, used to identify widgets of this type in stylesheets.

Source

fn len(&self) -> usize

Should return the amount of children this widget has. Must be consistent with visit_children().

Source

fn visit_children( &mut self, visitor: &mut dyn FnMut(&mut dyn GenericNode<'a, Message>), )

Applies a visitor to all childs of the widget. If an widget fails to visit it’s children, the children won’t be able to resolve their stylesheet, resulting in a panic when calling size, hit, event or draw.

Source

fn size(&self, state: &Self::State, style: &Stylesheet) -> (Size, Size)

Returns the (width, height) of this widget. The extents are defined as a Size, which will later be resolved to actual dimensions.

Source

fn draw( &mut self, state: &mut Self::State, layout: Rectangle, clip: Rectangle, style: &Stylesheet, ) -> Vec<Primitive<'a>>

Draw the widget. Returns a list of Primitives that should be drawn.

Arguments:

  • layout: the layout assigned to the widget
  • clip: a clipping rect for use with Primitive::PushClip.

Provided Methods§

Source

fn key(&self) -> u64

The key of this widget, used for resolving state.

Source

fn state(&self, _state: &Self::State) -> StateVec

The state of this widget, used for computing the style. If None is returned, Node will automatically compute a state, such as “hover” and “pressed”.

Source

fn is_empty(&self) -> bool

Returns whether this children has no children. Must be consistent with visit_children().

Source

fn hit( &self, _state: &Self::State, layout: Rectangle, clip: Rectangle, _style: &Stylesheet, x: f32, y: f32, ) -> bool

Perform a hit detect on the widget. Most widgets are fine with the default implementation, but some widgets (like Window need to report a miss (false) even when the queried position is within their layout.

Arguments:

  • layout: the layout assigned to the widget
  • clip: a clipping rect for mouse events. Mouse events outside of this rect should be considered invalid, such as with Scroll, where the widget would not be visible outside of the currently visible rect.
  • x: x mouse coordinate being queried
  • y: y mouse coordinate being queried
Source

fn focused(&self, _state: &Self::State) -> bool

Test the widget for focus exclusivity. If the widget or one of it’s descendants is in an exclusive focus state, this function should return true. In all other cases, it should return false. When a widget is in an exclusive focus state it is the only widget that is allowed to receive events in event. Widgets that intended to use this behaviour are modal dialogs, dropdown boxes, context menu’s, etc.

Source

fn event( &mut self, _state: &mut Self::State, _layout: Rectangle, _clip: Rectangle, _style: &Stylesheet, _event: Event, _context: &mut Context<Message>, )

Handle an event. If an event changes the graphical appearance of an Widget, redraw should be called to let the Ui know that the ui should be redrawn.

Arguments:

  • layout: the layout assigned to the widget
  • clip: a clipping rect for mouse events. Mouse events outside of this rect should be considered invalid, such as with Scroll, where the widget would not be visible outside of the currently visible rect.
  • event: the event that needs to be handled
  • context: context for submitting messages and requesting redraws of the ui.

Implementors§

Source§

impl<'a, T> Widget<'a, T> for Spacer

Source§

impl<'a, T> Widget<'a, T> for Text

Source§

impl<'a, T, F, S> Widget<'a, T> for Input<'a, T, F, S>
where T: 'a + Send, F: 'a + Send + Fn(String) -> T, S: 'a + Send + AsRef<str>,

Source§

impl<'a, T, F: Send + Fn(bool) -> T> Widget<'a, T> for Toggle<T, F>

Source§

impl<'a, T, Message: 'a, OnAccept, OnDrop> Widget<'a, Message> for Drop<'a, T, Message, OnAccept, OnDrop>
where T: DragDropId + Send + Sync, OnAccept: 'a + Send + Fn(T) -> bool, OnDrop: 'a + Send + Fn(T, (f32, f32)) -> Message,

Source§

impl<'a, T: 'a + Send> Widget<'a, T> for Button<'a, T>

Source§

impl<'a, T: 'a + Send> Widget<'a, T> for Column<'a, T>

Source§

impl<'a, T: 'a + Send> Widget<'a, T> for Layers<'a, T>

Source§

impl<'a, T: 'a + Send, S: Send + AsRef<[MenuItem<'a, T>]> + AsMut<[MenuItem<'a, T>]>> Widget<'a, T> for Menu<'a, T, S>

Source§

impl<'a, T: 'a> Widget<'a, T> for Dummy

Source§

impl<'a, T: 'a> Widget<'a, T> for Frame<'a, T>

Source§

impl<'a, T: 'a> Widget<'a, T> for Image<'a>

Source§

impl<'a, T: 'a> Widget<'a, T> for Panel<'a, T>

Source§

impl<'a, T: 'a> Widget<'a, T> for Progress<'a, T>

Source§

impl<'a, T: 'a> Widget<'a, T> for Row<'a, T>

Source§

impl<'a, T: 'a> Widget<'a, T> for Scroll<'a, T>

Source§

impl<'a, T: 'a> Widget<'a, T> for Window<'a, T>

Source§

impl<'a, T: 'a, F: 'a + Send + Fn(f32) -> T> Widget<'a, T> for Slider<'a, T, F>

Source§

impl<'a, T: DragDropId + Send + Sync, Message: 'a> Widget<'a, Message> for Drag<'a, T, Message>

Source§

impl<'a, T: Send + 'a, F: Send + Fn(usize) -> T> Widget<'a, T> for Dropdown<'a, T, F>