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