Skip to main content

vn_settings/
color.rs

1use super::{Settings, lookup, push_all};
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    #[must_use]
15    pub fn common() -> Self {
16        Self {
17            text_line: Color::WHITE.into(),
18            text_fill: Color::BLACK.into(),
19            name_line: Color::WHITE.into(),
20            name_fill: Color::BLACK.into(),
21        }
22    }
23}
24
25#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
26pub enum BoxColorParameter {
27    TextLine,
28    TextFill,
29    NameLine,
30    NameFill,
31}
32
33impl<T> Settings<BoxColorParameter> for BoxColors<T> {
34    type Value = T;
35
36    fn get_mut(&mut self, parameter: &BoxColorParameter) -> &mut T {
37        use BoxColorParameter::*;
38        match parameter {
39            TextLine => &mut self.text_line,
40            TextFill => &mut self.text_fill,
41            NameLine => &mut self.name_line,
42            NameFill => &mut self.name_fill,
43        }
44    }
45}
46
47resettable!(<T> BoxColors<T> { text_line, text_fill, name_line, name_fill });
48
49#[derive(Clone, Debug)]
50pub struct SelectableBoxColors<T> {
51    pub default: BoxColors<T>,
52    pub selected: BoxColors<T>,
53}
54
55impl<T: From<Color>> SelectableBoxColors<T> {
56    #[must_use]
57    pub fn common() -> Self {
58        Self {
59            default: BoxColors::common(),
60            selected: BoxColors {
61                text_fill: Color::gray(64).into(),
62                ..BoxColors::common()
63            },
64        }
65    }
66}
67
68#[derive(Clone, Debug, PartialEq, Eq, Hash)]
69pub struct SelectableBoxColorParameter {
70    pub selected: bool,
71    pub parameter: BoxColorParameter,
72}
73
74impl<T> Settings<SelectableBoxColorParameter> for SelectableBoxColors<T> {
75    type Value = T;
76
77    fn get_mut(&mut self, parameter: &SelectableBoxColorParameter) -> &mut T {
78        if parameter.selected {
79            &mut self.selected
80        } else {
81            &mut self.default
82        }
83        .get_mut(&parameter.parameter)
84    }
85}
86
87resettable!(<T> SelectableBoxColors<T> { default, selected });
88
89#[derive(Clone)]
90pub struct ColorSettings<T> {
91    pub background: T,
92    pub foreground: T,
93    pub dialog_box: BoxColors<T>,
94    pub select_box: SelectableBoxColors<T>,
95    pub revert_box: SelectableBoxColors<T>,
96    pub menu: SelectableBoxColors<T>,
97}
98
99impl<T: From<Color>> ColorSettings<T> {
100    #[must_use]
101    pub fn common() -> Self {
102        Self {
103            background: Color::BLACK.into(),
104            foreground: Color::BLACK.a(0).into(),
105            dialog_box: BoxColors::common(),
106            select_box: SelectableBoxColors::common(),
107            revert_box: SelectableBoxColors::common(),
108            menu: SelectableBoxColors {
109                default: BoxColors {
110                    text_fill: Color::gray(30).a(235).into(),
111                    text_line: Color::gray(175).into(),
112                    name_fill: Color::gray(30).a(235).into(),
113                    name_line: Color::WHITE.into(),
114                },
115                selected: BoxColors {
116                    text_fill: Color::rgba(70, 90, 150, 235).into(),
117                    text_line: Color::WHITE.into(),
118                    name_fill: Color::gray(30).a(235).into(),
119                    name_line: Color::WHITE.into(),
120                },
121            },
122        }
123    }
124}
125
126#[derive(Clone, Debug, PartialEq, Eq, Hash)]
127pub enum ColorParameter {
128    Background,
129    Foreground,
130    Dialog(BoxColorParameter),
131    Choice(SelectableBoxColorParameter),
132    Revert(SelectableBoxColorParameter),
133    Menu(SelectableBoxColorParameter),
134}
135
136impl<T> Settings<ColorParameter> for ColorSettings<T> {
137    type Value = T;
138
139    fn get_mut(&mut self, parameter: &ColorParameter) -> &mut T {
140        use ColorParameter::*;
141        match parameter {
142            Background => &mut self.background,
143            Foreground => &mut self.foreground,
144            Dialog(parameter) => self.dialog_box.get_mut(parameter),
145            Choice(parameter) => self.select_box.get_mut(parameter),
146            Revert(parameter) => self.revert_box.get_mut(parameter),
147            Menu(parameter) => self.menu.get_mut(parameter),
148        }
149    }
150}
151
152resettable!(<T> ColorSettings<T> { background, foreground, dialog_box, select_box, revert_box, menu });
153
154const BOX_COLOR_PARAMETERS: [(&str, BoxColorParameter); 4] = [
155    ("text-fill", BoxColorParameter::TextFill),
156    ("text-line", BoxColorParameter::TextLine),
157    ("name-fill", BoxColorParameter::NameFill),
158    ("name-line", BoxColorParameter::NameLine),
159];
160
161const SELECTIONS: [(&str, bool); 2] = [("default", false), ("select", true)];
162
163type SelectableColorKind = fn(SelectableBoxColorParameter) -> ColorParameter;
164
165const SELECTABLE_COLOR_KINDS: [(&str, SelectableColorKind); 3] = [
166    ("choice", ColorParameter::Choice),
167    ("revert", ColorParameter::Revert),
168    ("menu", ColorParameter::Menu),
169];
170
171impl BoxColorParameter {
172    fn parse(name: &str) -> Option<Self> {
173        lookup(&BOX_COLOR_PARAMETERS, name)
174    }
175
176    fn push_names(prefix: &str, out: &mut Vec<Box<str>>) {
177        push_all(&BOX_COLOR_PARAMETERS, prefix, out);
178    }
179}
180
181impl SelectableBoxColorParameter {
182    fn parse(name: &str) -> Option<Self> {
183        let (selection, parameter_name) = name.split_once('-')?;
184        Some(Self {
185            selected: lookup(&SELECTIONS, selection)?,
186            parameter: BoxColorParameter::parse(parameter_name)?,
187        })
188    }
189
190    fn push_names(prefix: &str, out: &mut Vec<Box<str>>) {
191        for (selection, _) in SELECTIONS {
192            BoxColorParameter::push_names(&format!("{prefix}{selection}-"), out);
193        }
194    }
195}
196
197impl ColorParameter {
198    pub fn parse(name: &str) -> Option<Self> {
199        Some(match name {
200            "background" => Self::Background,
201            "foreground" => Self::Foreground,
202            _ => {
203                let (kind, parameter_name) = name.split_once('-')?;
204                if kind == "dialog" {
205                    Self::Dialog(BoxColorParameter::parse(parameter_name)?)
206                } else {
207                    lookup(&SELECTABLE_COLOR_KINDS, kind)?(SelectableBoxColorParameter::parse(
208                        parameter_name,
209                    )?)
210                }
211            }
212        })
213    }
214
215    pub(crate) fn push_names(prefix: &str, out: &mut Vec<Box<str>>) {
216        out.push(format!("{prefix}background").into());
217        out.push(format!("{prefix}foreground").into());
218        BoxColorParameter::push_names(&format!("{prefix}dialog-"), out);
219        for (kind, _) in SELECTABLE_COLOR_KINDS {
220            SelectableBoxColorParameter::push_names(&format!("{prefix}{kind}-"), out);
221        }
222    }
223}