use blinc_core::{Brush, Color};
use blinc_layout::css_parser::{active_stylesheet, ElementState, Stylesheet};
use blinc_layout::element_style::ElementStyle;
#[derive(Clone, Debug, Default)]
pub struct CnStyleOverrides {
pub background: Option<Color>,
pub border_color: Option<Color>,
pub text_color: Option<Color>,
pub accent_color: Option<Color>,
pub opacity: Option<f32>,
pub corner_radius: Option<f32>,
}
impl CnStyleOverrides {
pub fn apply(&mut self, style: &ElementStyle) {
if let Some(Brush::Solid(color)) = style.background {
self.background = Some(color);
}
if let Some(color) = style.border_color {
self.border_color = Some(color);
}
if let Some(color) = style.text_color {
self.text_color = Some(color);
}
if let Some(color) = style.accent_color {
self.accent_color = Some(color);
}
if let Some(o) = style.opacity {
self.opacity = Some(o);
}
if let Some(cr) = style.corner_radius {
self.corner_radius = Some(cr.top_left);
}
}
}
pub fn resolve_class_overrides(
class_name: &str,
is_hovered: bool,
is_pressed: bool,
checked_class: Option<&str>,
disabled_class: Option<&str>,
) -> CnStyleOverrides {
let mut overrides = CnStyleOverrides::default();
let stylesheet = match active_stylesheet() {
Some(s) => s,
None => return overrides,
};
if let Some(style) = stylesheet.get_class(class_name) {
overrides.apply(style);
}
if is_hovered {
if let Some(style) = stylesheet.get_class_with_state(class_name, ElementState::Hover) {
overrides.apply(style);
}
}
if is_pressed {
if let Some(style) = stylesheet.get_class_with_state(class_name, ElementState::Active) {
overrides.apply(style);
}
}
if let Some(checked) = checked_class {
if let Some(style) = stylesheet.get_class(checked) {
overrides.apply(style);
}
}
if let Some(disabled) = disabled_class {
if let Some(style) = stylesheet.get_class(disabled) {
overrides.apply(style);
}
}
overrides
}
pub fn resolve_multi_class_overrides(
classes: &[&str],
is_hovered: bool,
is_pressed: bool,
extra_classes: &[&str],
) -> CnStyleOverrides {
let mut overrides = CnStyleOverrides::default();
let stylesheet = match active_stylesheet() {
Some(s) => s,
None => return overrides,
};
for class in classes {
if let Some(style) = stylesheet.get_class(class) {
overrides.apply(style);
}
if is_hovered {
if let Some(style) = stylesheet.get_class_with_state(class, ElementState::Hover) {
overrides.apply(style);
}
}
if is_pressed {
if let Some(style) = stylesheet.get_class_with_state(class, ElementState::Active) {
overrides.apply(style);
}
}
}
for class in extra_classes {
if let Some(style) = stylesheet.get_class(class) {
overrides.apply(style);
}
}
overrides
}