previews: run previews as notedeck apps

This allows ./preview to be a notedeck app runner. I am currently
using it for the ProfilePic app (which will because notedeck_viz)

Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
William Casarin
2024-12-20 15:39:26 -08:00
parent 475314da75
commit fcac49a0a5
19 changed files with 222 additions and 238 deletions

View File

@@ -0,0 +1,2 @@
// profile menu

View File

@@ -1,9 +1,10 @@
use crate::images::ImageType;
use crate::ui::{Preview, PreviewConfig, View};
use crate::ui::{Preview, PreviewConfig};
use egui::{vec2, Sense, TextureHandle};
use nostrdb::{Ndb, Transaction};
use tracing::info;
use notedeck::ImageCache;
use notedeck::{AppContext, ImageCache};
pub struct ProfilePic<'cache, 'url> {
cache: &'cache mut ImageCache,
@@ -132,22 +133,62 @@ mod preview {
use std::collections::HashSet;
pub struct ProfilePicPreview {
cache: ImageCache,
ndb: Ndb,
keys: Vec<ProfileKey>,
keys: Option<Vec<ProfileKey>>,
}
impl ProfilePicPreview {
fn new() -> Self {
let config = Config::new();
let ndb = Ndb::new(".", &config).expect("ndb");
let txn = Transaction::new(&ndb).unwrap();
ProfilePicPreview { keys: None }
}
fn show(&mut self, app: &mut AppContext<'_>, ui: &mut egui::Ui) {
egui::ScrollArea::both().show(ui, |ui| {
ui.horizontal_wrapped(|ui| {
let txn = Transaction::new(app.ndb).unwrap();
let keys = if let Some(keys) = &self.keys {
keys
} else {
return;
};
for key in keys {
let profile = app.ndb.get_profile_by_key(&txn, *key).unwrap();
let url = profile
.record()
.profile()
.expect("should have profile")
.picture()
.expect("should have picture");
let expand_size = 10.0;
let anim_speed = 0.05;
let (rect, size, _resp) = ui::anim::hover_expand(
ui,
egui::Id::new(profile.key().unwrap()),
ui::ProfilePic::default_size(),
expand_size,
anim_speed,
);
ui.put(rect, ui::ProfilePic::new(app.img_cache, url).size(size))
.on_hover_ui_at_pointer(|ui| {
ui.set_max_width(300.0);
ui.add(ui::ProfilePreview::new(&profile, app.img_cache));
});
}
});
});
}
fn setup(&mut self, ndb: &Ndb) {
let txn = Transaction::new(ndb).unwrap();
let filters = vec![Filter::new().kinds(vec![0]).build()];
let cache = ImageCache::new("cache/img".into());
let mut pks = HashSet::new();
let mut keys = HashSet::new();
for query_result in ndb.query(&txn, &filters, 2000).unwrap() {
for query_result in ndb.query(&txn, &filters, 20000).unwrap() {
pks.insert(query_result.note.pubkey());
}
@@ -170,44 +211,19 @@ mod preview {
keys.insert(profile.key().expect("should not be owned"));
}
let keys = keys.into_iter().collect();
ProfilePicPreview { cache, ndb, keys }
let keys: Vec<ProfileKey> = keys.into_iter().collect();
info!("Loaded {} profiles", keys.len());
self.keys = Some(keys);
}
}
impl View for ProfilePicPreview {
fn ui(&mut self, ui: &mut egui::Ui) {
egui::ScrollArea::both().show(ui, |ui| {
ui.horizontal_wrapped(|ui| {
let txn = Transaction::new(&self.ndb).unwrap();
for key in &self.keys {
let profile = self.ndb.get_profile_by_key(&txn, *key).unwrap();
let url = profile
.record()
.profile()
.expect("should have profile")
.picture()
.expect("should have picture");
impl notedeck::App for ProfilePicPreview {
fn update(&mut self, ctx: &mut AppContext<'_>, ui: &mut egui::Ui) {
if self.keys.is_none() {
self.setup(ctx.ndb);
}
let expand_size = 10.0;
let anim_speed = 0.05;
let (rect, size, _resp) = ui::anim::hover_expand(
ui,
egui::Id::new(profile.key().unwrap()),
ui::ProfilePic::default_size(),
expand_size,
anim_speed,
);
ui.put(rect, ui::ProfilePic::new(&mut self.cache, url).size(size))
.on_hover_ui_at_pointer(|ui| {
ui.set_max_width(300.0);
ui.add(ui::ProfilePreview::new(&profile, &mut self.cache));
});
}
});
});
self.show(ctx, ui)
}
}

View File

@@ -6,7 +6,7 @@ use egui_extras::Size;
use enostr::{NoteId, Pubkey};
use nostrdb::{Ndb, ProfileRecord, Transaction};
use notedeck::{DataPath, DataPathType, ImageCache, NotedeckTextStyle, UserAccount};
use notedeck::{ImageCache, NotedeckTextStyle, UserAccount};
pub struct ProfilePreview<'a, 'cache> {
profile: &'a ProfileRecord<'a>,
@@ -147,21 +147,17 @@ impl egui::Widget for SimpleProfilePreview<'_, '_> {
mod previews {
use super::*;
use crate::test_data::test_profile_record;
use crate::ui::{Preview, PreviewConfig, View};
use crate::ui::{Preview, PreviewConfig};
use notedeck::{App, AppContext};
pub struct ProfilePreviewPreview<'a> {
profile: ProfileRecord<'a>,
cache: ImageCache,
}
impl ProfilePreviewPreview<'_> {
pub fn new() -> Self {
let profile = test_profile_record();
let path = DataPath::new("previews")
.path(DataPathType::Cache)
.join(ImageCache::rel_dir());
let cache = ImageCache::new(path);
ProfilePreviewPreview { profile, cache }
ProfilePreviewPreview { profile }
}
}
@@ -171,9 +167,9 @@ mod previews {
}
}
impl View for ProfilePreviewPreview<'_> {
fn ui(&mut self, ui: &mut egui::Ui) {
ProfilePreview::new(&self.profile, &mut self.cache).ui(ui);
impl App for ProfilePreviewPreview<'_> {
fn update(&mut self, app: &mut AppContext<'_>, ui: &mut egui::Ui) {
ProfilePreview::new(&self.profile, app.img_cache).ui(ui);
}
}