Skip to content

Commit 3dc6014

Browse files
authored
Merge pull request #2755 from iced-rs/feature/image-scale
`scale` support for `image` widget
2 parents 24905eb + cdcfdb2 commit 3dc6014

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

widget/src/image.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ pub struct Image<Handle = image::Handle> {
6363
filter_method: FilterMethod,
6464
rotation: Rotation,
6565
opacity: f32,
66+
scale: f32,
6667
}
6768

6869
impl<Handle> Image<Handle> {
@@ -76,6 +77,7 @@ impl<Handle> Image<Handle> {
7677
filter_method: FilterMethod::default(),
7778
rotation: Rotation::default(),
7879
opacity: 1.0,
80+
scale: 1.0,
7981
}
8082
}
8183

@@ -119,6 +121,15 @@ impl<Handle> Image<Handle> {
119121
self.opacity = opacity.into();
120122
self
121123
}
124+
125+
/// Sets the scale of the [`Image`].
126+
///
127+
/// The region of the [`Image`] drawn will be scaled from the center by the given scale factor.
128+
/// This can be useful to create certain effects and animations, like smooth zoom in / out.
129+
pub fn scale(mut self, scale: impl Into<f32>) -> Self {
130+
self.scale = scale.into();
131+
self
132+
}
122133
}
123134

124135
/// Computes the layout of an [`Image`].
@@ -173,6 +184,7 @@ pub fn draw<Renderer, Handle>(
173184
filter_method: FilterMethod,
174185
rotation: Rotation,
175186
opacity: f32,
187+
scale: f32,
176188
) where
177189
Renderer: image::Renderer<Handle = Handle>,
178190
Handle: Clone,
@@ -184,12 +196,12 @@ pub fn draw<Renderer, Handle>(
184196
let bounds = layout.bounds();
185197
let adjusted_fit = content_fit.fit(rotated_size, bounds.size());
186198

187-
let scale = Vector::new(
199+
let fit_scale = Vector::new(
188200
adjusted_fit.width / rotated_size.width,
189201
adjusted_fit.height / rotated_size.height,
190202
);
191203

192-
let final_size = image_size * scale;
204+
let final_size = image_size * fit_scale * scale;
193205

194206
let position = match content_fit {
195207
ContentFit::None => Point::new(
@@ -276,6 +288,7 @@ where
276288
self.filter_method,
277289
self.rotation,
278290
self.opacity,
291+
self.scale,
279292
);
280293
}
281294
}

0 commit comments

Comments
 (0)