From 8cd18480bddbe6be82fbfe3af5a193b29242ff21 Mon Sep 17 00:00:00 2001 From: Michal Lebeda Date: Wed, 2 Aug 2023 14:35:53 +0200 Subject: [PATCH] Add return values to Frame::with_clip() and Frame::with_save() --- renderer/src/geometry.rs | 16 ++++++++++++---- wgpu/src/geometry.rs | 16 ++++++++++++---- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/renderer/src/geometry.rs b/renderer/src/geometry.rs index 04b5d9e671..06618ead59 100644 --- a/renderer/src/geometry.rs +++ b/renderer/src/geometry.rs @@ -113,12 +113,14 @@ impl Frame { /// This method is useful to compose transforms and perform drawing /// operations in different coordinate systems. #[inline] - pub fn with_save(&mut self, f: impl FnOnce(&mut Frame)) { + pub fn with_save(&mut self, f: impl FnOnce(&mut Frame) -> R) -> R { delegate!(self, frame, frame.push_transform()); - f(self); + let result = f(self); delegate!(self, frame, frame.pop_transform()); + + result } /// Executes the given drawing operations within a [`Rectangle`] region, @@ -128,7 +130,11 @@ impl Frame { /// This method is useful to perform drawing operations that need to be /// clipped. #[inline] - pub fn with_clip(&mut self, region: Rectangle, f: impl FnOnce(&mut Frame)) { + pub fn with_clip( + &mut self, + region: Rectangle, + f: impl FnOnce(&mut Frame) -> R, + ) -> R { let mut frame = match self { Self::TinySkia(_) => Self::TinySkia( iced_tiny_skia::geometry::Frame::new(region.size()), @@ -139,7 +145,7 @@ impl Frame { } }; - f(&mut frame); + let result = f(&mut frame); let origin = Point::new(region.x, region.y); @@ -154,6 +160,8 @@ impl Frame { #[allow(unreachable_patterns)] _ => unreachable!(), }; + + result } /// Applies a translation to the current transform of the [`Frame`]. diff --git a/wgpu/src/geometry.rs b/wgpu/src/geometry.rs index e421e0b05a..00421ad42a 100644 --- a/wgpu/src/geometry.rs +++ b/wgpu/src/geometry.rs @@ -355,12 +355,14 @@ impl Frame { /// This method is useful to compose transforms and perform drawing /// operations in different coordinate systems. #[inline] - pub fn with_save(&mut self, f: impl FnOnce(&mut Frame)) { + pub fn with_save(&mut self, f: impl FnOnce(&mut Frame) -> R) -> R { self.push_transform(); - f(self); + let result = f(self); self.pop_transform(); + + result } /// Pushes the current transform in the transform stack. @@ -380,14 +382,20 @@ impl Frame { /// This method is useful to perform drawing operations that need to be /// clipped. #[inline] - pub fn with_clip(&mut self, region: Rectangle, f: impl FnOnce(&mut Frame)) { + pub fn with_clip( + &mut self, + region: Rectangle, + f: impl FnOnce(&mut Frame) -> R, + ) -> R { let mut frame = Frame::new(region.size()); - f(&mut frame); + let result = f(&mut frame); let origin = Point::new(region.x, region.y); self.clip(frame, origin); + + result } /// Draws the clipped contents of the given [`Frame`] with origin at the given [`Point`].