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
use super::*;

pub trait Config<T> {
    fn get(&self) -> T;
    fn ui<'a>(&'a mut self) -> Box<dyn Widget + 'a>;
}

pub trait Configurable: Sized {
    type Config: Config<Self>;
    fn config(geng: &Rc<Geng>, theme: &Rc<Theme>, value: Self) -> Self::Config;
}

pub struct ShowValue<T: ToString + Clone> {
    theme: Rc<Theme>,
    value: T,
    text: Option<String>,
}

impl<T: ToString + Clone> Config<T> for ShowValue<T> {
    fn get(&self) -> T {
        self.value.clone()
    }
    fn ui<'a>(&'a mut self) -> Box<dyn Widget + 'a> {
        if self.text.is_none() {
            self.text = Some(self.value.to_string());
        }
        Box::new(text(
            self.text.as_ref().unwrap(),
            &self.theme.font,
            16.0,
            Color::GRAY,
        ))
    }
}

impl<T: ToString + Clone> Configurable for T {
    type Config = ShowValue<T>;
    fn config(
        #[allow(unused_variables)] geng: &Rc<Geng>,
        theme: &Rc<Theme>,
        value: T,
    ) -> ShowValue<T> {
        ShowValue {
            theme: theme.clone(),
            value,
            text: None,
        }
    }
}