notedeck

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

reply_description.rs (5135B)


      1 use crate::{actionbar::NoteAction, ui};
      2 use egui::{Label, RichText, Sense};
      3 use nostrdb::{Ndb, Note, NoteReply, Transaction};
      4 use notedeck::{ImageCache, NoteCache};
      5 
      6 #[must_use = "Please handle the resulting note action"]
      7 pub fn reply_desc(
      8     ui: &mut egui::Ui,
      9     txn: &Transaction,
     10     note_reply: &NoteReply,
     11     ndb: &Ndb,
     12     img_cache: &mut ImageCache,
     13     note_cache: &mut NoteCache,
     14 ) -> Option<NoteAction> {
     15     #[cfg(feature = "profiling")]
     16     puffin::profile_function!();
     17 
     18     let mut note_action: Option<NoteAction> = None;
     19     let size = 10.0;
     20     let selectable = false;
     21     let visuals = ui.visuals();
     22     let color = visuals.noninteractive().fg_stroke.color;
     23     let link_color = visuals.hyperlink_color;
     24 
     25     // note link renderer helper
     26     let note_link = |ui: &mut egui::Ui,
     27                      note_cache: &mut NoteCache,
     28                      img_cache: &mut ImageCache,
     29                      text: &str,
     30                      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(ndb, note_cache, img_cache, note)
     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) = 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(ndb, img_cache, txn, reply_note.pubkey())
     66             .size(size)
     67             .selectable(selectable)
     68             .show(ui)
     69             .inner;
     70 
     71         if action.is_some() {
     72             note_action = action;
     73         }
     74 
     75         ui.add(Label::new(RichText::new("'s").size(size).color(color)).selectable(selectable));
     76 
     77         note_link(ui, note_cache, img_cache, "thread", &reply_note);
     78     } else if let Some(root) = note_reply.root() {
     79         // replying to another post in a thread, not the root
     80 
     81         if let Ok(root_note) = ndb.get_note_by_id(txn, root.id) {
     82             if root_note.pubkey() == reply_note.pubkey() {
     83                 // simply "replying to bob's note" when replying to bob in his thread
     84                 let action = ui::Mention::new(ndb, img_cache, txn, reply_note.pubkey())
     85                     .size(size)
     86                     .selectable(selectable)
     87                     .show(ui)
     88                     .inner;
     89 
     90                 if action.is_some() {
     91                     note_action = action;
     92                 }
     93 
     94                 ui.add(
     95                     Label::new(RichText::new("'s").size(size).color(color)).selectable(selectable),
     96                 );
     97 
     98                 note_link(ui, note_cache, img_cache, "note", &reply_note);
     99             } else {
    100                 // replying to bob in alice's thread
    101 
    102                 let action = ui::Mention::new(ndb, img_cache, txn, reply_note.pubkey())
    103                     .size(size)
    104                     .selectable(selectable)
    105                     .show(ui)
    106                     .inner;
    107 
    108                 if action.is_some() {
    109                     note_action = action;
    110                 }
    111 
    112                 ui.add(
    113                     Label::new(RichText::new("'s").size(size).color(color)).selectable(selectable),
    114                 );
    115 
    116                 note_link(ui, note_cache, img_cache, "note", &reply_note);
    117 
    118                 ui.add(
    119                     Label::new(RichText::new("in").size(size).color(color)).selectable(selectable),
    120                 );
    121 
    122                 let action = ui::Mention::new(ndb, img_cache, txn, root_note.pubkey())
    123                     .size(size)
    124                     .selectable(selectable)
    125                     .show(ui)
    126                     .inner;
    127 
    128                 if action.is_some() {
    129                     note_action = action;
    130                 }
    131 
    132                 ui.add(
    133                     Label::new(RichText::new("'s").size(size).color(color)).selectable(selectable),
    134                 );
    135 
    136                 note_link(ui, note_cache, img_cache, "thread", &root_note);
    137             }
    138         } else {
    139             let action = ui::Mention::new(ndb, img_cache, txn, reply_note.pubkey())
    140                 .size(size)
    141                 .selectable(selectable)
    142                 .show(ui)
    143                 .inner;
    144 
    145             if action.is_some() {
    146                 note_action = action;
    147             }
    148 
    149             ui.add(
    150                 Label::new(RichText::new("in someone's thread").size(size).color(color))
    151                     .selectable(selectable),
    152             );
    153         }
    154     }
    155 
    156     note_action
    157 }