vn_settings/
color.rs

1use super::{Resettable, Settings};
2
3use simple_color::Color;
4
5#[derive(Clone, Debug)]
6pub struct BoxColors<T> {
7    pub text_line: T,
8    pub text_fill: T,
9    pub name_line: T,
10    pub name_fill: T,
11}
12
13impl<T: From<Color>> BoxColors<T> {
14    pub fn common() -> Self {
15        Self {
16            text_line: Color::WHITE.into(),
17            text_fill: Color::BLACK.into(),
18            name_line: Color::WHITE.into(),
19            name_fill: Color::BLACK.into(),
20        }
21    }
22}
23
24#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
25pub enum BoxColorParameter {
26    TextLine,
27    TextFill,
28    NameLine,
29    NameFill,
30}
31
32impl<T> Settings<BoxColorParameter> for BoxColors<T> {
33    type Value = T;
34
35    fn get_mut(&mut self, parameter: &BoxColorParameter) -> &mut T {
36        use BoxColorParameter::*;
37        match parameter {
38            TextLine => &mut self.text_line,
39            TextFill => &mut self.text_fill,
40            NameLine => &mut self.name_line,
41            NameFill => &mut self.name_fill,
42        }
43    }
44}
45
46impl<T: Resettable> Resettable for BoxColors<T> {
47    fn reset(&mut self) {
48        self.text_line.reset();
49        self.text_fill.reset();
50        self.name_line.reset();
51        self.name_fill.reset();
52    }
53}
54
55#[derive(Clone, Debug)]
56pub struct SelectableBoxColors<T> {
57    pub default: BoxColors<T>,
58    pub selected: BoxColors<T>,
59}
60
61impl<T: From<Color>> SelectableBoxColors<T> {
62    pub fn common() -> Self {
63        Self {
64            default: BoxColors::common(),
65            selected: BoxColors {
66                text_fill: Color::gray(64).into(),
67                ..BoxColors::common()
68            },
69        }
70    }
71}
72
73#[derive(Clone, Debug, PartialEq, Eq, Hash)]
74pub struct SelectableBoxColorParameter {
75    pub selected: bool,
76    pub parameter: BoxColorParameter,
77}
78
79impl<T> Settings<SelectableBoxColorParameter> for SelectableBoxColors<T> {
80    type Value = T;
81
82    fn get_mut(&mut self, parameter: &SelectableBoxColorParameter) -> &mut T {
83        if parameter.selected {
84            &mut self.selected
85        } else {
86            &mut self.default
87        }
88        .get_mut(&parameter.parameter)
89    }
90}
91
92impl<T: Resettable> Resettable for SelectableBoxColors<T> {
93    fn reset(&mut self) {
94        self.default.reset();
95        self.selected.reset();
96    }
97}
98
99#[derive(Clone)]
100pub struct ColorSettings<T> {
101    pub background: T,
102    pub foreground: T,
103    pub dialog_box: BoxColors<T>,
104    pub select_box: SelectableBoxColors<T>,
105    pub revert_box: SelectableBoxColors<T>,
106}
107
108impl<T: From<Color>> ColorSettings<T> {
109    pub fn common() -> Self {
110        Self {
111            background: Color::BLACK.into(),
112            foreground: Color::BLACK.a(0).into(),
113            dialog_box: BoxColors::common(),
114            select_box: SelectableBoxColors::common(),
115            revert_box: SelectableBoxColors::common(),
116        }
117    }
118}
119
120#[derive(Clone, Debug, PartialEq, Eq, Hash)]
121pub enum ColorParameter {
122    Background,
123    Foreground,
124    Dialog(BoxColorParameter),
125    Choice(SelectableBoxColorParameter),
126    Revert(SelectableBoxColorParameter),
127}
128
129impl<T> Settings<ColorParameter> for ColorSettings<T> {
130    type Value = T;
131
132    fn get_mut(&mut self, parameter: &ColorParameter) -> &mut T {
133        use ColorParameter::*;
134        match parameter {
135            Background => &mut self.background,
136            Foreground => &mut self.foreground,
137            Dialog(parameter) => self.dialog_box.get_mut(parameter),
138            Choice(parameter) => self.select_box.get_mut(parameter),
139            Revert(parameter) => self.revert_box.get_mut(parameter),
140        }
141    }
142}
143
144impl<T: Resettable> Resettable for ColorSettings<T> {
145    fn reset(&mut self) {
146        self.background.reset();
147        self.foreground.reset();
148        self.dialog_box.reset();
149        self.select_box.reset();
150        self.revert_box.reset();
151    }
152}