summaryrefslogtreecommitdiffstats
path: root/native/src/user_interface.rs
diff options
context:
space:
mode:
Diffstat (limited to 'native/src/user_interface.rs')
-rw-r--r--native/src/user_interface.rs100
1 files changed, 34 insertions, 66 deletions
diff --git a/native/src/user_interface.rs b/native/src/user_interface.rs
index 9833c815..08914bed 100644
--- a/native/src/user_interface.rs
+++ b/native/src/user_interface.rs
@@ -1,4 +1,6 @@
-use crate::{input::mouse, layout, Element, Event, Layout, Point, Size};
+use crate::{
+ input::mouse, layout, Clipboard, Element, Event, Layout, Point, Size,
+};
use std::hash::Hasher;
@@ -15,6 +17,7 @@ pub struct UserInterface<'a, Message, Renderer> {
hash: u64,
root: Element<'a, Message, Renderer>,
layout: layout::Node,
+ bounds: Size,
cursor_position: Point,
}
@@ -37,28 +40,11 @@ where
/// is naive way to set up our application loop:
///
/// ```no_run
- /// use iced_native::{UserInterface, Cache};
+ /// use iced_native::{UserInterface, Cache, Size};
/// use iced_wgpu::Renderer;
///
/// # mod iced_wgpu {
- /// # pub struct Renderer;
- /// #
- /// # impl Renderer {
- /// # pub fn new() -> Self { Renderer }
- /// # }
- /// #
- /// # impl iced_native::Renderer for Renderer { type Output = (); }
- /// #
- /// # impl iced_native::column::Renderer for Renderer {
- /// # fn draw<Message>(
- /// # &mut self,
- /// # _children: &[iced_native::Element<'_, Message, Self>],
- /// # _layout: iced_native::Layout<'_>,
- /// # _cursor_position: iced_native::Point,
- /// # ) -> Self::Output {
- /// # ()
- /// # }
- /// # }
+ /// # pub use iced_native::renderer::Null as Renderer;
/// # }
/// #
/// # use iced_native::Column;
@@ -75,6 +61,7 @@ where
/// let mut counter = Counter::new();
/// let mut cache = Cache::new();
/// let mut renderer = Renderer::new();
+ /// let mut window_size = Size::new(1024.0, 768.0);
///
/// // Application loop
/// loop {
@@ -83,6 +70,7 @@ where
/// // Build the user interface
/// let user_interface = UserInterface::build(
/// counter.view(),
+ /// window_size,
/// cache,
/// &mut renderer,
/// );
@@ -96,26 +84,30 @@ where
/// ```
pub fn build<E: Into<Element<'a, Message, Renderer>>>(
root: E,
+ bounds: Size,
cache: Cache,
renderer: &mut Renderer,
) -> Self {
let root = root.into();
- let hasher = &mut crate::Hasher::default();
- root.hash_layout(hasher);
+ let hash = {
+ let hasher = &mut crate::Hasher::default();
+ root.hash_layout(hasher);
- let hash = hasher.finish();
+ hasher.finish()
+ };
- let layout = if hash == cache.hash {
+ let layout = if hash == cache.hash && bounds == cache.bounds {
cache.layout
} else {
- renderer.layout(&root)
+ renderer.layout(&root, &layout::Limits::new(Size::ZERO, bounds))
};
UserInterface {
hash,
root,
layout,
+ bounds,
cursor_position: cache.cursor_position,
}
}
@@ -133,28 +125,11 @@ where
/// completing [the previous example](#example):
///
/// ```no_run
- /// use iced_native::{UserInterface, Cache};
+ /// use iced_native::{UserInterface, Cache, Size};
/// use iced_wgpu::Renderer;
///
/// # mod iced_wgpu {
- /// # pub struct Renderer;
- /// #
- /// # impl Renderer {
- /// # pub fn new() -> Self { Renderer }
- /// # }
- /// #
- /// # impl iced_native::Renderer for Renderer { type Output = (); }
- /// #
- /// # impl iced_native::column::Renderer for Renderer {
- /// # fn draw<Message>(
- /// # &mut self,
- /// # _children: &[iced_native::Element<'_, Message, Self>],
- /// # _layout: iced_native::Layout<'_>,
- /// # _cursor_position: iced_native::Point,
- /// # ) -> Self::Output {
- /// # ()
- /// # }
- /// # }
+ /// # pub use iced_native::renderer::Null as Renderer;
/// # }
/// #
/// # use iced_native::Column;
@@ -171,6 +146,7 @@ where
/// let mut counter = Counter::new();
/// let mut cache = Cache::new();
/// let mut renderer = Renderer::new();
+ /// let mut window_size = Size::new(1024.0, 768.0);
///
/// // Initialize our event storage
/// let mut events = Vec::new();
@@ -180,12 +156,13 @@ where
///
/// let mut user_interface = UserInterface::build(
/// counter.view(),
+ /// window_size,
/// cache,
/// &mut renderer,
/// );
///
/// // Update the user interface
- /// let messages = user_interface.update(&renderer, events.drain(..));
+ /// let messages = user_interface.update(events.drain(..), None, &renderer);
///
/// cache = user_interface.into_cache();
///
@@ -197,8 +174,9 @@ where
/// ```
pub fn update(
&mut self,
+ events: impl IntoIterator<Item = Event>,
+ clipboard: Option<&dyn Clipboard>,
renderer: &Renderer,
- events: impl Iterator<Item = Event>,
) -> Vec<Message> {
let mut messages = Vec::new();
@@ -213,6 +191,7 @@ where
self.cursor_position,
&mut messages,
renderer,
+ clipboard,
);
}
@@ -233,28 +212,11 @@ where
/// [completing the last example](#example-1):
///
/// ```no_run
- /// use iced_native::{UserInterface, Cache};
+ /// use iced_native::{UserInterface, Cache, Size};
/// use iced_wgpu::Renderer;
///
/// # mod iced_wgpu {
- /// # pub struct Renderer;
- /// #
- /// # impl Renderer {
- /// # pub fn new() -> Self { Renderer }
- /// # }
- /// #
- /// # impl iced_native::Renderer for Renderer { type Output = (); }
- /// #
- /// # impl iced_native::column::Renderer for Renderer {
- /// # fn draw<Message>(
- /// # &mut self,
- /// # _children: &[iced_native::Element<'_, Message, Self>],
- /// # _layout: iced_native::Layout<'_>,
- /// # _cursor_position: iced_native::Point,
- /// # ) -> Self::Output {
- /// # ()
- /// # }
- /// # }
+ /// # pub use iced_native::renderer::Null as Renderer;
/// # }
/// #
/// # use iced_native::Column;
@@ -271,6 +233,7 @@ where
/// let mut counter = Counter::new();
/// let mut cache = Cache::new();
/// let mut renderer = Renderer::new();
+ /// let mut window_size = Size::new(1024.0, 768.0);
/// let mut events = Vec::new();
///
/// loop {
@@ -278,11 +241,12 @@ where
///
/// let mut user_interface = UserInterface::build(
/// counter.view(),
+ /// window_size,
/// cache,
/// &mut renderer,
/// );
///
- /// let messages = user_interface.update(&renderer, events.drain(..));
+ /// let messages = user_interface.update(events.drain(..), None, &renderer);
///
/// // Draw the user interface
/// let mouse_cursor = user_interface.draw(&mut renderer);
@@ -300,6 +264,7 @@ where
pub fn draw(&self, renderer: &mut Renderer) -> Renderer::Output {
self.root.widget.draw(
renderer,
+ &Renderer::Defaults::default(),
Layout::new(&self.layout),
self.cursor_position,
)
@@ -314,6 +279,7 @@ where
Cache {
hash: self.hash,
layout: self.layout,
+ bounds: self.bounds,
cursor_position: self.cursor_position,
}
}
@@ -326,6 +292,7 @@ where
pub struct Cache {
hash: u64,
layout: layout::Node,
+ bounds: Size,
cursor_position: Point,
}
@@ -341,6 +308,7 @@ impl Cache {
Cache {
hash: 0,
layout: layout::Node::new(Size::new(0.0, 0.0)),
+ bounds: Size::ZERO,
cursor_position: Point::new(-1.0, -1.0),
}
}