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§

source§

impl<'a, Message, Renderer> Element<'a, Message, Renderer>

source

pub fn new(widget: impl Widget<Message, Renderer> + 'a) -> Selfwhere
    Renderer: Renderer,

Creates a new Element containing the given Widget.

source

pub fn as_widget(&self) -> &dyn Widget<Message, Renderer>

Returns a reference to the Widget of the Element,

source

pub fn as_widget_mut(&mut self) -> &mut dyn Widget<Message, Renderer>

Returns a mutable reference to the Widget of the Element,

source

pub fn map<B>(self, f: impl Fn(Message) -> B + 'a) -> Element<'a, B, Renderer>where
    Message: 'a,
    Renderer: Renderer + 'a,
    B: 'a,

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);
                }
            }
        }
    }
}
source

pub fn explain<C: Into<Color>>(self, color: C) -> Element<'a, Message, Renderer>where
    Message: 'static,
    Renderer: Renderer + 'a,

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§

source§

impl<'a, Message, Renderer> Borrow<dyn Widget<Message, Renderer> + 'a> for &Element<'a, Message, Renderer>

source§

fn borrow(&self) -> &(dyn Widget<Message, Renderer> + 'a)

Immutably borrows from an owned value. Read more
source§

impl<'a, Message, Renderer> Borrow<dyn Widget<Message, Renderer> + 'a> for Element<'a, Message, Renderer>

source§

fn borrow(&self) -> &(dyn Widget<Message, Renderer> + 'a)

Immutably borrows from an owned value. Read more
source§

impl<'a, Message, Renderer> From<&'a str> for Element<'a, Message, Renderer>where
    Renderer: Renderer + 'a,
    Renderer::Theme: StyleSheet,

source§

fn from(contents: &'a str) -> Self

Converts to this type from the input type.
source§

impl<'a, Message, Renderer> From<Button<'a, Message, Renderer>> for Element<'a, Message, Renderer>where
    Message: Clone + 'a,
    Renderer: Renderer + 'a,
    Renderer::Theme: StyleSheet,

source§

fn from(button: Button<'a, Message, Renderer>) -> Self

Converts to this type from the input type.
source§

impl<'a, Message, Renderer> From<Checkbox<'a, Message, Renderer>> for Element<'a, Message, Renderer>where
    Message: 'a,
    Renderer: 'a + Renderer,
    Renderer::Theme: StyleSheet + StyleSheet,

source§

fn from(
    checkbox: Checkbox<'a, Message, Renderer>
) -> Element<'a, Message, Renderer>

Converts to this type from the input type.
source§

impl<'a, Message, Renderer> From<Column<'a, Message, Renderer>> for Element<'a, Message, Renderer>where
    Message: 'a,
    Renderer: Renderer + 'a,

source§

fn from(column: Column<'a, Message, Renderer>) -> Self

Converts to this type from the input type.
source§

impl<'a, Message, Renderer> From<Container<'a, Message, Renderer>> for Element<'a, Message, Renderer>where
    Message: 'a,
    Renderer: 'a + Renderer,
    Renderer::Theme: StyleSheet,

source§

fn from(
    column: Container<'a, Message, Renderer>
) -> Element<'a, Message, Renderer>

Converts to this type from the input type.
source§

impl<'a, Message, Renderer, Handle> From<Image<Handle>> for Element<'a, Message, Renderer>where
    Renderer: Renderer<Handle = Handle>,
    Handle: Clone + Hash + 'a,

source§

fn from(image: Image<Handle>) -> Element<'a, Message, Renderer>

Converts to this type from the input type.
source§

impl<'a, Message, Renderer> From<PaneGrid<'a, Message, Renderer>> for Element<'a, Message, Renderer>where
    Message: 'a,
    Renderer: 'a + Renderer,
    Renderer::Theme: StyleSheet + StyleSheet,

source§

fn from(
    pane_grid: PaneGrid<'a, Message, Renderer>
) -> Element<'a, Message, Renderer>

Converts to this type from the input type.
source§

impl<'a, T, Message, Renderer> From<PickList<'a, T, Message, Renderer>> for Element<'a, Message, Renderer>where
    T: Clone + ToString + Eq + 'static + 'a,
    [T]: ToOwned<Owned = Vec<T>>,
    Message: 'a,
    Renderer: Renderer + 'a,
    Renderer::Theme: StyleSheet + StyleSheet + StyleSheet + StyleSheet,
    <Renderer::Theme as StyleSheet>::Style: From<<Renderer::Theme as StyleSheet>::Style>,

source§

fn from(pick_list: PickList<'a, T, Message, Renderer>) -> Self

Converts to this type from the input type.
source§

impl<'a, Message, Renderer> From<ProgressBar<Renderer>> for Element<'a, Message, Renderer>where
    Message: 'a,
    Renderer: 'a + Renderer,
    Renderer::Theme: StyleSheet,

source§

fn from(progress_bar: ProgressBar<Renderer>) -> Element<'a, Message, Renderer>

Converts to this type from the input type.
source§

impl<'a, Message, Renderer> From<Radio<Message, Renderer>> for Element<'a, Message, Renderer>where
    Message: 'a + Clone,
    Renderer: 'a + Renderer,
    Renderer::Theme: StyleSheet + StyleSheet,

source§

fn from(radio: Radio<Message, Renderer>) -> Element<'a, Message, Renderer>

Converts to this type from the input type.
source§

impl<'a, Message, Renderer> From<Row<'a, Message, Renderer>> for Element<'a, Message, Renderer>where
    Message: 'a,
    Renderer: Renderer + 'a,

source§

fn from(row: Row<'a, Message, Renderer>) -> Self

Converts to this type from the input type.
source§

impl<'a, Message, Renderer> From<Rule<Renderer>> for Element<'a, Message, Renderer>where
    Message: 'a,
    Renderer: 'a + Renderer,
    Renderer::Theme: StyleSheet,

source§

fn from(rule: Rule<Renderer>) -> Element<'a, Message, Renderer>

Converts to this type from the input type.
source§

impl<'a, Message, Renderer> From<Scrollable<'a, Message, Renderer>> for Element<'a, Message, Renderer>where
    Message: 'a,
    Renderer: 'a + Renderer,
    Renderer::Theme: StyleSheet,

source§

fn from(
    text_input: Scrollable<'a, Message, Renderer>
) -> Element<'a, Message, Renderer>

Converts to this type from the input type.
source§

impl<'a, T, Message, Renderer> From<Slider<'a, T, Message, Renderer>> for Element<'a, Message, Renderer>where
    T: 'a + Copy + Into<f64> + FromPrimitive,
    Message: 'a + Clone,
    Renderer: 'a + Renderer,
    Renderer::Theme: StyleSheet,

source§

fn from(
    slider: Slider<'a, T, Message, Renderer>
) -> Element<'a, Message, Renderer>

Converts to this type from the input type.
source§

impl<'a, Message, Renderer> From<Space> for Element<'a, Message, Renderer>where
    Renderer: Renderer,
    Message: 'a,

source§

fn from(space: Space) -> Element<'a, Message, Renderer>

Converts to this type from the input type.
source§

impl<'a, Message, Renderer> From<Svg<Renderer>> for Element<'a, Message, Renderer>where
    Renderer: Renderer + 'a,
    Renderer::Theme: StyleSheet,

source§

fn from(icon: Svg<Renderer>) -> Element<'a, Message, Renderer>

Converts to this type from the input type.
source§

impl<'a, Message, Renderer> From<Text<'a, Renderer>> for Element<'a, Message, Renderer>where
    Renderer: Renderer + 'a,
    Renderer::Theme: StyleSheet,

source§

fn from(text: Text<'a, Renderer>) -> Element<'a, Message, Renderer>

Converts to this type from the input type.
source§

impl<'a, Message, Renderer> From<TextInput<'a, Message, Renderer>> for Element<'a, Message, Renderer>where
    Message: 'a + Clone,
    Renderer: 'a + Renderer,
    Renderer::Theme: StyleSheet,

source§

fn from(
    text_input: TextInput<'a, Message, Renderer>
) -> Element<'a, Message, Renderer>

Converts to this type from the input type.
source§

impl<'a, Message, Renderer> From<Toggler<'a, Message, Renderer>> for Element<'a, Message, Renderer>where
    Message: 'a,
    Renderer: 'a + Renderer,
    Renderer::Theme: StyleSheet + StyleSheet,

source§

fn from(
    toggler: Toggler<'a, Message, Renderer>
) -> Element<'a, Message, Renderer>

Converts to this type from the input type.
source§

impl<'a, Message, Renderer> From<Tooltip<'a, Message, Renderer>> for Element<'a, Message, Renderer>where
    Message: 'a,
    Renderer: 'a + Renderer,
    Renderer::Theme: StyleSheet + StyleSheet,

source§

fn from(
    tooltip: Tooltip<'a, Message, Renderer>
) -> Element<'a, Message, Renderer>

Converts to this type from the input type.
source§

impl<'a, T, Message, Renderer> From<VerticalSlider<'a, T, Message, Renderer>> for Element<'a, Message, Renderer>where
    T: 'a + Copy + Into<f64> + FromPrimitive,
    Message: 'a + Clone,
    Renderer: 'a + Renderer,
    Renderer::Theme: StyleSheet,

source§

fn from(
    slider: VerticalSlider<'a, T, Message, Renderer>
) -> Element<'a, Message, Renderer>

Converts to this type from the input type.
source§

impl<'a, Message, Renderer, Handle> From<Viewer<Handle>> for Element<'a, Message, Renderer>where
    Renderer: 'a + Renderer<Handle = Handle>,
    Message: 'a,
    Handle: Clone + Hash + 'a,

source§

fn from(viewer: Viewer<Handle>) -> Element<'a, Message, Renderer>

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<'a, Message, Renderer> !RefUnwindSafe for Element<'a, Message, Renderer>

§

impl<'a, Message, Renderer> !Send for Element<'a, Message, Renderer>

§

impl<'a, Message, Renderer> !Sync for Element<'a, Message, Renderer>

§

impl<'a, Message, Renderer> Unpin for Element<'a, Message, Renderer>

§

impl<'a, Message, Renderer> !UnwindSafe for Element<'a, Message, Renderer>

Blanket Implementations§

source§

impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for Swhere
    T: FloatComponent,
    Swp: WhitePoint,
    Dwp: WhitePoint,
    D: AdaptFrom<S, Swp, Dwp, T>,

source§

fn adapt_into_using<M>(self, method: M) -> Dwhere
    M: TransformMatrix<Swp, Dwp, T>,

Convert the source color to the destination color using the specified method
source§

fn adapt_into(self) -> D

Convert the source color to the destination color using the bradford method by default
source§

impl<T> Any for Twhere
    T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere
    T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere
    T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere
    U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T, U> IntoColor<U> for Twhere
    U: FromColor<T>,

source§

fn into_color(self) -> U

Convert into T with values clamped to the color defined bounds Read more
source§

impl<T, U> IntoColorUnclamped<U> for Twhere
    U: FromColorUnclamped<T>,

source§

fn into_color_unclamped(self) -> U

Convert into T. The resulting color might be invalid in its color space Read more
source§

impl<T, U> TryFrom<U> for Twhere
    U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere
    U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T, U> TryIntoColor<U> for Twhere
    U: TryFromColor<T>,

source§

fn try_into_color(self) -> Result<U, OutOfBounds<U>>

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
§

impl<V, T> VZip<V> for Twhere
    V: MultiLane<T>,

§

fn vzip(self) -> V