notedeck

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

draft.rs (1955B)


      1 use egui::text::LayoutJob;
      2 use poll_promise::Promise;
      3 
      4 use crate::{
      5     media_upload::Nip94Event,
      6     post::PostBuffer,
      7     ui::{note::PostType, search::FocusState},
      8     Error,
      9 };
     10 use notedeck_ui::ProfileSearchResult;
     11 use std::collections::HashMap;
     12 
     13 #[derive(Default)]
     14 pub struct Draft {
     15     pub buffer: PostBuffer,
     16     pub cur_layout: Option<(String, LayoutJob)>, // `PostBuffer::text_buffer` to current `LayoutJob`
     17     pub cur_mention_hint: Option<MentionHint>,
     18     pub uploaded_media: Vec<Nip94Event>, // media uploads to include
     19     pub uploading_media: Vec<Promise<Result<Nip94Event, Error>>>, // promises that aren't ready yet
     20     pub upload_errors: Vec<String>,      // media upload errors to show the user
     21     pub focus_state: FocusState,
     22 }
     23 
     24 pub struct MentionHint {
     25     pub index: usize,
     26     pub pos: egui::Pos2,
     27     pub text: String,
     28     pub results: Vec<ProfileSearchResult>,
     29 }
     30 
     31 #[derive(Default)]
     32 pub struct Drafts {
     33     replies: HashMap<[u8; 32], Draft>,
     34     quotes: HashMap<[u8; 32], Draft>,
     35     compose: Draft,
     36 }
     37 
     38 impl Drafts {
     39     pub fn compose_mut(&mut self) -> &mut Draft {
     40         &mut self.compose
     41     }
     42 
     43     pub fn get_from_post_type(&mut self, post_type: &PostType) -> &mut Draft {
     44         match post_type {
     45             PostType::New => self.compose_mut(),
     46             PostType::Quote(note_id) => self.quote_mut(note_id.bytes()),
     47             PostType::Reply(note_id) => self.reply_mut(note_id.bytes()),
     48         }
     49     }
     50 
     51     pub fn reply_mut(&mut self, id: &[u8; 32]) -> &mut Draft {
     52         self.replies.entry(*id).or_default()
     53     }
     54 
     55     pub fn quote_mut(&mut self, id: &[u8; 32]) -> &mut Draft {
     56         self.quotes.entry(*id).or_default()
     57     }
     58 }
     59 
     60 impl Draft {
     61     pub fn new() -> Self {
     62         Draft::default()
     63     }
     64 
     65     pub fn clear(&mut self) {
     66         self.buffer = PostBuffer::default();
     67         self.upload_errors = Vec::new();
     68         self.uploaded_media = Vec::new();
     69         self.uploading_media = Vec::new();
     70     }
     71 }