use meo_canvas_scene::{
Corners, Sides,
style::{
Dimension, Length, PaintOrder,
effect::{BoxShadow, Effects, Mask, TextShadow, Transform},
layout::{
Align, BoxSizing, Direction, Display, FlexDirection, FlexWrap,
GridAutoFlow, GridPlacement, Justify, JustifyItems, JustifySelf,
LayoutStyle, Overflow, PositionType, TrackSize,
},
paint::{
BackgroundImage, BlendMode, BorderStyle, ColorValue, Gradient,
ObjectFit, PaintStyle,
},
text::{
FontStyle, FontVariant, FontWeight, LineHeight, Spacing, TextAlign,
TextDecoration, TextStroke, TextStyle, VerticalAlign,
},
},
};
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Style {
pub display: Option<Display>,
pub position_type: Option<PositionType>,
pub position: Option<Sides<Option<Length>>>,
pub width: Option<Dimension>,
pub height: Option<Dimension>,
pub min_width: Option<Dimension>,
pub min_height: Option<Dimension>,
pub max_width: Option<Dimension>,
pub max_height: Option<Dimension>,
pub aspect_ratio: Option<f32>,
pub margin: Option<Sides<Dimension>>,
pub padding: Option<Sides<Length>>,
pub border: Option<Sides<f32>>,
pub flex_direction: Option<FlexDirection>,
pub flex_wrap: Option<FlexWrap>,
pub flex_grow: Option<f32>,
pub flex_shrink: Option<f32>,
pub flex_basis: Option<Dimension>,
pub justify_content: Option<Justify>,
pub align_items: Option<Align>,
pub align_self: Option<Align>,
pub align_content: Option<Align>,
pub justify_items: Option<JustifyItems>,
pub justify_self: Option<JustifySelf>,
pub gap: Option<(Length, Length)>,
pub overflow: Option<(Overflow, Overflow)>,
pub box_sizing: Option<BoxSizing>,
pub direction: Option<Direction>,
pub grid_template_columns: Option<Vec<TrackSize>>,
pub grid_template_rows: Option<Vec<TrackSize>>,
pub grid_auto_rows: Option<TrackSize>,
pub grid_auto_columns: Option<TrackSize>,
pub grid_auto_flow: Option<GridAutoFlow>,
pub grid_column: Option<GridPlacement>,
pub grid_row: Option<GridPlacement>,
pub background_color: Option<ColorValue>,
pub gradient: Option<Gradient>,
pub background_image: Option<BackgroundImage>,
pub border_color: Option<Sides<Option<ColorValue>>>,
pub border_color_all: Option<ColorValue>,
pub border_style: Option<BorderStyle>,
pub border_radius: Option<Corners<f32>>,
pub opacity: Option<f32>,
pub mix_blend_mode: Option<BlendMode>,
pub dither: Option<bool>,
pub z_index: Option<i32>,
pub object_fit: Option<ObjectFit>,
pub object_position: Option<(Length, Length)>,
pub frame: Option<i32>,
pub font_family: Option<String>,
pub font_size: Option<f32>,
pub font_weight: Option<FontWeight>,
pub font_style: Option<FontStyle>,
pub color: Option<ColorValue>,
pub text_align: Option<TextAlign>,
pub text_decoration: Option<TextDecoration>,
pub vertical_align: Option<VerticalAlign>,
pub paint_order: Option<PaintOrder>,
pub line_height: Option<LineHeight>,
pub line_gap: Option<f32>,
pub font_variant: Option<Vec<FontVariant>>,
pub letter_spacing: Option<Spacing>,
pub word_spacing: Option<Spacing>,
pub text_stroke: Option<TextStroke>,
pub transform: Option<Transform>,
pub box_shadows: Option<Vec<BoxShadow>>,
pub text_shadows: Option<Vec<TextShadow>>,
pub mask: Option<Mask>,
pub filter: Option<String>,
pub backdrop_filter: Option<String>,
}
macro_rules! properties {
(
fields {
$(
$(#[$doc:meta])*
$kind:ident $name:ident: $field:ident, $type:ty;
)+
}
via {
$( $via:ident($($arg:ident: $arg_type:ty),*); )*
}
) => {
#[expect(
clippy::cast_sign_loss,
reason = "the subtraction is guarded to a positive difference"
)]
const fn span_between(start: i16, end: i16) -> u16 {
if end > start {
end.saturating_sub(start) as u16
} else {
1
}
}
impl Style {
$( properties!(@style $kind $(#[$doc])* $name: $field, $type); )+
}
pub trait Styled: Sized {
fn style_mut(&mut self) -> &mut Style;
$( properties!(@node $kind $name, $type); )+
$(
#[doc = concat!(
"Sets [`", stringify!($via), "`](Style::", stringify!($via), ") on this node."
)]
#[must_use]
fn $via(mut self, $($arg: $arg_type),*) -> Self {
let style = ::core::mem::replace(
<Self as Styled>::style_mut(&mut self),
Style::new(),
);
*<Self as Styled>::style_mut(&mut self) = style.$via($($arg),*);
self
}
)*
}
};
(@style plain $(#[$doc:meta])* $name:ident: $field:ident, $type:ty) => {
$(#[$doc])*
#[must_use]
pub const fn $name(mut self, value: $type) -> Self {
self.$field = Some(value);
self
}
};
(@style sides $(#[$doc:meta])* $name:ident: $field:ident, $type:ty) => {
$(#[$doc])*
#[must_use]
pub const fn $name(mut self, value: Sides<$type>) -> Self {
self.$field = Some(value);
self
}
};
(@style corners $(#[$doc:meta])* $name:ident: $field:ident, $type:ty) => {
$(#[$doc])*
#[must_use]
pub const fn $name(mut self, value: Corners<$type>) -> Self {
self.$field = Some(value);
self
}
};
(@node sides $name:ident, $type:ty) => {
#[doc = concat!(
"Sets [`", stringify!($name), "`](Style::", stringify!($name), ") on this node."
)]
#[doc = ""]
#[doc = "One value for every edge, or the four named."]
#[must_use]
fn $name(mut self, value: impl crate::unit::IntoSides<$type>) -> Self {
let style = ::core::mem::replace(
<Self as Styled>::style_mut(&mut self),
Style::new(),
);
*<Self as Styled>::style_mut(&mut self) = style.$name(value.into_sides());
self
}
};
(@node corners $name:ident, $type:ty) => {
#[doc = concat!(
"Sets [`", stringify!($name), "`](Style::", stringify!($name), ") on this node."
)]
#[doc = ""]
#[doc = "One radius for every corner, or the four named."]
#[must_use]
fn $name(mut self, value: impl crate::unit::IntoCorners<$type>) -> Self {
let style = ::core::mem::replace(
<Self as Styled>::style_mut(&mut self),
Style::new(),
);
*<Self as Styled>::style_mut(&mut self) = style.$name(value.into_corners());
self
}
};
(@style colour $(#[$doc:meta])* $name:ident: $field:ident, $type:ty) => {
properties!(@style plain $(#[$doc])* $name: $field, $type);
};
(@node colour $name:ident, $type:ty) => {
#[doc = concat!(
"Sets [`", stringify!($name), "`](Style::", stringify!($name), ") on this node."
)]
#[doc = ""]
#[doc = "A [`Color`](crate::scene::Color) or a [`ColorValue`], such as [`ColorValue::Current`]."]
#[must_use]
fn $name(mut self, value: impl Into<$type>) -> Self {
let style = ::core::mem::replace(
<Self as Styled>::style_mut(&mut self),
Style::new(),
);
*<Self as Styled>::style_mut(&mut self) = style.$name(value.into());
self
}
};
(@style owned $(#[$doc:meta])* $name:ident: $field:ident, $type:ty) => {
$(#[$doc])*
#[must_use]
pub fn $name(mut self, value: impl Into<$type>) -> Self {
self.$field = Some(value.into());
self
}
};
(@node plain $name:ident, $type:ty) => {
#[doc = concat!(
"Sets [`", stringify!($name), "`](Style::", stringify!($name), ") on this node."
)]
#[must_use]
fn $name(mut self, value: $type) -> Self {
let style = ::core::mem::replace(
<Self as Styled>::style_mut(&mut self),
Style::new(),
);
*<Self as Styled>::style_mut(&mut self) = style.$name(value);
self
}
};
(@node owned $name:ident, $type:ty) => {
#[doc = concat!(
"Sets [`", stringify!($name), "`](Style::", stringify!($name), ") on this node."
)]
#[must_use]
fn $name(mut self, value: impl Into<$type>) -> Self {
let style = ::core::mem::replace(self.style_mut(), Style::new());
*self.style_mut() = style.$name(value);
self
}
};
}
properties! {
fields {
plain display: display, Display;
plain position_type: position_type, PositionType;
sides position: position, Option<Length>;
plain aspect_ratio: aspect_ratio, f32;
sides margin: margin, Dimension;
sides padding: padding, Length;
sides border: border, f32;
plain flex_direction: flex_direction, FlexDirection;
plain flex_wrap: flex_wrap, FlexWrap;
plain flex_grow: flex_grow, f32;
plain flex_shrink: flex_shrink, f32;
plain flex_basis: flex_basis, Dimension;
plain justify_content: justify_content, Justify;
plain align_items: align_items, Align;
plain align_self: align_self, Align;
plain align_content: align_content, Align;
plain justify_items: justify_items, JustifyItems;
plain justify_self: justify_self, JustifySelf;
plain box_sizing: box_sizing, BoxSizing;
plain direction: direction, Direction;
owned grid_template_columns: grid_template_columns, Vec<TrackSize>;
owned grid_template_rows: grid_template_rows, Vec<TrackSize>;
plain grid_auto_rows: grid_auto_rows, TrackSize;
plain grid_auto_columns: grid_auto_columns, TrackSize;
plain grid_auto_flow: grid_auto_flow, GridAutoFlow;
plain grid_column: grid_column, GridPlacement;
plain grid_row: grid_row, GridPlacement;
colour background_color: background_color, ColorValue;
owned gradient: gradient, Gradient;
owned background_image: background_image, BackgroundImage;
colour border_color: border_color_all, ColorValue;
sides border_color_sides: border_color, Option<ColorValue>;
plain border_style: border_style, BorderStyle;
corners border_radius_corners: border_radius, f32;
plain opacity: opacity, f32;
plain mix_blend_mode: mix_blend_mode, BlendMode;
plain dither: dither, bool;
plain z_index: z_index, i32;
plain object_fit: object_fit, ObjectFit;
plain object_position: object_position, (Length, Length);
plain frame: frame, i32;
colour color: color, ColorValue;
owned font_family: font_family, String;
plain font_size: font_size, f32;
plain font_weight: font_weight, FontWeight;
plain font_style: font_style, FontStyle;
plain text_align: text_align, TextAlign;
plain text_decoration: text_decoration, TextDecoration;
plain vertical_align: vertical_align, VerticalAlign;
plain paint_order: paint_order, PaintOrder;
owned font_variant: font_variant, Vec<FontVariant>;
plain line_height: line_height, LineHeight;
plain line_gap: line_gap, f32;
plain text_stroke: text_stroke, TextStroke;
plain transform: transform, Transform;
owned box_shadow: box_shadows, Vec<BoxShadow>;
owned text_shadow: text_shadows, Vec<TextShadow>;
owned mask: mask, Mask;
owned filter: filter, String;
owned backdrop_filter: backdrop_filter, String;
}
via {
width(width: Length);
height(height: Length);
size(width: Length, height: Length);
min_width(width: Length);
min_height(height: Length);
max_width(width: Length);
max_height(height: Length);
gap(gap: Length);
gap_xy(row: Length, column: Length);
overflow(overflow: Overflow);
border_radius(border_radius: f32);
bold();
italic();
letter_spacing(spacing: Length);
word_spacing(spacing: Length);
}
}
impl Style {
#[must_use]
#[expect(
clippy::too_many_lines,
reason = "one line per property, which is the exhaustiveness that makes a forgotten field a build error"
)]
pub fn merge(mut self, other: Self) -> Self {
let Self {
display,
position_type,
position,
width,
height,
min_width,
min_height,
max_width,
max_height,
aspect_ratio,
margin,
padding,
border,
flex_direction,
flex_wrap,
flex_grow,
flex_shrink,
flex_basis,
justify_content,
align_items,
align_self,
align_content,
justify_items,
justify_self,
gap,
overflow,
box_sizing,
direction,
grid_template_columns,
grid_template_rows,
grid_auto_rows,
grid_auto_columns,
grid_auto_flow,
grid_column,
grid_row,
background_color,
gradient,
background_image,
border_color,
border_color_all,
border_style,
border_radius,
opacity,
mix_blend_mode,
dither,
z_index,
object_fit,
object_position,
frame,
font_family,
font_size,
font_weight,
font_style,
color,
text_align,
text_decoration,
vertical_align,
paint_order,
line_height,
line_gap,
font_variant,
letter_spacing,
word_spacing,
text_stroke,
transform,
box_shadows,
text_shadows,
mask,
filter,
backdrop_filter,
} = other;
self.display = display.or(self.display);
self.position_type = position_type.or(self.position_type);
self.position = position.or(self.position);
self.width = width.or(self.width);
self.height = height.or(self.height);
self.min_width = min_width.or(self.min_width);
self.min_height = min_height.or(self.min_height);
self.max_width = max_width.or(self.max_width);
self.max_height = max_height.or(self.max_height);
self.aspect_ratio = aspect_ratio.or(self.aspect_ratio);
self.margin = margin.or(self.margin);
self.padding = padding.or(self.padding);
self.border = border.or(self.border);
self.flex_direction = flex_direction.or(self.flex_direction);
self.flex_wrap = flex_wrap.or(self.flex_wrap);
self.flex_grow = flex_grow.or(self.flex_grow);
self.flex_shrink = flex_shrink.or(self.flex_shrink);
self.flex_basis = flex_basis.or(self.flex_basis);
self.justify_content = justify_content.or(self.justify_content);
self.align_items = align_items.or(self.align_items);
self.align_self = align_self.or(self.align_self);
self.align_content = align_content.or(self.align_content);
self.justify_items = justify_items.or(self.justify_items);
self.justify_self = justify_self.or(self.justify_self);
self.gap = gap.or(self.gap);
self.overflow = overflow.or(self.overflow);
self.box_sizing = box_sizing.or(self.box_sizing);
self.direction = direction.or(self.direction);
self.grid_template_columns =
grid_template_columns.or(self.grid_template_columns);
self.grid_template_rows =
grid_template_rows.or(self.grid_template_rows);
self.grid_auto_rows = grid_auto_rows.or(self.grid_auto_rows);
self.grid_auto_columns = grid_auto_columns.or(self.grid_auto_columns);
self.grid_auto_flow = grid_auto_flow.or(self.grid_auto_flow);
self.grid_column = grid_column.or(self.grid_column);
self.grid_row = grid_row.or(self.grid_row);
self.background_color = background_color.or(self.background_color);
self.gradient = gradient.or(self.gradient);
self.background_image = background_image.or(self.background_image);
self.border_color = border_color.or(self.border_color);
self.border_color_all = border_color_all.or(self.border_color_all);
self.border_style = border_style.or(self.border_style);
self.border_radius = border_radius.or(self.border_radius);
self.opacity = opacity.or(self.opacity);
self.mix_blend_mode = mix_blend_mode.or(self.mix_blend_mode);
self.dither = dither.or(self.dither);
self.z_index = z_index.or(self.z_index);
self.object_fit = object_fit.or(self.object_fit);
self.object_position = object_position.or(self.object_position);
self.frame = frame.or(self.frame);
self.font_family = font_family.or(self.font_family);
self.font_size = font_size.or(self.font_size);
self.font_weight = font_weight.or(self.font_weight);
self.font_style = font_style.or(self.font_style);
self.color = color.or(self.color);
self.text_align = text_align.or(self.text_align);
self.text_decoration = text_decoration.or(self.text_decoration);
self.vertical_align = vertical_align.or(self.vertical_align);
self.paint_order = paint_order.or(self.paint_order);
self.line_height = line_height.or(self.line_height);
self.line_gap = line_gap.or(self.line_gap);
self.font_variant = font_variant.or(self.font_variant);
self.letter_spacing = letter_spacing.or(self.letter_spacing);
self.word_spacing = word_spacing.or(self.word_spacing);
self.text_stroke = text_stroke.or(self.text_stroke);
self.transform = transform.or(self.transform);
self.box_shadows = box_shadows.or(self.box_shadows);
self.text_shadows = text_shadows.or(self.text_shadows);
self.mask = mask.or(self.mask);
self.filter = filter.or(self.filter);
self.backdrop_filter = backdrop_filter.or(self.backdrop_filter);
self
}
#[must_use]
pub const fn new() -> Self {
Self {
display: None,
position_type: None,
position: None,
width: None,
height: None,
min_width: None,
min_height: None,
max_width: None,
max_height: None,
aspect_ratio: None,
margin: None,
padding: None,
border: None,
flex_direction: None,
flex_wrap: None,
flex_grow: None,
flex_shrink: None,
flex_basis: None,
justify_content: None,
align_items: None,
align_self: None,
align_content: None,
justify_items: None,
justify_self: None,
gap: None,
overflow: None,
box_sizing: None,
direction: None,
grid_template_columns: None,
grid_template_rows: None,
grid_auto_rows: None,
grid_auto_columns: None,
grid_auto_flow: None,
grid_column: None,
grid_row: None,
background_color: None,
gradient: None,
background_image: None,
border_color: None,
border_color_all: None,
border_style: None,
border_radius: None,
opacity: None,
mix_blend_mode: None,
dither: None,
z_index: None,
object_fit: None,
object_position: None,
frame: None,
font_family: None,
font_size: None,
font_weight: None,
font_style: None,
color: None,
text_align: None,
text_decoration: None,
vertical_align: None,
paint_order: None,
line_height: None,
line_gap: None,
font_variant: None,
letter_spacing: None,
word_spacing: None,
text_stroke: None,
transform: None,
box_shadows: None,
text_shadows: None,
mask: None,
filter: None,
backdrop_filter: None,
}
}
#[must_use]
pub const fn width(mut self, width: Length) -> Self {
self.width = Some(widen(width));
self
}
#[must_use]
pub const fn height(mut self, height: Length) -> Self {
self.height = Some(widen(height));
self
}
#[must_use]
pub const fn size(self, width: Length, height: Length) -> Self {
self.width(width).height(height)
}
#[must_use]
pub const fn min_width(mut self, width: Length) -> Self {
self.min_width = Some(widen(width));
self
}
#[must_use]
pub const fn min_height(mut self, height: Length) -> Self {
self.min_height = Some(widen(height));
self
}
#[must_use]
pub const fn max_width(mut self, width: Length) -> Self {
self.max_width = Some(widen(width));
self
}
#[must_use]
pub const fn max_height(mut self, height: Length) -> Self {
self.max_height = Some(widen(height));
self
}
#[must_use]
pub const fn gap(mut self, gap: Length) -> Self {
self.gap = Some((gap, gap));
self
}
#[must_use]
pub const fn gap_xy(mut self, row: Length, column: Length) -> Self {
self.gap = Some((row, column));
self
}
#[must_use]
pub const fn overflow(mut self, overflow: Overflow) -> Self {
self.overflow = Some((overflow, overflow));
self
}
#[must_use]
pub const fn border_radius(mut self, border_radius: f32) -> Self {
self.border_radius = Some(Corners {
top_left: border_radius,
top_right: border_radius,
bottom_right: border_radius,
bottom_left: border_radius,
});
self
}
#[must_use]
pub const fn bold(mut self) -> Self {
self.font_weight = Some(FontWeight::BOLD);
self
}
#[must_use]
pub const fn italic(mut self) -> Self {
self.font_style = Some(FontStyle::Italic);
self
}
#[must_use]
pub fn columns(mut self, columns: u16) -> Self {
self.grid_template_columns =
Some(vec![TrackSize::Fraction(1.0); usize::from(columns)]);
self
}
#[must_use]
pub const fn grid_area(
mut self,
row_start: i16,
column_start: i16,
row_end: i16,
column_end: i16,
) -> Self {
self.grid_row = Some(GridPlacement {
start: Some(row_start),
span: Some(span_between(row_start, row_end)),
});
self.grid_column = Some(GridPlacement {
start: Some(column_start),
span: Some(span_between(column_start, column_end)),
});
self
}
#[must_use]
pub const fn letter_spacing(mut self, spacing: Length) -> Self {
self.letter_spacing = Some(to_spacing(spacing));
self
}
#[must_use]
pub const fn word_spacing(mut self, spacing: Length) -> Self {
self.word_spacing = Some(to_spacing(spacing));
self
}
#[must_use]
#[expect(
clippy::too_many_lines,
reason = "one line per property; splitting it would scatter the mapping"
)]
pub fn into_parts(self) -> (LayoutStyle, PaintStyle, TextStyle, Effects) {
let mut layout = LayoutStyle::default();
let mut paint = PaintStyle::default();
let mut text = TextStyle::default();
let mut effects = Effects::default();
if let Some(value) = self.display {
layout.display = value;
}
if let Some(value) = self.position_type {
layout.position_type = value;
}
if let Some(value) = self.position {
layout.inset = value;
}
if let Some(value) = self.width {
layout.size.0 = value;
}
if let Some(value) = self.height {
layout.size.1 = value;
}
if let Some(value) = self.min_width {
layout.min_size.0 = value;
}
if let Some(value) = self.min_height {
layout.min_size.1 = value;
}
if let Some(value) = self.max_width {
layout.max_size.0 = value;
}
if let Some(value) = self.max_height {
layout.max_size.1 = value;
}
layout.aspect_ratio = self.aspect_ratio;
if let Some(value) = self.margin {
layout.margin = value;
}
if let Some(value) = self.padding {
layout.padding = value;
}
if let Some(value) = self.border {
layout.border = value;
}
if let Some(value) = self.flex_direction {
layout.flex_direction = value;
}
if let Some(value) = self.flex_wrap {
layout.flex_wrap = value;
}
if let Some(value) = self.flex_grow {
layout.flex_grow = value;
}
if let Some(value) = self.flex_shrink {
layout.flex_shrink = value;
}
if let Some(value) = self.flex_basis {
layout.flex_basis = value;
}
layout.justify_content = self.justify_content;
layout.align_items = self.align_items;
layout.align_self = self.align_self;
layout.align_content = self.align_content;
layout.justify_items = self.justify_items;
layout.justify_self = self.justify_self;
if let Some(value) = self.gap {
layout.gap = value;
}
if let Some(value) = self.overflow {
layout.overflow = value;
}
if let Some(value) = self.box_sizing {
layout.box_sizing = value;
}
if let Some(value) = self.direction {
layout.direction = Some(value);
}
if let Some(value) = self.grid_template_columns {
layout.grid_template_columns = value;
}
if let Some(value) = self.grid_template_rows {
layout.grid_template_rows = value;
}
layout.grid_auto_rows = self.grid_auto_rows;
layout.grid_auto_columns = self.grid_auto_columns;
if let Some(value) = self.grid_auto_flow {
layout.grid_auto_flow = value;
}
if let Some(value) = self.grid_column {
layout.grid_column = value;
}
if let Some(value) = self.grid_row {
layout.grid_row = value;
}
if let Some(value) = self.background_color {
paint.background_color = value;
}
paint.gradient = self.gradient;
paint.background_image = self.background_image;
if let Some(value) = self.border_color {
paint.border_color = value;
}
if let Some(value) = self.border_color_all {
paint.border_color_all = value;
}
if let Some(value) = self.border_style {
paint.border_style = value;
}
if let Some(value) = self.border_radius {
paint.border_radius = value;
}
if let Some(value) = self.opacity {
paint.opacity = value;
}
if let Some(value) = self.mix_blend_mode {
paint.blend_mode = value;
}
if let Some(value) = self.dither {
paint.dither = value;
}
if let Some(value) = self.z_index {
paint.z_index = Some(value);
}
text.font_family = self.font_family;
text.font_size = self.font_size;
text.font_weight = self.font_weight;
text.font_style = self.font_style;
text.color = self.color;
text.text_align = self.text_align;
text.text_decoration = self.text_decoration;
text.vertical_align = self.vertical_align;
text.paint_order = self.paint_order;
text.line_height = self.line_height;
text.line_gap = self.line_gap;
text.font_variant = self.font_variant;
text.letter_spacing = self.letter_spacing;
text.word_spacing = self.word_spacing;
text.text_stroke = self.text_stroke;
effects.transform = self.transform;
if let Some(value) = self.box_shadows {
effects.box_shadows = value;
}
if let Some(value) = self.text_shadows {
effects.text_shadows = value;
}
effects.mask = self.mask;
effects.filter = self.filter;
effects.backdrop_filter = self.backdrop_filter;
(layout, paint, text, effects)
}
}
const fn widen(length: Length) -> Dimension {
match length {
Length::Points(points) => Dimension::Points(points),
Length::Percent(fraction) => Dimension::Percent(fraction),
}
}
const fn to_spacing(length: Length) -> Spacing {
match length {
Length::Points(points) => Spacing::Points(points),
Length::Percent(fraction) => Spacing::Em(fraction),
}
}
#[cfg(test)]
mod tests {
use meo_canvas_scene::style::{
Dimension, Length,
layout::{
Align, Display, FlexDirection, GridPlacement, Justify, LayoutStyle,
Overflow,
},
paint::PaintStyle,
text::{FontStyle, FontWeight, LineHeight, Spacing, TextStyle},
};
use super::Style;
use crate::{all, hex_rgb, pct, px, top, track, xy};
#[test]
fn a_new_style_sets_nothing() {
let style = Style::new();
assert!(style.width.is_none());
assert!(style.background_color.is_none());
assert!(style.font_size.is_none());
assert_eq!(style, Style::default());
}
#[test]
fn a_shorthand_is_the_long_form_and_nothing_else() {
assert_eq!(
Style::new().columns(3).grid_template_columns,
Style::new()
.grid_template_columns([
crate::fr(1.0),
crate::fr(1.0),
crate::fr(1.0)
])
.grid_template_columns
);
let area = Style::new().grid_area(1, 1, 3, 2);
let long = Style::new()
.grid_row(GridPlacement::spanning(1, 2))
.grid_column(GridPlacement::spanning(1, 1));
assert_eq!(area.grid_row, long.grid_row);
assert_eq!(area.grid_column, long.grid_column);
}
#[test]
fn an_empty_grid_area_becomes_the_smallest_one_that_means_anything() {
let backwards = Style::new().grid_area(3, 1, 1, 2);
assert_eq!(
backwards.grid_row,
Some(GridPlacement {
start: Some(3),
span: Some(1)
})
);
}
#[test]
fn a_const_style_is_reusable_without_a_clone() {
const CARD: Style = Style::new().padding(all(px(24.0))).gap(px(16.0));
let dark = CARD.background_color(hex_rgb(0x10_10_14).into());
let light = CARD.background_color(hex_rgb(0x1c_1c_22).into());
assert_eq!(dark.padding, light.padding);
assert_ne!(dark.background_color, light.background_color);
}
#[test]
fn an_unset_property_takes_the_scenes_default_rather_than_one_restated() {
let (layout, paint, text, effects) = Style::new().into_parts();
assert_eq!(layout, LayoutStyle::default());
assert_eq!(paint, PaintStyle::default());
assert_eq!(text, TextStyle::default());
assert_eq!(effects.box_shadows.len(), 0);
}
#[test]
fn layout_properties_reach_the_layout_half() {
let (layout, ..) = Style::new()
.display(Display::Grid)
.flex_direction(FlexDirection::Column)
.justify_content(Justify::SpaceBetween)
.align_items(Align::Center)
.width(px(320.0))
.height(pct(50.0))
.min_width(px(10.0))
.max_height(px(400.0))
.padding(xy(px(8.0), px(16.0)))
.margin(all(Dimension::Points(4.0)))
.border(top(2.0))
.flex_grow(1.0)
.flex_shrink(0.0)
.aspect_ratio(1.618)
.grid_template_columns([track(px(240.0))])
.into_parts();
assert_eq!(layout.display, Display::Grid);
assert_eq!(layout.flex_direction, FlexDirection::Column);
assert_eq!(layout.justify_content, Some(Justify::SpaceBetween));
assert_eq!(layout.align_items, Some(Align::Center));
assert_eq!(layout.size.0, Dimension::Points(320.0));
assert_eq!(layout.size.1, Dimension::Percent(0.5));
assert_eq!(layout.min_size.0, Dimension::Points(10.0));
assert_eq!(layout.max_size.1, Dimension::Points(400.0));
assert_eq!(layout.padding.top, Length::Points(8.0));
assert_eq!(layout.padding.left, Length::Points(16.0));
assert_eq!(layout.border.top.to_bits(), 2.0_f32.to_bits());
assert_eq!(layout.border.left.to_bits(), 0.0_f32.to_bits());
assert_eq!(layout.aspect_ratio, Some(1.618));
assert_eq!(layout.grid_template_columns.len(), 1);
}
#[test]
fn one_gap_sets_both_axes_and_the_pair_form_keeps_csss_order() {
let (one, ..) = Style::new().gap(px(12.0)).into_parts();
assert_eq!(one.gap, (Length::Points(12.0), Length::Points(12.0)));
let (pair, ..) = Style::new().gap_xy(px(4.0), px(8.0)).into_parts();
assert_eq!(pair.gap, (Length::Points(4.0), Length::Points(8.0)));
}
#[test]
fn paint_properties_reach_the_paint_half() {
let (_, paint, ..) = Style::new()
.background_color(hex_rgb(0x10_10_14).into())
.border_radius(12.0)
.opacity(0.5)
.z_index(3)
.dither(true)
.into_parts();
assert_eq!(paint.background_color, hex_rgb(0x10_10_14).into());
assert_eq!(paint.border_radius.top_left.to_bits(), 12.0_f32.to_bits());
assert_eq!(
paint.border_radius.bottom_right.to_bits(),
12.0_f32.to_bits()
);
assert_eq!(paint.opacity.to_bits(), 0.5_f32.to_bits());
assert_eq!(paint.z_index, Some(3));
assert!(paint.dither);
}
#[test]
fn color_is_the_text_colour_and_background_is_the_fill() {
let (_, paint, text, _) = Style::new()
.color(hex_rgb(0xff_ff_ff).into())
.background_color(hex_rgb(0x00_00_00).into())
.into_parts();
assert_eq!(text.color, Some(hex_rgb(0xff_ff_ff).into()));
assert_eq!(paint.background_color, hex_rgb(0x00_00_00).into());
}
#[test]
fn text_properties_reach_the_text_half() {
let (.., text, _) = Style::new()
.font_family("Inter")
.font_size(24.0)
.bold()
.italic()
.line_height(LineHeight::Number(1.4))
.letter_spacing(px(-0.5))
.word_spacing(pct(10.0))
.into_parts();
assert_eq!(text.font_family.as_deref(), Some("Inter"));
assert_eq!(text.font_size, Some(24.0));
assert_eq!(text.font_weight, Some(FontWeight::BOLD));
assert_eq!(text.font_style, Some(FontStyle::Italic));
assert_eq!(text.line_height, Some(LineHeight::Number(1.4)));
assert_eq!(text.letter_spacing, Some(Spacing::Points(-0.5)));
assert_eq!(text.word_spacing, Some(Spacing::Em(0.1)));
}
#[test]
fn the_bounds_and_overflow_setters_reach_their_fields() {
let (layout, ..) = Style::new()
.min_height(px(4.0))
.max_width(px(8.0))
.overflow(Overflow::Hidden)
.into_parts();
assert_eq!(layout.min_size.1, Dimension::Points(4.0));
assert_eq!(layout.max_size.0, Dimension::Points(8.0));
assert_eq!(layout.overflow, (Overflow::Hidden, Overflow::Hidden));
}
#[test]
fn a_later_setter_replaces_an_earlier_one() {
let (layout, ..) =
Style::new().width(px(10.0)).width(px(20.0)).into_parts();
assert_eq!(layout.size.0, Dimension::Points(20.0));
}
#[test]
fn a_literal_reaches_a_property_with_no_setter() {
let style = Style {
aspect_ratio: Some(1.618),
..Style::new().gap(px(8.0))
};
let (layout, ..) = style.into_parts();
assert_eq!(layout.aspect_ratio, Some(1.618));
assert_eq!(layout.gap.0, Length::Points(8.0));
}
}