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