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

A set of interactive graphical elements with a specific Layout.

It can be updated and drawn.

Iced tries to avoid dictating how to write your event loop. You are in charge of using this type in your system in any way you want.

Example

The integration_opengl & integration_wgpu examples use a UserInterface to integrate Iced in an existing graphical application.

Implementations§

source§

impl<'a, Message, Renderer> UserInterface<'a, Message, Renderer>where
    Renderer: Renderer,
    Renderer::Theme: StyleSheet,

source

pub fn build<E: Into<Element<'a, Message, Renderer>>>(
    root: E,
    bounds: Size,
    cache: Cache,
    renderer: &mut Renderer
) -> Self

Builds a user interface for an Element.

It is able to avoid expensive computations when using a Cache obtained from a previous instance of a UserInterface.

Example

Imagine we want to build a UserInterface for the counter example that we previously wrote. Here is naive way to set up our application loop:

use iced_native::Size;
use iced_native::user_interface::{self, UserInterface};
use iced_wgpu::Renderer;

// Initialization
let mut counter = Counter::new();
let mut cache = user_interface::Cache::new();
let mut renderer = Renderer::new();
let mut window_size = Size::new(1024.0, 768.0);

// Application loop
loop {
    // Process system events here...

    // Build the user interface
    let user_interface = UserInterface::build(
        counter.view(),
        window_size,
        cache,
        &mut renderer,
    );

    // Update and draw the user interface here...
    // ...

    // Obtain the cache for the next iteration
    cache = user_interface.into_cache();
}
source

pub fn update(
    &mut self,
    events: &[Event],
    cursor_position: Point,
    renderer: &mut Renderer,
    clipboard: &mut dyn Clipboard,
    messages: &mut Vec<Message>
) -> (State, Vec<Status>)

Updates the UserInterface by processing each provided Event.

It returns messages that may have been produced as a result of user interactions. You should feed these to your update logic.

Example

Let’s allow our counter to change state by completing the previous example:

use iced_native::{clipboard, Size, Point};
use iced_native::user_interface::{self, UserInterface};
use iced_wgpu::Renderer;

let mut counter = Counter::new();
let mut cache = user_interface::Cache::new();
let mut renderer = Renderer::new();
let mut window_size = Size::new(1024.0, 768.0);
let mut cursor_position = Point::default();
let mut clipboard = clipboard::Null;

// Initialize our event storage
let mut events = Vec::new();
let mut messages = Vec::new();

loop {
    // Obtain system events...

    let mut user_interface = UserInterface::build(
        counter.view(),
        window_size,
        cache,
        &mut renderer,
    );

    // Update the user interface
    let (state, event_statuses) = user_interface.update(
        &events,
        cursor_position,
        &mut renderer,
        &mut clipboard,
        &mut messages
    );

    cache = user_interface.into_cache();

    // Process the produced messages
    for message in messages.drain(..) {
        counter.update(message);
    }
}
source

pub fn draw(
    &mut self,
    renderer: &mut Renderer,
    theme: &Renderer::Theme,
    style: &Style,
    cursor_position: Point
) -> Interaction

Draws the UserInterface with the provided Renderer.

It returns the current mouse::Interaction. You should update the icon of the mouse cursor accordingly in your system.

Example

We can finally draw our counter by completing the last example:

use iced_native::clipboard;
use iced_native::renderer;
use iced_native::user_interface::{self, UserInterface};
use iced_native::{Size, Point, Theme};
use iced_wgpu::Renderer;

let mut counter = Counter::new();
let mut cache = user_interface::Cache::new();
let mut renderer = Renderer::new();
let mut window_size = Size::new(1024.0, 768.0);
let mut cursor_position = Point::default();
let mut clipboard = clipboard::Null;
let mut events = Vec::new();
let mut messages = Vec::new();

loop {
    // Obtain system events...

    let mut user_interface = UserInterface::build(
        counter.view(),
        window_size,
        cache,
        &mut renderer,
    );

    // Update the user interface
    let event_statuses = user_interface.update(
        &events,
        cursor_position,
        &mut renderer,
        &mut clipboard,
        &mut messages
    );

    // Draw the user interface
    let mouse_cursor = user_interface.draw(&mut renderer, &Theme::default(), &renderer::Style::default(), cursor_position);

    cache = user_interface.into_cache();

    for message in messages.drain(..) {
        counter.update(message);
    }

    // Update mouse cursor icon...
    // Flush rendering operations...
}
source

pub fn operate(
    &mut self,
    renderer: &Renderer,
    operation: &mut dyn Operation<Message>
)

Applies a widget::Operation to the UserInterface.

source

pub fn relayout(self, bounds: Size, renderer: &mut Renderer) -> Self

Relayouts and returns a new UserInterface using the provided bounds.

source

pub fn into_cache(self) -> Cache

Extract the Cache of the UserInterface, consuming it in the process.

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

impl<'a, Message, Renderer> !UnwindSafe for UserInterface<'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