Create explicit Thumbnail struct in gallery example
This commit is contained in:
parent
ec4d007a4c
commit
80ccbb8357
2 changed files with 46 additions and 45 deletions
|
|
@ -200,6 +200,7 @@ unused_results = "deny"
|
||||||
|
|
||||||
[workspace.lints.clippy]
|
[workspace.lints.clippy]
|
||||||
type-complexity = "allow"
|
type-complexity = "allow"
|
||||||
|
map-entry = "allow"
|
||||||
semicolon_if_nothing_returned = "deny"
|
semicolon_if_nothing_returned = "deny"
|
||||||
trivially-copy-pass-by-ref = "deny"
|
trivially-copy-pass-by-ref = "deny"
|
||||||
default_trait_access = "deny"
|
default_trait_access = "deny"
|
||||||
|
|
|
||||||
|
|
@ -202,19 +202,14 @@ fn card<'a>(
|
||||||
now: Instant,
|
now: Instant,
|
||||||
) -> Element<'a, Message> {
|
) -> Element<'a, Message> {
|
||||||
let image = if let Some(preview) = preview {
|
let image = if let Some(preview) = preview {
|
||||||
let thumbnail: Element<'_, _> = if let Preview::Ready {
|
let thumbnail: Element<'_, _> =
|
||||||
thumbnail,
|
if let Preview::Ready { thumbnail, .. } = &preview {
|
||||||
fade_in,
|
image(&thumbnail.handle)
|
||||||
zoom,
|
|
||||||
..
|
|
||||||
} = &preview
|
|
||||||
{
|
|
||||||
image(thumbnail)
|
|
||||||
.width(Fill)
|
.width(Fill)
|
||||||
.height(Fill)
|
.height(Fill)
|
||||||
.content_fit(ContentFit::Cover)
|
.content_fit(ContentFit::Cover)
|
||||||
.opacity(fade_in.interpolate(0.0, 1.0, now))
|
.opacity(thumbnail.fade_in.interpolate(0.0, 1.0, now))
|
||||||
.scale(zoom.interpolate(1.0, 1.1, now))
|
.scale(thumbnail.zoom.interpolate(1.0, 1.1, now))
|
||||||
.into()
|
.into()
|
||||||
} else {
|
} else {
|
||||||
horizontal_space().into()
|
horizontal_space().into()
|
||||||
|
|
@ -273,9 +268,7 @@ enum Preview {
|
||||||
},
|
},
|
||||||
Ready {
|
Ready {
|
||||||
blurhash: Option<Blurhash>,
|
blurhash: Option<Blurhash>,
|
||||||
thumbnail: image::Handle,
|
thumbnail: Thumbnail,
|
||||||
fade_in: Animation<bool>,
|
|
||||||
zoom: Animation<bool>,
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -284,6 +277,12 @@ struct Blurhash {
|
||||||
fade_in: Animation<bool>,
|
fade_in: Animation<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct Thumbnail {
|
||||||
|
handle: image::Handle,
|
||||||
|
fade_in: Animation<bool>,
|
||||||
|
zoom: Animation<bool>,
|
||||||
|
}
|
||||||
|
|
||||||
impl Preview {
|
impl Preview {
|
||||||
const WIDTH: u32 = 320;
|
const WIDTH: u32 = 320;
|
||||||
const HEIGHT: u32 = 410;
|
const HEIGHT: u32 = 410;
|
||||||
|
|
@ -304,15 +303,7 @@ impl Preview {
|
||||||
fn ready(rgba: Rgba) -> Self {
|
fn ready(rgba: Rgba) -> Self {
|
||||||
Self::Ready {
|
Self::Ready {
|
||||||
blurhash: None,
|
blurhash: None,
|
||||||
thumbnail: image::Handle::from_rgba(
|
thumbnail: Thumbnail::new(rgba),
|
||||||
rgba.width,
|
|
||||||
rgba.height,
|
|
||||||
rgba.pixels,
|
|
||||||
),
|
|
||||||
fade_in: Animation::new(false).slow().go(true),
|
|
||||||
zoom: Animation::new(false)
|
|
||||||
.quick()
|
|
||||||
.easing(animation::Easing::EaseInOut),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -323,29 +314,22 @@ impl Preview {
|
||||||
|
|
||||||
Self::Ready {
|
Self::Ready {
|
||||||
blurhash: Some(blurhash),
|
blurhash: Some(blurhash),
|
||||||
thumbnail: image::Handle::from_rgba(
|
thumbnail: Thumbnail::new(rgba),
|
||||||
rgba.width,
|
|
||||||
rgba.height,
|
|
||||||
rgba.pixels,
|
|
||||||
),
|
|
||||||
fade_in: Animation::new(false).slow().go(true),
|
|
||||||
zoom: Animation::new(false)
|
|
||||||
.quick()
|
|
||||||
.easing(animation::Easing::EaseInOut),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn toggle_zoom(&mut self, enabled: bool) {
|
fn toggle_zoom(&mut self, enabled: bool) {
|
||||||
if let Self::Ready { zoom, .. } = self {
|
if let Self::Ready { thumbnail, .. } = self {
|
||||||
zoom.go_mut(enabled);
|
thumbnail.zoom.go_mut(enabled);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_animating(&self, now: Instant) -> bool {
|
fn is_animating(&self, now: Instant) -> bool {
|
||||||
match &self {
|
match &self {
|
||||||
Self::Loading { blurhash } => blurhash.fade_in.is_animating(now),
|
Self::Loading { blurhash } => blurhash.fade_in.is_animating(now),
|
||||||
Self::Ready { fade_in, zoom, .. } => {
|
Self::Ready { thumbnail, .. } => {
|
||||||
fade_in.is_animating(now) || zoom.is_animating(now)
|
thumbnail.fade_in.is_animating(now)
|
||||||
|
|| thumbnail.zoom.is_animating(now)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -355,10 +339,26 @@ impl Preview {
|
||||||
Self::Loading { blurhash, .. } => Some(blurhash),
|
Self::Loading { blurhash, .. } => Some(blurhash),
|
||||||
Self::Ready {
|
Self::Ready {
|
||||||
blurhash: Some(blurhash),
|
blurhash: Some(blurhash),
|
||||||
fade_in,
|
thumbnail,
|
||||||
..
|
..
|
||||||
} if fade_in.is_animating(now) => Some(blurhash),
|
} if thumbnail.fade_in.is_animating(now) => Some(blurhash),
|
||||||
_ => None,
|
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),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue