notedeck

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

mention.rs (1870B)


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