move MediaCache rendering to render_media_cache call

Signed-off-by: kernelkind <kernelkind@gmail.com>
This commit is contained in:
kernelkind
2025-02-19 17:54:55 -05:00
parent 32b3e2110d
commit 7c2b4775f1
5 changed files with 81 additions and 135 deletions

View File

@@ -130,3 +130,10 @@ impl MediaCache {
&mut self.url_imgs &mut self.url_imgs
} }
} }
// TODO: temporary...
pub fn get_texture(textured_image: &TexturedImage) -> &TextureHandle {
match textured_image {
TexturedImage::Static(texture_handle) => texture_handle,
}
}

View File

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

View File

@@ -1,7 +1,7 @@
use crate::ui::{ use crate::ui::{
self, self,
images::render_media_cache,
note::{NoteOptions, NoteResponse}, note::{NoteOptions, NoteResponse},
ProfilePic,
}; };
use crate::{actionbar::NoteAction, images::ImageType, timeline::TimelineKind}; use crate::{actionbar::NoteAction, images::ImageType, timeline::TimelineKind};
use egui::{Color32, Hyperlink, Image, RichText}; use egui::{Color32, Hyperlink, Image, RichText};
@@ -295,57 +295,32 @@ fn image_carousel(
.show(ui, |ui| { .show(ui, |ui| {
ui.horizontal(|ui| { ui.horizontal(|ui| {
for image in images { for image in images {
// If the cache is empty, initiate the fetch render_media_cache(
let m_cached_promise = img_cache.map().get(&image); ui,
if m_cached_promise.is_none() { img_cache,
let res = crate::images::fetch_img( &image,
img_cache, ImageType::Content(width.round() as u32, height.round() as u32),
ui.ctx(), |ui| {
&image,
ImageType::Content(width.round() as u32, height.round() as u32),
);
img_cache.map_mut().insert(image.to_owned(), res);
}
// What is the state of the fetch?
match img_cache.map()[&image].ready() {
// Still waiting
None => {
ui.allocate_space(egui::vec2(spinsz, spinsz)); ui.allocate_space(egui::vec2(spinsz, spinsz));
//ui.add(egui::Spinner::new().size(spinsz));
}
// Failed to fetch image!
Some(Err(_err)) => {
// FIXME - use content-specific error instead
let no_pfp = crate::images::fetch_img(
img_cache,
ui.ctx(),
ProfilePic::no_pfp_url(),
ImageType::Profile(128),
);
img_cache.map_mut().insert(image.to_owned(), no_pfp);
// spin until next pass
ui.allocate_space(egui::vec2(spinsz, spinsz));
//ui.add(egui::Spinner::new().size(spinsz));
}
// Use the previously resolved image
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();
}
});
}
}, },
} |ui, _| {
ui.allocate_space(egui::vec2(spinsz, spinsz));
},
|ui, url, renderable_media| {
let img_resp = ui.add(
Image::new(notedeck::get_texture(renderable_media))
.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(url.to_owned());
ui.close_menu();
}
});
},
);
} }
}) })
.response .response

View File

