use crcbl_core::input::CursorIcon;
use crate::draw_list::CornerRadii;
use crate::tree::{
Align, BorderImageWidth, Direction, Display, Edges, FamilyName, FlexDirection, FlexWrap,
FontFamily, FontStyle, FontWeight, ImageName, Justify, Length, LengthAuto, LineHeight,
NavTarget, NavWrap, NodeStyle, Overflow, Position, TextAlign, TextDecoration, TextOverflow,
WhiteSpace,
};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Sides {
All,
Top,
Right,
Bottom,
Left,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Corners {
All,
TopLeft,
TopRight,
BottomRight,
BottomLeft,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Declaration {
Display(Display),
Position(Position),
Inset(Sides, LengthAuto),
Width(LengthAuto),
Height(LengthAuto),
MinWidth(LengthAuto),
MinHeight(LengthAuto),
MaxWidth(LengthAuto),
MaxHeight(LengthAuto),
Margin(Sides, LengthAuto),
Padding(Sides, Length),
BorderWidth(Sides, f32),
Overflow(Overflow),
FlexDirection(FlexDirection),
FlexWrap(FlexWrap),
JustifyContent(Option<Justify>),
AlignItems(Option<Align>),
AlignSelf(Option<Align>),
FlexGrow(f32),
FlexShrink(f32),
FlexBasis(LengthAuto),
ColumnGap(Length),
RowGap(Length),
Background([f32; 4]),
BorderColor([f32; 4]),
BorderRadius(Corners, f32),
Color([f32; 4]),
FontSize(f32),
FontFamily(FontFamily),
FamilyName(Option<FamilyName>),
FontWeight(FontWeight),
FontStyle(FontStyle),
LineHeight(LineHeight),
TextAlign(TextAlign),
WhiteSpace(WhiteSpace),
Cursor(Option<CursorIcon>),
TextOverflow(TextOverflow),
TextDecoration(TextDecoration),
OutlineWidth(f32),
OutlineColor([f32; 4]),
OutlineOffset(f32),
Opacity(f32),
BackgroundImage(Option<ImageName>),
BorderImageSource(Option<ImageName>),
BorderImageSlice(Sides, f32),
BorderImageFill(bool),
BorderImageWidth(Sides, BorderImageWidth),
Nav(Direction, NavTarget),
NavWrap(NavWrap),
}
fn set_sides<T: Copy>(edges: &mut Edges<T>, sides: Sides, value: T) {
match sides {
Sides::All => *edges = Edges::all(value),
Sides::Top => edges.top = value,
Sides::Right => edges.right = value,
Sides::Bottom => edges.bottom = value,
Sides::Left => edges.left = value,
}
}
impl Declaration {
pub fn apply(self, style: &mut NodeStyle) {
match self {
Self::Display(value) => style.display = value,
Self::Position(value) => style.position = value,
Self::Inset(sides, value) => set_sides(&mut style.inset, sides, value),
Self::Width(value) => style.width = value,
Self::Height(value) => style.height = value,
Self::MinWidth(value) => style.min_width = value,
Self::MinHeight(value) => style.min_height = value,
Self::MaxWidth(value) => style.max_width = value,
Self::MaxHeight(value) => style.max_height = value,
Self::Margin(sides, value) => set_sides(&mut style.margin, sides, value),
Self::Padding(sides, value) => set_sides(&mut style.padding, sides, value),
Self::BorderWidth(sides, value) => set_sides(&mut style.border, sides, value),
Self::Overflow(value) => style.overflow = value,
Self::FlexDirection(value) => style.flex_direction = value,
Self::FlexWrap(value) => style.flex_wrap = value,
Self::JustifyContent(value) => style.justify_content = value,
Self::AlignItems(value) => style.align_items = value,
Self::AlignSelf(value) => style.align_self = value,
Self::FlexGrow(value) => style.flex_grow = value,
Self::FlexShrink(value) => style.flex_shrink = value,
Self::FlexBasis(value) => style.flex_basis = value,
Self::ColumnGap(value) => style.column_gap = value,
Self::RowGap(value) => style.row_gap = value,
Self::Background(value) => style.background = value,
Self::BorderColor(value) => style.border_color = value,
Self::BorderRadius(corners, value) => {
let radii = &mut style.radii;
match corners {
Corners::All => *radii = CornerRadii::uniform(value),
Corners::TopLeft => radii.top_left = value,
Corners::TopRight => radii.top_right = value,
Corners::BottomRight => radii.bottom_right = value,
Corners::BottomLeft => radii.bottom_left = value,
}
}
Self::Color(value) => style.color = value,
Self::FontSize(value) => style.font_size = value,
Self::FontFamily(value) => style.font_family = value,
Self::FamilyName(value) => style.family_name = value,
Self::FontWeight(value) => style.font_weight = value,
Self::FontStyle(value) => style.font_style = value,
Self::LineHeight(value) => style.line_height = value,
Self::TextAlign(value) => style.text_align = value,
Self::WhiteSpace(value) => style.white_space = value,
Self::Cursor(value) => style.cursor = value,
Self::TextOverflow(value) => style.text_overflow = value,
Self::TextDecoration(value) => style.text_decoration = value,
Self::OutlineWidth(value) => style.outline_width = value,
Self::OutlineColor(value) => style.outline_color = value,
Self::OutlineOffset(value) => style.outline_offset = value,
Self::Opacity(value) => style.opacity = value,
Self::BackgroundImage(value) => style.background_image = value,
Self::BorderImageSource(value) => style.border_image.source = value,
Self::BorderImageSlice(sides, value) => {
set_sides(&mut style.border_image.slice, sides, value);
}
Self::BorderImageFill(value) => style.border_image.fill = value,
Self::BorderImageWidth(sides, value) => {
set_sides(&mut style.border_image.width, sides, value);
}
Self::Nav(direction, value) => *style.nav_mut(direction) = value,
Self::NavWrap(value) => style.nav_wrap = value,
}
}
}
impl NodeStyle {
#[must_use]
pub fn declarations(&self) -> Vec<Declaration> {
use Declaration as D;
let sides = [Sides::Top, Sides::Right, Sides::Bottom, Sides::Left];
let edges = |edges: Edges<LengthAuto>| [edges.top, edges.right, edges.bottom, edges.left];
let mut all = vec![
D::Display(self.display),
D::Position(self.position),
D::Width(self.width),
D::Height(self.height),
D::MinWidth(self.min_width),
D::MinHeight(self.min_height),
D::MaxWidth(self.max_width),
D::MaxHeight(self.max_height),
D::Overflow(self.overflow),
D::FlexDirection(self.flex_direction),
D::FlexWrap(self.flex_wrap),
D::JustifyContent(self.justify_content),
D::AlignItems(self.align_items),
D::AlignSelf(self.align_self),
D::FlexGrow(self.flex_grow),
D::FlexShrink(self.flex_shrink),
D::FlexBasis(self.flex_basis),
D::ColumnGap(self.column_gap),
D::RowGap(self.row_gap),
D::Background(self.background),
D::BorderColor(self.border_color),
D::Color(self.color),
D::FontSize(self.font_size),
D::FontFamily(self.font_family),
D::FamilyName(self.family_name),
D::FontWeight(self.font_weight),
D::FontStyle(self.font_style),
D::LineHeight(self.line_height),
D::TextAlign(self.text_align),
D::WhiteSpace(self.white_space),
D::Cursor(self.cursor),
D::TextOverflow(self.text_overflow),
D::TextDecoration(self.text_decoration),
D::OutlineWidth(self.outline_width),
D::OutlineColor(self.outline_color),
D::OutlineOffset(self.outline_offset),
D::Opacity(self.opacity),
D::BackgroundImage(self.background_image),
D::BorderImageSource(self.border_image.source),
D::BorderImageFill(self.border_image.fill),
D::NavWrap(self.nav_wrap),
];
for direction in Direction::ALL {
all.push(D::Nav(direction, self.nav(direction)));
}
let padding = self.padding;
let border = self.border;
for (at, side) in sides.into_iter().enumerate() {
all.push(D::Inset(side, edges(self.inset)[at]));
all.push(D::Margin(side, edges(self.margin)[at]));
all.push(D::Padding(
side,
[padding.top, padding.right, padding.bottom, padding.left][at],
));
all.push(D::BorderWidth(
side,
[border.top, border.right, border.bottom, border.left][at],
));
let image = self.border_image;
all.push(D::BorderImageSlice(
side,
[
image.slice.top,
image.slice.right,
image.slice.bottom,
image.slice.left,
][at],
));
all.push(D::BorderImageWidth(
side,
[
image.width.top,
image.width.right,
image.width.bottom,
image.width.left,
][at],
));
}
let radii = self.radii;
all.extend([
D::BorderRadius(Corners::TopLeft, radii.top_left),
D::BorderRadius(Corners::TopRight, radii.top_right),
D::BorderRadius(Corners::BottomRight, radii.bottom_right),
D::BorderRadius(Corners::BottomLeft, radii.bottom_left),
]);
all
}
}
#[cfg(all(test, feature = "parsed-font"))]
mod tests {
use super::*;
const fn px(value: f32) -> LengthAuto {
LengthAuto::Px(value)
}
#[test]
fn a_styles_declarations_rebuild_it_exactly() {
let style = NodeStyle {
inset: Edges {
top: px(1.0),
right: LengthAuto::Auto,
bottom: LengthAuto::Percent(0.2),
left: px(-3.0),
},
padding: Edges {
top: Length::Px(1.0),
right: Length::Px(2.0),
bottom: Length::Percent(0.3),
left: Length::Px(4.0),
},
border: Edges {
top: 1.0,
right: 2.0,
bottom: 3.0,
left: 4.0,
},
radii: CornerRadii {
top_left: 1.0,
top_right: 2.0,
bottom_right: 3.0,
bottom_left: 4.0,
},
background: [0.1, 0.2, 0.3, 0.4],
color: [0.5; 4],
font_size: 20.0,
font_family: FontFamily::Sans,
font_weight: FontWeight::BOLD,
font_style: FontStyle::Italic,
text_decoration: TextDecoration::Underline,
line_height: LineHeight::Multiple(1.5),
text_align: TextAlign::Center,
white_space: WhiteSpace::NoWrap,
cursor: Some(CursorIcon::Pointer),
text_overflow: TextOverflow::Ellipsis,
align_self: Some(Align::Center),
outline_width: 2.0,
outline_color: [0.9, 0.8, 0.7, 0.6],
outline_offset: -1.0,
opacity: 0.75,
nav_up: NavTarget::None,
nav_right: NavTarget::Id(crate::tree::NavId::new("next")),
nav_wrap: NavWrap::Horizontal,
background_image: Some(crate::tree::ImageName::new("sky")),
border_image: crate::tree::BorderImage {
source: Some(crate::tree::ImageName::new("frame")),
slice: Edges {
top: 1.0,
right: 2.0,
bottom: 3.0,
left: 4.0,
},
fill: true,
width: Edges {
top: BorderImageWidth::Px(5.0),
right: BorderImageWidth::Multiple(2.0),
bottom: BorderImageWidth::Px(7.0),
left: BorderImageWidth::Multiple(0.5),
},
},
..NodeStyle::DEFAULT
};
let mut rebuilt = NodeStyle {
width: px(99.0),
margin: Edges::all(LengthAuto::Auto),
color: [0.0; 4],
..NodeStyle::DEFAULT
};
for declaration in style.declarations() {
declaration.apply(&mut rebuilt);
}
assert_eq!(rebuilt, style);
}
}