mention.rs (2132B)
1 use crate::{colors, imgcache::ImageCache, ui}; 2 use nostrdb::{Ndb, Transaction}; 3 4 pub struct Mention<'a> { 5 ndb: &'a Ndb, 6 img_cache: &'a mut ImageCache, 7 txn: &'a Transaction, 8 pk: &'a [u8; 32], 9 selectable: bool, 10 size: f32, 11 } 12 13 impl<'a> Mention<'a> { 14 pub fn new( 15 ndb: &'a Ndb, 16 img_cache: &'a mut ImageCache, 17 txn: &'a Transaction, 18 pk: &'a [u8; 32], 19 ) -> Self { 20 let size = 16.0; 21 let selectable = true; 22 Mention { 23 ndb, 24 img_cache, 25 txn, 26 pk, 27 selectable, 28 size, 29 } 30 } 31 32 pub fn selectable(mut self, selectable: bool) -> Self { 33 self.selectable = selectable; 34 self 35 } 36 37 pub fn size(mut self, size: f32) -> Self { 38 self.size = size; 39 self 40 } 41 } 42 43 impl<'a> egui::Widget for Mention<'a> { 44 fn ui(self, ui: &mut egui::Ui) -> egui::Response { 45 mention_ui( 46 self.ndb, 47 self.img_cache, 48 self.txn, 49 self.pk, 50 ui, 51 self.size, 52 self.selectable, 53 ) 54 } 55 } 56 57 fn mention_ui( 58 ndb: &Ndb, 59 img_cache: &mut ImageCache, 60 txn: &Transaction, 61 pk: &[u8; 32], 62 ui: &mut egui::Ui, 63 size: f32, 64 selectable: bool, 65 ) -> egui::Response { 66 #[cfg(feature = "profiling")] 67 puffin::profile_function!(); 68 69 ui.horizontal(|ui| { 70 let profile = ndb.get_profile_by_pubkey(txn, pk).ok(); 71 72 let name: String = 73 if let Some(name) = profile.as_ref().and_then(crate::profile::get_profile_name) { 74 format!("@{}", name.username()) 75 } else { 76 "??".to_string() 77 }; 78 79 let resp = ui.add( 80 egui::Label::new(egui::RichText::new(name).color(colors::PURPLE).size(size)) 81 .selectable(selectable), 82 ); 83 84 if let Some(rec) = profile.as_ref() { 85 resp.on_hover_ui_at_pointer(|ui| { 86 ui.set_max_width(300.0); 87 ui.add(ui::ProfilePreview::new(rec, img_cache)); 88 }); 89 } 90 }) 91 .response 92 }