Trait cosmic::iced::widget::Component

source Β·
pub trait Component<Message, Theme = Theme, Renderer = Renderer> {
    type State: Default;
    type Event;

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

    // Provided methods
    fn operate(&self, _state: &mut Self::State, _operation: &mut dyn Operation) { ... }
    fn size_hint(&self) -> Size<Length> { ... }
}
πŸ‘ŽDeprecated since 0.13.0: components introduce encapsulated state and hamper the use of a single source of truth. Instead, leverage the Elm Architecture directly, or implement a custom widget
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

πŸ‘ŽDeprecated since 0.13.0: components introduce encapsulated state and hamper the use of a single source of truth. Instead, leverage the Elm Architecture directly, or implement a custom widget

The internal state of this Component.

source

type Event

πŸ‘ŽDeprecated since 0.13.0: components introduce encapsulated state and hamper the use of a single source of truth. Instead, leverage the Elm Architecture directly, or implement a custom widget

The type of event this Component handles internally.

Required Methods§

source

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

πŸ‘ŽDeprecated since 0.13.0: components introduce encapsulated state and hamper the use of a single source of truth. Instead, leverage the Elm Architecture directly, or implement a custom widget

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<'_, Self::Event, Theme, Renderer>

πŸ‘ŽDeprecated since 0.13.0: components introduce encapsulated state and hamper the use of a single source of truth. Instead, leverage the Elm Architecture directly, or implement a custom widget

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

Provided Methods§

source

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

πŸ‘ŽDeprecated since 0.13.0: components introduce encapsulated state and hamper the use of a single source of truth. Instead, leverage the Elm Architecture directly, or implement a custom widget

Update the Component state based on the provided Operation

By default, it does nothing.

source

fn size_hint(&self) -> Size<Length>

πŸ‘ŽDeprecated since 0.13.0: components introduce encapsulated state and hamper the use of a single source of truth. Instead, leverage the Elm Architecture directly, or implement a custom widget

Returns a Size hint for laying out the Component.

This hint may be used by some widget containers to adjust their sizing strategy during construction.

Implementors§