summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2025-02-09 08:12:04 +0100
committerLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2025-02-09 08:12:04 +0100
commit80ccbb835774cbd462076b35464514682d13459e (patch)
treee5ccd3cf1e2decb3fe15f6fe53ffe0a2142c2a8a /examples
parentec4d007a4cafa005a61aa24a47960e5d639b5587 (diff)
downloadiced-80ccbb835774cbd462076b35464514682d13459e.tar.gz
iced-80ccbb835774cbd462076b35464514682d13459e.tar.bz2
iced-80ccbb835774cbd462076b35464514682d13459e.zip
Create explicit `Thumbnail` struct in `gallery` example
Diffstat (limited to 'examples')
-rw-r--r--examples/gallery/src/main.rs90
1 files changed, 45 insertions, 45 deletions
diff --git a/examples/gallery/src/main.rs b/examples/gallery/src/main.rs
index 1b28f286..4728129e 100644
--- a/examples/gallery/src/main.rs
+++ b/examples/gallery/src/main.rs
@@ -202,23 +202,18 @@ fn card<'a>(
now: Instant,
) -> Element<'a, Message> {
let image = if let Some(preview) = preview {
- let thumbnail: Element<'_, _> = if let Preview::Ready {
- thumbnail,
- fade_in,
- zoom,
- ..
- } = &preview
- {
- image(thumbnail)
- .width(Fill)
- .height(Fill)
- .content_fit(ContentFit::Cover)
- .opacity(fade_in.interpolate(0.0, 1.0, now))
- .scale(zoom.interpolate(1.0, 1.1, now))
- .into()
- } else {
- horizontal_space().into()
- };
+ let thumbnail: Element<'_, _> =
+ if let Preview::Ready { thumbnail, .. } = &preview {
+ image(&thumbnail.handle)
+ .width(Fill)
+ .height(Fill)
+ .content_fit(ContentFit::Cover)
+ .opacity(thumbnail.fade_in.interpolate(0.0, 1.0, now))
+ .scale(thumbnail.zoom.interpolate(1.0, 1.1, now))
+ .into()
+ } else {
+ horizontal_space().into()
+ };
if let Some(blurhash) = preview.blurhash(now) {
let blurhash = image(&blurhash.handle)
@@ -273,9 +268,7 @@ enum Preview {
},
Ready {
blurhash: Option<Blurhash>,
- thumbnail: image::Handle,
- fade_in: Animation<bool>,
- zoom: Animation<bool>,
+ thumbnail: Thumbnail,
},
}
@@ -284,6 +277,12 @@ struct Blurhash {
fade_in: Animation<bool>,
}
+struct Thumbnail {
+ handle: image::Handle,
+ fade_in: Animation<bool>,
+ zoom: Animation<bool>,
+}
+
impl Preview {
const WIDTH: u32 = 320;
const HEIGHT: u32 = 410;
@@ -304,15 +303,7 @@ impl Preview {
fn ready(rgba: Rgba) -> Self {
Self::Ready {
blurhash: None,
- thumbnail: image::Handle::from_rgba(
- rgba.width,
- rgba.height,
- rgba.pixels,
- ),
- fade_in: Animation::new(false).slow().go(true),
- zoom: Animation::new(false)
- .quick()
- .easing(animation::Easing::EaseInOut),
+ thumbnail: Thumbnail::new(rgba),
}
}
@@ -323,29 +314,22 @@ impl Preview {
Self::Ready {
blurhash: Some(blurhash),
- thumbnail: image::Handle::from_rgba(
- rgba.width,
- rgba.height,
- rgba.pixels,
- ),
- fade_in: Animation::new(false).slow().go(true),
- zoom: Animation::new(false)
- .quick()
- .easing(animation::Easing::EaseInOut),
+ thumbnail: Thumbnail::new(rgba),
}
}
fn toggle_zoom(&mut self, enabled: bool) {
- if let Self::Ready { zoom, .. } = self {
- zoom.go_mut(enabled);
+ if let Self::Ready { thumbnail, .. } = self {
+ thumbnail.zoom.go_mut(enabled);
}
}
fn is_animating(&self, now: Instant) -> bool {
match &self {
Self::Loading { blurhash } => blurhash.fade_in.is_animating(now),
- Self::Ready { fade_in, zoom, .. } => {
- fade_in.is_animating(now) || zoom.is_animating(now)
+ Self::Ready { thumbnail, .. } => {
+ thumbnail.fade_in.is_animating(now)
+ || thumbnail.zoom.is_animating(now)
}
}
}
@@ -355,10 +339,26 @@ impl Preview {
Self::Loading { blurhash, .. } => Some(blurhash),
Self::Ready {
blurhash: Some(blurhash),
- fade_in,
+ thumbnail,
..
- } if fade_in.is_animating(now) => Some(blurhash),
- _ => None,
+ } if thumbnail.fade_in.is_animating(now) => Some(blurhash),
+ Self::Ready { .. } => None,
+ }
+ }
+}
+
+impl Thumbnail {
+ pub fn new(rgba: Rgba) -> Self {
+ Self {
+ handle: image::Handle::from_rgba(
+ rgba.width,
+ rgba.height,
+ rgba.pixels,
+ ),
+ fade_in: Animation::new(false).slow().go(true),
+ zoom: Animation::new(false)
+ .quick()
+ .easing(animation::Easing::EaseInOut),
}
}
}