logo
pub struct Element<'a, Message, Renderer> { /* private fields */ }
Expand description

A generic Widget.

It is useful to build composable user interfaces that do not leak implementation details in their view logic.

If you have a built-in widget, you should be able to use Into<Element> to turn it into an Element.

Implementations

Creates a new Element containing the given Widget.

Applies a transformation to the produced message of the Element.

This method is useful when you want to decouple different parts of your UI and make them composable.

Example

Imagine we want to use our counter. But instead of showing a single counter, we want to display many of them. We can reuse the Counter type as it is!

We use composition to model the state of our new application:

use counter::Counter;

struct ManyCounters {
    counters: Vec<Counter>,
}

We can store the state of multiple counters now. However, the messages we implemented before describe the user interactions of a single counter. Right now, we need to also identify which counter is receiving user interactions. Can we use composition again? Yes.

#[derive(Debug, Clone, Copy)]
pub enum Message {
    Counter(usize, counter::Message)
}

We compose the previous messages with the index of the counter producing them. Let’s implement our view logic now:

use iced_native::Element;
use iced_native::widget::Row;
use iced_wgpu::Renderer;

impl ManyCounters {
    pub fn view(&mut self) -> Row<Message, Renderer> {
        // We can quickly populate a `Row` by folding over our counters
        self.counters.iter_mut().enumerate().fold(
            Row::new().spacing(20),
            |row, (index, counter)| {
                // We display the counter
                let element: Element<counter::Message, Renderer> =
                    counter.view().into();

                row.push(
                    // Here we turn our `Element<counter::Message>` into
                    // an `Element<Message>` by combining the `index` and the
                    // message of the `element`.
                    element.map(move |message| Message::Counter(index, message))
                )
            }
        )
    }
}

Finally, our update logic is pretty straightforward: simple delegation.

impl ManyCounters {
    pub fn update(&mut self, message: Message) {
        match message {
            Message::Counter(index, counter_msg) => {
                if let Some(counter) = self.counters.get_mut(index) {
                    counter.update(counter_msg);
                }
            }
        }
    }
}

Marks the Element as to-be-explained.

The Renderer will explain the layout of the Element graphically. This can be very useful for debugging your layout!

Returns the width of the Element.

Returns the height of the Element.

Computes the layout of the Element in the given Limits.

Processes a runtime Event.

Draws the Element and its children using the given Layout.

Returns the current mouse::Interaction of the Element.

Returns the overlay of the Element, if there is any.

Trait Implementations

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts this type into the (usually inferred) input type.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.