From 21298b43f5a0ec709681041f90c3f4b36e066fef Mon Sep 17 00:00:00 2001 From: Ken Sedgwick Date: Wed, 25 Sep 2024 16:20:11 -0700 Subject: [PATCH] hitbox: cache note size instead of rect --- src/ui/note/mod.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/ui/note/mod.rs b/src/ui/note/mod.rs index 6259a274..0c83718d 100644 --- a/src/ui/note/mod.rs +++ b/src/ui/note/mod.rs @@ -20,6 +20,7 @@ use crate::{ notecache::{CachedNote, NoteCache}, ui::{self, View}, }; +use egui::emath::{pos2, Vec2}; use egui::{Id, Label, Pos2, Rect, Response, RichText, Sense}; use enostr::NoteId; use nostrdb::{Ndb, Note, NoteKey, NoteReply, Transaction}; @@ -584,19 +585,22 @@ fn get_reposted_note<'a>(ndb: &Ndb, txn: &'a Transaction, note: &Note) -> Option } fn note_hitbox_id(note_key: NoteKey) -> egui::Id { - Id::new(("note_rect", note_key)) + Id::new(("note_size", note_key)) } fn maybe_note_hitbox(ui: &mut egui::Ui, note_key: NoteKey) -> Option { ui.ctx() .data_mut(|d| d.get_persisted(note_hitbox_id(note_key))) - .map(|mut rect: Rect| { + .map(|note_size: Vec2| { let id = ui.make_persistent_id(("hitbox_interact", note_key)); - // Extend the hitbox to the full width of the container + // The hitbox should extend the entire width of the + // container. The hitbox height was cached last layout. let container_rect = ui.max_rect(); - rect.min.x = container_rect.min.x; - rect.max.x = container_rect.max.x; + let rect = Rect { + min: pos2(container_rect.min.x, container_rect.min.y), + max: pos2(container_rect.max.x, container_rect.min.y + note_size.y), + }; ui.interact(rect, id, egui::Sense::click()) }) @@ -613,7 +617,7 @@ fn check_note_hitbox( // Stash the dimensions of the note content so we can render the // hitbox in the next frame ui.ctx().data_mut(|d| { - d.insert_persisted(note_hitbox_id(note_key), note_response.rect); + d.insert_persisted(note_hitbox_id(note_key), note_response.rect.size()); }); // If there was an hitbox and it was clicked open the thread