diff options
author | 2020-06-23 06:44:34 +0200 | |
---|---|---|
committer | 2020-06-23 06:44:34 +0200 | |
commit | f30a666dc81fdc85d225dc83f1a33e32d5dccbd2 (patch) | |
tree | 579f3a0e8a050447c208282305c3de98f52f694e /native | |
parent | bbdf558bd7eb3abbf69c922b34075360cd5f12c5 (diff) | |
download | iced-f30a666dc81fdc85d225dc83f1a33e32d5dccbd2.tar.gz iced-f30a666dc81fdc85d225dc83f1a33e32d5dccbd2.tar.bz2 iced-f30a666dc81fdc85d225dc83f1a33e32d5dccbd2.zip |
Decouple `cursor_position` from `Cache`
Instead, we ask explicitly for it in the different `update` and `draw` methods.
This way, the runtime can derive the logical position of the cursor from
the source of truth.
Diffstat (limited to 'native')
-rw-r--r-- | native/src/program/state.rs | 13 | ||||
-rw-r--r-- | native/src/user_interface.rs | 46 |
2 files changed, 35 insertions, 24 deletions
diff --git a/native/src/program/state.rs b/native/src/program/state.rs index bb428198..fdc42e8b 100644 --- a/native/src/program/state.rs +++ b/native/src/program/state.rs @@ -1,5 +1,5 @@ use crate::{ - Cache, Clipboard, Command, Debug, Event, Program, Renderer, Size, + Cache, Clipboard, Command, Debug, Event, Point, Program, Renderer, Size, UserInterface, }; @@ -31,6 +31,7 @@ where pub fn new( mut program: P, bounds: Size, + cursor_position: Point, renderer: &mut P::Renderer, debug: &mut Debug, ) -> Self { @@ -43,7 +44,7 @@ where ); debug.draw_started(); - let primitive = user_interface.draw(renderer); + let primitive = user_interface.draw(renderer, cursor_position); debug.draw_finished(); let cache = Some(user_interface.into_cache()); @@ -104,8 +105,9 @@ where /// [`Program`]: trait.Program.html pub fn update( &mut self, - clipboard: Option<&dyn Clipboard>, bounds: Size, + cursor_position: Point, + clipboard: Option<&dyn Clipboard>, renderer: &mut P::Renderer, debug: &mut Debug, ) -> Option<Command<P::Message>> { @@ -120,6 +122,7 @@ where debug.event_processing_started(); let mut messages = user_interface.update( self.queued_events.drain(..), + cursor_position, clipboard, renderer, ); @@ -128,7 +131,7 @@ where if messages.is_empty() { debug.draw_started(); - self.primitive = user_interface.draw(renderer); + self.primitive = user_interface.draw(renderer, cursor_position); debug.draw_finished(); self.cache = Some(user_interface.into_cache()); @@ -159,7 +162,7 @@ where ); debug.draw_started(); - self.primitive = user_interface.draw(renderer); + self.primitive = user_interface.draw(renderer, cursor_position); debug.draw_finished(); self.cache = Some(user_interface.into_cache()); diff --git a/native/src/user_interface.rs b/native/src/user_interface.rs index e963b601..b9646043 100644 --- a/native/src/user_interface.rs +++ b/native/src/user_interface.rs @@ -1,4 +1,4 @@ -use crate::{layout, mouse, Clipboard, Element, Event, Layout, Point, Size}; +use crate::{layout, Clipboard, Element, Event, Layout, Point, Size}; use std::hash::Hasher; @@ -23,7 +23,6 @@ pub struct UserInterface<'a, Message, Renderer> { root: Element<'a, Message, Renderer>, layout: layout::Node, bounds: Size, - cursor_position: Point, } impl<'a, Message, Renderer> UserInterface<'a, Message, Renderer> @@ -115,7 +114,6 @@ where root, layout, bounds, - cursor_position: cache.cursor_position, } } @@ -132,7 +130,7 @@ where /// completing [the previous example](#example): /// /// ```no_run - /// use iced_native::{UserInterface, Cache, Size}; + /// use iced_native::{UserInterface, Cache, Size, Point}; /// use iced_wgpu::Renderer; /// /// # mod iced_wgpu { @@ -154,6 +152,7 @@ where /// let mut cache = Cache::new(); /// let mut renderer = Renderer::new(); /// let mut window_size = Size::new(1024.0, 768.0); + /// let mut cursor_position = Point::default(); /// /// // Initialize our event storage /// let mut events = Vec::new(); @@ -169,7 +168,12 @@ where /// ); /// /// // Update the user interface - /// let messages = user_interface.update(events.drain(..), None, &renderer); + /// let messages = user_interface.update( + /// events.drain(..), + /// cursor_position, + /// None, + /// &renderer, + /// ); /// /// cache = user_interface.into_cache(); /// @@ -182,20 +186,17 @@ where pub fn update( &mut self, events: impl IntoIterator<Item = Event>, + cursor_position: Point, clipboard: Option<&dyn Clipboard>, renderer: &Renderer, ) -> Vec<Message> { let mut messages = Vec::new(); for event in events { - if let Event::Mouse(mouse::Event::CursorMoved { x, y }) = event { - self.cursor_position = Point::new(x, y); - } - self.root.widget.on_event( event, Layout::new(&self.layout), - self.cursor_position, + cursor_position, &mut messages, renderer, clipboard, @@ -219,7 +220,7 @@ where /// [completing the last example](#example-1): /// /// ```no_run - /// use iced_native::{UserInterface, Cache, Size}; + /// use iced_native::{UserInterface, Cache, Size, Point}; /// use iced_wgpu::Renderer; /// /// # mod iced_wgpu { @@ -241,6 +242,7 @@ where /// let mut cache = 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 events = Vec::new(); /// /// loop { @@ -253,10 +255,15 @@ where /// &mut renderer, /// ); /// - /// let messages = user_interface.update(events.drain(..), None, &renderer); + /// let messages = user_interface.update( + /// events.drain(..), + /// cursor_position, + /// None, + /// &renderer, + /// ); /// /// // Draw the user interface - /// let mouse_cursor = user_interface.draw(&mut renderer); + /// let mouse_cursor = user_interface.draw(&mut renderer, cursor_position); /// /// cache = user_interface.into_cache(); /// @@ -268,12 +275,16 @@ where /// // Flush rendering operations... /// } /// ``` - pub fn draw(&self, renderer: &mut Renderer) -> Renderer::Output { + pub fn draw( + &self, + renderer: &mut Renderer, + cursor_position: Point, + ) -> Renderer::Output { self.root.widget.draw( renderer, &Renderer::Defaults::default(), Layout::new(&self.layout), - self.cursor_position, + cursor_position, ) } @@ -287,7 +298,6 @@ where hash: self.hash, layout: self.layout, bounds: self.bounds, - cursor_position: self.cursor_position, } } } @@ -300,7 +310,6 @@ pub struct Cache { hash: u64, layout: layout::Node, bounds: Size, - cursor_position: Point, } impl Cache { @@ -316,7 +325,6 @@ impl Cache { hash: 0, layout: layout::Node::new(Size::new(0.0, 0.0)), bounds: Size::ZERO, - cursor_position: Point::new(-1.0, -1.0), } } } @@ -329,7 +337,7 @@ impl Default for Cache { impl PartialEq for Cache { fn eq(&self, other: &Cache) -> bool { - self.hash == other.hash && self.cursor_position == other.cursor_position + self.hash == other.hash } } |