notedeck

One damus client to rule them all
git clone git://jb55.com/notedeck
Log | Files | Refs | README | LICENSE

commit faec75e1b6b042ed30eee8a17318ab115830301e
parent a4ec0982d2f4b6094b3972d9f480142fd76d5e75
Author: kernelkind <kernelkind@gmail.com>
Date:   Tue, 29 Apr 2025 10:37:47 -0400

images: move fetch to fn

Signed-off-by: kernelkind <kernelkind@gmail.com>

Diffstat:
Mcrates/notedeck_ui/src/images.rs | 58+++++++++++++++++++++++++++++++++-------------------------
1 file changed, 33 insertions(+), 25 deletions(-)

diff --git a/crates/notedeck_ui/src/images.rs b/crates/notedeck_ui/src/images.rs @@ -183,32 +183,40 @@ fn fetch_img_from_disk( let url = url.to_owned(); let path = path.to_owned(); - Promise::spawn_async(async move { - match cache_type { - MediaCacheType::Image => { - let data = fs::read(path).await?; - let image_buffer = - image::load_from_memory(&data).map_err(notedeck::Error::Image)?; - - let img = buffer_to_color_image( - image_buffer.as_flat_samples_u8(), - image_buffer.width(), - image_buffer.height(), - ); - Ok(TexturedImage::Static(ctx.load_texture( - &url, - img, - Default::default(), - ))) - } - MediaCacheType::Gif => { - let gif_bytes = fs::read(path.clone()).await?; // Read entire file into a Vec<u8> - generate_gif(ctx, url, &path, gif_bytes, false, |i| { - buffer_to_color_image(i.as_flat_samples_u8(), i.width(), i.height()) - }) - } + Promise::spawn_async( + async move { async_fetch_img_from_disk(ctx, url, &path, cache_type).await }, + ) +} + +async fn async_fetch_img_from_disk( + ctx: egui::Context, + url: String, + path: &path::Path, + cache_type: MediaCacheType, +) -> Result<TexturedImage, notedeck::Error> { + match cache_type { + MediaCacheType::Image => { + let data = fs::read(path).await?; + let image_buffer = image::load_from_memory(&data).map_err(notedeck::Error::Image)?; + + let img = buffer_to_color_image( + image_buffer.as_flat_samples_u8(), + image_buffer.width(), + image_buffer.height(), + ); + Ok(TexturedImage::Static(ctx.load_texture( + &url, + img, + Default::default(), + ))) + } + MediaCacheType::Gif => { + let gif_bytes = fs::read(path).await?; // Read entire file into a Vec<u8> + generate_gif(ctx, url, path, gif_bytes, false, |i| { + buffer_to_color_image(i.as_flat_samples_u8(), i.width(), i.height()) + }) } - }) + } } fn generate_gif(