Struct iced_native::Element

source ·
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.

Returns a reference to the Widget of the Element,

Returns a mutable reference to the Widget of the Element,

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!

Trait Implementations§

Immutably borrows from an owned value. Read more
Immutably borrows from an owned value. Read more
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 to this type from the input type.
Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Convert the source color to the destination color using the specified method Read more
Convert the source color to the destination color using the bradford method by default Read more
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.

Convert into T with values clamped to the color defined bounds Read more
Convert into T. The resulting color might be invalid in its color space Read more
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.
Convert into T, returning ok if the color is inside of its defined range, otherwise an OutOfBounds error is returned which contains the unclamped color. Read more