notedeck

One damus client to rule them all
git clone git://jb55.com/notedeck
Log | Files | Refs | README | LICENSE

commit a9f473e3c9063eaadad0a2e67533a2e3d9767910
parent d85c6043b700783d825f6e5971f976e198fde112
Author: kernelkind <kernelkind@gmail.com>
Date:   Fri,  7 Mar 2025 17:02:02 -0500

introduce NoteContext

Signed-off-by: kernelkind <kernelkind@gmail.com>

Diffstat:
Mcrates/notedeck_columns/src/nav.rs | 27+++++++++++----------------
Mcrates/notedeck_columns/src/timeline/route.rs | 43++++++++++++++++---------------------------
Mcrates/notedeck_columns/src/ui/note/contents.rs | 78+++++++++++++++++++++++++++++++++++++++---------------------------------------
Mcrates/notedeck_columns/src/ui/note/mod.rs | 130+++++++++++++++++++++++++++++++++----------------------------------------------
Mcrates/notedeck_columns/src/ui/note/post.rs | 64+++++++++++++++++++++++++++++++++++-----------------------------
Mcrates/notedeck_columns/src/ui/note/quote_repost.rs | 26++++++++------------------
Mcrates/notedeck_columns/src/ui/note/reply.rs | 42++++++++++++++----------------------------
Mcrates/notedeck_columns/src/ui/note/reply_description.rs | 144+++++++++++++++++++++++++++++++++++++++++++++----------------------------------
Mcrates/notedeck_columns/src/ui/profile/mod.rs | 61++++++++++++++++++++++++++++++-------------------------------
Mcrates/notedeck_columns/src/ui/search/mod.rs | 31+++++++++++++------------------
Mcrates/notedeck_columns/src/ui/thread.rs | 60+++++++++++++++++++++++++++++-------------------------------
Mcrates/notedeck_columns/src/ui/timeline.rs | 94++++++++++++++++++++++++++++++++-----------------------------------------------
12 files changed, 369 insertions(+), 431 deletions(-)

