Skip to content

Commit

Permalink
Add return values to Frame::with_clip() and Frame::with_save()
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalLebeda committed Aug 11, 2023
1 parent 50ce65b commit 8cd1848
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
16 changes: 12 additions & 4 deletions renderer/src/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<R>(&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,
Expand All @@ -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<R>(
&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()),
Expand All @@ -139,7 +145,7 @@ impl Frame {
}
};

f(&mut frame);
let result = f(&mut frame);

let origin = Point::new(region.x, region.y);

Expand All @@ -154,6 +160,8 @@ impl Frame {
#[allow(unreachable_patterns)]
_ => unreachable!(),
};

result
}

/// Applies a translation to the current transform of the [`Frame`].
Expand Down
16 changes: 12 additions & 4 deletions wgpu/src/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<R>(&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.
Expand All @@ -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<R>(
&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`].
Expand Down

0 comments on commit 8cd1848

Please sign in to comment.