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:
@@ -1,5 +1,5 @@
|
||||
use crate::login_manager::AcquireKeyState;
|
||||
use crate::ui::{Preview, PreviewConfig, View};
|
||||
use crate::ui::{Preview, PreviewConfig};
|
||||
use egui::TextEdit;
|
||||
use egui::{Align, Button, Color32, Frame, InnerResponse, Margin, RichText, Vec2};
|
||||
use enostr::Keypair;
|
||||
@@ -110,13 +110,14 @@ fn login_textedit(manager: &mut AcquireKeyState) -> TextEdit {
|
||||
|
||||
mod preview {
|
||||
use super::*;
|
||||
use notedeck::{App, AppContext};
|
||||
|
||||
pub struct AccountLoginPreview {
|
||||
manager: AcquireKeyState,
|
||||
}
|
||||
|
||||
impl View for AccountLoginPreview {
|
||||
fn ui(&mut self, ui: &mut egui::Ui) {
|
||||
impl App for AccountLoginPreview {
|
||||
fn update(&mut self, _app_ctx: &mut AppContext<'_>, ui: &mut egui::Ui) {
|
||||
AccountLoginView::new(&mut self.manager).ui(ui);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -297,10 +297,11 @@ fn paint_row(ui: &mut egui::Ui, row_glyphs: &[char], font_size: f32) -> Option<c
|
||||
mod preview {
|
||||
use crate::{
|
||||
deck_state::DeckState,
|
||||
ui::{Preview, PreviewConfig, View},
|
||||
ui::{Preview, PreviewConfig},
|
||||
};
|
||||
|
||||
use super::ConfigureDeckView;
|
||||
use notedeck::{App, AppContext};
|
||||
|
||||
pub struct ConfigureDeckPreview {
|
||||
state: DeckState,
|
||||
@@ -314,8 +315,8 @@ mod preview {
|
||||
}
|
||||
}
|
||||
|
||||
impl View for ConfigureDeckPreview {
|
||||
fn ui(&mut self, ui: &mut egui::Ui) {
|
||||
impl App for ConfigureDeckPreview {
|
||||
fn update(&mut self, _app_ctx: &mut AppContext<'_>, ui: &mut egui::Ui) {
|
||||
ConfigureDeckView::new(&mut self.state).ui(ui);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,10 +58,11 @@ fn delete_button() -> impl Widget {
|
||||
mod preview {
|
||||
use crate::{
|
||||
deck_state::DeckState,
|
||||
ui::{Preview, PreviewConfig, View},
|
||||
ui::{Preview, PreviewConfig},
|
||||
};
|
||||
|
||||
use super::EditDeckView;
|
||||
use notedeck::{App, AppContext};
|
||||
|
||||
pub struct EditDeckPreview {
|
||||
state: DeckState,
|
||||
@@ -75,8 +76,8 @@ mod preview {
|
||||
}
|
||||
}
|
||||
|
||||
impl View for EditDeckPreview {
|
||||
fn ui(&mut self, ui: &mut egui::Ui) {
|
||||
impl App for EditDeckPreview {
|
||||
fn update(&mut self, _app_ctx: &mut AppContext<'_>, ui: &mut egui::Ui) {
|
||||
EditDeckView::new(&mut self.state).ui(ui);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
use crate::draft::{Draft, Drafts};
|
||||
use crate::post::NewPost;
|
||||
use crate::ui::{self, Preview, PreviewConfig, View};
|
||||
use crate::ui::{self, Preview, PreviewConfig};
|
||||
use crate::Result;
|
||||
use egui::widgets::text_edit::TextEdit;
|
||||
use egui::{Frame, Layout};
|
||||
use enostr::{FilledKeypair, FullKeypair, NoteId, RelayPool};
|
||||
use nostrdb::{Config, Ndb, Transaction};
|
||||
use nostrdb::{Ndb, Transaction};
|
||||
use tracing::info;
|
||||
|
||||
use notedeck::{ImageCache, NoteCache};
|
||||
@@ -257,38 +257,31 @@ fn post_button(interactive: bool) -> impl egui::Widget {
|
||||
|
||||
mod preview {
|
||||
use super::*;
|
||||
use notedeck::{App, AppContext};
|
||||
|
||||
pub struct PostPreview {
|
||||
ndb: Ndb,
|
||||
img_cache: ImageCache,
|
||||
note_cache: NoteCache,
|
||||
draft: Draft,
|
||||
poster: FullKeypair,
|
||||
}
|
||||
|
||||
impl PostPreview {
|
||||
fn new() -> Self {
|
||||
let ndb = Ndb::new(".", &Config::new()).expect("ndb");
|
||||
|
||||
PostPreview {
|
||||
ndb,
|
||||
img_cache: ImageCache::new(".".into()),
|
||||
note_cache: NoteCache::default(),
|
||||
draft: Draft::new(),
|
||||
poster: FullKeypair::generate(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl View for PostPreview {
|
||||
fn ui(&mut self, ui: &mut egui::Ui) {
|
||||
let txn = Transaction::new(&self.ndb).expect("txn");
|
||||
impl App for PostPreview {
|
||||
fn update(&mut self, app: &mut AppContext<'_>, ui: &mut egui::Ui) {
|
||||
let txn = Transaction::new(app.ndb).expect("txn");
|
||||
PostView::new(
|
||||
&self.ndb,
|
||||
app.ndb,
|
||||
&mut self.draft,
|
||||
PostType::New,
|
||||
&mut self.img_cache,
|
||||
&mut self.note_cache,
|
||||
app.img_cache,
|
||||
app.note_cache,
|
||||
self.poster.to_filled(),
|
||||
)
|
||||
.ui(&txn, ui);
|
||||
|
||||
@@ -1,37 +1,26 @@
|
||||
use crate::ui::View;
|
||||
|
||||
pub struct PreviewConfig {
|
||||
pub is_mobile: bool,
|
||||
}
|
||||
|
||||
pub trait Preview {
|
||||
type Prev: View;
|
||||
type Prev: notedeck::App;
|
||||
|
||||
fn preview(cfg: PreviewConfig) -> Self::Prev;
|
||||
}
|
||||
|
||||
pub struct PreviewApp {
|
||||
view: Box<dyn View>,
|
||||
}
|
||||
|
||||
impl<V> From<V> for PreviewApp
|
||||
where
|
||||
V: View + 'static,
|
||||
{
|
||||
fn from(v: V) -> Self {
|
||||
PreviewApp::new(v)
|
||||
}
|
||||
view: Box<dyn notedeck::App>,
|
||||
}
|
||||
|
||||
impl PreviewApp {
|
||||
pub fn new(view: impl View + 'static) -> PreviewApp {
|
||||
pub fn new(view: impl notedeck::App + 'static) -> PreviewApp {
|
||||
let view = Box::new(view);
|
||||
Self { view }
|
||||
}
|
||||
}
|
||||
|
||||
impl eframe::App for PreviewApp {
|
||||
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
||||
egui::CentralPanel::default().show(ctx, |ui| self.view.ui(ui));
|
||||
impl notedeck::App for PreviewApp {
|
||||
fn update(&mut self, app_ctx: &mut notedeck::AppContext<'_>, ui: &mut egui::Ui) {
|
||||
self.view.update(app_ctx, ui);
|
||||
}
|
||||
}
|
||||
|
||||
2
crates/notedeck_columns/src/ui/profile/menu.rs
Normal file
2
crates/notedeck_columns/src/ui/profile/menu.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
|
||||
// profile menu
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -184,6 +184,7 @@ fn get_connection_icon(status: &RelayStatus) -> egui::Image<'static> {
|
||||
mod preview {
|
||||
use super::*;
|
||||
use crate::test_data::sample_pool;
|
||||
use notedeck::{App, AppContext};
|
||||
|
||||
pub struct RelayViewPreview {
|
||||
pool: RelayPool,
|
||||
@@ -197,8 +198,8 @@ mod preview {
|
||||
}
|
||||
}
|
||||
|
||||
impl View for RelayViewPreview {
|
||||
fn ui(&mut self, ui: &mut egui::Ui) {
|
||||
impl App for RelayViewPreview {
|
||||
fn update(&mut self, _app: &mut AppContext<'_>, ui: &mut egui::Ui) {
|
||||
self.pool.try_recv();
|
||||
RelayView::new(RelayPoolManager::new(&mut self.pool)).ui(ui);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user