use super::{Settings, lookup, push_all};
use simple_color::Color;
#[derive(Clone, Debug)]
pub struct BoxColors<T> {
pub text_line: T,
pub text_fill: T,
pub name_line: T,
pub name_fill: T,
}
impl<T: From<Color>> BoxColors<T> {
#[must_use]
pub fn common() -> Self {
Self {
text_line: Color::WHITE.into(),
text_fill: Color::BLACK.into(),
name_line: Color::WHITE.into(),
name_fill: Color::BLACK.into(),
}
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum BoxColorParameter {
TextLine,
TextFill,
NameLine,
NameFill,
}
impl<T> Settings<BoxColorParameter> for BoxColors<T> {
type Value = T;
fn get_mut(&mut self, parameter: &BoxColorParameter) -> &mut T {
use BoxColorParameter::*;
match parameter {
TextLine => &mut self.text_line,
TextFill => &mut self.text_fill,
NameLine => &mut self.name_line,
NameFill => &mut self.name_fill,
}
}
}
resettable!(<T> BoxColors<T> { text_line, text_fill, name_line, name_fill });
#[derive(Clone, Debug)]
pub struct SelectableBoxColors<T> {
pub default: BoxColors<T>,
pub selected: BoxColors<T>,
}
impl<T: From<Color>> SelectableBoxColors<T> {
#[must_use]
pub fn common() -> Self {
Self {
default: BoxColors::common(),
selected: BoxColors {
text_fill: Color::gray(64).into(),
..BoxColors::common()
},
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct SelectableBoxColorParameter {
pub selected: bool,
pub parameter: BoxColorParameter,
}
impl<T> Settings<SelectableBoxColorParameter> for SelectableBoxColors<T> {
type Value = T;
fn get_mut(&mut self, parameter: &SelectableBoxColorParameter) -> &mut T {
if parameter.selected {
&mut self.selected
} else {
&mut self.default
}
.get_mut(¶meter.parameter)
}
}
resettable!(<T> SelectableBoxColors<T> { default, selected });
#[derive(Clone)]
pub struct ColorSettings<T> {
pub background: T,
pub foreground: T,
pub dialog_box: BoxColors<T>,
pub select_box: SelectableBoxColors<T>,
pub revert_box: SelectableBoxColors<T>,
pub menu: SelectableBoxColors<T>,
}
impl<T: From<Color>> ColorSettings<T> {
#[must_use]
pub fn common() -> Self {
Self {
background: Color::BLACK.into(),
foreground: Color::BLACK.a(0).into(),
dialog_box: BoxColors::common(),
select_box: SelectableBoxColors::common(),
revert_box: SelectableBoxColors::common(),
menu: SelectableBoxColors {
default: BoxColors {
text_fill: Color::gray(30).a(235).into(),
text_line: Color::gray(175).into(),
name_fill: Color::gray(30).a(235).into(),
name_line: Color::WHITE.into(),
},
selected: BoxColors {
text_fill: Color::rgba(70, 90, 150, 235).into(),
text_line: Color::WHITE.into(),
name_fill: Color::gray(30).a(235).into(),
name_line: Color::WHITE.into(),
},
},
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum ColorParameter {
Background,
Foreground,
Dialog(BoxColorParameter),
Choice(SelectableBoxColorParameter),
Revert(SelectableBoxColorParameter),
Menu(SelectableBoxColorParameter),
}
impl<T> Settings<ColorParameter> for ColorSettings<T> {
type Value = T;
fn get_mut(&mut self, parameter: &ColorParameter) -> &mut T {
use ColorParameter::*;
match parameter {
Background => &mut self.background,
Foreground => &mut self.foreground,
Dialog(parameter) => self.dialog_box.get_mut(parameter),
Choice(parameter) => self.select_box.get_mut(parameter),
Revert(parameter) => self.revert_box.get_mut(parameter),
Menu(parameter) => self.menu.get_mut(parameter),
}
}
}
resettable!(<T> ColorSettings<T> { background, foreground, dialog_box, select_box, revert_box, menu });
const BOX_COLOR_PARAMETERS: [(&str, BoxColorParameter); 4] = [
("text-fill", BoxColorParameter::TextFill),
("text-line", BoxColorParameter::TextLine),
("name-fill", BoxColorParameter::NameFill),
("name-line", BoxColorParameter::NameLine),
];
const SELECTIONS: [(&str, bool); 2] = [("default", false), ("select", true)];
type SelectableColorKind = fn(SelectableBoxColorParameter) -> ColorParameter;
const SELECTABLE_COLOR_KINDS: [(&str, SelectableColorKind); 3] = [
("choice", ColorParameter::Choice),
("revert", ColorParameter::Revert),
("menu", ColorParameter::Menu),
];
impl BoxColorParameter {
fn parse(name: &str) -> Option<Self> {
lookup(&BOX_COLOR_PARAMETERS, name)
}
fn push_names(prefix: &str, out: &mut Vec<Box<str>>) {
push_all(&BOX_COLOR_PARAMETERS, prefix, out);
}
}
impl SelectableBoxColorParameter {
fn parse(name: &str) -> Option<Self> {
let (selection, parameter_name) = name.split_once('-')?;
Some(Self {
selected: lookup(&SELECTIONS, selection)?,
parameter: BoxColorParameter::parse(parameter_name)?,
})
}
fn push_names(prefix: &str, out: &mut Vec<Box<str>>) {
for (selection, _) in SELECTIONS {
BoxColorParameter::push_names(&format!("{prefix}{selection}-"), out);
}
}
}
impl ColorParameter {
pub fn parse(name: &str) -> Option<Self> {
Some(match name {
"background" => Self::Background,
"foreground" => Self::Foreground,
_ => {
let (kind, parameter_name) = name.split_once('-')?;
if kind == "dialog" {
Self::Dialog(BoxColorParameter::parse(parameter_name)?)
} else {
lookup(&SELECTABLE_COLOR_KINDS, kind)?(SelectableBoxColorParameter::parse(
parameter_name,
)?)
}
}
})
}
pub(crate) fn push_names(prefix: &str, out: &mut Vec<Box<str>>) {
out.push(format!("{prefix}background").into());
out.push(format!("{prefix}foreground").into());
BoxColorParameter::push_names(&format!("{prefix}dialog-"), out);
for (kind, _) in SELECTABLE_COLOR_KINDS {
SelectableBoxColorParameter::push_names(&format!("{prefix}{kind}-"), out);
}
}
}