update NoteContentsDriller to NoteContext

Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
William Casarin
2025-03-07 12:29:56 -08:00
parent 95d618e7fe
commit 65bd6a65f9
11 changed files with 146 additions and 146 deletions

View File

@@ -13,7 +13,7 @@ use notedeck::{supported_mime_hosted_at_url, Images, MediaCacheType, NoteCache};
/// Aggregates dependencies to reduce the number of parameters
/// passed to inner UI elements, minimizing prop drilling.
pub struct NoteContentsDriller<'d> {
pub struct NoteContext<'d> {
pub ndb: &'d Ndb,
pub img_cache: &'d mut Images,
pub note_cache: &'d mut NoteCache,
@@ -21,7 +21,7 @@ pub struct NoteContentsDriller<'d> {
}
pub struct NoteContents<'a, 'd> {
driller: &'a mut NoteContentsDriller<'d>,
note_context: &'a mut NoteContext<'d>,
txn: &'a Transaction,
note: &'a Note<'a>,
action: Option<NoteAction>,
@@ -30,12 +30,12 @@ pub struct NoteContents<'a, 'd> {
impl<'a, 'd> NoteContents<'a, 'd> {
#[allow(clippy::too_many_arguments)]
pub fn new(
driller: &'a mut NoteContentsDriller<'d>,
note_context: &'a mut NoteContext<'d>,
txn: &'a Transaction,
note: &'a Note,
) -> Self {
NoteContents {
driller,
note_context,
txn,
note,
action: None,
@@ -49,7 +49,7 @@ impl<'a, 'd> NoteContents<'a, 'd> {
impl egui::Widget for &mut NoteContents<'_, '_> {
fn ui(self, ui: &mut egui::Ui) -> egui::Response {
let result = render_note_contents(ui, self.driller, self.txn, self.note);
let result = render_note_contents(ui, self.note_context, self.txn, self.note);
self.action = result.action;
result.response
}
@@ -60,7 +60,7 @@ impl egui::Widget for &mut NoteContents<'_, '_> {
#[allow(clippy::too_many_arguments)]
pub fn render_note_preview(
ui: &mut egui::Ui,
driller: &mut NoteContentsDriller,
note_context: &mut NoteContext,
txn: &Transaction,
id: &[u8; 32],
parent: NoteKey,
@@ -68,7 +68,7 @@ pub fn render_note_preview(
#[cfg(feature = "profiling")]
puffin::profile_function!();
let note = if let Ok(note) = driller.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
@@ -101,7 +101,7 @@ pub fn render_note_preview(
ui.visuals().noninteractive().bg_stroke.color,
))
.show(ui, |ui| {
ui::NoteView::new(driller, &note)
ui::NoteView::new(note_context, &note)
.actionbar(false)
.small_pfp(true)
.wide(true)
@@ -116,7 +116,7 @@ pub fn render_note_preview(
#[allow(clippy::too_many_arguments)]
fn render_note_contents(
ui: &mut egui::Ui,
driller: &mut NoteContentsDriller,
note_context: &mut NoteContext,
txn: &Transaction,
note: &Note,
) -> NoteResponse {
@@ -124,15 +124,15 @@ fn render_note_contents(
puffin::profile_function!();
let note_key = note.key().expect("todo: implement non-db notes");
let selectable = driller.options.has_selectable_text();
let selectable = note_context.options.has_selectable_text();
let mut images: Vec<(String, MediaCacheType)> = vec![];
let mut note_action: Option<NoteAction> = None;
let mut inline_note: Option<(&[u8; 32], &str)> = None;
let hide_media = driller.options.has_hide_media();
let hide_media = note_context.options.has_hide_media();
let link_color = ui.visuals().hyperlink_color;
let response = ui.horizontal_wrapped(|ui| {
let blocks = if let Ok(blocks) = driller.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());
@@ -147,7 +147,7 @@ fn render_note_contents(
BlockType::MentionBech32 => match block.as_mention().unwrap() {
Mention::Profile(profile) => {
let act =
ui::Mention::new(driller.ndb, driller.img_cache, txn, profile.pubkey())
ui::Mention::new(note_context.ndb, note_context.img_cache, txn, profile.pubkey())
.show(ui)
.inner;
if act.is_some() {
@@ -157,7 +157,7 @@ fn render_note_contents(
Mention::Pubkey(npub) => {
let act =
ui::Mention::new(driller.ndb, driller.img_cache, txn, npub.pubkey())
ui::Mention::new(note_context.ndb, note_context.img_cache, txn, npub.pubkey())
.show(ui)
.inner;
if act.is_some() {
@@ -165,11 +165,11 @@ fn render_note_contents(
}
}
Mention::Note(note) if driller.options.has_note_previews() => {
Mention::Note(note) if note_context.options.has_note_previews() => {
inline_note = Some((note.id(), block.as_str()));
}
Mention::Event(note) if driller.options.has_note_previews() => {
Mention::Event(note) if note_context.options.has_note_previews() => {
inline_note = Some((note.id(), block.as_str()));
}
@@ -196,7 +196,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 driller.img_cache.urls, url)
supported_mime_hosted_at_url(&mut note_context.img_cache.urls, url)
{
images.push((url.to_string(), cache_type));
true
@@ -217,7 +217,7 @@ fn render_note_contents(
BlockType::Text => {
#[cfg(feature = "profiling")]
puffin::profile_scope!("text contents");
if driller.options.has_scramble_text() {
if note_context.options.has_scramble_text() {
ui.add(egui::Label::new(rot13(block.as_str())).selectable(selectable));
} else {
ui.add(egui::Label::new(block.as_str()).selectable(selectable));
@@ -232,15 +232,15 @@ fn render_note_contents(
});
let preview_note_action = if let Some((id, _block_str)) = inline_note {
render_note_preview(ui, driller, txn, id, note_key).action
render_note_preview(ui, note_context, txn, id, note_key).action
} else {
None
};
if !images.is_empty() && !driller.options.has_textmode() {
if !images.is_empty() && !note_context.options.has_textmode() {
ui.add_space(2.0);
let carousel_id = egui::Id::new(("carousel", note.key().expect("expected tx note")));
image_carousel(ui, driller.img_cache, images, carousel_id);
image_carousel(ui, note_context.img_cache, images, carousel_id);
ui.add_space(2.0);
}