Trait Component

Source
pub trait Component {
    type State: 'static + Any + Send + Sync;
    type Message: 'static;
    type Output: 'static;

    // Required methods
    fn mount(&self) -> Self::State;
    fn view<'a>(&'a self, state: &'a Self::State) -> Node<'a, Self::Message>;

    // Provided methods
    fn update(
        &self,
        _message: Self::Message,
        _state: State<'_, Self::State>,
        _context: Context<'_, Self::Message, Self::Output>,
    ) { ... }
    fn style() -> StyleBuilder { ... }
    fn into_node<'a>(self) -> Node<'a, Self::Output>
       where Self: 'a + Sized { ... }
    fn class<'a>(self, class: &'a str) -> Node<'a, Self::Output>
       where Self: 'a + Sized { ... }
    fn key<'a, K>(self, key: K) -> Node<'a, Self::Output>
       where Self: 'a + Sized,
             K: Hash { ... }
}
Expand description

A re-usable component for defining a fragment of a user interface. Components are the main building block for user interfaces in pixel-widgets.

The examples in this repository all implement some kind of Component, check them out if you just want to read some code.

Required Associated Types§

Source

type State: 'static + Any + Send + Sync

Mutable state associated with this Component.

Source

type Message: 'static

The message type this Component will receive from it’s view.

Source

type Output: 'static

The message type this Component submits to its parent.

Required Methods§

Source

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

Create a new State for the Component. This will be called only once when the Component is first created.

Source

fn view<'a>(&'a self, state: &'a Self::State) -> Node<'a, Self::Message>

Generate the view for the Component. This will be called just in time before ui rendering. When the Component is updated, the view will be invalidated and the runtime will have to call this function again.

Provided Methods§

Source

fn update( &self, _message: Self::Message, _state: State<'_, Self::State>, _context: Context<'_, Self::Message, Self::Output>, )

Update the Component state in response to the message. Asynchronous operations can be submitted to the context, which will result in more update calls in the future. Messages for the parent Component or root can also be submitted through the context.

Source

fn style() -> StyleBuilder

Returns a StyleBuilder with styling information scoped to this component. This method will be called when you call StyleBuilder::component() when building your style.

Source

fn into_node<'a>(self) -> Node<'a, Self::Output>
where Self: 'a + Sized,

Converts the component into a Node. This is used by the library to instantiate the component in a user interface.

Source

fn class<'a>(self, class: &'a str) -> Node<'a, Self::Output>
where Self: 'a + Sized,

Converts the component into a Node and sets a style class to it.

Source

fn key<'a, K>(self, key: K) -> Node<'a, Self::Output>
where Self: 'a + Sized, K: Hash,

Converts the component into a Node and sets a custom key to it.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§