Use TexturedImage in MediaCache

Signed-off-by: kernelkind <kernelkind@gmail.com>
This commit is contained in:
kernelkind
2025-02-19 17:39:46 -05:00
parent 4f4a0feb8c
commit 594ea0b42d
6 changed files with 61 additions and 34 deletions

View File

@@ -13,9 +13,13 @@ use std::path;
use std::path::PathBuf;
use tracing::warn;
pub type MediaCacheValue = Promise<Result<TextureHandle>>;
pub type MediaCacheValue = Promise<Result<TexturedImage>>;
pub type MediaCacheMap = HashMap<String, MediaCacheValue>;
pub enum TexturedImage {
Static(TextureHandle),
}
pub struct MediaCache {
pub cache_dir: path::PathBuf,
url_imgs: MediaCacheMap,

View File

@@ -31,7 +31,7 @@ pub use context::AppContext;
pub use error::{Error, FilterError};
pub use filter::{FilterState, FilterStates, UnifiedSubscription};
pub use fonts::NamedFontFamily;
pub use imgcache::MediaCache;
pub use imgcache::{MediaCache, TexturedImage};
pub use muted::{MuteFun, Muted};
pub use note::{NoteRef, RootIdError, RootNoteId, RootNoteIdBuf};
pub use notecache::{CachedNote, NoteCache};

View File

@@ -1,7 +1,8 @@
use egui::{pos2, Color32, ColorImage, Rect, Sense, SizeHint, TextureHandle};
use egui::{pos2, Color32, ColorImage, Rect, Sense, SizeHint};
use image::imageops::FilterType;
use notedeck::MediaCache;
use notedeck::Result;
use notedeck::TexturedImage;
use poll_promise::Promise;
use std::path;
use std::path::PathBuf;
@@ -177,7 +178,7 @@ fn fetch_img_from_disk(
ctx: &egui::Context,
url: &str,
path: &path::Path,
) -> Promise<Result<TextureHandle>> {
) -> Promise<Result<TexturedImage>> {
let ctx = ctx.clone();
let url = url.to_owned();
let path = path.to_owned();
@@ -195,7 +196,11 @@ fn fetch_img_from_disk(
flat_samples.as_slice(),
);
Ok(ctx.load_texture(&url, img, Default::default()))
Ok(TexturedImage::Static(ctx.load_texture(
&url,
img,
Default::default(),
)))
})
}
@@ -217,7 +222,7 @@ pub fn fetch_img(
ctx: &egui::Context,
url: &str,
imgtyp: ImageType,
) -> Promise<Result<TextureHandle>> {
) -> Promise<Result<TexturedImage>> {
let key = MediaCache::key(url);
let path = img_cache.cache_dir.join(key);
@@ -235,7 +240,7 @@ fn fetch_img_from_net(
ctx: &egui::Context,
url: &str,
imgtyp: ImageType,
) -> Promise<Result<TextureHandle>> {
) -> Promise<Result<TexturedImage>> {
let (sender, promise) = Promise::new();
let request = ehttp::Request::get(url);
let ctx = ctx.clone();
@@ -251,7 +256,7 @@ fn fetch_img_from_net(
// write to disk
std::thread::spawn(move || MediaCache::write(&cache_path, &cloned_url, img));
texture_handle
TexturedImage::Static(texture_handle)
});
sender.send(handle); // send the results back to the UI thread.

View File

@@ -329,20 +329,22 @@ fn image_carousel(
//ui.add(egui::Spinner::new().size(spinsz));
}
// Use the previously resolved image
Some(Ok(img)) => {
let img_resp = ui.add(
Image::new(img)
.max_height(height)
.rounding(5.0)
.fit_to_original_size(1.0),
);
img_resp.context_menu(|ui| {
if ui.button("Copy Link").clicked() {
ui.ctx().copy_text(image);
ui.close_menu();
}
});
}
Some(Ok(img)) => match img {
notedeck::TexturedImage::Static(texture_handle) => {
let img_resp = ui.add(
Image::new(texture_handle)
.max_height(height)
.rounding(5.0)
.fit_to_original_size(1.0),
);
img_resp.context_menu(|ui| {
if ui.button("Copy Link").clicked() {
ui.ctx().copy_text(image);
ui.close_menu();
}
});
}
},
}
}
})

View File

@@ -407,18 +407,26 @@ impl<'a> PostView<'a> {
media_size
};
let img_resp = ui.add(egui::Image::new(texture).max_size(size).rounding(12.0));
match texture {
notedeck::TexturedImage::Static(texture_handle) => {
let img_resp = ui.add(
egui::Image::new(texture_handle)
.max_size(size)
.rounding(12.0),
);
let remove_button_rect = {
let top_left = img_resp.rect.left_top();
let spacing = 13.0;
let center = Pos2::new(top_left.x + spacing, top_left.y + spacing);
egui::Rect::from_center_size(center, egui::vec2(26.0, 26.0))
};
if show_remove_upload_button(ui, remove_button_rect).clicked() {
to_remove.push(i);
let remove_button_rect = {
let top_left = img_resp.rect.left_top();
let spacing = 13.0;
let center = Pos2::new(top_left.x + spacing, top_left.y + spacing);
egui::Rect::from_center_size(center, egui::vec2(26.0, 26.0))
};
if show_remove_upload_button(ui, remove_button_rect).clicked() {
to_remove.push(i);
}
ui.advance_cursor_after_rect(img_resp.rect);
}
}
ui.advance_cursor_after_rect(img_resp.rect);
}
Some(Err(e)) => {
self.draft.upload_errors.push(e.to_string());

View File

@@ -119,10 +119,18 @@ fn render_pfp(
//error!("Image load error: {:?}", e);
paint_circle(ui, ui_size, border)
}
Some(Ok(img)) => pfp_image(ui, img, ui_size, border),
Some(Ok(img)) => match img {
notedeck::TexturedImage::Static(texture_handle) => {
pfp_image(ui, texture_handle, ui_size, border)
}
},
}
}
Some(Ok(img)) => pfp_image(ui, img, ui_size, border),
Some(Ok(img)) => match img {
notedeck::TexturedImage::Static(texture_handle) => {
pfp_image(ui, texture_handle, ui_size, border)
}
},
}
}