wip notedeck

This commit is contained in:
2025-01-07 11:23:34 +00:00
parent c8c5485581
commit 0e19c1a8f3
31 changed files with 725 additions and 2173 deletions

View File

@ -1,42 +1,37 @@
use crate::route::RouteServices;
use crate::services::ndb_wrapper::SubWrapper;
use crate::route::image_from_cache;
use egui::{vec2, Color32, Pos2, Response, Rounding, Sense, Ui, Vec2, Widget};
use nostrdb::NdbProfile;
use nostrdb::{Ndb, NdbProfile, Transaction};
use notedeck::ImageCache;
pub struct Avatar<'a> {
image: Option<&'a str>,
sub: Option<SubWrapper>,
pub struct Avatar {
image: Option<String>,
size: Option<f32>,
services: &'a RouteServices<'a>,
}
impl<'a> Avatar<'a> {
pub fn new_optional(img: Option<&'a str>, services: &'a RouteServices<'a>) -> Self {
impl Avatar {
pub fn new_optional(img: Option<&str>) -> Self {
Self {
image: img,
sub: None,
image: img.map(String::from),
size: None,
services,
}
}
pub fn from_profile(p: &'a Option<NdbProfile<'a>>, services: &'a RouteServices<'a>) -> Self {
pub fn pubkey(pk: &[u8; 32], ndb: &Ndb, tx: &Transaction) -> Self {
let picture = ndb
.get_profile_by_pubkey(&tx, pk)
.map(|p| p.record().profile().map(|p| p.picture()).unwrap_or(None))
.unwrap_or(None);
Self {
image: picture.map(|s| s.to_string()),
size: None,
}
}
pub fn from_profile(p: &Option<NdbProfile<'_>>) -> Self {
let img = p.map(|f| f.picture()).unwrap_or(None);
Self {
image: img,
sub: None,
image: img.map(String::from),
size: None,
services,
}
}
pub fn pubkey(pk: &[u8; 32], services: &'a RouteServices<'a>) -> Self {
let (p, sub) = services.ndb.fetch_profile(services.tx, pk);
Self {
image: p.map(|f| f.picture()).unwrap_or(None),
sub,
size: None,
services,
}
}
@ -54,21 +49,15 @@ impl<'a> Avatar<'a> {
);
response
}
}
impl<'a> Widget for Avatar<'a> {
fn ui(self, ui: &mut Ui) -> Response {
pub fn render(&self, ui: &mut Ui, img_cache: &mut ImageCache) -> Response {
let size_v = self.size.unwrap_or(40.);
let size = Vec2::new(size_v, size_v);
if !ui.is_visible() {
return Self::placeholder(ui, size_v);
}
match self
.image
.as_ref()
.map(|i| self.services.img_cache.load(*i))
{
Some(img) => img
match &self.image {
Some(img) => image_from_cache(img_cache, ui.ctx(), &img)
.fit_to_exact_size(size)
.rounding(Rounding::same(size_v))
.ui(ui),