introduce NoteContext

Signed-off-by: kernelkind <kernelkind@gmail.com>
This commit is contained in:
kernelkind
2025-03-07 17:02:02 -05:00
parent d85c6043b7
commit a9f473e3c9
12 changed files with 368 additions and 430 deletions

View File

@@ -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);
}