|
| 1 | +use taffy::prelude::*; |
| 2 | +use taffy::{ |
| 3 | + AlignContent, AlignItems, Dimension, GridPlacement, LengthPercentage, LengthPercentageAuto, |
| 4 | + TrackSizingFunction, |
| 5 | +}; |
| 6 | + |
| 7 | +use gosub_styling::css_values::CssValue; |
| 8 | +use gosub_styling::render_tree::{RenderNodeData, RenderTreeNode}; |
| 9 | + |
| 10 | +pub(crate) fn parse_len(node: &mut RenderTreeNode, name: &str) -> LengthPercentage { |
| 11 | + let Some(property) = node.get_property(name) else { |
| 12 | + return LengthPercentage::Length(0.0); |
| 13 | + }; |
| 14 | + |
| 15 | + property.compute_value(); |
| 16 | + |
| 17 | + match &property.actual { |
| 18 | + CssValue::Percentage(value) => LengthPercentage::Percent(*value), |
| 19 | + CssValue::Unit(..) => LengthPercentage::Length(property.actual.unit_to_px()), |
| 20 | + CssValue::String(_) => LengthPercentage::Length(property.actual.unit_to_px()), //HACK |
| 21 | + _ => LengthPercentage::Length(0.0), |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +pub(crate) fn parse_len_auto(node: &mut RenderTreeNode, name: &str) -> LengthPercentageAuto { |
| 26 | + let Some(property) = node.get_property(name) else { |
| 27 | + return LengthPercentageAuto::Auto; |
| 28 | + }; |
| 29 | + |
| 30 | + property.compute_value(); |
| 31 | + |
| 32 | + match &property.actual { |
| 33 | + CssValue::String(value) => match value.as_str() { |
| 34 | + "auto" => LengthPercentageAuto::Auto, |
| 35 | + _ => LengthPercentageAuto::Length(property.actual.unit_to_px()), //HACK |
| 36 | + }, |
| 37 | + CssValue::Percentage(value) => LengthPercentageAuto::Percent(*value), |
| 38 | + CssValue::Unit(..) => LengthPercentageAuto::Length(property.actual.unit_to_px()), |
| 39 | + _ => LengthPercentageAuto::Auto, |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +pub(crate) fn parse_dimension(node: &mut RenderTreeNode, name: &str) -> Dimension { |
| 44 | + let mut auto = Dimension::Auto; |
| 45 | + if let RenderNodeData::Text(text) = &node.data { |
| 46 | + if name == "width" { |
| 47 | + auto = Dimension::Length(text.width); |
| 48 | + } else if name == "height" { |
| 49 | + auto = Dimension::Length(text.height); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + let Some(property) = node.get_property(name) else { |
| 54 | + return auto; |
| 55 | + }; |
| 56 | + |
| 57 | + property.compute_value(); |
| 58 | + |
| 59 | + if name == "width" { |
| 60 | + println!("Width: {:?}", property.actual); |
| 61 | + } |
| 62 | + |
| 63 | + match &property.actual { |
| 64 | + CssValue::String(value) => match value.as_str() { |
| 65 | + "auto" => auto, |
| 66 | + s if s.ends_with('%') => { |
| 67 | + let value = s.trim_end_matches('%').parse::<f32>().unwrap_or(0.0); |
| 68 | + Dimension::Percent(value) |
| 69 | + } |
| 70 | + _ => Dimension::Length(property.actual.unit_to_px()), //HACK |
| 71 | + }, |
| 72 | + CssValue::Percentage(value) => Dimension::Percent(*value), |
| 73 | + CssValue::Unit(..) => Dimension::Length(property.actual.unit_to_px()), |
| 74 | + _ => auto, |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +pub(crate) fn parse_align_i(node: &mut RenderTreeNode, name: &str) -> Option<AlignItems> { |
| 79 | + let display = node.get_property(name)?; |
| 80 | + display.compute_value(); |
| 81 | + |
| 82 | + let CssValue::String(ref value) = display.actual else { |
| 83 | + return None; |
| 84 | + }; |
| 85 | + |
| 86 | + match value.as_str() { |
| 87 | + "start" => Some(AlignItems::Start), |
| 88 | + "end" => Some(AlignItems::End), |
| 89 | + "flex-start" => Some(AlignItems::FlexStart), |
| 90 | + "flex-end" => Some(AlignItems::FlexEnd), |
| 91 | + "center" => Some(AlignItems::Center), |
| 92 | + "baseline" => Some(AlignItems::Baseline), |
| 93 | + "stretch" => Some(AlignItems::Stretch), |
| 94 | + _ => None, |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +pub(crate) fn parse_align_c(node: &mut RenderTreeNode, name: &str) -> Option<AlignContent> { |
| 99 | + let display = node.get_property(name)?; |
| 100 | + |
| 101 | + display.compute_value(); |
| 102 | + |
| 103 | + let CssValue::String(ref value) = display.actual else { |
| 104 | + return None; |
| 105 | + }; |
| 106 | + |
| 107 | + match value.as_str() { |
| 108 | + "start" => Some(AlignContent::Start), |
| 109 | + "end" => Some(AlignContent::End), |
| 110 | + "flex-start" => Some(AlignContent::FlexStart), |
| 111 | + "flex-end" => Some(AlignContent::FlexEnd), |
| 112 | + "center" => Some(AlignContent::Center), |
| 113 | + "stretch" => Some(AlignContent::Stretch), |
| 114 | + "space-between" => Some(AlignContent::SpaceBetween), |
| 115 | + "space-around" => Some(AlignContent::SpaceAround), |
| 116 | + _ => None, |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +pub(crate) fn parse_tracking_sizing_function( |
| 121 | + node: &mut RenderTreeNode, |
| 122 | + name: &str, |
| 123 | +) -> Vec<TrackSizingFunction> { |
| 124 | + let Some(display) = node.get_property(name) else { |
| 125 | + return Vec::new(); |
| 126 | + }; |
| 127 | + |
| 128 | + display.compute_value(); |
| 129 | + |
| 130 | + let CssValue::String(ref _value) = display.actual else { |
| 131 | + return Vec::new(); |
| 132 | + }; |
| 133 | + |
| 134 | + Vec::new() //TODO: Implement this |
| 135 | +} |
| 136 | + |
| 137 | +#[allow(dead_code)] |
| 138 | +pub(crate) fn parse_non_repeated_tracking_sizing_function( |
| 139 | + _node: &mut RenderTreeNode, |
| 140 | + _name: &str, |
| 141 | +) -> NonRepeatedTrackSizingFunction { |
| 142 | + todo!("implement parse_non_repeated_tracking_sizing_function") |
| 143 | +} |
| 144 | + |
| 145 | +pub(crate) fn parse_grid_auto( |
| 146 | + node: &mut RenderTreeNode, |
| 147 | + name: &str, |
| 148 | +) -> Vec<NonRepeatedTrackSizingFunction> { |
| 149 | + let Some(display) = node.get_property(name) else { |
| 150 | + return Vec::new(); |
| 151 | + }; |
| 152 | + |
| 153 | + display.compute_value(); |
| 154 | + |
| 155 | + let CssValue::String(ref _value) = display.actual else { |
| 156 | + return Vec::new(); |
| 157 | + }; |
| 158 | + |
| 159 | + Vec::new() //TODO: Implement this |
| 160 | +} |
| 161 | + |
| 162 | +pub(crate) fn parse_grid_placement(node: &mut RenderTreeNode, name: &str) -> GridPlacement { |
| 163 | + let Some(display) = node.get_property(name) else { |
| 164 | + return GridPlacement::Auto; |
| 165 | + }; |
| 166 | + |
| 167 | + display.compute_value(); |
| 168 | + |
| 169 | + match &display.actual { |
| 170 | + CssValue::String(value) => { |
| 171 | + if value.starts_with("span") { |
| 172 | + let value = value.trim_start_matches("span").trim(); |
| 173 | + |
| 174 | + if let Ok(value) = value.parse::<u16>() { |
| 175 | + GridPlacement::from_span(value) |
| 176 | + } else { |
| 177 | + GridPlacement::Auto |
| 178 | + } |
| 179 | + } else if let Ok(value) = value.parse::<i16>() { |
| 180 | + GridPlacement::from_line_index(value) |
| 181 | + } else { |
| 182 | + GridPlacement::Auto |
| 183 | + } |
| 184 | + } |
| 185 | + CssValue::Number(value) => GridPlacement::from_line_index(*value as i16), |
| 186 | + _ => GridPlacement::Auto, |
| 187 | + } |
| 188 | +} |
0 commit comments