notedeck

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

thread.rs (3841B)


      1 use crate::{
      2     actionbar::NoteAction,
      3     timeline::{TimelineCache, TimelineCacheKey},
      4     ui::note::NoteOptions,
      5 };
      6 
      7 use nostrdb::{Ndb, Transaction};
      8 use notedeck::{ImageCache, MuteFun, NoteCache, RootNoteId, UnknownIds};
      9 use tracing::error;
     10 
     11 use super::timeline::TimelineTabView;
     12 
     13 pub struct ThreadView<'a> {
     14     timeline_cache: &'a mut TimelineCache,
     15     ndb: &'a Ndb,
     16     note_cache: &'a mut NoteCache,
     17     unknown_ids: &'a mut UnknownIds,
     18     img_cache: &'a mut ImageCache,
     19     selected_note_id: &'a [u8; 32],
     20     textmode: bool,
     21     id_source: egui::Id,
     22     is_muted: &'a MuteFun,
     23 }
     24 
     25 impl<'a> ThreadView<'a> {
     26     #[allow(clippy::too_many_arguments)]
     27     pub fn new(
     28         timeline_cache: &'a mut TimelineCache,
     29         ndb: &'a Ndb,
     30         note_cache: &'a mut NoteCache,
     31         unknown_ids: &'a mut UnknownIds,
     32         img_cache: &'a mut ImageCache,
     33         selected_note_id: &'a [u8; 32],
     34         textmode: bool,
     35         is_muted: &'a MuteFun,
     36     ) -> Self {
     37         let id_source = egui::Id::new("threadscroll_threadview");
     38         ThreadView {
     39             timeline_cache,
     40             ndb,
     41             note_cache,
     42             unknown_ids,
     43             img_cache,
     44             selected_note_id,
     45             textmode,
     46             id_source,
     47             is_muted,
     48         }
     49     }
     50 
     51     pub fn id_source(mut self, id: egui::Id) -> Self {
     52         self.id_source = id;
     53         self
     54     }
     55 
     56     pub fn ui(&mut self, ui: &mut egui::Ui) -> Option<NoteAction> {
     57         let txn = Transaction::new(self.ndb).expect("txn");
     58 
     59         ui.label(
     60             egui::RichText::new("Threads ALPHA! It's not done. Things will be broken.")
     61                 .color(egui::Color32::RED),
     62         );
     63 
     64         egui::ScrollArea::vertical()
     65             .id_salt(self.id_source)
     66             .animated(false)
     67             .auto_shrink([false, false])
     68             .scroll_bar_visibility(egui::scroll_area::ScrollBarVisibility::AlwaysVisible)
     69             .show(ui, |ui| {
     70                 let root_id =
     71                     match RootNoteId::new(self.ndb, self.note_cache, &txn, self.selected_note_id) {
     72                         Ok(root_id) => root_id,
     73 
     74                         Err(err) => {
     75                             ui.label(format!("Error loading thread: {:?}", err));
     76                             return None;
     77                         }
     78                     };
     79 
     80                 let thread_timeline = self
     81                     .timeline_cache
     82                     .notes(
     83                         self.ndb,
     84                         self.note_cache,
     85                         &txn,
     86                         TimelineCacheKey::Thread(root_id),
     87                     )
     88                     .get_ptr();
     89 
     90                 // TODO(jb55): skip poll if ThreadResult is fresh?
     91 
     92                 let reversed = true;
     93                 // poll for new notes and insert them into our existing notes
     94                 if let Err(err) = thread_timeline.poll_notes_into_view(
     95                     self.ndb,
     96                     &txn,
     97                     self.unknown_ids,
     98                     self.note_cache,
     99                     reversed,
    100                 ) {
    101                     error!("error polling notes into thread timeline: {err}");
    102                 }
    103 
    104                 // This is threadview. We are not the universe view...
    105                 let is_universe = false;
    106                 let mut note_options = NoteOptions::new(is_universe);
    107                 note_options.set_textmode(self.textmode);
    108 
    109                 TimelineTabView::new(
    110                     thread_timeline.current_view(),
    111                     true,
    112                     note_options,
    113                     &txn,
    114                     self.ndb,
    115                     self.note_cache,
    116                     self.img_cache,
    117                     self.is_muted,
    118                 )
    119                 .show(ui)
    120             })
    121             .inner
    122     }
    123 }