use crate::view::style::Style;
use crate::view::style_extra::{AUTO_BOTTOM, AUTO_LEFT, AUTO_RIGHT, AUTO_TOP, GridStyle};
use taffy::prelude::*;
pub(crate) fn apply(style: &Style, out: &mut taffy::Style) {
let Some(extra) = style.extra.as_deref() else { return };
if extra.reverse == Some(true) {
out.flex_direction = match out.flex_direction {
FlexDirection::Column | FlexDirection::ColumnReverse => FlexDirection::ColumnReverse,
_ => FlexDirection::RowReverse,
};
}
if let Some(content) = extra.align_content {
out.align_content = crate::view::layout::justify(content);
}
grid(&extra.grid, out);
let auto = |bit: u8| extra.margin_auto & bit != 0;
for (bit, side) in [
(AUTO_TOP, &mut out.margin.top),
(AUTO_RIGHT, &mut out.margin.right),
(AUTO_BOTTOM, &mut out.margin.bottom),
(AUTO_LEFT, &mut out.margin.left),
] {
if auto(bit) {
*side = LengthPercentageAuto::auto();
}
}
}
fn grid(grid: &GridStyle, out: &mut taffy::Style) {
if grid.is_grid() && out.display != Display::None {
out.display = Display::Grid;
if let Some(cols) = grid.cols {
out.grid_template_columns = vec![repeat(cols, vec![fr(1.0)])];
}
if let Some(rows) = grid.rows {
out.grid_template_rows = vec![repeat(rows, vec![fr(1.0)])];
}
}
if grid.flow_col == Some(true) {
out.grid_auto_flow = GridAutoFlow::Column;
}
out.grid_column = placement(grid.col_start, grid.col_span);
out.grid_row = placement(grid.row_start, grid.row_span);
}
fn placement(start: Option<i16>, span_by: Option<u16>) -> Line<GridPlacement> {
Line { start: start.map_or(GridPlacement::Auto, line), end: span_by.map_or(GridPlacement::Auto, span) }
}