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

@@ -13,13 +13,10 @@ use std::time::{Duration, Instant, SystemTime};
use hex::ToHex;
use sha2::Digest;
use std::path::{self};
use std::path::{Path, PathBuf};
use std::path::PathBuf;
use std::path::{self, Path};
use tracing::warn;
pub type MediaCacheValue = Promise<Option<Result<TexturedImage>>>;
pub type MediaCacheMap = HashMap<String, MediaCacheValue>;
#[derive(Default)]
pub struct TexturesCache {
cache: hashbrown::HashMap<String, TextureStateInternal>,
@@ -39,7 +36,6 @@ impl TexturesCache {
pub fn handle_and_get_or_insert(
&mut self,
url: &str,
closure: impl FnOnce() -> Promise<Option<Result<TexturedImage>>>,
) -> TextureState {
let internal = self.handle_and_get_state_internal(url, false, closure);
@@ -222,7 +218,7 @@ pub struct ImageFrame {
pub struct MediaCache {
pub cache_dir: path::PathBuf,
url_imgs: MediaCacheMap,
pub textures_cache: TexturesCache,
pub cache_type: MediaCacheType,
}
@@ -237,7 +233,7 @@ impl MediaCache {
let cache_dir = parent_dir.join(Self::rel_dir(cache_type));
Self {
cache_dir,
url_imgs: HashMap::new(),
textures_cache: TexturesCache::default(),
cache_type,
}
}
@@ -337,14 +333,6 @@ impl MediaCache {
}
Ok(())
}
pub fn map(&self) -> &MediaCacheMap {
&self.url_imgs
}
pub fn map_mut(&mut self) -> &mut MediaCacheMap {
&mut self.url_imgs
}
}
fn color_image_to_rgba(color_image: ColorImage) -> image::RgbaImage {

View File

@@ -1,6 +1,7 @@
use super::context::ContextSelection;
use crate::zaps::NoteZapTargetOwned;
use crate::{zaps::NoteZapTargetOwned, Images, MediaCacheType, TexturedImage};
use enostr::{NoteId, Pubkey};
use poll_promise::Promise;
#[derive(Debug)]
pub enum NoteAction {
@@ -24,6 +25,9 @@ pub enum NoteAction {
/// User has clicked the zap action
Zap(ZapAction),
/// User clicked on media
Media(MediaAction),
}
#[derive(Debug, Eq, PartialEq, Clone)]
@@ -37,3 +41,62 @@ pub struct ZapTargetAmount {
pub target: NoteZapTargetOwned,
pub specified_msats: Option<u64>, // if None use default amount
}
pub enum MediaAction {
FetchImage {
url: String,
cache_type: MediaCacheType,
no_pfp_promise: Promise<Option<Result<TexturedImage, crate::Error>>>,
},
DoneLoading {
url: String,
cache_type: MediaCacheType,
},
}
impl std::fmt::Debug for MediaAction {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::FetchImage {
url,
cache_type,
no_pfp_promise,
} => f
.debug_struct("FetchNoPfpImage")
.field("url", url)
.field("cache_type", cache_type)
.field("no_pfp_promise ready", &no_pfp_promise.ready().is_some())
.finish(),
Self::DoneLoading { url, cache_type } => f
.debug_struct("DoneLoading")
.field("url", url)
.field("cache_type", cache_type)
.finish(),
}
}
}
impl MediaAction {
pub fn process(self, images: &mut Images) {
match self {
MediaAction::FetchImage {
url,
cache_type,
no_pfp_promise: promise,
} => {
images
.get_cache_mut(cache_type)
.textures_cache
.insert_pending(&url, promise);
}
MediaAction::DoneLoading { url, cache_type } => {
let cache = match cache_type {
MediaCacheType::Image => &mut images.static_imgs,
MediaCacheType::Gif => &mut images.gifs,
};
cache.textures_cache.move_to_loaded(&url);
}
}
}
}

View File

@@ -1,7 +1,7 @@
mod action;
mod context;
pub use action::{NoteAction, ZapAction, ZapTargetAmount};
pub use action::{MediaAction, NoteAction, ZapAction, ZapTargetAmount};
pub use context::{BroadcastContext, ContextSelection, NoteContextSelection};
use crate::JobPool;