diff --git a/crates/notedeck_columns/src/nav.rs b/crates/notedeck_columns/src/nav.rs @@ -16,7 +16,7 @@ use crate::{ column::NavTitle, configure_deck::ConfigureDeckView, edit_deck::{EditDeckResponse, EditDeckView}, - note::{PostAction, PostType}, + note::{contents::NoteContext, PostAction, PostType}, profile::EditProfileView, search::{FocusState, SearchView}, support::SupportView, @@ -244,12 +244,14 @@ fn render_nav_body( col: usize, inner_rect: egui::Rect, ) -> Option<RenderNavAction> { + let mut note_context = NoteContext { + ndb: ctx.ndb, + img_cache: ctx.img_cache, + note_cache: ctx.note_cache, + }; match top { Route::Timeline(kind) => render_timeline_route( - ctx.ndb, - ctx.img_cache, ctx.unknown_ids, - ctx.note_cache, &mut app.timeline_cache, ctx.accounts, kind, @@ -257,6 +259,7 @@ fn render_nav_body( app.note_options, depth, ui, + &mut note_context, ), Route::Accounts(amr) => { @@ -307,11 +310,9 @@ fn render_nav_body( let response = egui::ScrollArea::vertical() .show(ui, |ui| { ui::PostReplyView::new( - ctx.ndb, + &mut note_context, poster, draft, - ctx.note_cache, - ctx.img_cache, &note, inner_rect, app.note_options, @@ -349,10 +350,8 @@ fn render_nav_body( let response = egui::ScrollArea::vertical() .show(ui, |ui| { crate::ui::note::QuoteRepostView::new( - ctx.ndb, + &mut note_context, poster, - ctx.note_cache, - ctx.img_cache, draft, &note, inner_rect, @@ -376,11 +375,9 @@ fn render_nav_body( let txn = Transaction::new(ctx.ndb).expect("txn"); let post_response = ui::PostView::new( - ctx.ndb, + &mut note_context, draft, PostType::New, - ctx.img_cache, - ctx.note_cache, kp, inner_rect, app.note_options, @@ -421,13 +418,11 @@ fn render_nav_body( } SearchView::new( - ctx.ndb, &txn, - ctx.note_cache, - ctx.img_cache, &ctx.accounts.mutefun(), app.note_options, search_buffer, + &mut note_context, ) .show(ui) .map(RenderNavAction::NoteAction) diff --git a/crates/notedeck_columns/src/timeline/route.rs b/crates/notedeck_columns/src/timeline/route.rs @@ -2,19 +2,19 @@ use crate::{ nav::RenderNavAction, profile::ProfileAction, timeline::{TimelineCache, TimelineKind}, - ui::{self, note::NoteOptions, profile::ProfileView}, + ui::{ + self, + note::{contents::NoteContext, NoteOptions}, + profile::ProfileView, + }, }; use enostr::Pubkey; -use nostrdb::Ndb; -use notedeck::{Accounts, Images, MuteFun, NoteCache, UnknownIds}; +use notedeck::{Accounts, MuteFun, UnknownIds}; #[allow(clippy::too_many_arguments)] pub fn render_timeline_route( - ndb: &Ndb, - img_cache: &mut Images, unknown_ids: &mut UnknownIds, - note_cache: &mut NoteCache, timeline_cache: &mut TimelineCache, accounts: &mut Accounts, kind: &TimelineKind, @@ -22,6 +22,7 @@ pub fn render_timeline_route( mut note_options: NoteOptions, depth: usize, ui: &mut egui::Ui, + note_context: &mut NoteContext, ) -> Option<RenderNavAction> { if kind == &TimelineKind::Universe { note_options.set_hide_media(true); @@ -38,11 +39,9 @@ pub fn render_timeline_route( let note_action = ui::TimelineView::new( kind, timeline_cache, - ndb, - note_cache, - img_cache, - note_options, &accounts.mutefun(), + note_context, + note_options, ) .ui(ui); @@ -54,26 +53,22 @@ pub fn render_timeline_route( render_profile_route( pubkey, accounts, - ndb, timeline_cache, - img_cache, - note_cache, unknown_ids, col, ui, &accounts.mutefun(), note_options, + note_context, ) } else { // we render profiles like timelines if they are at the root let note_action = ui::TimelineView::new( kind, timeline_cache, - ndb, - note_cache, - img_cache, - note_options, &accounts.mutefun(), + note_context, + note_options, ) .ui(ui); @@ -83,13 +78,11 @@ pub fn render_timeline_route( TimelineKind::Thread(id) => ui::ThreadView::new( timeline_cache, - ndb, - note_cache, unknown_ids, - img_cache, id.selected_or_root(), note_options, &accounts.mutefun(), + note_context, ) .id_source(egui::Id::new(("threadscroll", col))) .ui(ui) @@ -101,27 +94,23 @@ pub fn render_timeline_route( pub fn render_profile_route( pubkey: &Pubkey, accounts: &Accounts, - ndb: &Ndb, timeline_cache: &mut TimelineCache, - img_cache: &mut Images, - note_cache: &mut NoteCache, unknown_ids: &mut UnknownIds, col: usize, ui: &mut egui::Ui, is_muted: &MuteFun, note_options: NoteOptions, + note_context: &mut NoteContext, ) -> Option<RenderNavAction> { let action = ProfileView::new( pubkey, accounts, col, timeline_cache, - ndb, - note_cache, - img_cache, + note_options, unknown_ids, is_muted, - note_options, + note_context, ) .ui(ui); diff --git a/crates/notedeck_columns/src/ui/note/contents.rs b/crates/notedeck_columns/src/ui/note/contents.rs @@ -11,30 +11,32 @@ use tracing::warn; use notedeck::{supported_mime_hosted_at_url, Images, MediaCacheType, NoteCache}; -pub struct NoteContents<'a> { - ndb: &'a Ndb, - img_cache: &'a mut Images, - note_cache: &'a mut NoteCache, +/// Aggregates dependencies to reduce the number of parameters +/// passed to inner UI elements, minimizing prop drilling. +pub struct NoteContext<'d> { + pub ndb: &'d Ndb, + pub img_cache: &'d mut Images, + pub note_cache: &'d mut NoteCache, +} + +pub struct NoteContents<'a, 'd> { + note_context: &'a mut NoteContext<'d>, txn: &'a Transaction, note: &'a Note<'a>, options: NoteOptions, action: Option<NoteAction>, } -impl<'a> NoteContents<'a> { +impl<'a, 'd> NoteContents<'a, 'd> { #[allow(clippy::too_many_arguments)] pub fn new( - ndb: &'a Ndb, - img_cache: &'a mut Images, - note_cache: &'a mut NoteCache, + note_context: &'a mut NoteContext<'d>, txn: &'a Transaction, note: &'a Note, options: ui::note::NoteOptions, ) -> Self { NoteContents { - ndb, - img_cache, - note_cache, + note_context, txn, note, options, @@ -47,17 +49,9 @@ impl<'a> NoteContents<'a> { } } -impl egui::Widget for &mut NoteContents<'_> { +impl egui::Widget for &mut NoteContents<'_, '_> { fn ui(self, ui: &mut egui::Ui) -> egui::Response { - let result = render_note_contents( - ui, - self.ndb, - self.img_cache, - self.note_cache, - self.txn, - self.note, - self.options, - ); + let result = render_note_contents(ui, self.note_context, self.txn, self.note, self.options); self.action = result.action; result.response } @@ -68,9 +62,7 @@ impl egui::Widget for &mut NoteContents<'_> { #[allow(clippy::too_many_arguments)] pub fn render_note_preview( ui: &mut egui::Ui, - ndb: &Ndb, - note_cache: &mut NoteCache, - img_cache: &mut Images, + note_context: &mut NoteContext, txn: &Transaction, id: &[u8; 32], parent: NoteKey, @@ -79,7 +71,7 @@ pub fn render_note_preview( #[cfg(feature = "profiling")] puffin::profile_function!(); - let note = if let Ok(note) = ndb.get_note_by_id(txn, id) { + let note = if let Ok(note) = note_context.ndb.get_note_by_id(txn, id) { // TODO: support other preview kinds if note.kind() == 1 { note @@ -112,7 +104,7 @@ pub fn render_note_preview( ui.visuals().noninteractive().bg_stroke.color, )) .show(ui, |ui| { - ui::NoteView::new(ndb, note_cache, img_cache, &note, note_options) + ui::NoteView::new(note_context, &note, note_options) .actionbar(false) .small_pfp(true) .wide(true) @@ -128,9 +120,7 @@ pub fn render_note_preview( #[allow(clippy::too_many_arguments)] fn render_note_contents( ui: &mut egui::Ui, - ndb: &Ndb, - img_cache: &mut Images, - note_cache: &mut NoteCache, + note_context: &mut NoteContext, txn: &Transaction, note: &Note, options: NoteOptions, @@ -152,7 +142,7 @@ fn render_note_contents( } let response = ui.horizontal_wrapped(|ui| { - let blocks = if let Ok(blocks) = ndb.get_blocks_by_key(txn, note_key) { + let blocks = if let Ok(blocks) = note_context.ndb.get_blocks_by_key(txn, note_key) { blocks } else { warn!("missing note content blocks? '{}'", note.content()); @@ -166,18 +156,28 @@ fn render_note_contents( match block.blocktype() { BlockType::MentionBech32 => match block.as_mention().unwrap() { Mention::Profile(profile) => { - let act = ui::Mention::new(ndb, img_cache, txn, profile.pubkey()) - .show(ui) - .inner; + let act = ui::Mention::new( + note_context.ndb, + note_context.img_cache, + txn, + profile.pubkey(), + ) + .show(ui) + .inner; if act.is_some() { note_action = act; } } Mention::Pubkey(npub) => { - let act = ui::Mention::new(ndb, img_cache, txn, npub.pubkey()) - .show(ui) - .inner; + let act = ui::Mention::new( + note_context.ndb, + note_context.img_cache, + txn, + npub.pubkey(), + ) + .show(ui) + .inner; if act.is_some() { note_action = act; } @@ -214,7 +214,7 @@ fn render_note_contents( let mut found_supported = || -> bool { let url = block.as_str(); if let Some(cache_type) = - supported_mime_hosted_at_url(&mut img_cache.urls, url) + supported_mime_hosted_at_url(&mut note_context.img_cache.urls, url) { images.push((url.to_string(), cache_type)); true @@ -250,7 +250,7 @@ fn render_note_contents( }); let preview_note_action = if let Some((id, _block_str)) = inline_note { - render_note_preview(ui, ndb, note_cache, img_cache, txn, id, note_key, options).action + render_note_preview(ui, note_context, txn, id, note_key, options).action } else { None }; @@ -258,7 +258,7 @@ fn render_note_contents( if !images.is_empty() && !options.has_textmode() { ui.add_space(2.0); let carousel_id = egui::Id::new(("carousel", note.key().expect("expected tx note"))); - image_carousel(ui, img_cache, images, carousel_id); + image_carousel(ui, note_context.img_cache, images, carousel_id); ui.add_space(2.0); } diff --git a/crates/notedeck_columns/src/ui/note/mod.rs b/crates/notedeck_columns/src/ui/note/mod.rs @@ -7,6 +7,7 @@ pub mod reply; pub mod reply_description; pub use contents::NoteContents; +use contents::NoteContext; pub use context::{NoteContextButton, NoteContextSelection}; pub use options::NoteOptions; pub use post::{PostAction, PostResponse, PostType, PostView}; @@ -25,14 +26,12 @@ use egui::emath::{pos2, Vec2}; use egui::{Id, Label, Pos2, Rect, Response, RichText, Sense}; use enostr::{NoteId, Pubkey}; use nostrdb::{Ndb, Note, NoteKey, Transaction}; -use notedeck::{CachedNote, Images, NoteCache, NotedeckTextStyle}; +use notedeck::{CachedNote, NoteCache, NotedeckTextStyle}; use super::profile::preview::one_line_display_name_widget; -pub struct NoteView<'a> { - ndb: &'a Ndb, - note_cache: &'a mut NoteCache, - img_cache: &'a mut Images, +pub struct NoteView<'a, 'd> { + note_context: &'a mut NoteContext<'d>, parent: Option<NoteKey>, note: &'a nostrdb::Note<'a>, flags: NoteOptions, @@ -64,17 +63,15 @@ impl NoteResponse { } } -impl View for NoteView<'_> { +impl View for NoteView<'_, '_> { fn ui(&mut self, ui: &mut egui::Ui) { self.show(ui); } } -impl<'a> NoteView<'a> { +impl<'a, 'd> NoteView<'a, 'd> { pub fn new( - ndb: &'a Ndb, - note_cache: &'a mut NoteCache, - img_cache: &'a mut Images, + note_context: &'a mut NoteContext<'d>, note: &'a nostrdb::Note<'a>, mut flags: NoteOptions, ) -> Self { @@ -83,9 +80,7 @@ impl<'a> NoteView<'a> { let parent: Option<NoteKey> = None; Self { - ndb, - note_cache, - img_cache, + note_context, parent, note, flags, @@ -155,12 +150,16 @@ impl<'a> NoteView<'a> { let txn = self.note.txn().expect("todo: implement non-db notes"); ui.with_layout(egui::Layout::left_to_right(egui::Align::TOP), |ui| { - let profile = self.ndb.get_profile_by_pubkey(txn, self.note.pubkey()); + let profile = self + .note_context + .ndb + .get_profile_by_pubkey(txn, self.note.pubkey()); //ui.horizontal(|ui| { ui.spacing_mut().item_spacing.x = 2.0; let cached_note = self + .note_context .note_cache .cached_note_or_insert_mut(note_key, self.note); @@ -180,9 +179,7 @@ impl<'a> NoteView<'a> { }); ui.add(&mut NoteContents::new( - self.ndb, - self.img_cache, - self.note_cache, + self.note_context, txn, self.note, self.flags, @@ -231,14 +228,17 @@ impl<'a> NoteView<'a> { anim_speed, ); - ui.put(rect, ui::ProfilePic::new(self.img_cache, pic).size(size)) - .on_hover_ui_at_pointer(|ui| { - ui.set_max_width(300.0); - ui.add(ui::ProfilePreview::new( - profile.as_ref().unwrap(), - self.img_cache, - )); - }); + ui.put( + rect, + ui::ProfilePic::new(self.note_context.img_cache, pic).size(size), + ) + .on_hover_ui_at_pointer(|ui| { + ui.set_max_width(300.0); + ui.add(ui::ProfilePreview::new( + profile.as_ref().unwrap(), + self.note_context.img_cache, + )); + }); if resp.hovered() || resp.clicked() { ui::show_pointer(ui); @@ -254,7 +254,7 @@ impl<'a> NoteView<'a> { ui.put( rect, - ui::ProfilePic::new(self.img_cache, ui::ProfilePic::no_pfp_url()) + ui::ProfilePic::new(self.note_context.img_cache, ui::ProfilePic::no_pfp_url()) .size(pfp_size), ) .interact(sense) @@ -267,8 +267,11 @@ impl<'a> NoteView<'a> { NoteResponse::new(self.textmode_ui(ui)) } else { let txn = self.note.txn().expect("txn"); - if let Some(note_to_repost) = get_reposted_note(self.ndb, txn, self.note) { - let profile = self.ndb.get_profile_by_pubkey(txn, self.note.pubkey()); + if let Some(note_to_repost) = get_reposted_note(self.note_context.ndb, txn, self.note) { + let profile = self + .note_context + .ndb + .get_profile_by_pubkey(txn, self.note.pubkey()); let style = NotedeckTextStyle::Small; ui.horizontal(|ui| { @@ -285,7 +288,7 @@ impl<'a> NoteView<'a> { if let Ok(rec) = &profile { resp.on_hover_ui_at_pointer(|ui| { ui.set_max_width(300.0); - ui.add(ui::ProfilePreview::new(rec, self.img_cache)); + ui.add(ui::ProfilePreview::new(rec, self.note_context.img_cache)); }); } let color = ui.style().visuals.noninteractive().fg_stroke.color; @@ -296,14 +299,7 @@ impl<'a> NoteView<'a> { .text_style(style.text_style()), ); }); - NoteView::new( - self.ndb, - self.note_cache, - self.img_cache, - &note_to_repost, - self.flags, - ) - .show(ui) + NoteView::new(self.note_context, &note_to_repost, self.flags).show(ui) } else { self.show_standard(ui) } @@ -340,7 +336,10 @@ impl<'a> NoteView<'a> { let mut selected_option: Option<NoteContextSelection> = None; let hitbox_id = note_hitbox_id(note_key, self.options(), self.parent); - let profile = self.ndb.get_profile_by_pubkey(txn, self.note.pubkey()); + let profile = self + .note_context + .ndb + .get_profile_by_pubkey(txn, self.note.pubkey()); let maybe_hitbox = maybe_note_hitbox(ui, hitbox_id); // wide design @@ -357,12 +356,18 @@ impl<'a> NoteView<'a> { ui.vertical(|ui| { ui.add_sized([size.x, self.options().pfp_size()], |ui: &mut egui::Ui| { ui.horizontal_centered(|ui| { - NoteView::note_header(ui, self.note_cache, self.note, &profile); + NoteView::note_header( + ui, + self.note_context.note_cache, + self.note, + &profile, + ); }) .response }); let note_reply = self + .note_context .note_cache .cached_note_or_insert_mut(note_key, self.note) .reply @@ -371,15 +376,7 @@ impl<'a> NoteView<'a> { if note_reply.reply().is_some() { let action = ui .horizontal(|ui| { - reply_desc( - ui, - txn, - &note_reply, - self.ndb, - self.img_cache, - self.note_cache, - self.flags, - ) + reply_desc(ui, txn, &note_reply, self.note_context, self.flags) }) .inner; @@ -390,14 +387,7 @@ impl<'a> NoteView<'a> { }); }); - let mut contents = NoteContents::new( - self.ndb, - self.img_cache, - self.note_cache, - txn, - self.note, - self.options(), - ); + let mut contents = NoteContents::new(self.note_context, txn, self.note, self.flags); ui.add(&mut contents); @@ -423,26 +413,20 @@ impl<'a> NoteView<'a> { }; ui.with_layout(egui::Layout::top_down(egui::Align::LEFT), |ui| { - NoteView::note_header(ui, self.note_cache, self.note, &profile); + NoteView::note_header(ui, self.note_context.note_cache, self.note, &profile); ui.horizontal(|ui| { ui.spacing_mut().item_spacing.x = 2.0; let note_reply = self + .note_context .note_cache .cached_note_or_insert_mut(note_key, self.note) .reply .borrow(self.note.tags()); if note_reply.reply().is_some() { - let action = reply_desc( - ui, - txn, - &note_reply, - self.ndb, - self.img_cache, - self.note_cache, - self.flags, - ); + let action = + reply_desc(ui, txn, &note_reply, self.note_context, self.flags); if action.is_some() { note_action = action; @@ -450,14 +434,8 @@ impl<'a> NoteView<'a> { } }); - let mut contents = NoteContents::new( - self.ndb, - self.img_cache, - self.note_cache, - txn, - self.note, - self.options(), - ); + let mut contents = + NoteContents::new(self.note_context, txn, self.note, self.flags); ui.add(&mut contents); if let Some(action) = contents.action() { @@ -490,8 +468,8 @@ impl<'a> NoteView<'a> { let note_action = if note_hitbox_clicked(ui, hitbox_id, &response.rect, maybe_hitbox) { if let Ok(selection) = ThreadSelection::from_note_id( - self.ndb, - self.note_cache, + self.note_context.ndb, + self.note_context.note_cache, self.note.txn().unwrap(), NoteId::new(*self.note.id()), ) { diff --git a/crates/notedeck_columns/src/ui/note/post.rs b/crates/notedeck_columns/src/ui/note/post.rs @@ -5,7 +5,7 @@ use crate::post::{downcast_post_buffer, MentionType, NewPost}; use crate::profile::get_display_name; use crate::ui::images::render_images; use crate::ui::search_results::SearchResultsView; -use crate::ui::{self, note::NoteOptions, Preview, PreviewConfig}; +use crate::ui::{self, Preview, PreviewConfig}; use crate::Result; use egui::text::{CCursorRange, LayoutJob}; use egui::text_edit::TextEditOutput; @@ -14,18 +14,17 @@ use egui::{vec2, Frame, Layout, Margin, Pos2, ScrollArea, Sense, TextBuffer}; use enostr::{FilledKeypair, FullKeypair, NoteId, Pubkey, RelayPool}; use nostrdb::{Ndb, Transaction}; -use notedeck::{supported_mime_hosted_at_url, Images, NoteCache}; +use notedeck::supported_mime_hosted_at_url; use tracing::error; -use super::contents::render_note_preview; +use super::contents::{render_note_preview, NoteContext}; use super::NoteContextSelection; +use super::NoteOptions; -pub struct PostView<'a> { - ndb: &'a Ndb, +pub struct PostView<'a, 'd> { + note_context: &'a mut NoteContext<'d>, draft: &'a mut Draft, post_type: PostType, - img_cache: &'a mut Images, - note_cache: &'a mut NoteCache, poster: FilledKeypair<'a>, id_source: Option<egui::Id>, inner_rect: egui::Rect, @@ -85,24 +84,20 @@ pub struct PostResponse { pub context_selection: Option<NoteContextSelection>, } -impl<'a> PostView<'a> { +impl<'a, 'd> PostView<'a, 'd> { #[allow(clippy::too_many_arguments)] pub fn new( - ndb: &'a Ndb, + note_context: &'a mut NoteContext<'d>, draft: &'a mut Draft, post_type: PostType, - img_cache: &'a mut Images, - note_cache: &'a mut NoteCache, poster: FilledKeypair<'a>, inner_rect: egui::Rect, note_options: NoteOptions, ) -> Self { let id_source: Option<egui::Id> = None; PostView { - ndb, + note_context, draft, - img_cache, - note_cache, poster, id_source, post_type, @@ -123,17 +118,21 @@ impl<'a> PostView<'a> { // TODO: refactor pfp control to do all of this for us let poster_pfp = self + .note_context .ndb .get_profile_by_pubkey(txn, self.poster.pubkey.bytes()) .as_ref() .ok() - .and_then(|p| Some(ui::ProfilePic::from_profile(self.img_cache, p)?.size(pfp_size))); + .and_then(|p| { + Some(ui::ProfilePic::from_profile(self.note_context.img_cache, p)?.size(pfp_size)) + }); if let Some(pfp) = poster_pfp { ui.add(pfp); } else { ui.add( - ui::ProfilePic::new(self.img_cache, ui::ProfilePic::no_pfp_url()).size(pfp_size), + ui::ProfilePic::new(self.note_context.img_cache, ui::ProfilePic::no_pfp_url()) + .size(pfp_size), ); } @@ -239,20 +238,25 @@ impl<'a> PostView<'a> { hint_rect }; - let res = if let Ok(res) = self.ndb.search_profile(txn, mention_str, 10) { + let res = if let Ok(res) = self.note_context.ndb.search_profile(txn, mention_str, 10) { res } else { return; }; - let resp = - SearchResultsView::new(self.img_cache, self.ndb, txn, &res).show_in_rect(hint_rect, ui); + let resp = SearchResultsView::new( + self.note_context.img_cache, + self.note_context.ndb, + txn, + &res, + ) + .show_in_rect(hint_rect, ui); match resp { ui::search_results::SearchResultsResponse::SelectResult(selection) => { if let Some(hint_index) = selection { if let Some(pk) = res.get(hint_index) { - let record = self.ndb.get_profile_by_pubkey(txn, pk); + let record = self.note_context.ndb.get_profile_by_pubkey(txn, pk); self.draft.buffer.select_mention_and_replace_name( mention.index, @@ -327,9 +331,7 @@ impl<'a> PostView<'a> { ui.set_max_width(set_width); let resp = render_note_preview( ui, - self.ndb, - self.note_cache, - self.img_cache, + self.note_context, txn, id.bytes(), nostrdb::NoteKey::new(0), @@ -420,11 +422,11 @@ impl<'a> PostView<'a> { }; if let Some(cache_type) = - supported_mime_hosted_at_url(&mut self.img_cache.urls, &media.url) + supported_mime_hosted_at_url(&mut self.note_context.img_cache.urls, &media.url) { render_images( ui, - self.img_cache, + self.note_context.img_cache, &media.url, crate::images::ImageType::Content(width, height), cache_type, @@ -727,12 +729,16 @@ mod preview { impl App for PostPreview { fn update(&mut self, app: &mut AppContext<'_>, ui: &mut egui::Ui) { let txn = Transaction::new(app.ndb).expect("txn"); + let mut note_context = NoteContext { + ndb: app.ndb, + img_cache: app.img_cache, + note_cache: app.note_cache, + }; + PostView::new( - app.ndb, + &mut note_context, &mut self.draft, PostType::New, - app.img_cache, - app.note_cache, self.poster.to_filled(), ui.available_rect_before_wrap(), NoteOptions::default(), @@ -741,7 +747,7 @@ mod preview { } } - impl Preview for PostView<'_> { + impl Preview for PostView<'_, '_> { type Prev = PostPreview; fn preview(_cfg: PreviewConfig) -> Self::Prev { diff --git a/crates/notedeck_columns/src/ui/note/quote_repost.rs b/crates/notedeck_columns/src/ui/note/quote_repost.rs @@ -1,19 +1,15 @@ use enostr::{FilledKeypair, NoteId}; -use nostrdb::Ndb; -use notedeck::{Images, NoteCache}; use crate::{ draft::Draft, - ui::{self, note::NoteOptions}, + ui::{self}, }; -use super::{PostResponse, PostType}; +use super::{contents::NoteContext, NoteOptions, PostResponse, PostType}; -pub struct QuoteRepostView<'a> { - ndb: &'a Ndb, +pub struct QuoteRepostView<'a, 'd> { + note_context: &'a mut NoteContext<'d>, poster: FilledKeypair<'a>, - note_cache: &'a mut NoteCache, - img_cache: &'a mut Images, draft: &'a mut Draft, quoting_note: &'a nostrdb::Note<'a>, id_source: Option<egui::Id>, @@ -21,13 +17,11 @@ pub struct QuoteRepostView<'a> { note_options: NoteOptions, } -impl<'a> QuoteRepostView<'a> { +impl<'a, 'd> QuoteRepostView<'a, 'd> { #[allow(clippy::too_many_arguments)] pub fn new( - ndb: &'a Ndb, + note_context: &'a mut NoteContext<'d>, poster: FilledKeypair<'a>, - note_cache: &'a mut NoteCache, - img_cache: &'a mut Images, draft: &'a mut Draft, quoting_note: &'a nostrdb::Note<'a>, inner_rect: egui::Rect, @@ -35,10 +29,8 @@ impl<'a> QuoteRepostView<'a> { ) -> Self { let id_source: Option<egui::Id> = None; QuoteRepostView { - ndb, + note_context, poster, - note_cache, - img_cache, draft, quoting_note, id_source, @@ -52,11 +44,9 @@ impl<'a> QuoteRepostView<'a> { let quoting_note_id = self.quoting_note.id(); let post_resp = ui::PostView::new( - self.ndb, + self.note_context, self.draft, PostType::Quote(NoteId::new(quoting_note_id.to_owned())), - self.img_cache, - self.note_cache, self.poster, self.inner_rect, self.note_options, diff --git a/crates/notedeck_columns/src/ui/note/reply.rs b/crates/notedeck_columns/src/ui/note/reply.rs @@ -1,16 +1,14 @@ use crate::draft::Draft; use crate::ui; -use crate::ui::note::{NoteOptions, PostResponse, PostType}; +use crate::ui::note::{PostResponse, PostType}; use enostr::{FilledKeypair, NoteId}; -use nostrdb::Ndb; -use notedeck::{Images, NoteCache}; +use super::contents::NoteContext; +use super::NoteOptions; -pub struct PostReplyView<'a> { - ndb: &'a Ndb, +pub struct PostReplyView<'a, 'd> { + note_context: &'a mut NoteContext<'d>, poster: FilledKeypair<'a>, - note_cache: &'a mut NoteCache, - img_cache: &'a mut Images, draft: &'a mut Draft, note: &'a nostrdb::Note<'a>, id_source: Option<egui::Id>, @@ -18,26 +16,22 @@ pub struct PostReplyView<'a> { note_options: NoteOptions, } -impl<'a> PostReplyView<'a> { +impl<'a, 'd> PostReplyView<'a, 'd> { #[allow(clippy::too_many_arguments)] pub fn new( - ndb: &'a Ndb, + note_context: &'a mut NoteContext<'d>, poster: FilledKeypair<'a>, draft: &'a mut Draft, - note_cache: &'a mut NoteCache, - img_cache: &'a mut Images, note: &'a nostrdb::Note<'a>, inner_rect: egui::Rect, note_options: NoteOptions, ) -> Self { let id_source: Option<egui::Id> = None; PostReplyView { - ndb, + note_context, poster, draft, note, - note_cache, - img_cache, id_source, inner_rect, note_options, @@ -71,17 +65,11 @@ impl<'a> PostReplyView<'a> { let selection = egui::Frame::none() .outer_margin(egui::Margin::same(note_offset)) .show(ui, |ui| { - ui::NoteView::new( - self.ndb, - self.note_cache, - self.img_cache, - self.note, - self.note_options, - ) - .actionbar(false) - .medium_pfp(true) - .options_button(true) - .show(ui) + ui::NoteView::new(self.note_context, self.note, self.note_options) + .actionbar(false) + .medium_pfp(true) + .options_button(true) + .show(ui) }) .inner .context_selection; @@ -92,11 +80,9 @@ impl<'a> PostReplyView<'a> { let mut post_response = { ui::PostView::new( - self.ndb, + self.note_context, self.draft, PostType::Reply(NoteId::new(*replying_to)), - self.img_cache, - self.note_cache, self.poster, self.inner_rect, self.note_options, diff --git a/crates/notedeck_columns/src/ui/note/reply_description.rs b/crates/notedeck_columns/src/ui/note/reply_description.rs @@ -1,19 +1,18 @@ use crate::{ actionbar::NoteAction, - ui::{self, note::NoteOptions}, + ui::{self}, }; use egui::{Label, RichText, Sense}; -use nostrdb::{Ndb, Note, NoteReply, Transaction}; -use notedeck::{Images, NoteCache}; +use nostrdb::{Note, NoteReply, Transaction}; + +use super::{contents::NoteContext, NoteOptions}; #[must_use = "Please handle the resulting note action"] pub fn reply_desc( ui: &mut egui::Ui, txn: &Transaction, note_reply: &NoteReply, - ndb: &Ndb, - img_cache: &mut Images, - note_cache: &mut NoteCache, + note_context: &mut NoteContext, note_options: NoteOptions, ) -> Option<NoteAction> { #[cfg(feature = "profiling")] @@ -27,38 +26,34 @@ pub fn reply_desc( let link_color = visuals.hyperlink_color; // note link renderer helper - let note_link = |ui: &mut egui::Ui, - note_cache: &mut NoteCache, - img_cache: &mut Images, - text: &str, - note: &Note<'_>| { - let r = ui.add( - Label::new(RichText::new(text).size(size).color(link_color)) - .sense(Sense::click()) - .selectable(selectable), - ); - - if r.clicked() { - // TODO: jump to note - } + let note_link = + |ui: &mut egui::Ui, note_context: &mut NoteContext, text: &str, note: &Note<'_>| { + let r = ui.add( + Label::new(RichText::new(text).size(size).color(link_color)) + .sense(Sense::click()) + .selectable(selectable), + ); - if r.hovered() { - r.on_hover_ui_at_pointer(|ui| { - ui.set_max_width(400.0); - ui::NoteView::new(ndb, note_cache, img_cache, note, note_options) - .actionbar(false) - .wide(true) - .is_preview(true) - .show(ui); - }); - } - }; + if r.clicked() { + // TODO: jump to note + } + + if r.hovered() { + r.on_hover_ui_at_pointer(|ui| { + ui.set_max_width(400.0); + ui::NoteView::new(note_context, note, note_options) + .actionbar(false) + .wide(true) + .show(ui); + }); + } + }; ui.add(Label::new(RichText::new("replying to").size(size).color(color)).selectable(selectable)); let reply = note_reply.reply()?; - let reply_note = if let Ok(reply_note) = ndb.get_note_by_id(txn, reply.id) { + let reply_note = if let Ok(reply_note) = note_context.ndb.get_note_by_id(txn, reply.id) { reply_note } else { ui.add(Label::new(RichText::new("a note").size(size).color(color)).selectable(selectable)); @@ -67,11 +62,16 @@ pub fn reply_desc( if note_reply.is_reply_to_root() { // We're replying to the root, let's show this - let action = ui::Mention::new(ndb, img_cache, txn, reply_note.pubkey()) - .size(size) - .selectable(selectable) - .show(ui) - .inner; + let action = ui::Mention::new( + note_context.ndb, + note_context.img_cache, + txn, + reply_note.pubkey(), + ) + .size(size) + .selectable(selectable) + .show(ui) + .inner; if action.is_some() { note_action = action; @@ -79,18 +79,23 @@ pub fn reply_desc( ui.add(Label::new(RichText::new("'s").size(size).color(color)).selectable(selectable)); - note_link(ui, note_cache, img_cache, "thread", &reply_note); + note_link(ui, note_context, "thread", &reply_note); } else if let Some(root) = note_reply.root() { // replying to another post in a thread, not the root - if let Ok(root_note) = ndb.get_note_by_id(txn, root.id) { + if let Ok(root_note) = note_context.ndb.get_note_by_id(txn, root.id) { if root_note.pubkey() == reply_note.pubkey() { // simply "replying to bob's note" when replying to bob in his thread - let action = ui::Mention::new(ndb, img_cache, txn, reply_note.pubkey()) - .size(size) - .selectable(selectable) - .show(ui) - .inner; + let action = ui::Mention::new( + note_context.ndb, + note_context.img_cache, + txn, + reply_note.pubkey(), + ) + .size(size) + .selectable(selectable) + .show(ui) + .inner; if action.is_some() { note_action = action; @@ -100,15 +105,20 @@ pub fn reply_desc( Label::new(RichText::new("'s").size(size).color(color)).selectable(selectable), ); - note_link(ui, note_cache, img_cache, "note", &reply_note); + note_link(ui, note_context, "note", &reply_note); } else { // replying to bob in alice's thread - let action = ui::Mention::new(ndb, img_cache, txn, reply_note.pubkey()) - .size(size) - .selectable(selectable) - .show(ui) - .inner; + let action = ui::Mention::new( + note_context.ndb, + note_context.img_cache, + txn, + reply_note.pubkey(), + ) + .size(size) + .selectable(selectable) + .show(ui) + .inner; if action.is_some() { note_action = action; @@ -118,17 +128,22 @@ pub fn reply_desc( Label::new(RichText::new("'s").size(size).color(color)).selectable(selectable), ); - note_link(ui, note_cache, img_cache, "note", &reply_note); + note_link(ui, note_context, "note", &reply_note); ui.add( Label::new(RichText::new("in").size(size).color(color)).selectable(selectable), ); - let action = ui::Mention::new(ndb, img_cache, txn, root_note.pubkey()) - .size(size) - .selectable(selectable) - .show(ui) - .inner; + let action = ui::Mention::new( + note_context.ndb, + note_context.img_cache, + txn, + root_note.pubkey(), + ) + .size(size) + .selectable(selectable) + .show(ui) + .inner; if action.is_some() { note_action = action; @@ -138,14 +153,19 @@ pub fn reply_desc( Label::new(RichText::new("'s").size(size).color(color)).selectable(selectable), ); - note_link(ui, note_cache, img_cache, "thread", &root_note); + note_link(ui, note_context, "thread", &root_note); } } else { - let action = ui::Mention::new(ndb, img_cache, txn, reply_note.pubkey()) - .size(size) - .selectable(selectable) - .show(ui) - .inner; + let action = ui::Mention::new( + note_context.ndb, + note_context.img_cache, + txn, + reply_note.pubkey(), + ) + .size(size) + .selectable(selectable) + .show(ui) + .inner; if action.is_some() { note_action = action; diff --git a/crates/notedeck_columns/src/ui/profile/mod.rs b/crates/notedeck_columns/src/ui/profile/mod.rs @@ -6,7 +6,7 @@ pub use edit::EditProfileView; use egui::load::TexturePoll; use egui::{vec2, Color32, Label, Layout, Rect, RichText, Rounding, ScrollArea, Sense, Stroke}; use enostr::Pubkey; -use nostrdb::{Ndb, ProfileRecord, Transaction}; +use nostrdb::{ProfileRecord, Transaction}; pub use picture::ProfilePic; pub use preview::ProfilePreview; use tracing::error; @@ -16,26 +16,24 @@ use crate::{ colors, images, profile::get_display_name, timeline::{TimelineCache, TimelineKind}, - ui::{ - note::NoteOptions, - timeline::{tabs_ui, TimelineTabView}, - }, + ui::timeline::{tabs_ui, TimelineTabView}, NostrName, }; -use notedeck::{Accounts, Images, MuteFun, NoteCache, NotedeckTextStyle, UnknownIds}; +use notedeck::{Accounts, MuteFun, NotedeckTextStyle, UnknownIds}; -pub struct ProfileView<'a> { +use super::note::contents::NoteContext; +use super::note::NoteOptions; + +pub struct ProfileView<'a, 'd> { pubkey: &'a Pubkey, accounts: &'a Accounts, col_id: usize, timeline_cache: &'a mut TimelineCache, note_options: NoteOptions, - ndb: &'a Ndb, - note_cache: &'a mut NoteCache, - img_cache: &'a mut Images, unknown_ids: &'a mut UnknownIds, is_muted: &'a MuteFun, + note_context: &'a mut NoteContext<'d>, } pub enum ProfileViewAction { @@ -43,31 +41,27 @@ pub enum ProfileViewAction { Note(NoteAction), } -impl<'a> ProfileView<'a> { +impl<'a, 'd> ProfileView<'a, 'd> { #[allow(clippy::too_many_arguments)] pub fn new( pubkey: &'a Pubkey, accounts: &'a Accounts, col_id: usize, timeline_cache: &'a mut TimelineCache, - ndb: &'a Ndb, - note_cache: &'a mut NoteCache, - img_cache: &'a mut Images, + note_options: NoteOptions, unknown_ids: &'a mut UnknownIds, is_muted: &'a MuteFun, - note_options: NoteOptions, + note_context: &'a mut NoteContext<'d>, ) -> Self { ProfileView { pubkey, accounts, col_id, timeline_cache, - ndb, - note_cache, - img_cache, - unknown_ids, note_options, + unknown_ids, is_muted, + note_context, } } @@ -78,8 +72,12 @@ impl<'a> ProfileView<'a> { .id_salt(scroll_id) .show(ui, |ui| { let mut action = None; - let txn = Transaction::new(self.ndb).expect("txn"); - if let Ok(profile) = self.ndb.get_profile_by_pubkey(&txn, self.pubkey.bytes()) { + let txn = Transaction::new(self.note_context.ndb).expect("txn"); + if let Ok(profile) = self + .note_context + .ndb + .get_profile_by_pubkey(&txn, self.pubkey.bytes()) + { if self.profile_body(ui, profile) { action = Some(ProfileViewAction::EditProfile); } @@ -87,8 +85,8 @@ impl<'a> ProfileView<'a> { let profile_timeline = self .timeline_cache .notes( - self.ndb, - self.note_cache, + self.note_context.ndb, + self.note_context.note_cache, &txn, &TimelineKind::Profile(*self.pubkey), ) @@ -100,10 +98,10 @@ impl<'a> ProfileView<'a> { let reversed = false; // poll for new notes and insert them into our existing notes if let Err(e) = profile_timeline.poll_notes_into_view( - self.ndb, + self.note_context.ndb, &txn, self.unknown_ids, - self.note_cache, + self.note_context.note_cache, reversed, ) { error!("Profile::poll_notes_into_view: {e}"); @@ -114,10 +112,8 @@ impl<'a> ProfileView<'a> { reversed, self.note_options, &txn, - self.ndb, - self.note_cache, - self.img_cache, self.is_muted, + self.note_context, ) .show(ui) { @@ -149,9 +145,12 @@ impl<'a> ProfileView<'a> { ui.horizontal(|ui| { ui.put( pfp_rect, - ProfilePic::new(self.img_cache, get_profile_url(Some(&profile))) - .size(size) - .border(ProfilePic::border_stroke(ui)), + ProfilePic::new( + self.note_context.img_cache, + get_profile_url(Some(&profile)), + ) + .size(size) + .border(ProfilePic::border_stroke(ui)), ); if ui.add(copy_key_widget(&pfp_rect)).clicked() { diff --git a/crates/notedeck_columns/src/ui/search/mod.rs b/crates/notedeck_columns/src/ui/search/mod.rs @@ -1,12 +1,12 @@ use egui::{vec2, Align, Color32, RichText, Rounding, Stroke, TextEdit}; -use super::padding; +use super::{note::contents::NoteContext, padding}; use crate::{ actionbar::NoteAction, ui::{note::NoteOptions, timeline::TimelineTabView}, }; -use nostrdb::{Filter, Ndb, Transaction}; -use notedeck::{Images, MuteFun, NoteCache, NoteRef}; +use nostrdb::{Filter, Transaction}; +use notedeck::{MuteFun, NoteRef}; use std::time::{Duration, Instant}; use tracing::{error, info, warn}; @@ -14,34 +14,28 @@ mod state; pub use state::{FocusState, SearchQueryState, SearchState}; -pub struct SearchView<'a> { +pub struct SearchView<'a, 'd> { query: &'a mut SearchQueryState, - ndb: &'a Ndb, note_options: NoteOptions, txn: &'a Transaction, - note_cache: &'a mut NoteCache, - img_cache: &'a mut Images, is_muted: &'a MuteFun, + note_context: &'a mut NoteContext<'d>, } -impl<'a> SearchView<'a> { +impl<'a, 'd> SearchView<'a, 'd> { pub fn new( - ndb: &'a Ndb, txn: &'a Transaction, - note_cache: &'a mut NoteCache, - img_cache: &'a mut Images, is_muted: &'a MuteFun, note_options: NoteOptions, query: &'a mut SearchQueryState, + note_context: &'a mut NoteContext<'d>, ) -> Self { Self { - ndb, txn, - note_cache, - img_cache, is_muted, query, note_options, + note_context, } } @@ -78,10 +72,8 @@ impl<'a> SearchView<'a> { reversed, self.note_options, self.txn, - self.ndb, - self.note_cache, - self.img_cache, self.is_muted, + self.note_context, ) .show(ui) }) @@ -105,7 +97,10 @@ impl<'a> SearchView<'a> { // TODO: execute in thread let before = Instant::now(); - let qrs = self.ndb.query(self.txn, &[filter], max_results as i32); + let qrs = self + .note_context + .ndb + .query(self.txn, &[filter], max_results as i32); let after = Instant::now(); let duration = after - before; diff --git a/crates/notedeck_columns/src/ui/thread.rs b/crates/notedeck_columns/src/ui/thread.rs @@ -1,50 +1,46 @@ use crate::{ actionbar::NoteAction, timeline::{ThreadSelection, TimelineCache, TimelineKind}, - ui::note::NoteOptions, }; -use nostrdb::{Ndb, Transaction}; -use notedeck::{Images, MuteFun, NoteCache, RootNoteId, UnknownIds}; +use nostrdb::Transaction; +use notedeck::{MuteFun, RootNoteId, UnknownIds}; use tracing::error; -use super::timeline::TimelineTabView; +use super::{ + note::{contents::NoteContext, NoteOptions}, + timeline::TimelineTabView, +}; -pub struct ThreadView<'a> { +pub struct ThreadView<'a, 'd> { timeline_cache: &'a mut TimelineCache, - ndb: &'a Ndb, - note_cache: &'a mut NoteCache, unknown_ids: &'a mut UnknownIds, - img_cache: &'a mut Images, selected_note_id: &'a [u8; 32], note_options: NoteOptions, id_source: egui::Id, is_muted: &'a MuteFun, + note_context: &'a mut NoteContext<'d>, } -impl<'a> ThreadView<'a> { +impl<'a, 'd> ThreadView<'a, 'd> { #[allow(clippy::too_many_arguments)] pub fn new( timeline_cache: &'a mut TimelineCache, - ndb: &'a Ndb, - note_cache: &'a mut NoteCache, unknown_ids: &'a mut UnknownIds, - img_cache: &'a mut Images, selected_note_id: &'a [u8; 32], note_options: NoteOptions, is_muted: &'a MuteFun, + note_context: &'a mut NoteContext<'d>, ) -> Self { let id_source = egui::Id::new("threadscroll_threadview"); ThreadView { timeline_cache, - ndb, - note_cache, unknown_ids, - img_cache, selected_note_id, note_options, id_source, is_muted, + note_context, } } @@ -54,7 +50,7 @@ impl<'a> ThreadView<'a> { } pub fn ui(&mut self, ui: &mut egui::Ui) -> Option<NoteAction> { - let txn = Transaction::new(self.ndb).expect("txn"); + let txn = Transaction::new(self.note_context.ndb).expect("txn"); ui.label( egui::RichText::new("Threads ALPHA! It's not done. Things will be broken.") @@ -67,21 +63,25 @@ impl<'a> ThreadView<'a> { .auto_shrink([false, false]) .scroll_bar_visibility(egui::scroll_area::ScrollBarVisibility::AlwaysVisible) .show(ui, |ui| { - let root_id = - match RootNoteId::new(self.ndb, self.note_cache, &txn, self.selected_note_id) { - Ok(root_id) => root_id, + let root_id = match RootNoteId::new( + self.note_context.ndb, + self.note_context.note_cache, + &txn, + self.selected_note_id, + ) { + Ok(root_id) => root_id, - Err(err) => { - ui.label(format!("Error loading thread: {:?}", err)); - return None; - } - }; + Err(err) => { + ui.label(format!("Error loading thread: {:?}", err)); + return None; + } + }; let thread_timeline = self .timeline_cache .notes( - self.ndb, - self.note_cache, + self.note_context.ndb, + self.note_context.note_cache, &txn, &TimelineKind::Thread(ThreadSelection::from_root_id(root_id.to_owned())), ) @@ -92,10 +92,10 @@ impl<'a> ThreadView<'a> { let reversed = true; // poll for new notes and insert them into our existing notes if let Err(err) = thread_timeline.poll_notes_into_view( - self.ndb, + self.note_context.ndb, &txn, self.unknown_ids, - self.note_cache, + self.note_context.note_cache, reversed, ) { error!("error polling notes into thread timeline: {err}"); @@ -106,10 +106,8 @@ impl<'a> ThreadView<'a> { true, self.note_options, &txn, - self.ndb, - self.note_cache, - self.img_cache, self.is_muted, + self.note_context, ) .show(ui) }) diff --git a/crates/notedeck_columns/src/ui/timeline.rs b/crates/notedeck_columns/src/ui/timeline.rs @@ -5,64 +5,57 @@ use crate::timeline::TimelineTab; use crate::{ timeline::{TimelineCache, TimelineKind, ViewFilter}, ui, - ui::note::NoteOptions, }; use egui::containers::scroll_area::ScrollBarVisibility; use egui::{vec2, Direction, Layout, Pos2, Stroke}; use egui_tabs::TabColor; -use nostrdb::{Ndb, Transaction}; +use nostrdb::Transaction; use notedeck::note::root_note_id_from_selected_id; -use notedeck::{Images, MuteFun, NoteCache}; +use notedeck::MuteFun; use tracing::{error, warn}; use super::anim::{AnimationHelper, ICON_EXPANSION_MULTIPLE}; +use super::note::contents::NoteContext; +use super::note::NoteOptions; -pub struct TimelineView<'a> { +pub struct TimelineView<'a, 'd> { timeline_id: &'a TimelineKind, timeline_cache: &'a mut TimelineCache, - ndb: &'a Ndb, - note_cache: &'a mut NoteCache, - img_cache: &'a mut Images, note_options: NoteOptions, reverse: bool, is_muted: &'a MuteFun, + note_context: &'a mut NoteContext<'d>, } -impl<'a> TimelineView<'a> { +impl<'a, 'd> TimelineView<'a, 'd> { #[allow(clippy::too_many_arguments)] pub fn new( timeline_id: &'a TimelineKind, timeline_cache: &'a mut TimelineCache, - ndb: &'a Ndb, - note_cache: &'a mut NoteCache, - img_cache: &'a mut Images, - note_options: NoteOptions, is_muted: &'a MuteFun, - ) -> TimelineView<'a> { + note_context: &'a mut NoteContext<'d>, + note_options: NoteOptions, + ) -> Self { let reverse = false; TimelineView { - ndb, timeline_id, timeline_cache, - note_cache, - img_cache, - reverse, note_options, + reverse, is_muted, + note_context, } } pub fn ui(&mut self, ui: &mut egui::Ui) -> Option<NoteAction> { timeline_ui( ui, - self.ndb, self.timeline_id, self.timeline_cache, - self.note_cache, - self.img_cache, self.reverse, self.note_options, self.is_muted, + self.note_context, ) } @@ -75,14 +68,12 @@ impl<'a> TimelineView<'a> { #[allow(clippy::too_many_arguments)] fn timeline_ui( ui: &mut egui::Ui, - ndb: &Ndb, timeline_id: &TimelineKind, timeline_cache: &mut TimelineCache, - note_cache: &mut NoteCache, - img_cache: &mut Images, reversed: bool, note_options: NoteOptions, is_muted: &MuteFun, + note_context: &mut NoteContext, ) -> Option<NoteAction> { //padding(4.0, ui, |ui| ui.heading("Notifications")); /* @@ -151,16 +142,15 @@ fn timeline_ui( return None; }; - let txn = Transaction::new(ndb).expect("failed to create txn"); + let txn = Transaction::new(note_context.ndb).expect("failed to create txn"); + TimelineTabView::new( timeline.current_view(), reversed, note_options, &txn, - ndb, - note_cache, - img_cache, is_muted, + note_context, ) .show(ui) }); @@ -315,38 +305,32 @@ fn shrink_range_to_width(range: egui::Rangef, width: f32) -> egui::Rangef { egui::Rangef::new(min, max) } -pub struct TimelineTabView<'a> { +pub struct TimelineTabView<'a, 'd> { tab: &'a TimelineTab, reversed: bool, note_options: NoteOptions, txn: &'a Transaction, - ndb: &'a Ndb, - note_cache: &'a mut NoteCache, - img_cache: &'a mut Images, is_muted: &'a MuteFun, + note_context: &'a mut NoteContext<'d>, } -impl<'a> TimelineTabView<'a> { +impl<'a, 'd> TimelineTabView<'a, 'd> { #[allow(clippy::too_many_arguments)] pub fn new( tab: &'a TimelineTab, reversed: bool, note_options: NoteOptions, txn: &'a Transaction, - ndb: &'a Ndb, - note_cache: &'a mut NoteCache, - img_cache: &'a mut Images, is_muted: &'a MuteFun, + note_context: &'a mut NoteContext<'d>, ) -> Self { Self { tab, reversed, - txn, note_options, - ndb, - note_cache, - img_cache, + txn, is_muted, + note_context, } } @@ -371,17 +355,21 @@ impl<'a> TimelineTabView<'a> { let note_key = self.tab.notes[ind].key; - let note = if let Ok(note) = self.ndb.get_note_by_key(self.txn, note_key) { - note - } else { - warn!("failed to query note {:?}", note_key); - return 0; - }; + let note = + if let Ok(note) = self.note_context.ndb.get_note_by_key(self.txn, note_key) { + note + } else { + warn!("failed to query note {:?}", note_key); + return 0; + }; // should we mute the thread? we might not have it! - let muted = if let Ok(root_id) = - root_note_id_from_selected_id(self.ndb, self.note_cache, self.txn, note.id()) - { + let muted = if let Ok(root_id) = root_note_id_from_selected_id( + self.note_context.ndb, + self.note_context.note_cache, + self.txn, + note.id(), + ) { is_muted(&note, root_id.bytes()) } else { false @@ -389,14 +377,8 @@ impl<'a> TimelineTabView<'a> { if !muted { ui::padding(8.0, ui, |ui| { - let resp = ui::NoteView::new( - self.ndb, - self.note_cache, - self.img_cache, - &note, - self.note_options, - ) - .show(ui); + let resp = + ui::NoteView::new(self.note_context, &note, self.note_options).show(ui); if let Some(note_action) = resp.action { action = Some(note_action)