Struct iced_native::user_interface::UserInterface
source · 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,
impl<'a, Message, Renderer> UserInterface<'a, Message, Renderer>where Renderer: Renderer, Renderer::Theme: StyleSheet,
sourcepub fn build<E: Into<Element<'a, Message, Renderer>>>(
root: E,
bounds: Size,
cache: Cache,
renderer: &mut Renderer
) -> Self
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();
}
sourcepub fn update(
&mut self,
events: &[Event],
cursor_position: Point,
renderer: &mut Renderer,
clipboard: &mut dyn Clipboard,
messages: &mut Vec<Message>
) -> (State, Vec<Status>)
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);
}
}
sourcepub fn draw(
&mut self,
renderer: &mut Renderer,
theme: &Renderer::Theme,
style: &Style,
cursor_position: Point
) -> Interaction
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...
}
sourcepub fn operate(
&mut self,
renderer: &Renderer,
operation: &mut dyn Operation<Message>
)
pub fn operate( &mut self, renderer: &Renderer, operation: &mut dyn Operation<Message> )
Applies a widget::Operation
to the UserInterface
.
sourcepub fn relayout(self, bounds: Size, renderer: &mut Renderer) -> Self
pub fn relayout(self, bounds: Size, renderer: &mut Renderer) -> Self
Relayouts and returns a new UserInterface
using the provided
bounds.
sourcepub fn into_cache(self) -> Cache
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>,
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>,
fn adapt_into_using<M>(self, method: M) -> Dwhere M: TransformMatrix<Swp, Dwp, T>,
source§fn adapt_into(self) -> D
fn adapt_into(self) -> D
source§impl<T, U> IntoColor<U> for Twhere
U: FromColor<T>,
impl<T, U> IntoColor<U> for Twhere U: FromColor<T>,
source§fn into_color(self) -> U
fn into_color(self) -> U
source§impl<T, U> IntoColorUnclamped<U> for Twhere
U: FromColorUnclamped<T>,
impl<T, U> IntoColorUnclamped<U> for Twhere U: FromColorUnclamped<T>,
source§fn into_color_unclamped(self) -> U
fn into_color_unclamped(self) -> U
source§impl<T, U> TryIntoColor<U> for Twhere
U: TryFromColor<T>,
impl<T, U> TryIntoColor<U> for Twhere U: TryFromColor<T>,
source§fn try_into_color(self) -> Result<U, OutOfBounds<U>>
fn try_into_color(self) -> Result<U, OutOfBounds<U>>
OutOfBounds
error is returned which contains
the unclamped color. Read more