hitbox: cache note size instead of rect

This commit is contained in:
Ken Sedgwick 2024-09-25 16:20:11 -07:00 committed by William Casarin
parent c25fde4a06
commit 21298b43f5

View File

@ -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<Response> {
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