From 61b96caf219009e8328572aeb8826fa2b7552d42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Sat, 26 Apr 2025 04:50:59 +0200 Subject: [PATCH] Reduce nesting in `Rectangle::offset` --- core/src/rectangle.rs | 36 ++++++++++++++++++------------------ examples/gallery/src/main.rs | 3 +-- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/core/src/rectangle.rs b/core/src/rectangle.rs index 5ffdafa1..1984394c 100644 --- a/core/src/rectangle.rs +++ b/core/src/rectangle.rs @@ -160,25 +160,25 @@ impl Rectangle { /// Computes the offset that must be applied to the [`Rectangle`] to be placed /// inside the given `container`. pub fn offset(&self, container: &Rectangle) -> Vector { - if let Some(intersection) = self.intersection(container) { - let left = intersection.x - self.x; - let top = intersection.y - self.y; + let Some(intersection) = self.intersection(container) else { + return Vector::ZERO; + }; - Vector::new( - if left > 0.0 { - left - } else { - intersection.x + intersection.width - self.x - self.width - }, - if top > 0.0 { - top - } else { - intersection.y + intersection.height - self.y - self.height - }, - ) - } else { - Vector::ZERO - } + let left = intersection.x - self.x; + let top = intersection.y - self.y; + + Vector::new( + if left > 0.0 { + left + } else { + intersection.x + intersection.width - self.x - self.width + }, + if top > 0.0 { + top + } else { + intersection.y + intersection.height - self.y - self.height + }, + ) } /// Returns true if the current [`Rectangle`] is completely within the given diff --git a/examples/gallery/src/main.rs b/examples/gallery/src/main.rs index 4b8af044..709771c1 100644 --- a/examples/gallery/src/main.rs +++ b/examples/gallery/src/main.rs @@ -211,8 +211,7 @@ fn card<'a>( .opacity(thumbnail.fade_in.interpolate(0.0, 1.0, now)) .scale(thumbnail.zoom.interpolate(1.0, 1.1, now)) .translate(move |bounds, viewport| { - let final_bounds = bounds.zoom(1.1); - final_bounds.offset(&viewport.shrink(10)) + bounds.zoom(1.1).offset(&viewport.shrink(10)) * thumbnail.zoom.interpolate(0.0, 1.0, now) }) .style(move |_theme| image::Style {