2022-01-12 11:22:57 +07:00
|
|
|
//! Implement your own event loop to drive a user interface.
|
2023-03-04 05:37:11 +01:00
|
|
|
use crate::core::event::{self, Event};
|
|
|
|
|
use crate::core::layout;
|
|
|
|
|
use crate::core::mouse;
|
2025-08-23 05:15:57 +02:00
|
|
|
use crate::core::overlay;
|
2023-03-04 05:37:11 +01:00
|
|
|
use crate::core::renderer;
|
|
|
|
|
use crate::core::widget;
|
|
|
|
|
use crate::core::window;
|
2025-01-10 07:12:31 +09:00
|
|
|
use crate::core::{
|
2025-02-02 20:45:29 +01:00
|
|
|
Clipboard, Element, InputMethod, Layout, Rectangle, Shell, Size, Vector,
|
2025-01-10 07:12:31 +09:00
|
|
|
};
|
2019-08-23 05:53:54 +02:00
|
|
|
|
2019-08-29 00:58:42 +02:00
|
|
|
/// A set of interactive graphical elements with a specific [`Layout`].
|
|
|
|
|
///
|
2019-08-29 03:33:02 +02:00
|
|
|
/// It can be updated and drawn.
|
2019-08-29 00:58:42 +02:00
|
|
|
///
|
|
|
|
|
/// Iced tries to avoid dictating how to write your event loop. You are in
|
2019-08-29 03:33:02 +02:00
|
|
|
/// charge of using this type in your system in any way you want.
|
2019-08-29 00:58:42 +02:00
|
|
|
///
|
2020-04-02 02:35:36 +02:00
|
|
|
/// # Example
|
2023-05-11 15:50:35 +02:00
|
|
|
/// The [`integration`] example uses a [`UserInterface`] to integrate Iced in an
|
|
|
|
|
/// existing graphical application.
|
2020-04-02 02:35:36 +02:00
|
|
|
///
|
2024-09-18 02:38:49 +02:00
|
|
|
/// [`integration`]: https://github.com/iced-rs/iced/tree/0.13/examples/integration
|
2024-01-21 17:56:01 +01:00
|
|
|
pub struct UserInterface<'a, Message, Theme, Renderer> {
|
|
|
|
|
root: Element<'a, Message, Theme, Renderer>,
|
2022-02-22 14:10:49 +07:00
|
|
|
base: layout::Node,
|
2022-07-27 06:49:20 +02:00
|
|
|
state: widget::Tree,
|
2025-05-02 23:11:47 +02:00
|
|
|
overlay: Option<Overlay>,
|
2020-01-10 03:10:58 +01:00
|
|
|
bounds: Size,
|
2019-08-23 05:53:54 +02:00
|
|
|
}
|
|
|
|
|
|
2025-05-02 23:11:47 +02:00
|
|
|
struct Overlay {
|
|
|
|
|
layout: layout::Node,
|
|
|
|
|
interaction: mouse::Interaction,
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-21 17:56:01 +01:00
|
|
|
impl<'a, Message, Theme, Renderer> UserInterface<'a, Message, Theme, Renderer>
|
2019-10-05 03:56:18 +02:00
|
|
|
where
|
2023-03-05 06:35:20 +01:00
|
|
|
Renderer: crate::core::Renderer,
|
2019-10-05 03:56:18 +02:00
|
|
|
{
|
2019-08-29 00:58:42 +02:00
|
|
|
/// 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](index.html#usage). Here
|
2019-09-03 04:49:58 +02:00
|
|
|
/// is naive way to set up our application loop:
|
2019-08-29 00:58:42 +02:00
|
|
|
///
|
|
|
|
|
/// ```no_run
|
|
|
|
|
/// # mod iced_wgpu {
|
2024-03-22 05:27:31 +01:00
|
|
|
/// # pub type Renderer = ();
|
2019-08-29 00:58:42 +02:00
|
|
|
/// # }
|
|
|
|
|
/// #
|
|
|
|
|
/// # pub struct Counter;
|
|
|
|
|
/// #
|
|
|
|
|
/// # impl Counter {
|
|
|
|
|
/// # pub fn new() -> Self { Counter }
|
2024-01-21 17:56:01 +01:00
|
|
|
/// # pub fn view(&self) -> iced_core::Element<(), (), Renderer> { unimplemented!() }
|
2023-03-04 05:37:11 +01:00
|
|
|
/// # pub fn update(&mut self, _: ()) {}
|
2019-08-29 00:58:42 +02:00
|
|
|
/// # }
|
2023-03-05 06:35:20 +01:00
|
|
|
/// use iced_runtime::core::Size;
|
|
|
|
|
/// use iced_runtime::user_interface::{self, UserInterface};
|
2023-03-04 05:37:11 +01:00
|
|
|
/// use iced_wgpu::Renderer;
|
|
|
|
|
///
|
2019-08-29 00:58:42 +02:00
|
|
|
/// // Initialization
|
|
|
|
|
/// let mut counter = Counter::new();
|
2022-01-11 13:47:43 +07:00
|
|
|
/// let mut cache = user_interface::Cache::new();
|
2024-03-22 05:27:31 +01:00
|
|
|
/// let mut renderer = Renderer::default();
|
2020-01-10 03:10:58 +01:00
|
|
|
/// let mut window_size = Size::new(1024.0, 768.0);
|
2019-08-29 00:58:42 +02:00
|
|
|
///
|
|
|
|
|
/// // Application loop
|
|
|
|
|
/// loop {
|
|
|
|
|
/// // Process system events here...
|
|
|
|
|
///
|
|
|
|
|
/// // Build the user interface
|
|
|
|
|
/// let user_interface = UserInterface::build(
|
|
|
|
|
/// counter.view(),
|
2020-01-10 03:10:58 +01:00
|
|
|
/// window_size,
|
2019-08-29 00:58:42 +02:00
|
|
|
/// cache,
|
2019-09-19 15:01:12 +02:00
|
|
|
/// &mut renderer,
|
2019-08-29 00:58:42 +02:00
|
|
|
/// );
|
|
|
|
|
///
|
|
|
|
|
/// // Update and draw the user interface here...
|
|
|
|
|
/// // ...
|
|
|
|
|
///
|
|
|
|
|
/// // Obtain the cache for the next iteration
|
|
|
|
|
/// cache = user_interface.into_cache();
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
2024-01-21 17:56:01 +01:00
|
|
|
pub fn build<E: Into<Element<'a, Message, Theme, Renderer>>>(
|
2019-08-29 00:58:42 +02:00
|
|
|
root: E,
|
2020-01-10 03:10:58 +01:00
|
|
|
bounds: Size,
|
2022-07-27 06:49:20 +02:00
|
|
|
cache: Cache,
|
2019-11-11 06:07:31 +01:00
|
|
|
renderer: &mut Renderer,
|
2019-08-23 05:53:54 +02:00
|
|
|
) -> Self {
|
2025-08-20 22:42:15 +02:00
|
|
|
let mut root = root.into();
|
2019-08-29 00:58:42 +02:00
|
|
|
|
2022-07-27 06:49:20 +02:00
|
|
|
let Cache { mut state } = cache;
|
|
|
|
|
state.diff(root.as_widget());
|
|
|
|
|
|
2025-08-20 22:42:15 +02:00
|
|
|
let base = root.as_widget_mut().layout(
|
2023-08-30 06:36:24 +02:00
|
|
|
&mut state,
|
2023-08-30 04:31:21 +02:00
|
|
|
renderer,
|
|
|
|
|
&layout::Limits::new(Size::ZERO, bounds),
|
|
|
|
|
);
|
2022-08-05 23:51:32 +02:00
|
|
|
|
2019-08-23 05:53:54 +02:00
|
|
|
UserInterface {
|
2020-07-05 05:44:10 +02:00
|
|
|
root,
|
2020-04-16 13:22:00 +02:00
|
|
|
base,
|
2022-07-27 06:49:20 +02:00
|
|
|
state,
|
2022-02-22 14:10:49 +07:00
|
|
|
overlay: None,
|
2020-01-10 03:10:58 +01:00
|
|
|
bounds,
|
2019-08-23 05:53:54 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-29 00:58:42 +02:00
|
|
|
/// 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
|
2019-11-21 13:47:20 +01:00
|
|
|
/// Let's allow our [counter](index.html#usage) to change state by
|
|
|
|
|
/// completing [the previous example](#example):
|
2019-08-29 00:58:42 +02:00
|
|
|
///
|
|
|
|
|
/// ```no_run
|
|
|
|
|
/// # mod iced_wgpu {
|
2024-03-22 05:27:31 +01:00
|
|
|
/// # pub type Renderer = ();
|
2019-08-29 00:58:42 +02:00
|
|
|
/// # }
|
|
|
|
|
/// #
|
|
|
|
|
/// # pub struct Counter;
|
|
|
|
|
/// #
|
|
|
|
|
/// # impl Counter {
|
|
|
|
|
/// # pub fn new() -> Self { Counter }
|
2024-01-21 17:56:01 +01:00
|
|
|
/// # pub fn view(&self) -> iced_core::Element<(), (), Renderer> { unimplemented!() }
|
2023-03-04 05:37:11 +01:00
|
|
|
/// # pub fn update(&mut self, _: ()) {}
|
2019-08-29 00:58:42 +02:00
|
|
|
/// # }
|
2023-06-08 20:35:40 +02:00
|
|
|
/// use iced_runtime::core::clipboard;
|
|
|
|
|
/// use iced_runtime::core::mouse;
|
|
|
|
|
/// use iced_runtime::core::Size;
|
2023-03-05 06:35:20 +01:00
|
|
|
/// use iced_runtime::user_interface::{self, UserInterface};
|
2023-03-04 05:37:11 +01:00
|
|
|
/// use iced_wgpu::Renderer;
|
|
|
|
|
///
|
2019-08-29 00:58:42 +02:00
|
|
|
/// let mut counter = Counter::new();
|
2022-01-11 13:47:43 +07:00
|
|
|
/// let mut cache = user_interface::Cache::new();
|
2024-03-22 05:27:31 +01:00
|
|
|
/// let mut renderer = Renderer::default();
|
2020-01-10 03:10:58 +01:00
|
|
|
/// let mut window_size = Size::new(1024.0, 768.0);
|
2023-06-08 20:35:40 +02:00
|
|
|
/// let mut cursor = mouse::Cursor::default();
|
2021-03-10 01:59:02 +01:00
|
|
|
/// let mut clipboard = clipboard::Null;
|
2019-08-29 00:58:42 +02:00
|
|
|
///
|
|
|
|
|
/// // Initialize our event storage
|
|
|
|
|
/// let mut events = Vec::new();
|
2020-11-12 02:00:08 +01:00
|
|
|
/// let mut messages = Vec::new();
|
2019-08-29 00:58:42 +02:00
|
|
|
///
|
|
|
|
|
/// loop {
|
2020-11-12 02:00:08 +01:00
|
|
|
/// // Obtain system events...
|
2019-08-29 00:58:42 +02:00
|
|
|
///
|
|
|
|
|
/// let mut user_interface = UserInterface::build(
|
|
|
|
|
/// counter.view(),
|
2020-01-10 03:10:58 +01:00
|
|
|
/// window_size,
|
2019-08-29 00:58:42 +02:00
|
|
|
/// cache,
|
2019-09-19 15:01:12 +02:00
|
|
|
/// &mut renderer,
|
2019-08-29 00:58:42 +02:00
|
|
|
/// );
|
|
|
|
|
///
|
2020-11-12 02:51:26 +01:00
|
|
|
/// // Update the user interface
|
2022-01-11 13:47:43 +07:00
|
|
|
/// let (state, event_statuses) = user_interface.update(
|
2020-11-12 02:51:26 +01:00
|
|
|
/// &events,
|
2023-06-08 20:35:40 +02:00
|
|
|
/// cursor,
|
2021-11-29 16:22:01 +07:00
|
|
|
/// &mut renderer,
|
2021-03-10 01:59:02 +01:00
|
|
|
/// &mut clipboard,
|
2020-11-12 02:51:26 +01:00
|
|
|
/// &mut messages
|
|
|
|
|
/// );
|
2019-08-29 00:58:42 +02:00
|
|
|
///
|
|
|
|
|
/// cache = user_interface.into_cache();
|
|
|
|
|
///
|
|
|
|
|
/// // Process the produced messages
|
2020-11-12 02:00:08 +01:00
|
|
|
/// for message in messages.drain(..) {
|
2019-08-29 00:58:42 +02:00
|
|
|
/// counter.update(message);
|
|
|
|
|
/// }
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
2019-08-23 05:53:54 +02:00
|
|
|
pub fn update(
|
|
|
|
|
&mut self,
|
2020-11-12 02:51:26 +01:00
|
|
|
events: &[Event],
|
2023-06-08 20:35:40 +02:00
|
|
|
cursor: mouse::Cursor,
|
2021-11-29 16:22:01 +07:00
|
|
|
renderer: &mut Renderer,
|
2021-03-10 01:59:02 +01:00
|
|
|
clipboard: &mut dyn Clipboard,
|
2020-11-12 02:00:08 +01:00
|
|
|
messages: &mut Vec<Message>,
|
2022-01-11 13:47:43 +07:00
|
|
|
) -> (State, Vec<event::Status>) {
|
2023-01-12 02:59:08 +01:00
|
|
|
let mut outdated = false;
|
2025-02-02 20:45:29 +01:00
|
|
|
let mut redraw_request = window::RedrawRequest::Wait;
|
2025-02-12 08:46:35 +01:00
|
|
|
let mut input_method = InputMethod::Disabled;
|
2025-10-15 19:08:39 +02:00
|
|
|
let mut has_layout_changed = false;
|
2025-04-26 02:51:32 +02:00
|
|
|
let viewport = Rectangle::with_size(self.bounds);
|
2023-01-12 02:59:08 +01:00
|
|
|
|
2025-05-02 23:11:47 +02:00
|
|
|
let mut maybe_overlay = self
|
|
|
|
|
.root
|
|
|
|
|
.as_widget_mut()
|
|
|
|
|
.overlay(
|
|
|
|
|
&mut self.state,
|
|
|
|
|
Layout::new(&self.base),
|
|
|
|
|
renderer,
|
|
|
|
|
&viewport,
|
|
|
|
|
Vector::ZERO,
|
|
|
|
|
)
|
|
|
|
|
.map(overlay::Nested::new);
|
2022-01-11 13:47:43 +07:00
|
|
|
|
2025-05-02 23:11:47 +02:00
|
|
|
let (base_cursor, overlay_statuses, overlay_interaction) =
|
|
|
|
|
if maybe_overlay.is_some() {
|
|
|
|
|
let bounds = self.bounds;
|
2022-05-13 19:07:54 +02:00
|
|
|
|
2025-05-02 23:11:47 +02:00
|
|
|
let mut overlay = maybe_overlay.as_mut().unwrap();
|
|
|
|
|
let mut layout = overlay.layout(renderer, bounds);
|
|
|
|
|
let mut event_statuses = Vec::new();
|
2020-07-05 05:44:10 +02:00
|
|
|
|
2025-05-02 23:11:47 +02:00
|
|
|
for event in events {
|
|
|
|
|
let mut shell = Shell::new(messages);
|
2022-05-13 19:07:54 +02:00
|
|
|
|
2025-05-02 23:11:47 +02:00
|
|
|
overlay.update(
|
|
|
|
|
event,
|
|
|
|
|
Layout::new(&layout),
|
|
|
|
|
cursor,
|
|
|
|
|
renderer,
|
|
|
|
|
clipboard,
|
|
|
|
|
&mut shell,
|
|
|
|
|
);
|
2022-05-13 19:07:54 +02:00
|
|
|
|
2025-05-02 23:11:47 +02:00
|
|
|
event_statuses.push(shell.event_status());
|
|
|
|
|
redraw_request = redraw_request.min(shell.redraw_request());
|
|
|
|
|
input_method.merge(shell.input_method());
|
2023-01-12 02:59:08 +01:00
|
|
|
|
2025-05-02 23:11:47 +02:00
|
|
|
if shell.is_layout_invalid() {
|
|
|
|
|
drop(maybe_overlay);
|
2022-05-13 19:07:54 +02:00
|
|
|
|
2025-08-20 22:42:15 +02:00
|
|
|
self.base = self.root.as_widget_mut().layout(
|
2025-05-02 23:11:47 +02:00
|
|
|
&mut self.state,
|
|
|
|
|
renderer,
|
|
|
|
|
&layout::Limits::new(Size::ZERO, self.bounds),
|
|
|
|
|
);
|
2021-11-29 16:22:01 +07:00
|
|
|
|
2025-05-02 23:11:47 +02:00
|
|
|
maybe_overlay = self
|
|
|
|
|
.root
|
2023-02-18 14:31:38 -08:00
|
|
|
.as_widget_mut()
|
|
|
|
|
.overlay(
|
|
|
|
|
&mut self.state,
|
|
|
|
|
Layout::new(&self.base),
|
|
|
|
|
renderer,
|
2025-04-26 02:51:32 +02:00
|
|
|
&viewport,
|
2024-02-01 01:08:21 +01:00
|
|
|
Vector::ZERO,
|
2023-02-18 14:31:38 -08:00
|
|
|
)
|
2025-05-02 23:11:47 +02:00
|
|
|
.map(overlay::Nested::new);
|
2021-11-29 16:22:01 +07:00
|
|
|
|
2025-05-02 23:11:47 +02:00
|
|
|
if maybe_overlay.is_none() {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-05-13 19:07:54 +02:00
|
|
|
|
2025-05-02 23:11:47 +02:00
|
|
|
overlay = maybe_overlay.as_mut().unwrap();
|
2022-05-13 19:07:54 +02:00
|
|
|
|
2025-05-02 23:11:47 +02:00
|
|
|
shell.revalidate_layout(|| {
|
|
|
|
|
layout = overlay.layout(renderer, bounds);
|
2025-10-15 19:08:39 +02:00
|
|
|
has_layout_changed = true;
|
2025-05-02 23:11:47 +02:00
|
|
|
});
|
|
|
|
|
}
|
2021-11-29 16:22:01 +07:00
|
|
|
|
2025-05-02 23:11:47 +02:00
|
|
|
if shell.are_widgets_invalid() {
|
|
|
|
|
outdated = true;
|
|
|
|
|
}
|
2022-05-13 19:07:54 +02:00
|
|
|
}
|
2020-04-16 13:22:00 +02:00
|
|
|
|
2025-05-02 23:11:47 +02:00
|
|
|
let (base_cursor, interaction) =
|
|
|
|
|
if let Some(overlay) = maybe_overlay.as_mut() {
|
|
|
|
|
let interaction = cursor
|
|
|
|
|
.position()
|
|
|
|
|
.map(|cursor_position| {
|
|
|
|
|
overlay.mouse_interaction(
|
|
|
|
|
Layout::new(&layout),
|
|
|
|
|
mouse::Cursor::Available(cursor_position),
|
|
|
|
|
renderer,
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
.unwrap_or_default();
|
|
|
|
|
|
|
|
|
|
if interaction == mouse::Interaction::None {
|
|
|
|
|
(cursor, mouse::Interaction::None)
|
|
|
|
|
} else {
|
|
|
|
|
(mouse::Cursor::Unavailable, interaction)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
(cursor, mouse::Interaction::None)
|
|
|
|
|
};
|
2020-07-05 05:44:10 +02:00
|
|
|
|
2025-05-02 23:11:47 +02:00
|
|
|
self.overlay = Some(Overlay {
|
|
|
|
|
layout,
|
|
|
|
|
interaction,
|
|
|
|
|
});
|
2020-07-05 05:44:10 +02:00
|
|
|
|
2025-05-02 23:11:47 +02:00
|
|
|
(base_cursor, event_statuses, interaction)
|
|
|
|
|
} else {
|
|
|
|
|
(
|
|
|
|
|
cursor,
|
|
|
|
|
vec![event::Status::Ignored; events.len()],
|
|
|
|
|
mouse::Interaction::None,
|
|
|
|
|
)
|
|
|
|
|
};
|
2023-07-15 10:04:25 -07:00
|
|
|
|
2025-05-02 23:11:47 +02:00
|
|
|
drop(maybe_overlay);
|
2022-05-13 19:07:54 +02:00
|
|
|
|
2022-01-11 13:47:43 +07:00
|
|
|
let event_statuses = events
|
2020-11-12 02:51:26 +01:00
|
|
|
.iter()
|
2023-08-26 01:34:42 +02:00
|
|
|
.zip(overlay_statuses)
|
2020-11-12 02:51:26 +01:00
|
|
|
.map(|(event, overlay_status)| {
|
2022-05-26 10:43:23 -07:00
|
|
|
if matches!(overlay_status, event::Status::Captured) {
|
|
|
|
|
return overlay_status;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-29 16:22:01 +07:00
|
|
|
let mut shell = Shell::new(messages);
|
|
|
|
|
|
2024-10-25 22:06:06 +02:00
|
|
|
self.root.as_widget_mut().update(
|
2022-07-27 06:49:20 +02:00
|
|
|
&mut self.state,
|
2020-11-12 02:51:26 +01:00
|
|
|
event,
|
2022-02-22 14:10:49 +07:00
|
|
|
Layout::new(&self.base),
|
2020-11-12 02:51:26 +01:00
|
|
|
base_cursor,
|
|
|
|
|
renderer,
|
|
|
|
|
clipboard,
|
2021-11-29 16:22:01 +07:00
|
|
|
&mut shell,
|
2023-07-15 10:04:25 -07:00
|
|
|
&viewport,
|
2020-11-12 02:51:26 +01:00
|
|
|
);
|
2019-08-23 05:53:54 +02:00
|
|
|
|
2024-10-25 19:28:18 +02:00
|
|
|
if shell.event_status() == event::Status::Captured {
|
2022-11-14 00:50:18 +01:00
|
|
|
self.overlay = None;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-02 20:45:29 +01:00
|
|
|
redraw_request = redraw_request.min(shell.redraw_request());
|
|
|
|
|
input_method.merge(shell.input_method());
|
2023-01-12 02:59:08 +01:00
|
|
|
|
2022-01-11 13:47:43 +07:00
|
|
|
shell.revalidate_layout(|| {
|
2025-10-15 19:08:39 +02:00
|
|
|
has_layout_changed = true;
|
|
|
|
|
|
2025-08-20 22:42:15 +02:00
|
|
|
self.base = self.root.as_widget_mut().layout(
|
2023-08-30 06:36:24 +02:00
|
|
|
&mut self.state,
|
2023-08-30 04:31:21 +02:00
|
|
|
renderer,
|
2021-11-29 16:22:01 +07:00
|
|
|
&layout::Limits::new(Size::ZERO, self.bounds),
|
|
|
|
|
);
|
|
|
|
|
|
2025-05-15 17:21:58 +02:00
|
|
|
if let Some(mut overlay) = self
|
|
|
|
|
.root
|
|
|
|
|
.as_widget_mut()
|
|
|
|
|
.overlay(
|
|
|
|
|
&mut self.state,
|
|
|
|
|
Layout::new(&self.base),
|
|
|
|
|
renderer,
|
|
|
|
|
&viewport,
|
|
|
|
|
Vector::ZERO,
|
|
|
|
|
)
|
|
|
|
|
.map(overlay::Nested::new)
|
|
|
|
|
{
|
|
|
|
|
let layout = overlay.layout(renderer, self.bounds);
|
|
|
|
|
let interaction = overlay.mouse_interaction(
|
|
|
|
|
Layout::new(&layout),
|
|
|
|
|
cursor,
|
|
|
|
|
renderer,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
self.overlay = Some(Overlay {
|
|
|
|
|
layout,
|
|
|
|
|
interaction,
|
|
|
|
|
});
|
|
|
|
|
}
|
2021-11-29 16:22:01 +07:00
|
|
|
});
|
|
|
|
|
|
2022-01-11 13:47:43 +07:00
|
|
|
if shell.are_widgets_invalid() {
|
2023-01-12 02:59:08 +01:00
|
|
|
outdated = true;
|
2022-01-11 13:47:43 +07:00
|
|
|
}
|
|
|
|
|
|
2024-10-25 19:28:18 +02:00
|
|
|
shell.event_status().merge(overlay_status)
|
2020-11-12 02:51:26 +01:00
|
|
|
})
|
2022-01-11 13:47:43 +07:00
|
|
|
.collect();
|
|
|
|
|
|
2025-05-02 23:11:47 +02:00
|
|
|
let mouse_interaction =
|
|
|
|
|
if overlay_interaction == mouse::Interaction::None {
|
|
|
|
|
self.root.as_widget().mouse_interaction(
|
|
|
|
|
&self.state,
|
|
|
|
|
Layout::new(&self.base),
|
|
|
|
|
base_cursor,
|
|
|
|
|
&viewport,
|
|
|
|
|
renderer,
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
overlay_interaction
|
|
|
|
|
};
|
|
|
|
|
|
2023-01-12 02:59:08 +01:00
|
|
|
(
|
|
|
|
|
if outdated {
|
|
|
|
|
State::Outdated
|
|
|
|
|
} else {
|
2025-01-10 07:12:31 +09:00
|
|
|
State::Updated {
|
2025-05-02 23:11:47 +02:00
|
|
|
mouse_interaction,
|
2025-01-10 07:12:31 +09:00
|
|
|
redraw_request,
|
2025-02-02 20:45:29 +01:00
|
|
|
input_method,
|
2025-10-15 19:08:39 +02:00
|
|
|
has_layout_changed,
|
2025-01-10 07:12:31 +09:00
|
|
|
}
|
2023-01-12 02:59:08 +01:00
|
|
|
},
|
|
|
|
|
event_statuses,
|
|
|
|
|
)
|
2019-08-23 05:53:54 +02:00
|
|
|
}
|
|
|
|
|
|
2019-08-29 00:58:42 +02:00
|
|
|
/// Draws the [`UserInterface`] with the provided [`Renderer`].
|
|
|
|
|
///
|
2022-04-30 14:20:52 +02:00
|
|
|
/// It returns the current [`mouse::Interaction`]. You should update the
|
|
|
|
|
/// icon of the mouse cursor accordingly in your system.
|
2019-08-29 00:58:42 +02:00
|
|
|
///
|
2023-09-09 12:24:47 +02:00
|
|
|
/// [`Renderer`]: crate::core::Renderer
|
2019-08-29 00:58:42 +02:00
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
/// We can finally draw our [counter](index.html#usage) by
|
|
|
|
|
/// [completing the last example](#example-1):
|
|
|
|
|
///
|
|
|
|
|
/// ```no_run
|
|
|
|
|
/// # mod iced_wgpu {
|
2024-03-22 05:27:31 +01:00
|
|
|
/// # pub type Renderer = ();
|
2023-03-04 05:37:11 +01:00
|
|
|
/// # pub type Theme = ();
|
2019-08-29 00:58:42 +02:00
|
|
|
/// # }
|
|
|
|
|
/// #
|
|
|
|
|
/// # pub struct Counter;
|
|
|
|
|
/// #
|
|
|
|
|
/// # impl Counter {
|
|
|
|
|
/// # pub fn new() -> Self { Counter }
|
2024-01-21 17:56:01 +01:00
|
|
|
/// # pub fn view(&self) -> Element<(), (), Renderer> { unimplemented!() }
|
2023-03-04 05:37:11 +01:00
|
|
|
/// # pub fn update(&mut self, _: ()) {}
|
2019-08-29 00:58:42 +02:00
|
|
|
/// # }
|
2023-03-05 06:35:20 +01:00
|
|
|
/// use iced_runtime::core::clipboard;
|
2023-06-08 20:35:40 +02:00
|
|
|
/// use iced_runtime::core::mouse;
|
2023-03-05 06:35:20 +01:00
|
|
|
/// use iced_runtime::core::renderer;
|
2023-06-08 20:35:40 +02:00
|
|
|
/// use iced_runtime::core::{Element, Size};
|
2023-03-05 06:35:20 +01:00
|
|
|
/// use iced_runtime::user_interface::{self, UserInterface};
|
2023-03-04 05:37:11 +01:00
|
|
|
/// use iced_wgpu::{Renderer, Theme};
|
|
|
|
|
///
|
2019-08-29 00:58:42 +02:00
|
|
|
/// let mut counter = Counter::new();
|
2022-01-11 13:47:43 +07:00
|
|
|
/// let mut cache = user_interface::Cache::new();
|
2024-03-22 05:27:31 +01:00
|
|
|
/// let mut renderer = Renderer::default();
|
2020-01-10 03:10:58 +01:00
|
|
|
/// let mut window_size = Size::new(1024.0, 768.0);
|
2023-06-08 20:35:40 +02:00
|
|
|
/// let mut cursor = mouse::Cursor::default();
|
2021-03-10 01:59:02 +01:00
|
|
|
/// let mut clipboard = clipboard::Null;
|
2019-08-29 00:58:42 +02:00
|
|
|
/// let mut events = Vec::new();
|
2020-11-12 02:00:08 +01:00
|
|
|
/// let mut messages = Vec::new();
|
2023-03-04 05:37:11 +01:00
|
|
|
/// let mut theme = Theme::default();
|
2019-08-29 00:58:42 +02:00
|
|
|
///
|
|
|
|
|
/// loop {
|
2020-11-12 02:00:08 +01:00
|
|
|
/// // Obtain system events...
|
2019-08-29 00:58:42 +02:00
|
|
|
///
|
|
|
|
|
/// let mut user_interface = UserInterface::build(
|
|
|
|
|
/// counter.view(),
|
2020-01-10 03:10:58 +01:00
|
|
|
/// window_size,
|
2019-08-29 00:58:42 +02:00
|
|
|
/// cache,
|
2019-09-19 15:01:12 +02:00
|
|
|
/// &mut renderer,
|
2019-08-29 00:58:42 +02:00
|
|
|
/// );
|
|
|
|
|
///
|
2020-11-12 02:51:26 +01:00
|
|
|
/// // Update the user interface
|
|
|
|
|
/// let event_statuses = user_interface.update(
|
|
|
|
|
/// &events,
|
2023-06-08 20:35:40 +02:00
|
|
|
/// cursor,
|
2021-11-29 16:22:01 +07:00
|
|
|
/// &mut renderer,
|
2021-03-10 01:59:02 +01:00
|
|
|
/// &mut clipboard,
|
2020-11-12 02:51:26 +01:00
|
|
|
/// &mut messages
|
|
|
|
|
/// );
|
2019-08-29 00:58:42 +02:00
|
|
|
///
|
|
|
|
|
/// // Draw the user interface
|
2023-06-08 20:35:40 +02:00
|
|
|
/// let mouse_interaction = user_interface.draw(&mut renderer, &theme, &renderer::Style::default(), cursor);
|
2019-08-29 00:58:42 +02:00
|
|
|
///
|
|
|
|
|
/// cache = user_interface.into_cache();
|
|
|
|
|
///
|
2020-11-12 02:00:08 +01:00
|
|
|
/// for message in messages.drain(..) {
|
2019-08-29 00:58:42 +02:00
|
|
|
/// counter.update(message);
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// // Update mouse cursor icon...
|
|
|
|
|
/// // Flush rendering operations...
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
2021-10-18 16:43:18 +07:00
|
|
|
pub fn draw(
|
|
|
|
|
&mut self,
|
|
|
|
|
renderer: &mut Renderer,
|
2024-01-21 17:56:01 +01:00
|
|
|
theme: &Theme,
|
2022-07-08 20:07:33 +02:00
|
|
|
style: &renderer::Style,
|
2023-06-08 20:35:40 +02:00
|
|
|
cursor: mouse::Cursor,
|
2025-05-02 23:11:47 +02:00
|
|
|
) {
|
2020-08-18 03:37:32 +02:00
|
|
|
let viewport = Rectangle::with_size(self.bounds);
|
2025-08-17 00:58:37 +02:00
|
|
|
renderer.reset(viewport);
|
2020-08-18 03:37:32 +02:00
|
|
|
|
2025-05-02 23:11:47 +02:00
|
|
|
let base_cursor = match &self.overlay {
|
|
|
|
|
None
|
|
|
|
|
| Some(Overlay {
|
|
|
|
|
interaction: mouse::Interaction::None,
|
|
|
|
|
..
|
|
|
|
|
}) => cursor,
|
|
|
|
|
_ => mouse::Cursor::Unavailable,
|
2021-10-28 16:43:16 +07:00
|
|
|
};
|
2021-10-18 16:43:18 +07:00
|
|
|
|
2022-07-27 06:49:20 +02:00
|
|
|
self.root.as_widget().draw(
|
|
|
|
|
&self.state,
|
2022-03-01 14:02:46 +07:00
|
|
|
renderer,
|
2022-05-14 01:47:55 +02:00
|
|
|
theme,
|
2022-07-08 20:07:33 +02:00
|
|
|
style,
|
2022-03-01 14:02:46 +07:00
|
|
|
Layout::new(&self.base),
|
|
|
|
|
base_cursor,
|
|
|
|
|
&viewport,
|
|
|
|
|
);
|
|
|
|
|
|
2021-10-28 16:43:16 +07:00
|
|
|
let Self {
|
|
|
|
|
overlay,
|
|
|
|
|
root,
|
|
|
|
|
base,
|
|
|
|
|
..
|
|
|
|
|
} = self;
|
|
|
|
|
|
2025-05-02 23:11:47 +02:00
|
|
|
let Some(Overlay { layout, .. }) = overlay.as_ref() else {
|
|
|
|
|
return;
|
|
|
|
|
};
|
2022-07-27 06:49:20 +02:00
|
|
|
|
2025-05-02 23:11:47 +02:00
|
|
|
let overlay = root
|
|
|
|
|
.as_widget_mut()
|
|
|
|
|
.overlay(
|
|
|
|
|
&mut self.state,
|
|
|
|
|
Layout::new(base),
|
|
|
|
|
renderer,
|
|
|
|
|
&viewport,
|
|
|
|
|
Vector::ZERO,
|
|
|
|
|
)
|
|
|
|
|
.map(overlay::Nested::new);
|
|
|
|
|
|
|
|
|
|
if let Some(mut overlay) = overlay {
|
|
|
|
|
overlay.draw(renderer, theme, style, Layout::new(layout), cursor);
|
|
|
|
|
}
|
2019-08-23 05:53:54 +02:00
|
|
|
}
|
|
|
|
|
|
2022-07-28 02:46:51 +02:00
|
|
|
/// Applies a [`widget::Operation`] to the [`UserInterface`].
|
|
|
|
|
pub fn operate(
|
|
|
|
|
&mut self,
|
|
|
|
|
renderer: &Renderer,
|
2024-08-08 01:25:00 +02:00
|
|
|
operation: &mut dyn widget::Operation,
|
2022-07-28 02:46:51 +02:00
|
|
|
) {
|
2025-04-26 02:51:32 +02:00
|
|
|
let viewport = Rectangle::with_size(self.bounds);
|
|
|
|
|
|
2025-08-20 22:42:15 +02:00
|
|
|
self.root.as_widget_mut().operate(
|
2022-07-28 03:53:47 +02:00
|
|
|
&mut self.state,
|
|
|
|
|
Layout::new(&self.base),
|
2022-12-22 14:29:24 +01:00
|
|
|
renderer,
|
2022-07-28 03:53:47 +02:00
|
|
|
operation,
|
|
|
|
|
);
|
2022-07-28 02:46:51 +02:00
|
|
|
|
2023-02-18 14:31:38 -08:00
|
|
|
if let Some(mut overlay) = self
|
|
|
|
|
.root
|
|
|
|
|
.as_widget_mut()
|
2024-02-01 01:08:21 +01:00
|
|
|
.overlay(
|
|
|
|
|
&mut self.state,
|
|
|
|
|
Layout::new(&self.base),
|
|
|
|
|
renderer,
|
2025-04-26 02:51:32 +02:00
|
|
|
&viewport,
|
2024-02-01 01:08:21 +01:00
|
|
|
Vector::ZERO,
|
|
|
|
|
)
|
2023-02-18 14:31:38 -08:00
|
|
|
.map(overlay::Nested::new)
|
|
|
|
|
{
|
2022-11-18 12:09:18 -08:00
|
|
|
if self.overlay.is_none() {
|
2025-05-02 23:11:47 +02:00
|
|
|
self.overlay = Some(Overlay {
|
|
|
|
|
layout: overlay.layout(renderer, self.bounds),
|
|
|
|
|
interaction: mouse::Interaction::None,
|
|
|
|
|
});
|
2022-07-28 02:46:51 +02:00
|
|
|
}
|
2022-11-18 12:09:18 -08:00
|
|
|
|
|
|
|
|
overlay.operate(
|
2025-05-02 23:11:47 +02:00
|
|
|
Layout::new(&self.overlay.as_ref().unwrap().layout),
|
2022-12-22 14:29:24 +01:00
|
|
|
renderer,
|
2022-11-18 12:09:18 -08:00
|
|
|
operation,
|
|
|
|
|
);
|
2022-07-28 02:46:51 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-05 02:11:11 +01:00
|
|
|
/// Relayouts and returns a new [`UserInterface`] using the provided
|
|
|
|
|
/// bounds.
|
|
|
|
|
pub fn relayout(self, bounds: Size, renderer: &mut Renderer) -> Self {
|
2022-07-27 06:49:20 +02:00
|
|
|
Self::build(self.root, bounds, Cache { state: self.state }, renderer)
|
2020-11-05 02:11:11 +01:00
|
|
|
}
|
|
|
|
|
|
2019-08-29 00:58:42 +02:00
|
|
|
/// Extract the [`Cache`] of the [`UserInterface`], consuming it in the
|
|
|
|
|
/// process.
|
2019-08-23 05:53:54 +02:00
|
|
|
pub fn into_cache(self) -> Cache {
|
2022-07-27 06:49:20 +02:00
|
|
|
Cache { state: self.state }
|
2020-07-08 08:09:14 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-29 03:33:02 +02:00
|
|
|
/// Reusable data of a specific [`UserInterface`].
|
2022-07-27 06:49:20 +02:00
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub struct Cache {
|
|
|
|
|
state: widget::Tree,
|
|
|
|
|
}
|
2019-08-23 05:53:54 +02:00
|
|
|
|
|
|
|
|
impl Cache {
|
2019-08-29 03:33:02 +02:00
|
|
|
/// Creates an empty [`Cache`].
|
|
|
|
|
///
|
|
|
|
|
/// You should use this to initialize a [`Cache`] before building your first
|
|
|
|
|
/// [`UserInterface`].
|
2019-08-23 05:53:54 +02:00
|
|
|
pub fn new() -> Cache {
|
2022-07-27 06:49:20 +02:00
|
|
|
Cache {
|
|
|
|
|
state: widget::Tree::empty(),
|
|
|
|
|
}
|
2019-08-23 05:53:54 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for Cache {
|
|
|
|
|
fn default() -> Cache {
|
|
|
|
|
Cache::new()
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-11 13:47:43 +07:00
|
|
|
|
2022-01-12 11:22:57 +07:00
|
|
|
/// The resulting state after updating a [`UserInterface`].
|
2025-02-02 20:45:29 +01:00
|
|
|
#[derive(Debug, Clone)]
|
2022-01-11 13:47:43 +07:00
|
|
|
pub enum State {
|
2022-01-12 11:22:57 +07:00
|
|
|
/// The [`UserInterface`] is outdated and needs to be rebuilt.
|
2022-01-11 13:47:43 +07:00
|
|
|
Outdated,
|
2022-01-12 11:22:57 +07:00
|
|
|
|
|
|
|
|
/// The [`UserInterface`] is up-to-date and can be reused without
|
|
|
|
|
/// rebuilding.
|
2023-01-12 02:59:08 +01:00
|
|
|
Updated {
|
2025-05-02 23:11:47 +02:00
|
|
|
/// The current [`mouse::Interaction`] of the user interface.
|
|
|
|
|
mouse_interaction: mouse::Interaction,
|
2025-02-02 20:45:29 +01:00
|
|
|
/// The [`window::RedrawRequest`] describing when a redraw should be performed.
|
|
|
|
|
redraw_request: window::RedrawRequest,
|
|
|
|
|
/// The current [`InputMethod`] strategy of the user interface.
|
|
|
|
|
input_method: InputMethod,
|
2025-10-15 19:08:39 +02:00
|
|
|
/// Whether the layout of the [`UserInterface`] has changed.
|
|
|
|
|
has_layout_changed: bool,
|
2023-01-12 02:59:08 +01:00
|
|
|
},
|
2022-01-11 13:47:43 +07:00
|
|
|
}
|
2025-10-15 19:08:39 +02:00
|
|
|
|
|
|
|
|
impl State {
|
|
|
|
|
/// Returns whether the layout of the [`UserInterface`] has changed.
|
|
|
|
|
pub fn has_layout_changed(&self) -> bool {
|
|
|
|
|
match self {
|
|
|
|
|
State::Outdated => true,
|
|
|
|
|
State::Updated {
|
|
|
|
|
has_layout_changed, ..
|
|
|
|
|
} => *has_layout_changed,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|