notedeck

One damus client to rule them all
git clone git://jb55.com/notedeck
Log | Files | Refs | README | LICENSE

mention.rs (2368B)


      1 use crate::ProfilePreview;
      2 use egui::Sense;
      3 use enostr::Pubkey;
      4 use nostrdb::{Ndb, Transaction};
      5 use notedeck::{name::get_display_name, Images, NoteAction};
      6 
      7 pub struct Mention<'a> {
      8     ndb: &'a Ndb,
      9     img_cache: &'a mut Images,
     10     txn: &'a Transaction,
     11     pk: &'a [u8; 32],
     12     selectable: bool,
     13     size: Option<f32>,
     14 }
     15 
     16 impl<'a> Mention<'a> {
     17     pub fn new(
     18         ndb: &'a Ndb,
     19         img_cache: &'a mut Images,
     20         txn: &'a Transaction,
     21         pk: &'a [u8; 32],
     22     ) -> Self {
     23         let size = None;
     24         let selectable = true;
     25         Mention {
     26             ndb,
     27             img_cache,
     28             txn,
     29             pk,
     30             selectable,
     31             size,
     32         }
     33     }
     34 
     35     pub fn selectable(mut self, selectable: bool) -> Self {
     36         self.selectable = selectable;
     37         self
     38     }
     39 
     40     pub fn size(mut self, size: f32) -> Self {
     41         self.size = Some(size);
     42         self
     43     }
     44 
     45     pub fn show(self, ui: &mut egui::Ui) -> Option<NoteAction> {
     46         mention_ui(
     47             self.ndb,
     48             self.img_cache,
     49             self.txn,
     50             self.pk,
     51             ui,
     52             self.size,
     53             self.selectable,
     54         )
     55     }
     56 }
     57 
     58 #[allow(clippy::too_many_arguments)]
     59 #[profiling::function]
     60 fn mention_ui(
     61     ndb: &Ndb,
     62     img_cache: &mut Images,
     63     txn: &Transaction,
     64     pk: &[u8; 32],
     65     ui: &mut egui::Ui,
     66     size: Option<f32>,
     67     selectable: bool,
     68 ) -> Option<NoteAction> {
     69     let link_color = ui.visuals().hyperlink_color;
     70 
     71     let profile = ndb.get_profile_by_pubkey(txn, pk).ok();
     72 
     73     let name: String = format!(
     74         "@{}",
     75         get_display_name(profile.as_ref()).username_or_displayname()
     76     );
     77 
     78     let mut text = egui::RichText::new(name).color(link_color);
     79     if let Some(size) = size {
     80         text = text.size(size);
     81     }
     82 
     83     let resp = ui
     84         .add(
     85             egui::Label::new(text)
     86                 .sense(Sense::click())
     87                 .selectable(selectable),
     88         )
     89         .on_hover_cursor(egui::CursorIcon::PointingHand);
     90 
     91     let note_action = if resp.clicked() {
     92         Some(NoteAction::Profile(Pubkey::new(*pk)))
     93     } else {
     94         None
     95     };
     96 
     97     if let Some(rec) = profile.as_ref() {
     98         resp.on_hover_ui_at_pointer(|ui| {
     99             ui.set_max_width(300.0);
    100             ui.add(ProfilePreview::new(rec, img_cache));
    101         });
    102     }
    103 
    104     note_action
    105 }