Refactor slightly
This commit is contained in:
@@ -4,6 +4,10 @@ use crate::draw;
|
||||
use fontconfig::Fontconfig as FontConfig;
|
||||
use rusttype::Font;
|
||||
use std::{
|
||||
cell::RefCell,
|
||||
mem,
|
||||
rc::Rc,
|
||||
thread,
|
||||
collections::HashMap,
|
||||
fs::File,
|
||||
hash,
|
||||
@@ -11,6 +15,7 @@ use std::{
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
|
||||
/// FontRef is used to store Fonts on widgets.
|
||||
pub type FontRef<'a> = &'a rusttype::Font<'a>;
|
||||
|
||||
@@ -126,3 +131,31 @@ impl FontMap {
|
||||
.expect("no font at specified size")
|
||||
}
|
||||
}
|
||||
|
||||
pub enum MaybeFontMap {
|
||||
Waiting(thread::JoinHandle<FontMap>),
|
||||
Ready(Rc<RefCell<FontMap>>),
|
||||
Invalid,
|
||||
}
|
||||
|
||||
impl MaybeFontMap {
|
||||
pub fn unwrap(&self) -> Rc<RefCell<FontMap>> {
|
||||
match self {
|
||||
MaybeFontMap::Ready(f) => f.clone(),
|
||||
_ => panic!("fontmap not yet ready"),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn resolve(&mut self) {
|
||||
if matches!(self, MaybeFontMap::Waiting(_)) {
|
||||
let s = mem::replace(self, MaybeFontMap::Invalid);
|
||||
match s {
|
||||
MaybeFontMap::Waiting(handle) => {
|
||||
*self = MaybeFontMap::Ready(Rc::new(RefCell::new(handle.join().unwrap())));
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user