From fea315cb99f62f5c539c4322509ea97556997a30 Mon Sep 17 00:00:00 2001 From: Ken Sedgwick Date: Tue, 17 Sep 2024 10:55:05 -0700 Subject: [PATCH 1/3] add note underbutton to detect clicks and open thread --- src/ui/note/mod.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/src/ui/note/mod.rs b/src/ui/note/mod.rs index 3042688..9d1e40d 100644 --- a/src/ui/note/mod.rs +++ b/src/ui/note/mod.rs @@ -16,7 +16,7 @@ use crate::{ notecache::{CachedNote, NoteCache}, ui::{self, View}, }; -use egui::{Label, RichText, Sense}; +use egui::{Id, Label, Response, RichText, Sense}; use enostr::NoteId; use nostrdb::{Ndb, Note, NoteKey, NoteReply, Transaction}; @@ -385,6 +385,8 @@ impl<'a> NoteView<'a> { let mut note_action: Option = None; let profile = self.ndb.get_profile_by_pubkey(txn, self.note.pubkey()); + let maybe_underbutt = maybe_note_underbutton(ui, note_key); + // wide design let response = if self.options().has_wide() { ui.horizontal(|ui| { @@ -468,6 +470,15 @@ impl<'a> NoteView<'a> { .response }; + note_action = check_note_underbutton( + ui, + self.note.id(), + note_key, + &response, + maybe_underbutt, + note_action, + ); + NoteResponse { response, action: note_action, @@ -499,6 +510,42 @@ fn get_reposted_note<'a>(ndb: &Ndb, txn: &'a Transaction, note: &Note) -> Option note.filter(|note| note.kind() == 1) } +fn maybe_note_underbutton(ui: &mut egui::Ui, note_key: NoteKey) -> Option { + let underbuttid = Id::new(("note_rect", note_key)); + let maybe_underbutt = ui + .ctx() + .data_mut(|d| d.get_persisted(underbuttid)) + .map(|rect| { + let id = ui.make_persistent_id(("under_button_interact", note_key)); + ui.interact(rect, id, egui::Sense::click()) + }); + maybe_underbutt +} + +fn check_note_underbutton( + ui: &mut egui::Ui, + note_id: &[u8; 32], + note_key: NoteKey, + note_response: &Response, + maybe_underbutt: Option, + prior_action: Option, +) -> Option { + // Stash the dimensions of the note content so we can render the + // underbutton in the next frame + let underbuttid = Id::new(("note_rect", note_key)); + ui.ctx().data_mut(|d| { + d.insert_persisted(underbuttid, note_response.rect); + }); + + // If there was an underbutton and it was clicked open the thread + match maybe_underbutt { + Some(underbutt) if underbutt.clicked() => { + Some(BarAction::OpenThread(NoteId::new(*note_id))) + } + _ => prior_action, + } +} + fn render_note_actionbar( ui: &mut egui::Ui, note_id: &[u8; 32], From e16eeb4d1b800b9d3c8f15bea1b694d79fbfdba7 Mon Sep 17 00:00:00 2001 From: William Casarin Date: Tue, 17 Sep 2024 15:12:20 -0700 Subject: [PATCH 2/3] notes: rename "underbutt" to "hitbox" Signed-off-by: William Casarin --- src/ui/note/mod.rs | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/src/ui/note/mod.rs b/src/ui/note/mod.rs index 9d1e40d..99b33b6 100644 --- a/src/ui/note/mod.rs +++ b/src/ui/note/mod.rs @@ -384,8 +384,7 @@ impl<'a> NoteView<'a> { let txn = self.note.txn().expect("todo: support non-db notes"); let mut note_action: Option = None; let profile = self.ndb.get_profile_by_pubkey(txn, self.note.pubkey()); - - let maybe_underbutt = maybe_note_underbutton(ui, note_key); + let maybe_hitbox = maybe_note_hitbox(ui, note_key); // wide design let response = if self.options().has_wide() { @@ -470,12 +469,12 @@ impl<'a> NoteView<'a> { .response }; - note_action = check_note_underbutton( + note_action = check_note_hitbox( ui, self.note.id(), note_key, &response, - maybe_underbutt, + maybe_hitbox, note_action, ); @@ -510,35 +509,35 @@ fn get_reposted_note<'a>(ndb: &Ndb, txn: &'a Transaction, note: &Note) -> Option note.filter(|note| note.kind() == 1) } -fn maybe_note_underbutton(ui: &mut egui::Ui, note_key: NoteKey) -> Option { - let underbuttid = Id::new(("note_rect", note_key)); - let maybe_underbutt = ui - .ctx() - .data_mut(|d| d.get_persisted(underbuttid)) +fn note_hitbox_id(note_key: NoteKey) -> egui::Id { + Id::new(("note_rect", 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(|rect| { let id = ui.make_persistent_id(("under_button_interact", note_key)); ui.interact(rect, id, egui::Sense::click()) - }); - maybe_underbutt + }) } -fn check_note_underbutton( +fn check_note_hitbox( ui: &mut egui::Ui, note_id: &[u8; 32], note_key: NoteKey, note_response: &Response, - maybe_underbutt: Option, + maybe_hitbox: Option, prior_action: Option, ) -> Option { // Stash the dimensions of the note content so we can render the // underbutton in the next frame - let underbuttid = Id::new(("note_rect", note_key)); ui.ctx().data_mut(|d| { - d.insert_persisted(underbuttid, note_response.rect); + d.insert_persisted(note_hitbox_id(note_key), note_response.rect); }); // If there was an underbutton and it was clicked open the thread - match maybe_underbutt { + match maybe_hitbox { Some(underbutt) if underbutt.clicked() => { Some(BarAction::OpenThread(NoteId::new(*note_id))) } From a8731c0210c7dbf650e6da7eb5ae907f52031760 Mon Sep 17 00:00:00 2001 From: William Casarin Date: Tue, 17 Sep 2024 15:14:57 -0700 Subject: [PATCH 3/3] remove thread button now that we have note hitboxes Signed-off-by: William Casarin --- src/ui/note/mod.rs | 26 -------------------------- 1 file changed, 26 deletions(-) diff --git a/src/ui/note/mod.rs b/src/ui/note/mod.rs index 99b33b6..cf4cc69 100644 --- a/src/ui/note/mod.rs +++ b/src/ui/note/mod.rs @@ -552,12 +552,9 @@ fn render_note_actionbar( ) -> egui::InnerResponse> { ui.horizontal(|ui| { let reply_resp = reply_button(ui, note_key); - let thread_resp = thread_button(ui, note_key); if reply_resp.clicked() { Some(BarAction::Reply(NoteId::new(*note_id))) - } else if thread_resp.clicked() { - Some(BarAction::OpenThread(NoteId::new(*note_id))) } else { None } @@ -610,29 +607,6 @@ fn reply_button(ui: &mut egui::Ui, note_key: NoteKey) -> egui::Response { resp.union(put_resp) } -fn thread_button(ui: &mut egui::Ui, note_key: NoteKey) -> egui::Response { - let id = ui.id().with(("thread_anim", note_key)); - let size = 8.0; - let expand_size = 5.0; - let anim_speed = 0.05; - - let (rect, size, resp) = ui::anim::hover_expand(ui, id, size, expand_size, anim_speed); - - let color = if ui.style().visuals.dark_mode { - egui::Color32::WHITE - } else { - egui::Color32::BLACK - }; - - ui.painter_at(rect).circle_stroke( - rect.center(), - (size - 1.0) / 2.0, - egui::Stroke::new(1.0, color), - ); - - resp -} - fn repost_icon() -> egui::Image<'static> { let img_data = egui::include_image!("../../../assets/icons/repost_icon_4x.png"); egui::Image::new(img_data)