Skip to main content

Component

Trait Component 

Source
pub trait Component<'a, Message, Theme = Theme, Renderer = Renderer<Renderer, Renderer>> {
    type State: Default + 'static;
    type Event: 'static;

    // Required methods
    fn update(
        &self,
        state: &mut Self::State,
        event: Self::Event,
        renderer: &Renderer,
    ) -> Option<Message>;
    fn view(
        &self,
        state: &Self::State,
    ) -> Element<'a, Self::Event, Theme, Renderer>;

    // Provided methods
    fn listen(
        &self,
        _state: &Self::State,
        _event: &Event,
        _bounds: Rectangle,
        _cursor: Cursor,
    ) -> Action<Self::Event> { ... }
    fn mouse_interaction(&self, _state: &Self::State) -> Interaction { ... }
    fn diff(&mut self, _state: &mut Self::State) { ... }
    fn operate(
        &self,
        _state: &Self::State,
        _bounds: Rectangle,
        _operation: &mut dyn Operation,
    ) { ... }
}
Expand description

A reusable, custom widget that uses The Elm Architecture.

A Component allows you to implement custom widgets as if they were iced applications with encapsulated state.

In other words, a Component allows you to turn iced applications into custom widgets and embed them without cumbersome wiring.

A Component produces widgets that may fire an Event and update the internal state of the Component.

Additionally, a Component is capable of producing a Message to notify the parent application of any relevant interactions.

§State

A component can store its state in one of two ways: either as data within the implementor of the trait, or in a type State that is managed by the runtime and provided to the trait methods. These two approaches are not mutually exclusive and have opposite pros and cons.

For instance, if a piece of state is needed by multiple components that reside in different branches of the tree, then it’s more convenient to let a common ancestor store it and pass it down.

On the other hand, if a piece of state is only needed by the component itself, you can store it as part of its internal State.

Required Associated Types§

Source

type State: Default + 'static

The internal state of this Component.

Source

type Event: 'static

The type of event this Component handles internally.

Required Methods§

Source

fn update( &self, state: &mut Self::State, event: Self::Event, renderer: &Renderer, ) -> Option<Message>

Processes an Event and updates the Component state accordingly.

It can produce a Message for the parent application.

Source

fn view(&self, state: &Self::State) -> Element<'a, Self::Event, Theme, Renderer>

Produces the widgets of the Component, which may trigger an Event on user interaction.

Provided Methods§

Source

fn listen( &self, _state: &Self::State, _event: &Event, _bounds: Rectangle, _cursor: Cursor, ) -> Action<Self::Event>

Listens to a runtime Event and performs an Action as a result.

If the Action publishes a Component::Event, it will be immediately fed to update.

By default, it returns Action::none.

Source

fn mouse_interaction(&self, _state: &Self::State) -> Interaction

Returns the current mouse::Interaction of the Component.

This interaction will override any interaction produced by the view of the Component.

By default, it returns mouse::Interaction::None.

Source

fn diff(&mut self, _state: &mut Self::State)

Reconciles the current Component with its internal State persisted in the widget tree.

This method will be called every time the widget tree changes. You can leverage it to detect and react to changes in the Component.

By default, it does nothing.

Source

fn operate( &self, _state: &Self::State, _bounds: Rectangle, _operation: &mut dyn Operation, )

Run the provided widget::Operation on the Component.

By default, it does nothing.

Implementors§