notedeck

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

deck_state.rs (1781B)


      1 use crate::{app_style::emoji_font_family, decks::Deck};
      2 
      3 /// State for UI creating/editing deck
      4 pub struct DeckState {
      5     pub deck_name: String,
      6     pub selected_glyph: Option<char>,
      7     pub selecting_glyph: bool,
      8     pub warn_no_title: bool,
      9     pub warn_no_icon: bool,
     10     glyph_options: Option<Vec<char>>,
     11 }
     12 
     13 impl DeckState {
     14     pub fn load(&mut self, deck: &Deck) {
     15         self.deck_name = deck.name.clone();
     16         self.selected_glyph = Some(deck.icon);
     17     }
     18 
     19     pub fn from_deck(deck: &Deck) -> Self {
     20         let deck_name = deck.name.clone();
     21         let selected_glyph = Some(deck.icon);
     22         Self {
     23             deck_name,
     24             selected_glyph,
     25             ..Default::default()
     26         }
     27     }
     28 
     29     pub fn clear(&mut self) {
     30         *self = Default::default();
     31     }
     32 
     33     pub fn get_glyph_options(&mut self, ui: &egui::Ui) -> &Vec<char> {
     34         self.glyph_options
     35             .get_or_insert_with(|| available_characters(ui, emoji_font_family()))
     36     }
     37 }
     38 
     39 impl Default for DeckState {
     40     fn default() -> Self {
     41         Self {
     42             deck_name: Default::default(),
     43             selected_glyph: Default::default(),
     44             selecting_glyph: true,
     45             warn_no_icon: Default::default(),
     46             warn_no_title: Default::default(),
     47             glyph_options: Default::default(),
     48         }
     49     }
     50 }
     51 
     52 fn available_characters(ui: &egui::Ui, family: egui::FontFamily) -> Vec<char> {
     53     ui.fonts(|f| {
     54         f.lock()
     55             .fonts
     56             .font(&egui::FontId::new(10.0, family)) // size is arbitrary for getting the characters
     57             .characters()
     58             .iter()
     59             .map(|(chr, _v)| chr)
     60             .filter(|chr| !chr.is_whitespace() && !chr.is_ascii_control())
     61             .copied()
     62             .collect()
     63     })
     64 }