@@ -1,8 +1,8 @@
use crate::draft::{Draft, Drafts, MentionHint}; use crate::draft::{Draft, Drafts, MentionHint};
use crate::images::fetch_img;
use crate::media_upload::{nostrbuild_nip96_upload, MediaPath}; use crate::media_upload::{nostrbuild_nip96_upload, MediaPath};
use crate::post::{downcast_post_buffer, MentionType, NewPost}; use crate::post::{downcast_post_buffer, MentionType, NewPost};
use crate::profile::get_display_name; use crate::profile::get_display_name;
use crate::ui::images::render_media_cache;
use crate::ui::search_results::SearchResultsView; use crate::ui::search_results::SearchResultsView;
use crate::ui::{self, note::NoteOptions, Preview, PreviewConfig}; use crate::ui::{self, note::NoteOptions, Preview, PreviewConfig};
use crate::Result; use crate::Result;
@@ -13,7 +13,7 @@ use egui::{vec2, Frame, Layout, Margin, Pos2, ScrollArea, Sense, TextBuffer};
use enostr::{FilledKeypair, FullKeypair, NoteId, Pubkey, RelayPool}; use enostr::{FilledKeypair, FullKeypair, NoteId, Pubkey, RelayPool};
use nostrdb::{Ndb, Transaction}; use nostrdb::{Ndb, Transaction};
use notedeck::{MediaCache, NoteCache}; use notedeck::{get_texture, MediaCache, NoteCache};
use tracing::error; use tracing::error;
use super::contents::render_note_preview; use super::contents::render_note_preview;
@@ -384,21 +384,19 @@ impl<'a> PostView<'a> {
} else { } else {
(300, 300) (300, 300)
}; };
let m_cached_promise = self.img_cache.map().get(&media.url); render_media_cache(
if m_cached_promise.is_none() { ui,
let promise = fetch_img( self.img_cache,
self.img_cache, &media.url,
ui.ctx(), crate::images::ImageType::Content(width, height),
&media.url, |ui| {
crate::images::ImageType::Content(width, height), ui.spinner();
); },
self.img_cache |_, e| {
.map_mut() self.draft.upload_errors.push(e.clone());
.insert(media.url.to_owned(), promise); error!("{e}");
} },
|ui, _, renderable_media| {
match self.img_cache.map()[&media.url].ready() {
Some(Ok(texture)) => {
let media_size = vec2(width as f32, height as f32); let media_size = vec2(width as f32, height as f32);
let max_size = vec2(300.0, 300.0); let max_size = vec2(300.0, 300.0);
let size = if media_size.x > max_size.x || media_size.y > max_size.y { let size = if media_size.x > max_size.x || media_size.y > max_size.y {
@@ -407,35 +405,25 @@ impl<'a> PostView<'a> {
media_size media_size
}; };
match texture { let texture_handle = get_texture(renderable_media);
notedeck::TexturedImage::Static(texture_handle) => { let img_resp = ui.add(
let img_resp = ui.add( egui::Image::new(texture_handle)
egui::Image::new(texture_handle) .max_size(size)
.max_size(size) .rounding(12.0),
.rounding(12.0), );
);
let remove_button_rect = { let remove_button_rect = {
let top_left = img_resp.rect.left_top(); let top_left = img_resp.rect.left_top();
let spacing = 13.0; let spacing = 13.0;
let center = Pos2::new(top_left.x + spacing, top_left.y + spacing); let center = Pos2::new(top_left.x + spacing, top_left.y + spacing);
egui::Rect::from_center_size(center, egui::vec2(26.0, 26.0)) egui::Rect::from_center_size(center, egui::vec2(26.0, 26.0))
}; };
if show_remove_upload_button(ui, remove_button_rect).clicked() { if show_remove_upload_button(ui, remove_button_rect).clicked() {
to_remove.push(i); 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()); );
error!("{e}");
}
None => {
ui.spinner();
}
}
} }
to_remove.reverse(); to_remove.reverse();
for i in to_remove { for i in to_remove {

View File

@@ -1,4 +1,5 @@
use crate::images::ImageType; use crate::images::ImageType;
use crate::ui::images::render_media_cache;
use crate::ui::{Preview, PreviewConfig}; use crate::ui::{Preview, PreviewConfig};
use egui::{vec2, Sense, Stroke, TextureHandle}; use egui::{vec2, Sense, Stroke, TextureHandle};
use nostrdb::{Ndb, Transaction}; use nostrdb::{Ndb, Transaction};
@@ -91,47 +92,22 @@ fn render_pfp(
// We will want to downsample these so it's not blurry on hi res displays // We will want to downsample these so it's not blurry on hi res displays
let img_size = 128u32; let img_size = 128u32;
let m_cached_promise = img_cache.map().get(url); render_media_cache(
if m_cached_promise.is_none() { ui,
let res = crate::images::fetch_img(img_cache, ui.ctx(), url, ImageType::Profile(img_size)); img_cache,
img_cache.map_mut().insert(url.to_owned(), res); url,
} ImageType::Profile(img_size),
|ui| {
match img_cache.map()[url].ready() { paint_circle(ui, ui_size, border);
None => paint_circle(ui, ui_size, border),
// Failed to fetch profile!
Some(Err(_err)) => {
let m_failed_promise = img_cache.map().get(url);
if m_failed_promise.is_none() {
let no_pfp = crate::images::fetch_img(
img_cache,
ui.ctx(),
ProfilePic::no_pfp_url(),
ImageType::Profile(img_size),
);
img_cache.map_mut().insert(url.to_owned(), no_pfp);
}
match img_cache.map().get(url).unwrap().ready() {
None => paint_circle(ui, ui_size, border),
Some(Err(_e)) => {
//error!("Image load error: {:?}", e);
paint_circle(ui, ui_size, border)
}
Some(Ok(img)) => match img {
notedeck::TexturedImage::Static(texture_handle) => {
pfp_image(ui, texture_handle, ui_size, border)
}
},
}
}
Some(Ok(img)) => match img {
notedeck::TexturedImage::Static(texture_handle) => {
pfp_image(ui, texture_handle, ui_size, border)
}
}, },
} |ui, _| {
paint_circle(ui, ui_size, border);
},
|ui, _, renderable_media| {
let texture_handle = notedeck::get_texture(renderable_media);
pfp_image(ui, texture_handle, ui_size, border);
},
)
} }
fn pfp_image( fn pfp_image(