reply_description.rs (5595B)
1 use crate::{ 2 actionbar::NoteAction, 3 ui::{self}, 4 }; 5 use egui::{Label, RichText, Sense}; 6 use nostrdb::{Note, NoteReply, Transaction}; 7 8 use super::{contents::NoteContext, NoteOptions}; 9 10 #[must_use = "Please handle the resulting note action"] 11 pub fn reply_desc( 12 ui: &mut egui::Ui, 13 txn: &Transaction, 14 note_reply: &NoteReply, 15 note_context: &mut NoteContext, 16 note_options: NoteOptions, 17 ) -> Option<NoteAction> { 18 #[cfg(feature = "profiling")] 19 puffin::profile_function!(); 20 21 let mut note_action: Option<NoteAction> = None; 22 let size = 10.0; 23 let selectable = false; 24 let visuals = ui.visuals(); 25 let color = visuals.noninteractive().fg_stroke.color; 26 let link_color = visuals.hyperlink_color; 27 28 // note link renderer helper 29 let note_link = 30 |ui: &mut egui::Ui, note_context: &mut NoteContext, text: &str, note: &Note<'_>| { 31 let r = ui.add( 32 Label::new(RichText::new(text).size(size).color(link_color)) 33 .sense(Sense::click()) 34 .selectable(selectable), 35 ); 36 37 if r.clicked() { 38 // TODO: jump to note 39 } 40 41 if r.hovered() { 42 r.on_hover_ui_at_pointer(|ui| { 43 ui.set_max_width(400.0); 44 ui::NoteView::new(note_context, note, note_options) 45 .actionbar(false) 46 .wide(true) 47 .show(ui); 48 }); 49 } 50 }; 51 52 ui.add(Label::new(RichText::new("replying to").size(size).color(color)).selectable(selectable)); 53 54 let reply = note_reply.reply()?; 55 56 let reply_note = if let Ok(reply_note) = note_context.ndb.get_note_by_id(txn, reply.id) { 57 reply_note 58 } else { 59 ui.add(Label::new(RichText::new("a note").size(size).color(color)).selectable(selectable)); 60 return None; 61 }; 62 63 if note_reply.is_reply_to_root() { 64 // We're replying to the root, let's show this 65 let action = ui::Mention::new( 66 note_context.ndb, 67 note_context.img_cache, 68 txn, 69 reply_note.pubkey(), 70 ) 71 .size(size) 72 .selectable(selectable) 73 .show(ui) 74 .inner; 75 76 if action.is_some() { 77 note_action = action; 78 } 79 80 ui.add(Label::new(RichText::new("'s").size(size).color(color)).selectable(selectable)); 81 82 note_link(ui, note_context, "thread", &reply_note); 83 } else if let Some(root) = note_reply.root() { 84 // replying to another post in a thread, not the root 85 86 if let Ok(root_note) = note_context.ndb.get_note_by_id(txn, root.id) { 87 if root_note.pubkey() == reply_note.pubkey() { 88 // simply "replying to bob's note" when replying to bob in his thread 89 let action = ui::Mention::new( 90 note_context.ndb, 91 note_context.img_cache, 92 txn, 93 reply_note.pubkey(), 94 ) 95 .size(size) 96 .selectable(selectable) 97 .show(ui) 98 .inner; 99 100 if action.is_some() { 101 note_action = action; 102 } 103 104 ui.add( 105 Label::new(RichText::new("'s").size(size).color(color)).selectable(selectable), 106 ); 107 108 note_link(ui, note_context, "note", &reply_note); 109 } else { 110 // replying to bob in alice's thread 111 112 let action = ui::Mention::new( 113 note_context.ndb, 114 note_context.img_cache, 115 txn, 116 reply_note.pubkey(), 117 ) 118 .size(size) 119 .selectable(selectable) 120 .show(ui) 121 .inner; 122 123 if action.is_some() { 124 note_action = action; 125 } 126 127 ui.add( 128 Label::new(RichText::new("'s").size(size).color(color)).selectable(selectable), 129 ); 130 131 note_link(ui, note_context, "note", &reply_note); 132 133 ui.add( 134 Label::new(RichText::new("in").size(size).color(color)).selectable(selectable), 135 ); 136 137 let action = ui::Mention::new( 138 note_context.ndb, 139 note_context.img_cache, 140 txn, 141 root_note.pubkey(), 142 ) 143 .size(size) 144 .selectable(selectable) 145 .show(ui) 146 .inner; 147 148 if action.is_some() { 149 note_action = action; 150 } 151 152 ui.add( 153 Label::new(RichText::new("'s").size(size).color(color)).selectable(selectable), 154 ); 155 156 note_link(ui, note_context, "thread", &root_note); 157 } 158 } else { 159 let action = ui::Mention::new( 160 note_context.ndb, 161 note_context.img_cache, 162 txn, 163 reply_note.pubkey(), 164 ) 165 .size(size) 166 .selectable(selectable) 167 .show(ui) 168 .inner; 169 170 if action.is_some() { 171 note_action = action; 172 } 173 174 ui.add( 175 Label::new(RichText::new("in someone's thread").size(size).color(color)) 176 .selectable(selectable), 177 ); 178 } 179 } 180 181 note_action 182 }