From cdcfdb2e4e8416f3ebb57d10201d326e76e73c59 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 27 Jan 2025 00:36:21 +0100 Subject: Implement `scale` support for `image` widget --- widget/src/image.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'widget') diff --git a/widget/src/image.rs b/widget/src/image.rs index f5a9c7f3..6c84ec92 100644 --- a/widget/src/image.rs +++ b/widget/src/image.rs @@ -63,6 +63,7 @@ pub struct Image { filter_method: FilterMethod, rotation: Rotation, opacity: f32, + scale: f32, } impl Image { @@ -76,6 +77,7 @@ impl Image { filter_method: FilterMethod::default(), rotation: Rotation::default(), opacity: 1.0, + scale: 1.0, } } @@ -119,6 +121,15 @@ impl Image { self.opacity = opacity.into(); self } + + /// Sets the scale of the [`Image`]. + /// + /// The region of the [`Image`] drawn will be scaled from the center by the given scale factor. + /// This can be useful to create certain effects and animations, like smooth zoom in / out. + pub fn scale(mut self, scale: impl Into) -> Self { + self.scale = scale.into(); + self + } } /// Computes the layout of an [`Image`]. @@ -173,6 +184,7 @@ pub fn draw( filter_method: FilterMethod, rotation: Rotation, opacity: f32, + scale: f32, ) where Renderer: image::Renderer, Handle: Clone, @@ -184,12 +196,12 @@ pub fn draw( let bounds = layout.bounds(); let adjusted_fit = content_fit.fit(rotated_size, bounds.size()); - let scale = Vector::new( + let fit_scale = Vector::new( adjusted_fit.width / rotated_size.width, adjusted_fit.height / rotated_size.height, ); - let final_size = image_size * scale; + let final_size = image_size * fit_scale * scale; let position = match content_fit { ContentFit::None => Point::new( @@ -276,6 +288,7 @@ where self.filter_method, self.rotation, self.opacity, + self.scale, ); } } -- cgit