notedeck

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

reply.rs (4303B)


      1 use crate::draft::Draft;
      2 use crate::ui;
      3 use crate::ui::note::{PostResponse, PostType};
      4 use enostr::{FilledKeypair, NoteId};
      5 use nostrdb::Ndb;
      6 
      7 use notedeck::{ImageCache, NoteCache};
      8 
      9 pub struct PostReplyView<'a> {
     10     ndb: &'a Ndb,
     11     poster: FilledKeypair<'a>,
     12     note_cache: &'a mut NoteCache,
     13     img_cache: &'a mut ImageCache,
     14     draft: &'a mut Draft,
     15     note: &'a nostrdb::Note<'a>,
     16     id_source: Option<egui::Id>,
     17 }
     18 
     19 impl<'a> PostReplyView<'a> {
     20     pub fn new(
     21         ndb: &'a Ndb,
     22         poster: FilledKeypair<'a>,
     23         draft: &'a mut Draft,
     24         note_cache: &'a mut NoteCache,
     25         img_cache: &'a mut ImageCache,
     26         note: &'a nostrdb::Note<'a>,
     27     ) -> Self {
     28         let id_source: Option<egui::Id> = None;
     29         PostReplyView {
     30             ndb,
     31             poster,
     32             draft,
     33             note,
     34             note_cache,
     35             img_cache,
     36             id_source,
     37         }
     38     }
     39 
     40     pub fn id_source(mut self, id: egui::Id) -> Self {
     41         self.id_source = Some(id);
     42         self
     43     }
     44 
     45     pub fn id(&self) -> egui::Id {
     46         self.id_source
     47             .unwrap_or_else(|| egui::Id::new("post-reply-view"))
     48     }
     49 
     50     pub fn show(&mut self, ui: &mut egui::Ui) -> PostResponse {
     51         ui.vertical(|ui| {
     52             let avail_rect = ui.available_rect_before_wrap();
     53 
     54             // This is the offset of the post view's pfp. We use this
     55             // to indent things so that the reply line is aligned
     56             let pfp_offset = ui::PostView::outer_margin()
     57                 + ui::PostView::inner_margin()
     58                 + ui::ProfilePic::small_size() / 2.0;
     59 
     60             let note_offset = pfp_offset
     61                 - ui::ProfilePic::medium_size() / 2.0
     62                 - ui::NoteView::expand_size() / 2.0;
     63 
     64             egui::Frame::none()
     65                 .outer_margin(egui::Margin::same(note_offset))
     66                 .show(ui, |ui| {
     67                     ui::NoteView::new(self.ndb, self.note_cache, self.img_cache, self.note)
     68                         .actionbar(false)
     69                         .medium_pfp(true)
     70                         .options_button(true)
     71                         .show(ui);
     72                 });
     73 
     74             let id = self.id();
     75             let replying_to = self.note.id();
     76             let rect_before_post = ui.min_rect();
     77 
     78             let post_response = {
     79                 ui::PostView::new(
     80                     self.ndb,
     81                     self.draft,
     82                     PostType::Reply(NoteId::new(*replying_to)),
     83                     self.img_cache,
     84                     self.note_cache,
     85                     self.poster,
     86                 )
     87                 .id_source(id)
     88                 .ui(self.note.txn().unwrap(), ui)
     89             };
     90 
     91             //
     92             // reply line
     93             //
     94 
     95             // Position and draw the reply line
     96             let mut rect = ui.min_rect();
     97 
     98             // Position the line right above the poster's profile pic in
     99             // the post box. Use the PostView's margin values to
    100             // determine this offset.
    101             rect.min.x = avail_rect.min.x + pfp_offset;
    102 
    103             // honestly don't know what the fuck I'm doing here. just trying
    104             // to get the line under the profile picture
    105             rect.min.y = avail_rect.min.y
    106                 + (ui::ProfilePic::medium_size() / 2.0
    107                     + ui::ProfilePic::medium_size()
    108                     + ui::NoteView::expand_size() * 2.0)
    109                 + 1.0;
    110 
    111             // For some reason we need to nudge the reply line's height a
    112             // few more pixels?
    113             let nudge = if post_response.edit_response.has_focus() {
    114                 // we nudge by one less pixel if focused, otherwise it
    115                 // overlaps the focused PostView purple border color
    116                 2.0
    117             } else {
    118                 // we have to nudge by one more pixel when not focused
    119                 // otherwise it looks like there's a gap(?)
    120                 3.0
    121             };
    122 
    123             rect.max.y = rect_before_post.max.y + ui::PostView::outer_margin() + nudge;
    124 
    125             ui.painter().vline(
    126                 rect.left(),
    127                 rect.y_range(),
    128                 ui.visuals().widgets.noninteractive.bg_stroke,
    129             );
    130 
    131             post_response
    132         })
    133         .inner
    134     }
    135 }