use super::{
recipe::{Case, Interaction, Name, ResolvedRecipe},
theme::{Themed, current_theme},
};
use crate::node::{BackgroundColor, TextColor, color::Color};
pub(crate) struct EngineLook {
recipe: ResolvedRecipe,
}
impl EngineLook {
pub(crate) fn of<C: Themed>(states: &[Name]) -> Self {
Self::in_theme::<C>(¤t_theme(), states)
}
pub(crate) fn in_theme<C: Themed>(theme: &crate::app::theme::Theme, states: &[Name]) -> Self {
let mut case = Case::new();
for state in states {
case = case.state(*state, true);
}
Self {
recipe: theme.resolve::<C>(&case, None),
}
}
fn rest(&self, part: impl Into<Name>, interaction: Option<Interaction>) -> super::PartStyle {
self.recipe.part(part).forced(interaction).rest()
}
fn palette_color(&self, color: Option<BackgroundColor>) -> Option<Color> {
let palette = self.recipe.theme().palette();
color.and_then(|color| color.resolve(&palette))
}
pub(crate) fn background(&self, part: impl Into<Name>, or: Color) -> Color {
self.palette_color(self.rest(part, None).background).unwrap_or(or)
}
pub(crate) fn foreground(&self, part: impl Into<Name>, or: Color) -> Color {
let foreground = self.rest(part, None).foreground.map(|color| match color {
TextColor::Color(color) => BackgroundColor::Color(color),
TextColor::Palette(role) => BackgroundColor::Palette(role),
});
self.palette_color(foreground).unwrap_or(or)
}
pub(crate) fn opacity(&self, part: impl Into<Name>, or: f32) -> f32 {
self.rest(part, None).opacity_in(self.recipe.theme()).unwrap_or(or)
}
pub(crate) fn fill(&self, part: impl Into<Name>, interaction: Option<Interaction>) -> Option<BackgroundColor> {
self.rest(part, interaction).background
}
pub(crate) fn edge(&self, part: impl Into<Name>, interaction: Option<Interaction>) -> Option<BackgroundColor> {
self.rest(part, interaction).border.map(|border| border.color)
}
pub(crate) fn ink(&self, part: impl Into<Name>) -> Option<TextColor> {
self.rest(part, None).foreground
}
}