fix(animated-image): update frames and fix compilation errors

This commit is contained in:
Hojjat 2026-04-09 18:32:14 -06:00
parent 6caccaba33
commit 64e2cbbeab

View file

@ -14,10 +14,10 @@ use iced_core::image::Renderer as ImageRenderer;
use iced_core::mouse::Cursor; use iced_core::mouse::Cursor;
use iced_core::widget::{Tree, tree}; use iced_core::widget::{Tree, tree};
use iced_core::{ use iced_core::{
Clipboard, ContentFit, Element, Event, Layout, Length, Rectangle, Shell, Size, Vector, Widget, Clipboard, ContentFit, Element, Event, Layout, Length, Rectangle, Rotation, Shell, Size,
event, layout, renderer, window, Widget, event, layout, renderer, window,
}; };
use iced_widget::image::{self, Handle}; use iced_widget::image::{self, FilterMethod, Handle};
use image_rs::AnimationDecoder; use image_rs::AnimationDecoder;
use image_rs::codecs::gif::GifDecoder; use image_rs::codecs::gif::GifDecoder;
use image_rs::codecs::png::PngDecoder; use image_rs::codecs::png::PngDecoder;
@ -146,7 +146,7 @@ impl Frames {
match image_type { match image_type {
ImageType::Gif => Self::from_decoder(GifDecoder::new(io::Cursor::new(bytes))?), ImageType::Gif => Self::from_decoder(GifDecoder::new(io::Cursor::new(bytes))?),
ImageType::Apng => Self::from_decoder(PngDecoder::new(io::Cursor::new(bytes))?.apng()), ImageType::Apng => Self::from_decoder(PngDecoder::new(io::Cursor::new(bytes))?.apng()?),
ImageType::WebP => Self::from_decoder(WebPDecoder::new(io::Cursor::new(bytes))?), ImageType::WebP => Self::from_decoder(WebPDecoder::new(io::Cursor::new(bytes))?),
} }
} }
@ -168,10 +168,10 @@ impl Frames {
let first = frames.first().cloned().unwrap(); let first = frames.first().cloned().unwrap();
let total_bytes = frames let total_bytes = frames
.iter() .iter()
.map(|f| match f.handle.data() { .map(|f| match &f.handle {
iced_core::image::Handle::Path(..) => 0, Handle::Path(..) => 0,
iced_core::image::Handle::Bytes(_, b) => b.len(), Handle::Bytes(_, b) => b.len(),
iced_core::image::Handle::Rgba { pixels, .. } => pixels.len(), Handle::Rgba { pixels, .. } => pixels.len(),
}) })
.sum::<usize>() .sum::<usize>()
.try_into() .try_into()
@ -324,7 +324,11 @@ where
&self.frames.first.handle, &self.frames.first.handle,
self.width, self.width,
self.height, self.height,
None,
self.content_fit, self.content_fit,
Rotation::default(),
false,
[0.0; 4],
) )
} }
@ -371,37 +375,18 @@ where
) { ) {
let state = tree.state.downcast_ref::<State>(); let state = tree.state.downcast_ref::<State>();
// Pulled from iced_native::widget::<Image as Widget>::draw iced_widget::image::draw(
// renderer,
// TODO: export iced_native::widget::image::draw as standalone function layout,
{ &state.current.frame.handle,
let Size { width, height } = renderer.dimensions(&state.current.frame.handle); None,
let image_size = Size::new(width as f32, height as f32); iced_core::border::Radius::default(),
self.content_fit,
let bounds = layout.bounds(); FilterMethod::default(),
let adjusted_fit = self.content_fit.fit(image_size, bounds.size()); Rotation::default(),
1.0,
let render = |renderer: &mut Renderer| { 1.0,
let offset = Vector::new( );
(bounds.width - adjusted_fit.width).max(0.0) / 2.0,
(bounds.height - adjusted_fit.height).max(0.0) / 2.0,
);
let drawing_bounds = Rectangle {
width: adjusted_fit.width,
height: adjusted_fit.height,
..bounds
};
renderer.draw(state.current.frame.handle.clone(), drawing_bounds + offset);
};
if adjusted_fit.width > bounds.width || adjusted_fit.height > bounds.height {
renderer.with_layer(bounds, render);
} else {
render(renderer);
}
}
} }
} }