notedeck

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

deck_state.rs (1847B)


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