From c25fde4a06bdbaee3117324bab7c3082b92771e1 Mon Sep 17 00:00:00 2001 From: Ken Sedgwick Date: Tue, 24 Sep 2024 14:54:12 -0700 Subject: [PATCH] hitbox: handle hits on quoted notes correctly --- src/ui/note/contents.rs | 50 +++++++++++++++++++++++------------------ src/ui/note/mod.rs | 20 +++++++++++------ 2 files changed, 41 insertions(+), 29 deletions(-) diff --git a/src/ui/note/contents.rs b/src/ui/note/contents.rs index 084eb75..837e425 100644 --- a/src/ui/note/contents.rs +++ b/src/ui/note/contents.rs @@ -1,7 +1,8 @@ +use crate::actionbar::BarAction; use crate::images::ImageType; use crate::imgcache::ImageCache; use crate::notecache::NoteCache; -use crate::ui::note::NoteOptions; +use crate::ui::note::{NoteOptions, NoteResponse}; use crate::ui::ProfilePic; use crate::{colors, ui}; use egui::{Color32, Hyperlink, Image, RichText}; @@ -16,6 +17,7 @@ pub struct NoteContents<'a> { note: &'a Note<'a>, note_key: NoteKey, options: NoteOptions, + action: Option, } impl<'a> NoteContents<'a> { @@ -36,13 +38,18 @@ impl<'a> NoteContents<'a> { note, note_key, options, + action: None, } } + + pub fn action(&self) -> Option { + self.action + } } -impl egui::Widget for NoteContents<'_> { +impl egui::Widget for &mut NoteContents<'_> { fn ui(self, ui: &mut egui::Ui) -> egui::Response { - render_note_contents( + let result = render_note_contents( ui, self.ndb, self.img_cache, @@ -51,8 +58,9 @@ impl egui::Widget for NoteContents<'_> { self.note, self.note_key, self.options, - ) - .response + ); + self.action = result.action; + result.response } } @@ -66,7 +74,7 @@ pub fn render_note_preview( txn: &Transaction, id: &[u8; 32], _id_str: &str, -) -> egui::Response { +) -> NoteResponse { #[cfg(feature = "profiling")] puffin::profile_function!(); @@ -75,13 +83,13 @@ pub fn render_note_preview( if note.kind() == 1 { note } else { - return ui.colored_label( + return NoteResponse::new(ui.colored_label( Color32::RED, format!("TODO: can't preview kind {}", note.kind()), - ); + )); } } else { - return ui.colored_label(Color32::RED, "TODO: COULD NOT LOAD"); + return NoteResponse::new(ui.colored_label(Color32::RED, "TODO: COULD NOT LOAD")); /* return ui .horizontal(|ui| { @@ -103,19 +111,15 @@ pub fn render_note_preview( ui.visuals().noninteractive().bg_stroke.color, )) .show(ui, |ui| { - let resp = ui::NoteView::new(ndb, note_cache, img_cache, ¬e) + ui::NoteView::new(ndb, note_cache, img_cache, ¬e) .actionbar(false) .small_pfp(true) .wide(true) .note_previews(false) .options_button(true) - .show(ui); - - if let Some(context) = resp.context_selection { - context.process(ui, ¬e); - } + .show(ui) }) - .response + .inner } #[allow(clippy::too_many_arguments)] @@ -128,7 +132,7 @@ fn render_note_contents( note: &Note, note_key: NoteKey, options: NoteOptions, -) -> egui::InnerResponse<()> { +) -> NoteResponse { #[cfg(feature = "profiling")] puffin::profile_function!(); @@ -136,7 +140,7 @@ fn render_note_contents( let mut images: Vec = vec![]; let mut inline_note: Option<(&[u8; 32], &str)> = None; - let resp = ui.horizontal_wrapped(|ui| { + let response = ui.horizontal_wrapped(|ui| { let blocks = if let Ok(blocks) = ndb.get_blocks_by_key(txn, note_key) { blocks } else { @@ -204,9 +208,11 @@ fn render_note_contents( } }); - if let Some((id, block_str)) = inline_note { - render_note_preview(ui, ndb, note_cache, img_cache, txn, id, block_str); - } + let note_action = if let Some((id, block_str)) = inline_note { + render_note_preview(ui, ndb, note_cache, img_cache, txn, id, block_str).action + } else { + None + }; if !images.is_empty() && !options.has_textmode() { ui.add_space(2.0); @@ -215,7 +221,7 @@ fn render_note_contents( ui.add_space(2.0); } - resp + NoteResponse::new(response.response).with_action(note_action) } fn image_carousel( diff --git a/src/ui/note/mod.rs b/src/ui/note/mod.rs index 75dacc6..6259a27 100644 --- a/src/ui/note/mod.rs +++ b/src/ui/note/mod.rs @@ -281,7 +281,7 @@ impl<'a> NoteView<'a> { ) }); - ui.add(NoteContents::new( + ui.add(&mut NoteContents::new( self.ndb, self.img_cache, self.note_cache, @@ -476,7 +476,7 @@ impl<'a> NoteView<'a> { }); }); - let resp = ui.add(NoteContents::new( + let mut contents = NoteContents::new( self.ndb, self.img_cache, self.note_cache, @@ -484,10 +484,13 @@ impl<'a> NoteView<'a> { self.note, note_key, self.options(), - )); + ); + let resp = ui.add(&mut contents); + note_action = note_action.or(contents.action()); if self.options().has_actionbar() { - note_action = render_note_actionbar(ui, self.note.id(), note_key).inner; + let ab = render_note_actionbar(ui, self.note.id(), note_key); + note_action = note_action.or(ab.inner); } resp @@ -520,7 +523,7 @@ impl<'a> NoteView<'a> { } }); - ui.add(NoteContents::new( + let mut contents = NoteContents::new( self.ndb, self.img_cache, self.note_cache, @@ -528,10 +531,13 @@ impl<'a> NoteView<'a> { self.note, note_key, self.options(), - )); + ); + ui.add(&mut contents); + note_action = note_action.or(contents.action()); if self.options().has_actionbar() { - note_action = render_note_actionbar(ui, self.note.id(), note_key).inner; + let ab = render_note_actionbar(ui, self.note.id(), note_key); + note_action = note_action.or(ab.inner); } }); })