//! Map Dioxus layout attributes to Ratatui Layout/Constraint. use ratatui::layout::{Constraint, Direction, Layout, Rect}; use std::collections::HashMap; pub fn direction_from_attrs(attrs: &HashMap) -> Direction { match attrs.get("flex_direction").or(attrs.get("direction")).map(|s| s.as_str()) { Some("row") | Some("horizontal") => Direction::Horizontal, _ => Direction::Vertical, } } pub fn constraint_from_attrs(attrs: &HashMap, direction: Direction) -> Constraint { let size_attr = match direction { Direction::Vertical => attrs.get("height"), Direction::Horizontal => attrs.get("width"), }; if let Some(size) = size_attr { return parse_constraint(size); } if let Some(flex) = attrs.get("flex") { if let Ok(r) = flex.parse::() { return Constraint::Fill(r); } } Constraint::Fill(1) } fn parse_constraint(s: &str) -> Constraint { let s = s.trim(); if s.ends_with('%') { if let Ok(p) = s.trim_end_matches('%').parse::() { return Constraint::Percentage(p); } } let numeric = s.trim_end_matches(|c: char| c.is_alphabetic()); if let Ok(n) = numeric.parse::() { return Constraint::Length(n); } Constraint::Fill(1) } pub fn split_area(area: Rect, direction: Direction, constraints: &[Constraint]) -> Vec { if constraints.is_empty() { return vec![area]; } Layout::default().direction(direction).constraints(constraints).split(area).to_vec() }