From 16b63031c15d3469c82a24936494e59c36d4d529 Mon Sep 17 00:00:00 2001 From: Cory Forsstrom Date: Wed, 23 Feb 2022 10:14:08 -0800 Subject: Add with_clip for canvas --- graphics/src/widget/canvas/frame.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/graphics/src/widget/canvas/frame.rs b/graphics/src/widget/canvas/frame.rs index 4873e7fb..01b27451 100644 --- a/graphics/src/widget/canvas/frame.rs +++ b/graphics/src/widget/canvas/frame.rs @@ -244,6 +244,30 @@ impl Frame { self.transforms.current = self.transforms.previous.pop().unwrap(); } + /// TODO... + #[inline] + pub fn with_clip( + &mut self, + translation: Vector, + size: Size, + f: impl FnOnce(&mut Frame), + ) { + let mut frame = Frame::new(self.size()); + frame.translate(translation); + + f(&mut frame); + + self.primitives.push(Primitive::Clip { + bounds: Rectangle { + x: translation.x, + y: translation.y, + width: size.width, + height: size.height, + }, + content: Box::new(frame.into_geometry().into_primitive()), + }); + } + /// Applies a translation to the current transform of the [`Frame`]. #[inline] pub fn translate(&mut self, translation: Vector) { -- cgit From f40da376ce1e7a508d8dde85697326b8a0eed373 Mon Sep 17 00:00:00 2001 From: Cory Forsstrom Date: Wed, 23 Feb 2022 11:36:41 -0800 Subject: Add doc comment --- graphics/src/widget/canvas/frame.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/graphics/src/widget/canvas/frame.rs b/graphics/src/widget/canvas/frame.rs index 01b27451..23db843d 100644 --- a/graphics/src/widget/canvas/frame.rs +++ b/graphics/src/widget/canvas/frame.rs @@ -244,7 +244,14 @@ impl Frame { self.transforms.current = self.transforms.previous.pop().unwrap(); } - /// TODO... + /// Stores the current transform of the [`Frame`] and executes the given + /// drawing operations within a clipped [`Rectange`] at translation / size, + /// restoring the transform afterwards. + /// + /// This method is userful to perform drawing operations that need to be + /// clipped. + /// + /// [`Rectange`]: crate::Rectangle #[inline] pub fn with_clip( &mut self, -- cgit From 644c1b70ca3d9e3695fa9a159e1a2882d80949b9 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 28 Feb 2022 18:15:39 +0700 Subject: Skip `Canvas::draw` if `bounds` have no logical pixels --- graphics/src/widget/canvas.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/graphics/src/widget/canvas.rs b/graphics/src/widget/canvas.rs index 157aa25c..65d7e37e 100644 --- a/graphics/src/widget/canvas.rs +++ b/graphics/src/widget/canvas.rs @@ -211,6 +211,11 @@ where use iced_native::Renderer as _; let bounds = layout.bounds(); + + if bounds.width < 1.0 || bounds.height < 1.0 { + return; + } + let translation = Vector::new(bounds.x, bounds.y); let cursor = Cursor::from_window_position(cursor_position); -- cgit From f85b421ae7a029ea34c91be90da1a20505d6cd4b Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 1 Mar 2022 14:02:46 +0700 Subject: Fix overlay flickering after `Application::update` --- native/src/user_interface.rs | 46 +++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/native/src/user_interface.rs b/native/src/user_interface.rs index 00015f8b..6fc6a479 100644 --- a/native/src/user_interface.rs +++ b/native/src/user_interface.rs @@ -345,30 +345,36 @@ where let viewport = Rectangle::with_size(self.bounds); - if let Some(layout) = &self.overlay { - let base_cursor = if layout.bounds().contains(cursor_position) { - Point::new(-1.0, -1.0) - } else { - cursor_position - }; + let base_cursor = if let Some(overlay) = + self.root.overlay(Layout::new(&self.base), renderer) + { + let overlay_layout = self + .overlay + .take() + .unwrap_or_else(|| overlay.layout(renderer, self.bounds)); - self.root.widget.draw( - renderer, - &renderer::Style::default(), - Layout::new(&self.base), - base_cursor, - &viewport, - ); + let new_cursor_position = + if overlay_layout.bounds().contains(cursor_position) { + Point::new(-1.0, -1.0) + } else { + cursor_position + }; + + self.overlay = Some(overlay_layout); + + new_cursor_position } else { - self.root.widget.draw( - renderer, - &renderer::Style::default(), - Layout::new(&self.base), - cursor_position, - &viewport, - ); + cursor_position }; + self.root.widget.draw( + renderer, + &renderer::Style::default(), + Layout::new(&self.base), + base_cursor, + &viewport, + ); + let base_interaction = self.root.widget.mouse_interaction( Layout::new(&self.base), cursor_position, -- cgit From 27e859e1537d62fd7a5f1f4fc4d760a7a39bc804 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 2 Mar 2022 20:54:24 +0700 Subject: Use `Rectangle` directly in `Frame::with_clip` --- graphics/src/widget/canvas/frame.rs | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/graphics/src/widget/canvas/frame.rs b/graphics/src/widget/canvas/frame.rs index 23db843d..08518f40 100644 --- a/graphics/src/widget/canvas/frame.rs +++ b/graphics/src/widget/canvas/frame.rs @@ -244,34 +244,24 @@ impl Frame { self.transforms.current = self.transforms.previous.pop().unwrap(); } - /// Stores the current transform of the [`Frame`] and executes the given - /// drawing operations within a clipped [`Rectange`] at translation / size, - /// restoring the transform afterwards. + /// Executes the given drawing operations within a [`Rectangle`] region, + /// clipping any geometry that overflows its bounds. Any transformations + /// performed are local to the provided closure. /// - /// This method is userful to perform drawing operations that need to be + /// This method is useful to perform drawing operations that need to be /// clipped. - /// - /// [`Rectange`]: crate::Rectangle #[inline] - pub fn with_clip( - &mut self, - translation: Vector, - size: Size, - f: impl FnOnce(&mut Frame), - ) { - let mut frame = Frame::new(self.size()); - frame.translate(translation); + pub fn with_clip(&mut self, region: Rectangle, f: impl FnOnce(&mut Frame)) { + let mut frame = Frame::new(region.size()); f(&mut frame); self.primitives.push(Primitive::Clip { - bounds: Rectangle { - x: translation.x, - y: translation.y, - width: size.width, - height: size.height, - }, - content: Box::new(frame.into_geometry().into_primitive()), + bounds: region, + content: Box::new(Primitive::Translate { + translation: Vector::new(region.x, region.y), + content: Box::new(frame.into_geometry().into_primitive()), + }), }); } -- cgit