implement blurring

Signed-off-by: kernelkind <kernelkind@gmail.com>
This commit is contained in:
kernelkind
2025-04-18 22:38:04 -05:00
parent 7d2112b472
commit b2abe495ca
10 changed files with 877 additions and 240 deletions

View File

@@ -1,4 +1,8 @@
use std::cell::OnceCell;
use crate::{
blur::imeta_blurhashes,
contacts::trust_media_from_pk2,
jobs::JobsCache,
note::{NoteAction, NoteOptions, NoteResponse, NoteView},
};
@@ -8,9 +12,9 @@ use enostr::KeypairUnowned;
use nostrdb::{BlockType, Mention, Note, NoteKey, Transaction};
use tracing::warn;
use notedeck::{supported_mime_hosted_at_url, MediaCacheType, NoteContext};
use notedeck::NoteContext;
use super::media::image_carousel;
use super::media::{find_renderable_media, image_carousel, RenderableMedia};
pub struct NoteContents<'a, 'd> {
note_context: &'a mut NoteContext<'d>,
@@ -116,7 +120,6 @@ pub fn render_note_contents(
) -> NoteResponse {
let note_key = note.key().expect("todo: implement non-db notes");
let selectable = options.has_selectable_text();
let mut images: Vec<(String, MediaCacheType)> = vec![];
let mut note_action: Option<NoteAction> = None;
let mut inline_note: Option<(&[u8; 32], &str)> = None;
let hide_media = options.has_hide_media();
@@ -131,6 +134,9 @@ pub fn render_note_contents(
let _ = ui.allocate_at_least(egui::vec2(ui.available_width(), 0.0), egui::Sense::click());
}
let mut supported_medias: Vec<RenderableMedia> = vec![];
let blurhashes = OnceCell::new();
let response = ui.horizontal_wrapped(|ui| {
let blocks = if let Ok(blocks) = note_context.ndb.get_blocks_by_key(txn, note_key) {
blocks
@@ -199,15 +205,19 @@ pub fn render_note_contents(
BlockType::Url => {
let mut found_supported = || -> bool {
let url = block.as_str();
if let Some(cache_type) =
supported_mime_hosted_at_url(&mut note_context.img_cache.urls, url)
{
images.push((url.to_string(), cache_type));
true
} else {
false
}
let blurs = blurhashes.get_or_init(|| imeta_blurhashes(note));
let Some(media_type) =
find_renderable_media(&mut note_context.img_cache.urls, blurs, url)
else {
return false;
};
supported_medias.push(media_type);
true
};
if hide_media || !found_supported() {
ui.add(Hyperlink::from_label_and_url(
RichText::new(block.as_str()).color(link_color),
@@ -266,14 +276,33 @@ pub fn render_note_contents(
None
};
if !images.is_empty() && !options.has_textmode() {
let mut media_action = None;
if !supported_medias.is_empty() && !options.has_textmode() {
ui.add_space(2.0);
let carousel_id = egui::Id::new(("carousel", note.key().expect("expected tx note")));
image_carousel(ui, note_context.img_cache, images, carousel_id);
let trusted_media = trust_media_from_pk2(
note_context.ndb,
txn,
cur_acc.as_ref().map(|k| k.pubkey.bytes()),
note.pubkey(),
);
media_action = image_carousel(
ui,
note_context.img_cache,
note_context.job_pool,
jobs,
supported_medias,
carousel_id,
trusted_media,
);
ui.add_space(2.0);
}
let note_action = preview_note_action.or(note_action);
let note_action = preview_note_action
.or(note_action)
.or(media_action.map(NoteAction::Media));
NoteResponse::new(response.response).with_action(note_action)
}