hitbox: handle hits on quoted notes correctly

This commit is contained in:
Ken Sedgwick 2024-09-24 14:54:12 -07:00 committed by William Casarin
parent 5b7c9c9234
commit c25fde4a06
2 changed files with 41 additions and 29 deletions

View File

@ -1,7 +1,8 @@
use crate::actionbar::BarAction;
use crate::images::ImageType; use crate::images::ImageType;
use crate::imgcache::ImageCache; use crate::imgcache::ImageCache;
use crate::notecache::NoteCache; use crate::notecache::NoteCache;
use crate::ui::note::NoteOptions; use crate::ui::note::{NoteOptions, NoteResponse};
use crate::ui::ProfilePic; use crate::ui::ProfilePic;
use crate::{colors, ui}; use crate::{colors, ui};
use egui::{Color32, Hyperlink, Image, RichText}; use egui::{Color32, Hyperlink, Image, RichText};
@ -16,6 +17,7 @@ pub struct NoteContents<'a> {
note: &'a Note<'a>, note: &'a Note<'a>,
note_key: NoteKey, note_key: NoteKey,
options: NoteOptions, options: NoteOptions,
action: Option<BarAction>,
} }
impl<'a> NoteContents<'a> { impl<'a> NoteContents<'a> {
@ -36,13 +38,18 @@ impl<'a> NoteContents<'a> {
note, note,
note_key, note_key,
options, options,
action: None,
} }
} }
pub fn action(&self) -> Option<BarAction> {
self.action
}
} }
impl egui::Widget for NoteContents<'_> { impl egui::Widget for &mut NoteContents<'_> {
fn ui(self, ui: &mut egui::Ui) -> egui::Response { fn ui(self, ui: &mut egui::Ui) -> egui::Response {
render_note_contents( let result = render_note_contents(
ui, ui,
self.ndb, self.ndb,
self.img_cache, self.img_cache,
@ -51,8 +58,9 @@ impl egui::Widget for NoteContents<'_> {
self.note, self.note,
self.note_key, self.note_key,
self.options, self.options,
) );
.response self.action = result.action;
result.response
} }
} }
@ -66,7 +74,7 @@ pub fn render_note_preview(
txn: &Transaction, txn: &Transaction,
id: &[u8; 32], id: &[u8; 32],
_id_str: &str, _id_str: &str,
) -> egui::Response { ) -> NoteResponse {
#[cfg(feature = "profiling")] #[cfg(feature = "profiling")]
puffin::profile_function!(); puffin::profile_function!();
@ -75,13 +83,13 @@ pub fn render_note_preview(
if note.kind() == 1 { if note.kind() == 1 {
note note
} else { } else {
return ui.colored_label( return NoteResponse::new(ui.colored_label(
Color32::RED, Color32::RED,
format!("TODO: can't preview kind {}", note.kind()), format!("TODO: can't preview kind {}", note.kind()),
); ));
} }
} else { } 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 return ui
.horizontal(|ui| { .horizontal(|ui| {
@ -103,19 +111,15 @@ pub fn render_note_preview(
ui.visuals().noninteractive().bg_stroke.color, ui.visuals().noninteractive().bg_stroke.color,
)) ))
.show(ui, |ui| { .show(ui, |ui| {
let resp = ui::NoteView::new(ndb, note_cache, img_cache, &note) ui::NoteView::new(ndb, note_cache, img_cache, &note)
.actionbar(false) .actionbar(false)
.small_pfp(true) .small_pfp(true)
.wide(true) .wide(true)
.note_previews(false) .note_previews(false)
.options_button(true) .options_button(true)
.show(ui); .show(ui)
if let Some(context) = resp.context_selection {
context.process(ui, &note);
}
}) })
.response .inner
} }
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
@ -128,7 +132,7 @@ fn render_note_contents(
note: &Note, note: &Note,
note_key: NoteKey, note_key: NoteKey,
options: NoteOptions, options: NoteOptions,
) -> egui::InnerResponse<()> { ) -> NoteResponse {
#[cfg(feature = "profiling")] #[cfg(feature = "profiling")]
puffin::profile_function!(); puffin::profile_function!();
@ -136,7 +140,7 @@ fn render_note_contents(
let mut images: Vec<String> = vec![]; let mut images: Vec<String> = vec![];
let mut inline_note: Option<(&[u8; 32], &str)> = None; 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) { let blocks = if let Ok(blocks) = ndb.get_blocks_by_key(txn, note_key) {
blocks blocks
} else { } else {
@ -204,9 +208,11 @@ fn render_note_contents(
} }
}); });
if let Some((id, block_str)) = inline_note { let note_action = if let Some((id, block_str)) = inline_note {
render_note_preview(ui, ndb, note_cache, img_cache, txn, id, block_str); render_note_preview(ui, ndb, note_cache, img_cache, txn, id, block_str).action
} } else {
None
};
if !images.is_empty() && !options.has_textmode() { if !images.is_empty() && !options.has_textmode() {
ui.add_space(2.0); ui.add_space(2.0);
@ -215,7 +221,7 @@ fn render_note_contents(
ui.add_space(2.0); ui.add_space(2.0);
} }
resp NoteResponse::new(response.response).with_action(note_action)
} }
fn image_carousel( fn image_carousel(

View File

@ -281,7 +281,7 @@ impl<'a> NoteView<'a> {
) )
}); });
ui.add(NoteContents::new( ui.add(&mut NoteContents::new(
self.ndb, self.ndb,
self.img_cache, self.img_cache,
self.note_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.ndb,
self.img_cache, self.img_cache,
self.note_cache, self.note_cache,
@ -484,10 +484,13 @@ impl<'a> NoteView<'a> {
self.note, self.note,
note_key, note_key,
self.options(), self.options(),
)); );
let resp = ui.add(&mut contents);
note_action = note_action.or(contents.action());
if self.options().has_actionbar() { 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 resp
@ -520,7 +523,7 @@ impl<'a> NoteView<'a> {
} }
}); });
ui.add(NoteContents::new( let mut contents = NoteContents::new(
self.ndb, self.ndb,
self.img_cache, self.img_cache,
self.note_cache, self.note_cache,
@ -528,10 +531,13 @@ impl<'a> NoteView<'a> {
self.note, self.note,
note_key, note_key,
self.options(), self.options(),
)); );
ui.add(&mut contents);
note_action = note_action.or(contents.action());
if self.options().has_actionbar() { 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);
} }
}); });
}) })