feat: defer image load
This commit is contained in:
@ -1,47 +1,42 @@
|
||||
use crate::route::RouteServices;
|
||||
use crate::services::image_cache::ImageCache;
|
||||
use crate::services::ndb_wrapper::SubWrapper;
|
||||
use egui::{vec2, Color32, Image, Pos2, Response, Rounding, Sense, Ui, Vec2, Widget};
|
||||
use egui::{vec2, Color32, Pos2, Response, Rounding, Sense, Ui, Vec2, Widget};
|
||||
use nostrdb::NdbProfile;
|
||||
|
||||
pub struct Avatar<'a> {
|
||||
image: Option<Image<'a>>,
|
||||
image: Option<&'a str>,
|
||||
sub: Option<SubWrapper>,
|
||||
size: Option<f32>,
|
||||
services: &'a RouteServices<'a>,
|
||||
}
|
||||
|
||||
impl<'a> Avatar<'a> {
|
||||
pub fn new(img: Image<'a>) -> Self {
|
||||
Self {
|
||||
image: Some(img),
|
||||
sub: None,
|
||||
size: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_optional(img: Option<Image<'a>>) -> Self {
|
||||
pub fn new_optional(img: Option<&'a str>, services: &'a RouteServices<'a>) -> Self {
|
||||
Self {
|
||||
image: img,
|
||||
sub: None,
|
||||
size: None,
|
||||
services,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_profile(p: &'a Option<NdbProfile<'a>>, svc: &'a ImageCache) -> Self {
|
||||
let img = p.map_or(None, |f| f.picture().map(|f| svc.load(f)));
|
||||
pub fn from_profile(p: &'a Option<NdbProfile<'a>>, services: &'a RouteServices<'a>) -> Self {
|
||||
let img = p.map(|f| f.picture()).unwrap_or(None);
|
||||
Self {
|
||||
image: img,
|
||||
sub: None,
|
||||
size: None,
|
||||
services,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn pubkey(pk: &[u8; 32], svc: &'a RouteServices<'a>) -> Self {
|
||||
let (p, sub) = svc.ndb.fetch_profile(svc.tx, pk);
|
||||
pub fn pubkey(pk: &[u8; 32], services: &'a RouteServices<'a>) -> Self {
|
||||
let (p, sub) = services.ndb.fetch_profile(services.tx, pk);
|
||||
Self {
|
||||
image: p.and_then(|p| p.picture().map(|p| svc.img_cache.load(p))),
|
||||
image: p.map(|f| f.picture()).unwrap_or(None),
|
||||
sub,
|
||||
size: None,
|
||||
services,
|
||||
}
|
||||
}
|
||||
|
||||
@ -65,10 +60,14 @@ impl<'a> Widget for Avatar<'a> {
|
||||
fn ui(self, ui: &mut Ui) -> Response {
|
||||
let size_v = self.size.unwrap_or(40.);
|
||||
let size = Vec2::new(size_v, size_v);
|
||||
if !ui.is_rect_visible(ui.cursor()) {
|
||||
if !ui.is_visible() {
|
||||
return Self::placeholder(ui, size_v);
|
||||
}
|
||||
match self.image {
|
||||
match self
|
||||
.image
|
||||
.as_ref()
|
||||
.map(|i| self.services.img_cache.load(*i))
|
||||
{
|
||||
Some(img) => img
|
||||
.fit_to_exact_size(size)
|
||||
.rounding(Rounding::same(size_v))
|
||||
|
Reference in New Issue
Block a user