Reduce nesting in Rectangle::offset

This commit is contained in:
Héctor Ramón Jiménez 2025-04-26 04:50:59 +02:00
parent 00e4de88bf
commit 61b96caf21
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
2 changed files with 19 additions and 20 deletions

View file

@ -160,25 +160,25 @@ impl Rectangle<f32> {
/// Computes the offset that must be applied to the [`Rectangle`] to be placed /// Computes the offset that must be applied to the [`Rectangle`] to be placed
/// inside the given `container`. /// inside the given `container`.
pub fn offset(&self, container: &Rectangle) -> Vector { pub fn offset(&self, container: &Rectangle) -> Vector {
if let Some(intersection) = self.intersection(container) { let Some(intersection) = self.intersection(container) else {
let left = intersection.x - self.x; return Vector::ZERO;
let top = intersection.y - self.y; };
Vector::new( let left = intersection.x - self.x;
if left > 0.0 { let top = intersection.y - self.y;
left
} else { Vector::new(
intersection.x + intersection.width - self.x - self.width if left > 0.0 {
}, left
if top > 0.0 { } else {
top intersection.x + intersection.width - self.x - self.width
} else { },
intersection.y + intersection.height - self.y - self.height if top > 0.0 {
}, top
) } else {
} else { intersection.y + intersection.height - self.y - self.height
Vector::ZERO },
} )
} }
/// Returns true if the current [`Rectangle`] is completely within the given /// Returns true if the current [`Rectangle`] is completely within the given

View file

@ -211,8 +211,7 @@ fn card<'a>(
.opacity(thumbnail.fade_in.interpolate(0.0, 1.0, now)) .opacity(thumbnail.fade_in.interpolate(0.0, 1.0, now))
.scale(thumbnail.zoom.interpolate(1.0, 1.1, now)) .scale(thumbnail.zoom.interpolate(1.0, 1.1, now))
.translate(move |bounds, viewport| { .translate(move |bounds, viewport| {
let final_bounds = bounds.zoom(1.1); bounds.zoom(1.1).offset(&viewport.shrink(10))
final_bounds.offset(&viewport.shrink(10))
* thumbnail.zoom.interpolate(0.0, 1.0, now) * thumbnail.zoom.interpolate(0.0, 1.0, now)
}) })
.style(move |_theme| image::Style { .style(move |_theme| image::Style {