notedeck

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

reply.rs (4424B)


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