From c9b37954c6bd81382a0f375301972326568710be Mon Sep 17 00:00:00 2001 From: RobertasJ Date: Sat, 30 Nov 2024 12:16:42 +0100 Subject: [PATCH 1/4] functions and other values --- crates/state/src/values/size.rs | 136 ++++++++++++++ crates/state/tests/parse_size.rs | 73 +++++++ crates/torin/src/measure.rs | 12 ++ crates/torin/src/values/size.rs | 162 +++++++++++++++- crates/torin/tests/size.rs | 313 +++++++++++++++++++++++++++++-- 5 files changed, 670 insertions(+), 26 deletions(-) create mode 100644 crates/state/src/values/size.rs create mode 100644 crates/state/tests/parse_size.rs diff --git a/crates/state/src/values/size.rs b/crates/state/src/values/size.rs new file mode 100644 index 000000000..217b860e8 --- /dev/null +++ b/crates/state/src/values/size.rs @@ -0,0 +1,136 @@ +use nom::{ + branch::alt, + bytes::complete::tag, + character::complete::multispace0, + combinator::{ + map, + opt, + }, + multi::many1, + number::complete::float, + sequence::{ + preceded, + tuple, + }, + IResult, +}; +use torin::{ + geometry::Length, + size::{ + Dimension, + DynamicCalculation, + LexFunction, + Size, + }, +}; + +use crate::{ + Parse, + ParseError, +}; + +impl Parse for Size { + fn parse(value: &str) -> Result { + if value == "auto" { + Ok(Size::Inner) + } else if value == "flex" { + Ok(Size::Flex(Length::new(1.0))) + } else if value.contains("flex") { + Ok(Size::Flex(Length::new( + value + .replace("flex(", "") + .replace(')', "") + .parse::() + .map_err(|_| ParseError)?, + ))) + } else if value == "fill" { + Ok(Size::Fill) + } else if value == "fill-min" { + Ok(Size::FillMinimum) + } else if value.contains("calc") { + Ok(Size::DynamicCalculations(Box::new(parse_calc(value)?))) + } else if value.contains('%') { + Ok(Size::Percentage(Length::new( + value + .replace('%', "") + .parse::() + .map_err(|_| ParseError)?, + ))) + } else if value.contains('v') { + Ok(Size::RootPercentage(Length::new( + value + .replace('v', "") + .parse::() + .map_err(|_| ParseError)?, + ))) + } else if value.contains('a') { + Ok(Size::InnerPercentage(Length::new( + value + .replace('a', "") + .parse::() + .map_err(|_| ParseError)?, + ))) + } else { + Ok(Size::Pixels(Length::new( + value.parse::().map_err(|_| ParseError)?, + ))) + } + } +} + +pub fn parse_calc(mut value: &str) -> Result, ParseError> { + // No need to parse this using nom + + value = value + .strip_prefix("calc(") + .ok_or(ParseError)? + .strip_suffix(')') + .ok_or(ParseError)?; + fn inner_parse(value: &str) -> IResult<&str, Vec> { + many1(preceded( + multispace0, + alt(( + map(tag("+"), |_| DynamicCalculation::Add), + map(tag("-"), |_| DynamicCalculation::Sub), + map(tag("*"), |_| DynamicCalculation::Mul), + map(tag("/"), |_| DynamicCalculation::Div), + map(tag("("), |_| DynamicCalculation::OpenParenthesis), + map(tag(")"), |_| DynamicCalculation::ClosedParenthesis), + map(tag(","), |_| DynamicCalculation::FunctionSeparator), + map( + tuple((float, tag("%"), opt(tag("'")))), + |(v, _, inv): (f32, _, Option<&str>)| { + if inv.is_some() { + DynamicCalculation::Percentage(Dimension::Other(v)) + } else { + DynamicCalculation::Percentage(Dimension::Current(v)) + } + }, + ), + map( + tuple((float, tag("v"), opt(tag("'")))), + |(v, _, inv): (f32, _, Option<&str>)| { + if inv.is_some() { + DynamicCalculation::RootPercentage(Dimension::Other(v)) + } else { + DynamicCalculation::RootPercentage(Dimension::Current(v)) + } + }, + ), + map(float, DynamicCalculation::Pixels), + map(tag("min"), |_| { + DynamicCalculation::Function(LexFunction::Min) + }), + map(tag("max"), |_| { + DynamicCalculation::Function(LexFunction::Max) + }), + map(tag("clamp"), |_| { + DynamicCalculation::Function(LexFunction::Clamp) + }), + )), + ))(value) + } + let tokens = inner_parse(value).map_err(|_| ParseError)?.1; + + Ok(tokens) +} diff --git a/crates/state/tests/parse_size.rs b/crates/state/tests/parse_size.rs new file mode 100644 index 000000000..adc4c8c0c --- /dev/null +++ b/crates/state/tests/parse_size.rs @@ -0,0 +1,73 @@ +use freya_node_state::Parse; +use torin::{ + geometry::Length, + size::{ + Dimension, + DynamicCalculation, + LexFunction, + Size, + }, +}; + +#[test] +fn parse_pixel_size() { + let size = Size::parse("123"); + assert_eq!(size, Ok(Size::Pixels(Length::new(123.0)))); +} + +#[test] +fn parse_relative_size() { + let size = Size::parse("78.123%"); + assert_eq!(size, Ok(Size::Percentage(Length::new(78.123)))); +} + +#[test] +fn parse_auto_size() { + let size = Size::parse("auto"); + assert_eq!(size, Ok(Size::Inner)); +} + +#[test] +fn parse_calc_size() { + let size = Size::parse("calc(90%- 5%* 123.6/ 50v(5 + 6) - min(50v, 50v', 100%, 100%') max(50v) clamp(50v, 5.0, 100%))"); + assert_eq!( + size, + Ok(Size::DynamicCalculations(Box::new(vec![ + DynamicCalculation::Percentage(Dimension::Current(90.0)), + DynamicCalculation::Sub, + DynamicCalculation::Percentage(Dimension::Current(5.0)), + DynamicCalculation::Mul, + DynamicCalculation::Pixels(123.6), + DynamicCalculation::Div, + DynamicCalculation::RootPercentage(Dimension::Current(50.0)), + DynamicCalculation::OpenParenthesis, + DynamicCalculation::Pixels(5.0), + DynamicCalculation::Add, + DynamicCalculation::Pixels(6.0), + DynamicCalculation::ClosedParenthesis, + DynamicCalculation::Sub, + DynamicCalculation::Function(LexFunction::Min), + DynamicCalculation::OpenParenthesis, + DynamicCalculation::RootPercentage(Dimension::Current(50.0)), + DynamicCalculation::FunctionSeparator, + DynamicCalculation::RootPercentage(Dimension::Other(50.0)), + DynamicCalculation::FunctionSeparator, + DynamicCalculation::Percentage(Dimension::Current(100.0)), + DynamicCalculation::FunctionSeparator, + DynamicCalculation::Percentage(Dimension::Other(100.0)), + DynamicCalculation::ClosedParenthesis, + DynamicCalculation::Function(LexFunction::Max), + DynamicCalculation::OpenParenthesis, + DynamicCalculation::RootPercentage(Dimension::Current(50.0)), + DynamicCalculation::ClosedParenthesis, + DynamicCalculation::Function(LexFunction::Clamp), + DynamicCalculation::OpenParenthesis, + DynamicCalculation::RootPercentage(Dimension::Current(50.0)), + DynamicCalculation::FunctionSeparator, + DynamicCalculation::Pixels(5.0), + DynamicCalculation::FunctionSeparator, + DynamicCalculation::Percentage(Dimension::Current(100.0)), + DynamicCalculation::ClosedParenthesis, + ]))) + ); +} diff --git a/crates/torin/src/measure.rs b/crates/torin/src/measure.rs index a1db6ddf0..7d4bad775 100644 --- a/crates/torin/src/measure.rs +++ b/crates/torin/src/measure.rs @@ -84,23 +84,27 @@ where area_size.width = node.width.min_max( area_size.width, parent_area.size.width, + parent_area.size.height, available_parent_area.size.width, node.margin.left(), node.margin.horizontal(), &node.minimum_width, &node.maximum_width, self.layout_metadata.root_area.width(), + self.layout_metadata.root_area.height(), phase, ); area_size.height = node.height.min_max( area_size.height, parent_area.size.height, + parent_area.size.width, available_parent_area.size.height, node.margin.top(), node.margin.vertical(), &node.minimum_height, &node.maximum_height, self.layout_metadata.root_area.height(), + self.layout_metadata.root_area.width(), phase, ); @@ -124,12 +128,14 @@ where area_size.width = node.width.min_max( custom_size.width, parent_area.size.width, + parent_area.size.height, available_parent_area.size.width, node.margin.left(), node.margin.horizontal(), &node.minimum_width, &node.maximum_width, self.layout_metadata.root_area.width(), + self.layout_metadata.root_area.height(), phase, ); } @@ -137,12 +143,14 @@ where area_size.height = node.height.min_max( custom_size.height, parent_area.size.height, + parent_area.size.width, available_parent_area.size.height, node.margin.top(), node.margin.vertical(), &node.minimum_height, &node.maximum_height, self.layout_metadata.root_area.height(), + self.layout_metadata.root_area.width(), phase, ); } @@ -173,12 +181,14 @@ where inner_size.width = node.width.min_max( available_parent_area.width(), parent_area.size.width, + parent_area.size.height, available_parent_area.width(), node.margin.left(), node.margin.horizontal(), &node.minimum_width, &node.maximum_width, self.layout_metadata.root_area.width(), + self.layout_metadata.root_area.height(), phase, ); } @@ -186,12 +196,14 @@ where inner_size.height = node.height.min_max( available_parent_area.height(), parent_area.size.height, + parent_area.size.width, available_parent_area.height(), node.margin.top(), node.margin.vertical(), &node.minimum_height, &node.maximum_height, self.layout_metadata.root_area.height(), + self.layout_metadata.root_area.width(), phase, ); } diff --git a/crates/torin/src/values/size.rs b/crates/torin/src/values/size.rs index cae8c19cd..4a7aded0b 100644 --- a/crates/torin/src/values/size.rs +++ b/crates/torin/src/values/size.rs @@ -62,12 +62,16 @@ impl Size { } } + // i really do not want to make a new type just so this lint goes away + #[allow(clippy::too_many_arguments)] pub fn eval( &self, parent_value: f32, + other_parent_value: f32, available_parent_value: f32, parent_margin: f32, root_value: f32, + other_root_value: f32, phase: Phase, ) -> Option { match self { @@ -81,6 +85,22 @@ impl Size { Self::Flex(_) | Self::FillMinimum if phase == Phase::Final => { Some(available_parent_value) } + Size::Pixels(px) => Some(px.get() + parent_margin), + Size::Percentage(per) => Some(parent_value / 100.0 * per.get()), + Size::DynamicCalculations(calculations) => Some( + run_calculations( + calculations.deref(), + parent_value, + other_parent_value, + root_value, + other_root_value, + ) + .unwrap_or(0.0), + ), + Size::Fill => Some(available_parent_value), + Size::FillMinimum if phase == Phase::Final => Some(available_parent_value), + Size::RootPercentage(per) => Some(root_value / 100.0 * per.get()), + Size::Flex(_) if phase == Phase::Final => Some(available_parent_value), _ => None, } } @@ -90,20 +110,24 @@ impl Size { &self, value: f32, parent_value: f32, + other_parent_value: f32, available_parent_value: f32, single_margin: f32, margin: f32, minimum: &Self, maximum: &Self, root_value: f32, + other_root_value: f32, phase: Phase, ) -> f32 { let value = self .eval( parent_value, + other_parent_value, available_parent_value, margin, root_value, + other_root_value, phase, ) .unwrap_or(value + margin); @@ -111,17 +135,21 @@ impl Size { let minimum_value = minimum .eval( parent_value, + other_parent_value, available_parent_value, margin, root_value, + other_root_value, phase, ) .map(|v| v + single_margin); let maximum_value = maximum.eval( parent_value, + other_parent_value, available_parent_value, margin, root_value, + other_root_value, phase, ); @@ -170,9 +198,41 @@ pub enum DynamicCalculation { Add, OpenParenthesis, ClosedParenthesis, - Percentage(f32), - RootPercentage(f32), + FunctionSeparator, + Percentage(Dimension), + RootPercentage(Dimension), + // no dimension because this isnt using any parent value Pixels(f32), + Function(LexFunction), +} + +// a token of a function name, not the whole function, this is a lex token not a parse tree. +#[derive(Copy, Clone, Debug, PartialEq)] +pub enum LexFunction { + Min, + Max, + Clamp, +} + +/// dimension as in height/width, allows for the use of the parent height and parent width inside +/// of one side, for example: +/// ```rust +/// rsx! { +/// rect { +/// width: "200", +/// height: "400", +/// rect { +/// width: "calc(min(100%, 100%'))", // the ' signifies the other side which would be +/// // the height +/// height: "calc(min(100%, 100%'))" +/// } +/// } +/// } +/// ``` +#[derive(Copy, Clone, Debug, PartialEq)] +pub enum Dimension { + Current(f32), + Other(f32), } impl Scaled for DynamicCalculation { @@ -195,6 +255,29 @@ impl std::fmt::Display for DynamicCalculation { Self::Percentage(p) => f.write_fmt(format_args!("{p}%")), Self::RootPercentage(p) => f.write_fmt(format_args!("{p}v")), Self::Pixels(s) => f.write_fmt(format_args!("{s}")), + DynamicCalculation::Sub => f.write_str("-"), + DynamicCalculation::Mul => f.write_str("*"), + DynamicCalculation::Div => f.write_str("/"), + DynamicCalculation::Add => f.write_str("+"), + DynamicCalculation::OpenParenthesis => f.write_str("("), + DynamicCalculation::ClosedParenthesis => f.write_str(")"), + DynamicCalculation::FunctionSeparator => f.write_str(","), + DynamicCalculation::Percentage(Dimension::Current(p)) => { + f.write_fmt(format_args!("{p}%")) + } + DynamicCalculation::Percentage(Dimension::Other(p)) => { + f.write_fmt(format_args!("{p}%'")) + } + DynamicCalculation::RootPercentage(Dimension::Current(p)) => { + f.write_fmt(format_args!("{p}v")) + } + DynamicCalculation::RootPercentage(Dimension::Other(p)) => { + f.write_fmt(format_args!("{p}v")) + } + DynamicCalculation::Pixels(s) => f.write_fmt(format_args!("{s}")), + DynamicCalculation::Function(LexFunction::Min) => f.write_str("min"), + DynamicCalculation::Function(LexFunction::Max) => f.write_str("max"), + DynamicCalculation::Function(LexFunction::Clamp) => f.write_str("clamp"), } } } @@ -203,7 +286,9 @@ impl std::fmt::Display for DynamicCalculation { struct DynamicCalculationEvaluator<'a> { calcs: Iter<'a, DynamicCalculation>, parent_value: f32, + other_parent_value: f32, root_value: f32, + other_root_value: f32, current: Option<&'a DynamicCalculation>, } @@ -212,11 +297,20 @@ impl<'a> DynamicCalculationEvaluator<'a> { calcs: Iter<'a, DynamicCalculation>, parent_value: f32, root_value: f32, + ) -> Self { + pub fn new( + calcs: Iter<'a, DynamicCalculation>, + parent_value: f32, + other_parent_value: f32, + root_value: f32, + other_root_value: f32, ) -> Self { Self { calcs, parent_value, + other_parent_value, root_value, + other_root_value, current: None, } } @@ -303,14 +397,22 @@ impl<'a> DynamicCalculationEvaluator<'a> { /// ` fn parse_value(&mut self) -> Option<(f32, bool)> { match self.current? { - DynamicCalculation::Percentage(value) => { + DynamicCalculation::Percentage(Dimension::Current(value)) => { self.current = self.calcs.next(); Some(((self.parent_value / 100.0 * value).round(), false)) } - DynamicCalculation::RootPercentage(value) => { + DynamicCalculation::Percentage(Dimension::Other(value)) => { + self.current = self.calcs.next(); + Some(((self.other_parent_value / 100.0 * value).round(), false)) + } + DynamicCalculation::RootPercentage(Dimension::Current(value)) => { self.current = self.calcs.next(); Some(((self.root_value / 100.0 * value).round(), false)) } + DynamicCalculation::RootPercentage(Dimension::Other(value)) => { + self.current = self.calcs.next(); + Some(((self.other_root_value / 100.0 * value).round(), false)) + } DynamicCalculation::Pixels(value) => { self.current = self.calcs.next(); Some((*value, false)) @@ -318,17 +420,53 @@ impl<'a> DynamicCalculationEvaluator<'a> { DynamicCalculation::OpenParenthesis => { // function should return on DynamicCalculation::ClosedParenthesis because it does // not have a precedence, thats how it actually works - let val = self.parse_expression(0); + let val = self.parse_expression(0)?; if self.current != Some(&DynamicCalculation::ClosedParenthesis) { return None; } self.current = self.calcs.next(); - Some((val?, true)) + Some((val, true)) + } + DynamicCalculation::Function(func) => { + // oh god here we gg + self.current = self.calcs.next(); + let vals = self.parse_function_inputs()?; + let res = match func { + LexFunction::Min => vals.into_iter().reduce(f32::min), + LexFunction::Max => vals.into_iter().reduce(f32::max), + LexFunction::Clamp => { + if vals.len() != 3 { + None + } else { + Some(vals[0].max(vals[1].min(vals[2]))) + } + } + }?; + + Some((res, true)) } _ => None, } } + fn parse_function_inputs(&mut self) -> Option> { + let mut res = vec![]; + loop { + let val = self.parse_expression(0)?; + match self.current? { + DynamicCalculation::FunctionSeparator => {} + DynamicCalculation::ClosedParenthesis => { + res.push(val); + break; + } + _ => return None, + } + res.push(val); + } + self.current = self.calcs.next(); + Some(res) + } + /// parses out the prefix, like a + or - fn parse_prefix(&mut self) -> Option> { match self.current? { @@ -349,7 +487,6 @@ impl<'a> DynamicCalculationEvaluator<'a> { match self.current? { DynamicCalculation::Add | DynamicCalculation::Sub => Some(1), DynamicCalculation::Mul | DynamicCalculation::Div => Some(2), - DynamicCalculation::OpenParenthesis => Some(0), _ => None, } } @@ -360,7 +497,16 @@ impl<'a> DynamicCalculationEvaluator<'a> { pub fn run_calculations( calcs: &[DynamicCalculation], parent_value: f32, + other_parent_value: f32, root_value: f32, + other_root_value: f32, ) -> Option { - DynamicCalculationEvaluator::new(calcs.iter(), parent_value, root_value).evaluate() + DynamicCalculationEvaluator::new( + calcs.iter(), + parent_value, + other_parent_value, + root_value, + other_root_value, + ) + .evaluate() } diff --git a/crates/torin/tests/size.rs b/crates/torin/tests/size.rs index 982887e46..f52371707 100644 --- a/crates/torin/tests/size.rs +++ b/crates/torin/tests/size.rs @@ -782,25 +782,63 @@ pub fn inner_percentage() { #[test] pub fn test_calc() { const PARENT_VALUE: f32 = 500.0; + const OTHER_PARENT_VALUE: f32 = 100.0; assert_eq!( run_calculations( &[DynamicCalculation::Pixels(10.0)], PARENT_VALUE, - PARENT_VALUE + OTHER_PARENT_VALUE, + PARENT_VALUE, + OTHER_PARENT_VALUE, ), Some(10.0) ); assert_eq!( run_calculations( - &[DynamicCalculation::Percentage(87.5)], + &[DynamicCalculation::Percentage(Dimension::Current(87.5))], + PARENT_VALUE, + OTHER_PARENT_VALUE, PARENT_VALUE, - PARENT_VALUE + OTHER_PARENT_VALUE, ), Some((87.5 / 100.0 * PARENT_VALUE).round()) ); + assert_eq!( + run_calculations( + &[DynamicCalculation::Percentage(Dimension::Other(87.5))], + PARENT_VALUE, + OTHER_PARENT_VALUE, + PARENT_VALUE, + OTHER_PARENT_VALUE, + ), + Some((87.5 / 100.0 * OTHER_PARENT_VALUE).round()) + ); + + assert_eq!( + run_calculations( + &[DynamicCalculation::RootPercentage(Dimension::Current(87.5))], + PARENT_VALUE, + OTHER_PARENT_VALUE, + PARENT_VALUE, + OTHER_PARENT_VALUE, + ), + Some((87.5 / 100.0 * PARENT_VALUE).round()) + ); + + assert_eq!( + run_calculations( + &[DynamicCalculation::RootPercentage(Dimension::Other(87.5))], + PARENT_VALUE, + OTHER_PARENT_VALUE, + PARENT_VALUE, + OTHER_PARENT_VALUE, + ), + Some((87.5 / 100.0 * OTHER_PARENT_VALUE).round()) + ); + assert_eq!( run_calculations( &[ @@ -808,10 +846,12 @@ pub fn test_calc() { DynamicCalculation::Add, DynamicCalculation::Pixels(20.0), DynamicCalculation::Mul, - DynamicCalculation::Percentage(50.0) + DynamicCalculation::Percentage(Dimension::Current(50.0)) ], PARENT_VALUE, - PARENT_VALUE + OTHER_PARENT_VALUE, + PARENT_VALUE, + OTHER_PARENT_VALUE, ), Some(10.0 + 20.0 * (50.0 / 100.0 * PARENT_VALUE).round()) ); @@ -821,7 +861,7 @@ pub fn test_calc() { &[ DynamicCalculation::Pixels(10.0), DynamicCalculation::Add, - DynamicCalculation::Percentage(10.0), + DynamicCalculation::Percentage(Dimension::Current(10.0)), DynamicCalculation::Add, DynamicCalculation::Pixels(30.0), DynamicCalculation::Mul, @@ -832,7 +872,9 @@ pub fn test_calc() { DynamicCalculation::Pixels(2.0) ], PARENT_VALUE, - PARENT_VALUE + OTHER_PARENT_VALUE, + PARENT_VALUE, + OTHER_PARENT_VALUE, ), Some(10.0 + (10.0 / 100.0 * PARENT_VALUE).round() + 30.0 * 10.0 + 75.0 * 2.0) ); @@ -844,7 +886,9 @@ pub fn test_calc() { DynamicCalculation::Pixels(20.0) ], PARENT_VALUE, - PARENT_VALUE + OTHER_PARENT_VALUE, + PARENT_VALUE, + OTHER_PARENT_VALUE, ), None ); @@ -853,7 +897,9 @@ pub fn test_calc() { run_calculations( &[DynamicCalculation::Pixels(10.0), DynamicCalculation::Add], PARENT_VALUE, - PARENT_VALUE + OTHER_PARENT_VALUE, + PARENT_VALUE, + OTHER_PARENT_VALUE, ), None ); @@ -862,7 +908,9 @@ pub fn test_calc() { run_calculations( &[DynamicCalculation::Add, DynamicCalculation::Pixels(10.0)], PARENT_VALUE, - PARENT_VALUE + OTHER_PARENT_VALUE, + PARENT_VALUE, + OTHER_PARENT_VALUE, ), // Because +10 is just 10 Some(10.0) @@ -878,7 +926,9 @@ pub fn test_calc() { DynamicCalculation::Pixels(10.0) ], PARENT_VALUE, - PARENT_VALUE + OTHER_PARENT_VALUE, + PARENT_VALUE, + OTHER_PARENT_VALUE, ), Some(20.0) ); @@ -886,12 +936,14 @@ pub fn test_calc() { assert_eq!( run_calculations( &[ - DynamicCalculation::Percentage(50.0), + DynamicCalculation::Percentage(Dimension::Current(50.0)), DynamicCalculation::Sub, - DynamicCalculation::RootPercentage(20.0) + DynamicCalculation::RootPercentage(Dimension::Current(20.0)) ], PARENT_VALUE, - PARENT_VALUE + OTHER_PARENT_VALUE, + PARENT_VALUE, + OTHER_PARENT_VALUE, ), Some((PARENT_VALUE * 0.5) - (PARENT_VALUE * 0.20)) ); @@ -904,7 +956,9 @@ pub fn test_calc() { DynamicCalculation::ClosedParenthesis ], PARENT_VALUE, - PARENT_VALUE + OTHER_PARENT_VALUE, + PARENT_VALUE, + OTHER_PARENT_VALUE, ), Some(10.0) ); @@ -927,7 +981,9 @@ pub fn test_calc() { DynamicCalculation::Pixels(10.0) ], PARENT_VALUE, - PARENT_VALUE + OTHER_PARENT_VALUE, + PARENT_VALUE, + OTHER_PARENT_VALUE, ), Some((10.0 * (10.0 + 20.0) * 10.0) + (10.0 * (10.0) * 10.0)) ); @@ -942,7 +998,9 @@ pub fn test_calc() { DynamicCalculation::Pixels(20.0) ], PARENT_VALUE, - PARENT_VALUE + OTHER_PARENT_VALUE, + PARENT_VALUE, + OTHER_PARENT_VALUE, ), Some(-1.0 * 10.0 * 20.0) ); @@ -954,8 +1012,227 @@ pub fn test_calc() { DynamicCalculation::Pixels(10.0) ], PARENT_VALUE, - PARENT_VALUE + OTHER_PARENT_VALUE, + PARENT_VALUE, + OTHER_PARENT_VALUE, + ), + None + ); + + assert_eq!( + run_calculations( + &[ + DynamicCalculation::Function(LexFunction::Min), + DynamicCalculation::OpenParenthesis, + DynamicCalculation::Pixels(10.0), + DynamicCalculation::FunctionSeparator, + DynamicCalculation::Pixels(20.0), + DynamicCalculation::ClosedParenthesis, + ], + PARENT_VALUE, + OTHER_PARENT_VALUE, + PARENT_VALUE, + OTHER_PARENT_VALUE, + ), + Some(10.0) + ); + + assert_eq!( + run_calculations( + &[ + DynamicCalculation::Function(LexFunction::Max), + DynamicCalculation::OpenParenthesis, + DynamicCalculation::Pixels(10.0), + DynamicCalculation::FunctionSeparator, + DynamicCalculation::Pixels(30.0), + DynamicCalculation::FunctionSeparator, + DynamicCalculation::Pixels(20.0), + DynamicCalculation::ClosedParenthesis, + ], + PARENT_VALUE, + OTHER_PARENT_VALUE, + PARENT_VALUE, + OTHER_PARENT_VALUE, + ), + Some(30.0) + ); + + assert_eq!( + run_calculations( + &[ + DynamicCalculation::Function(LexFunction::Max), + DynamicCalculation::OpenParenthesis, + DynamicCalculation::Pixels(10.0), + DynamicCalculation::FunctionSeparator, + DynamicCalculation::ClosedParenthesis, + ], + PARENT_VALUE, + OTHER_PARENT_VALUE, + PARENT_VALUE, + OTHER_PARENT_VALUE, + ), + None + ); + + assert_eq!( + run_calculations( + &[ + DynamicCalculation::Function(LexFunction::Max), + DynamicCalculation::OpenParenthesis, + DynamicCalculation::Pixels(10.0), + ], + PARENT_VALUE, + OTHER_PARENT_VALUE, + PARENT_VALUE, + OTHER_PARENT_VALUE, + ), + None + ); + + assert_eq!( + run_calculations( + &[ + DynamicCalculation::Function(LexFunction::Clamp), + DynamicCalculation::OpenParenthesis, + DynamicCalculation::Pixels(10.0), + DynamicCalculation::FunctionSeparator, + DynamicCalculation::Pixels(30.0), + DynamicCalculation::FunctionSeparator, + DynamicCalculation::Pixels(20.0), + DynamicCalculation::ClosedParenthesis, + ], + PARENT_VALUE, + OTHER_PARENT_VALUE, + PARENT_VALUE, + OTHER_PARENT_VALUE, + ), + Some(20.0) + ); + + assert_eq!( + run_calculations( + &[ + DynamicCalculation::Function(LexFunction::Clamp), + DynamicCalculation::OpenParenthesis, + DynamicCalculation::Pixels(10.0), + DynamicCalculation::FunctionSeparator, + DynamicCalculation::Pixels(5.0), + DynamicCalculation::FunctionSeparator, + DynamicCalculation::Pixels(20.0), + DynamicCalculation::ClosedParenthesis, + ], + PARENT_VALUE, + OTHER_PARENT_VALUE, + PARENT_VALUE, + OTHER_PARENT_VALUE, + ), + Some(10.0) + ); + + assert_eq!( + run_calculations( + &[ + DynamicCalculation::Function(LexFunction::Clamp), + DynamicCalculation::OpenParenthesis, + DynamicCalculation::Pixels(10.0), + DynamicCalculation::FunctionSeparator, + DynamicCalculation::Pixels(30.0), + DynamicCalculation::ClosedParenthesis, + ], + PARENT_VALUE, + OTHER_PARENT_VALUE, + PARENT_VALUE, + OTHER_PARENT_VALUE, ), None ); } + +#[test] +pub fn test_scaling_factor() { + const PARENT_VALUE: f32 = 500.0; + const OTHER_PARENT_VALUE: f32 = 500.0; + + assert_eq!( + { + let mut size = + Size::DynamicCalculations(Box::new(vec![DynamicCalculation::Pixels(10.0)])); + size.scale(1.5); + size + } + .eval( + PARENT_VALUE, + OTHER_PARENT_VALUE, + PARENT_VALUE, + 0.0, + PARENT_VALUE, + OTHER_PARENT_VALUE, + Phase::Initial + ), + Some((10.0) * 1.5) + ); + + assert_eq!( + { + let mut size = Size::DynamicCalculations(Box::new(vec![ + DynamicCalculation::Pixels(10.0), + DynamicCalculation::OpenParenthesis, + DynamicCalculation::Pixels(10.0), + DynamicCalculation::Add, + DynamicCalculation::Pixels(20.0), + DynamicCalculation::ClosedParenthesis, + DynamicCalculation::Pixels(10.0), + DynamicCalculation::Add, + DynamicCalculation::Pixels(10.0), + DynamicCalculation::OpenParenthesis, + DynamicCalculation::Pixels(10.0), + DynamicCalculation::ClosedParenthesis, + DynamicCalculation::Pixels(10.0), + ])); + size.scale(1.5); + size + } + .eval( + PARENT_VALUE, + OTHER_PARENT_VALUE, + PARENT_VALUE, + 0.0, + PARENT_VALUE, + OTHER_PARENT_VALUE, + Phase::Initial + ), + Some(((10.0 * (10.0 + 20.0) * 10.0) + (10.0 * (10.0) * 10.0)) * 1.5) + ); + + assert_eq!( + { + let mut size = Size::DynamicCalculations(Box::new(vec![ + DynamicCalculation::Pixels(10.0), + DynamicCalculation::OpenParenthesis, + DynamicCalculation::Pixels(10.0), + DynamicCalculation::Add, + DynamicCalculation::Pixels(20.0), + DynamicCalculation::ClosedParenthesis, + DynamicCalculation::Pixels(10.0), + DynamicCalculation::Add, + DynamicCalculation::Pixels(10.0), + DynamicCalculation::OpenParenthesis, + DynamicCalculation::Pixels(10.0), + DynamicCalculation::ClosedParenthesis, + DynamicCalculation::Pixels(10.0), + ])); + size.scale(1.2); + size + } + .eval( + PARENT_VALUE, + OTHER_PARENT_VALUE, + PARENT_VALUE, + 0.0, + PARENT_VALUE, + OTHER_PARENT_VALUE, + Phase::Initial + ), + Some(((10.0 * (10.0 + 20.0) * 10.0) + (10.0 * (10.0) * 10.0)) * 1.2) + ); +} From a1e12513f48d3700b7499502e85d26f2af40e607 Mon Sep 17 00:00:00 2001 From: RobertasJ Date: Sat, 30 Nov 2024 14:16:43 +0100 Subject: [PATCH 2/4] move these up because they should be highest up --- crates/state/src/values/size.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/crates/state/src/values/size.rs b/crates/state/src/values/size.rs index 217b860e8..1d3a175e2 100644 --- a/crates/state/src/values/size.rs +++ b/crates/state/src/values/size.rs @@ -90,6 +90,15 @@ pub fn parse_calc(mut value: &str) -> Result, ParseError many1(preceded( multispace0, alt(( + map(tag("min"), |_| { + DynamicCalculation::Function(LexFunction::Min) + }), + map(tag("max"), |_| { + DynamicCalculation::Function(LexFunction::Max) + }), + map(tag("clamp"), |_| { + DynamicCalculation::Function(LexFunction::Clamp) + }), map(tag("+"), |_| DynamicCalculation::Add), map(tag("-"), |_| DynamicCalculation::Sub), map(tag("*"), |_| DynamicCalculation::Mul), @@ -118,15 +127,6 @@ pub fn parse_calc(mut value: &str) -> Result, ParseError }, ), map(float, DynamicCalculation::Pixels), - map(tag("min"), |_| { - DynamicCalculation::Function(LexFunction::Min) - }), - map(tag("max"), |_| { - DynamicCalculation::Function(LexFunction::Max) - }), - map(tag("clamp"), |_| { - DynamicCalculation::Function(LexFunction::Clamp) - }), )), ))(value) } From 07a5eb868c47236df286c06f327497740689adc8 Mon Sep 17 00:00:00 2001 From: RobertasJ Date: Fri, 20 Dec 2024 00:45:40 +0100 Subject: [PATCH 3/4] new stuff --- crates/state/src/values/size.rs | 55 ++-- crates/state/tests/parse_size.rs | 59 +++-- crates/torin/src/lib.rs | 3 +- crates/torin/src/measure.rs | 51 ++-- crates/torin/src/values/size.rs | 174 ++++++++----- crates/torin/tests/size.rs | 433 ++----------------------------- 6 files changed, 209 insertions(+), 566 deletions(-) diff --git a/crates/state/src/values/size.rs b/crates/state/src/values/size.rs index 1d3a175e2..51948aa67 100644 --- a/crates/state/src/values/size.rs +++ b/crates/state/src/values/size.rs @@ -2,16 +2,10 @@ use nom::{ branch::alt, bytes::complete::tag, character::complete::multispace0, - combinator::{ - map, - opt, - }, + combinator::map, multi::many1, number::complete::float, - sequence::{ - preceded, - tuple, - }, + sequence::preceded, IResult, }; use torin::{ @@ -99,6 +93,31 @@ pub fn parse_calc(mut value: &str) -> Result, ParseError map(tag("clamp"), |_| { DynamicCalculation::Function(LexFunction::Clamp) }), + map(tag("scale"), |_| DynamicCalculation::ScalingFactor), + map(tag("parent.width"), |_| { + DynamicCalculation::Parent(Dimension::Width) + }), + map(tag("parent.height"), |_| { + DynamicCalculation::Parent(Dimension::Height) + }), + map(tag("parent.other"), |_| { + DynamicCalculation::Parent(Dimension::Other) + }), + map(tag("parent"), |_| { + DynamicCalculation::Parent(Dimension::Current) + }), + map(tag("root.width"), |_| { + DynamicCalculation::Parent(Dimension::Width) + }), + map(tag("root.height"), |_| { + DynamicCalculation::Parent(Dimension::Height) + }), + map(tag("root.other"), |_| { + DynamicCalculation::Parent(Dimension::Other) + }), + map(tag("root"), |_| { + DynamicCalculation::Parent(Dimension::Current) + }), map(tag("+"), |_| DynamicCalculation::Add), map(tag("-"), |_| DynamicCalculation::Sub), map(tag("*"), |_| DynamicCalculation::Mul), @@ -106,26 +125,6 @@ pub fn parse_calc(mut value: &str) -> Result, ParseError map(tag("("), |_| DynamicCalculation::OpenParenthesis), map(tag(")"), |_| DynamicCalculation::ClosedParenthesis), map(tag(","), |_| DynamicCalculation::FunctionSeparator), - map( - tuple((float, tag("%"), opt(tag("'")))), - |(v, _, inv): (f32, _, Option<&str>)| { - if inv.is_some() { - DynamicCalculation::Percentage(Dimension::Other(v)) - } else { - DynamicCalculation::Percentage(Dimension::Current(v)) - } - }, - ), - map( - tuple((float, tag("v"), opt(tag("'")))), - |(v, _, inv): (f32, _, Option<&str>)| { - if inv.is_some() { - DynamicCalculation::RootPercentage(Dimension::Other(v)) - } else { - DynamicCalculation::RootPercentage(Dimension::Current(v)) - } - }, - ), map(float, DynamicCalculation::Pixels), )), ))(value) diff --git a/crates/state/tests/parse_size.rs b/crates/state/tests/parse_size.rs index adc4c8c0c..aaa49d888 100644 --- a/crates/state/tests/parse_size.rs +++ b/crates/state/tests/parse_size.rs @@ -29,44 +29,51 @@ fn parse_auto_size() { #[test] fn parse_calc_size() { - let size = Size::parse("calc(90%- 5%* 123.6/ 50v(5 + 6) - min(50v, 50v', 100%, 100%') max(50v) clamp(50v, 5.0, 100%))"); + let size = Size::parse("calc(min(max(clamp(1, 2, 3), 4), parent.width + root.height - root.other * 2 / 1, scale, parent, root, parent.height, parent.other, +5.0, -3.0))"); assert_eq!( size, Ok(Size::DynamicCalculations(Box::new(vec![ - DynamicCalculation::Percentage(Dimension::Current(90.0)), - DynamicCalculation::Sub, - DynamicCalculation::Percentage(Dimension::Current(5.0)), - DynamicCalculation::Mul, - DynamicCalculation::Pixels(123.6), - DynamicCalculation::Div, - DynamicCalculation::RootPercentage(Dimension::Current(50.0)), - DynamicCalculation::OpenParenthesis, - DynamicCalculation::Pixels(5.0), - DynamicCalculation::Add, - DynamicCalculation::Pixels(6.0), - DynamicCalculation::ClosedParenthesis, - DynamicCalculation::Sub, DynamicCalculation::Function(LexFunction::Min), DynamicCalculation::OpenParenthesis, - DynamicCalculation::RootPercentage(Dimension::Current(50.0)), - DynamicCalculation::FunctionSeparator, - DynamicCalculation::RootPercentage(Dimension::Other(50.0)), - DynamicCalculation::FunctionSeparator, - DynamicCalculation::Percentage(Dimension::Current(100.0)), - DynamicCalculation::FunctionSeparator, - DynamicCalculation::Percentage(Dimension::Other(100.0)), - DynamicCalculation::ClosedParenthesis, DynamicCalculation::Function(LexFunction::Max), DynamicCalculation::OpenParenthesis, - DynamicCalculation::RootPercentage(Dimension::Current(50.0)), - DynamicCalculation::ClosedParenthesis, DynamicCalculation::Function(LexFunction::Clamp), DynamicCalculation::OpenParenthesis, - DynamicCalculation::RootPercentage(Dimension::Current(50.0)), + DynamicCalculation::Pixels(1.0), + DynamicCalculation::FunctionSeparator, + DynamicCalculation::Pixels(2.0), + DynamicCalculation::FunctionSeparator, + DynamicCalculation::Pixels(3.0), + DynamicCalculation::ClosedParenthesis, + DynamicCalculation::FunctionSeparator, + DynamicCalculation::Pixels(4.0), + DynamicCalculation::ClosedParenthesis, + DynamicCalculation::FunctionSeparator, + DynamicCalculation::Parent(Dimension::Width), + DynamicCalculation::Add, + DynamicCalculation::Root(Dimension::Height), + DynamicCalculation::Sub, + DynamicCalculation::Root(Dimension::Other), + DynamicCalculation::Mul, + DynamicCalculation::Pixels(2.0), + DynamicCalculation::Div, + DynamicCalculation::Pixels(1.0), DynamicCalculation::FunctionSeparator, + DynamicCalculation::ScalingFactor, + DynamicCalculation::FunctionSeparator, + DynamicCalculation::Parent(Dimension::Current), + DynamicCalculation::FunctionSeparator, + DynamicCalculation::Root(Dimension::Current), + DynamicCalculation::FunctionSeparator, + DynamicCalculation::Parent(Dimension::Height), + DynamicCalculation::FunctionSeparator, + DynamicCalculation::Parent(Dimension::Other), + DynamicCalculation::FunctionSeparator, + DynamicCalculation::Add, DynamicCalculation::Pixels(5.0), DynamicCalculation::FunctionSeparator, - DynamicCalculation::Percentage(Dimension::Current(100.0)), + DynamicCalculation::Sub, + DynamicCalculation::Pixels(3.0), DynamicCalculation::ClosedParenthesis, ]))) ); diff --git a/crates/torin/src/lib.rs b/crates/torin/src/lib.rs index 7348b76e2..a0ac704ed 100644 --- a/crates/torin/src/lib.rs +++ b/crates/torin/src/lib.rs @@ -11,7 +11,7 @@ pub mod custom_measurer; pub mod dom_adapter; pub mod geometry; -mod measure; +pub mod measure; pub mod node; pub mod scaled; pub mod sendanymap; @@ -26,6 +26,7 @@ pub mod prelude { dom_adapter::*, gaps::*, geometry::*, + measure::*, node::*, scaled::*, sendanymap::*, diff --git a/crates/torin/src/measure.rs b/crates/torin/src/measure.rs index 7d4bad775..bf683935f 100644 --- a/crates/torin/src/measure.rs +++ b/crates/torin/src/measure.rs @@ -23,6 +23,7 @@ use crate::{ Length, Torin, }, + size::EvalDimension, }; /// Some layout strategies require two-phase measurements @@ -83,28 +84,26 @@ where // Compute the width and height given the size, the minimum size, the maximum size and margins area_size.width = node.width.min_max( area_size.width, - parent_area.size.width, - parent_area.size.height, - available_parent_area.size.width, + EvalDimension::Width, + parent_area, + available_parent_area.width(), node.margin.left(), node.margin.horizontal(), &node.minimum_width, &node.maximum_width, - self.layout_metadata.root_area.width(), - self.layout_metadata.root_area.height(), + &self.layout_metadata.root_area, phase, ); area_size.height = node.height.min_max( area_size.height, - parent_area.size.height, - parent_area.size.width, - available_parent_area.size.height, + EvalDimension::Height, + parent_area, + available_parent_area.height(), node.margin.top(), node.margin.vertical(), &node.minimum_height, &node.maximum_height, - self.layout_metadata.root_area.height(), - self.layout_metadata.root_area.width(), + &self.layout_metadata.root_area, phase, ); @@ -127,30 +126,28 @@ where if node.width.inner_sized() { area_size.width = node.width.min_max( custom_size.width, - parent_area.size.width, - parent_area.size.height, - available_parent_area.size.width, + EvalDimension::Width, + parent_area, + available_parent_area.width(), node.margin.left(), node.margin.horizontal(), &node.minimum_width, &node.maximum_width, - self.layout_metadata.root_area.width(), - self.layout_metadata.root_area.height(), + &self.layout_metadata.root_area, phase, ); } if node.height.inner_sized() { area_size.height = node.height.min_max( custom_size.height, - parent_area.size.height, - parent_area.size.width, - available_parent_area.size.height, + EvalDimension::Height, + parent_area, + available_parent_area.height(), node.margin.top(), node.margin.vertical(), &node.minimum_height, &node.maximum_height, - self.layout_metadata.root_area.height(), - self.layout_metadata.root_area.width(), + &self.layout_metadata.root_area, phase, ); } @@ -180,30 +177,28 @@ where if node.width.inner_sized() { inner_size.width = node.width.min_max( available_parent_area.width(), - parent_area.size.width, - parent_area.size.height, + EvalDimension::Width, + parent_area, available_parent_area.width(), node.margin.left(), node.margin.horizontal(), &node.minimum_width, &node.maximum_width, - self.layout_metadata.root_area.width(), - self.layout_metadata.root_area.height(), + &self.layout_metadata.root_area, phase, ); } if node.height.inner_sized() { inner_size.height = node.height.min_max( available_parent_area.height(), - parent_area.size.height, - parent_area.size.width, + EvalDimension::Height, + parent_area, available_parent_area.height(), node.margin.top(), node.margin.vertical(), &node.minimum_height, &node.maximum_height, - self.layout_metadata.root_area.height(), - self.layout_metadata.root_area.width(), + &self.layout_metadata.root_area, phase, ); } diff --git a/crates/torin/src/values/size.rs b/crates/torin/src/values/size.rs index 4a7aded0b..ba0b61a9a 100644 --- a/crates/torin/src/values/size.rs +++ b/crates/torin/src/values/size.rs @@ -5,6 +5,7 @@ pub use euclid::Rect; use crate::{ geometry::Length, measure::Phase, + prelude::Area, scaled::Scaled, }; @@ -66,14 +67,21 @@ impl Size { #[allow(clippy::too_many_arguments)] pub fn eval( &self, - parent_value: f32, - other_parent_value: f32, + dimension: EvalDimension, + parent_value: &Area, available_parent_value: f32, parent_margin: f32, - root_value: f32, - other_root_value: f32, + root_value: &Area, phase: Phase, ) -> Option { + let current_parent_value = match dimension { + EvalDimension::Width => parent_value.width(), + EvalDimension::Height => parent_value.height(), + }; + let current_root_value = match dimension { + EvalDimension::Width => root_value.width(), + EvalDimension::Height => root_value.height(), + }; match self { Self::Pixels(px) => Some(px.get() + parent_margin), Self::Percentage(per) => Some(parent_value / 100.0 * per.get()), @@ -86,20 +94,14 @@ impl Size { Some(available_parent_value) } Size::Pixels(px) => Some(px.get() + parent_margin), - Size::Percentage(per) => Some(parent_value / 100.0 * per.get()), + Size::Percentage(per) => Some(current_parent_value / 100.0 * per.get()), Size::DynamicCalculations(calculations) => Some( - run_calculations( - calculations.deref(), - parent_value, - other_parent_value, - root_value, - other_root_value, - ) - .unwrap_or(0.0), + run_calculations(calculations.deref(), parent_value, root_value, dimension) + .unwrap_or(0.0), ), Size::Fill => Some(available_parent_value), Size::FillMinimum if phase == Phase::Final => Some(available_parent_value), - Size::RootPercentage(per) => Some(root_value / 100.0 * per.get()), + Size::RootPercentage(per) => Some(current_root_value / 100.0 * per.get()), Size::Flex(_) if phase == Phase::Final => Some(available_parent_value), _ => None, } @@ -109,47 +111,43 @@ impl Size { pub fn min_max( &self, value: f32, - parent_value: f32, - other_parent_value: f32, + dimension: EvalDimension, + parent_value: &Area, available_parent_value: f32, single_margin: f32, margin: f32, minimum: &Self, maximum: &Self, - root_value: f32, - other_root_value: f32, + root_value: &Area, phase: Phase, ) -> f32 { let value = self .eval( + dimension, parent_value, - other_parent_value, available_parent_value, margin, root_value, - other_root_value, phase, ) .unwrap_or(value + margin); let minimum_value = minimum .eval( + dimension, parent_value, - other_parent_value, available_parent_value, margin, root_value, - other_root_value, phase, ) .map(|v| v + single_margin); let maximum_value = maximum.eval( + dimension, parent_value, - other_parent_value, available_parent_value, margin, root_value, - other_root_value, phase, ); @@ -185,6 +183,12 @@ impl Scaled for Size { Self::DynamicCalculations(calcs) => { calcs.iter_mut().for_each(|calc| calc.scale(scale_factor)); } + Size::Pixels(s) => *s *= scale_factor, + Size::DynamicCalculations(calcs) => calcs.iter_mut().for_each(|v| { + if v == &mut DynamicCalculation::ScalingFactor { + *v = DynamicCalculation::Pixels(scale_factor); + } + }), _ => (), } } @@ -199,9 +203,11 @@ pub enum DynamicCalculation { OpenParenthesis, ClosedParenthesis, FunctionSeparator, - Percentage(Dimension), - RootPercentage(Dimension), - // no dimension because this isnt using any parent value + // this one works real weird, we actually replace it with a pixel value when we run the scale + // function + ScalingFactor, + Parent(Dimension), + Root(Dimension), Pixels(f32), Function(LexFunction), } @@ -231,8 +237,10 @@ pub enum LexFunction { /// ``` #[derive(Copy, Clone, Debug, PartialEq)] pub enum Dimension { - Current(f32), - Other(f32), + Current, + Other, + Width, + Height, } impl Scaled for DynamicCalculation { @@ -262,22 +270,19 @@ impl std::fmt::Display for DynamicCalculation { DynamicCalculation::OpenParenthesis => f.write_str("("), DynamicCalculation::ClosedParenthesis => f.write_str(")"), DynamicCalculation::FunctionSeparator => f.write_str(","), - DynamicCalculation::Percentage(Dimension::Current(p)) => { - f.write_fmt(format_args!("{p}%")) - } - DynamicCalculation::Percentage(Dimension::Other(p)) => { - f.write_fmt(format_args!("{p}%'")) - } - DynamicCalculation::RootPercentage(Dimension::Current(p)) => { - f.write_fmt(format_args!("{p}v")) - } - DynamicCalculation::RootPercentage(Dimension::Other(p)) => { - f.write_fmt(format_args!("{p}v")) - } DynamicCalculation::Pixels(s) => f.write_fmt(format_args!("{s}")), DynamicCalculation::Function(LexFunction::Min) => f.write_str("min"), DynamicCalculation::Function(LexFunction::Max) => f.write_str("max"), DynamicCalculation::Function(LexFunction::Clamp) => f.write_str("clamp"), + DynamicCalculation::ScalingFactor => f.write_str("scale"), + DynamicCalculation::Parent(Dimension::Current) => f.write_str("parent"), + DynamicCalculation::Parent(Dimension::Other) => f.write_str("parent.other"), + DynamicCalculation::Parent(Dimension::Width) => f.write_str("parent.width"), + DynamicCalculation::Parent(Dimension::Height) => f.write_str("parent.height"), + DynamicCalculation::Root(Dimension::Current) => f.write_str("root"), + DynamicCalculation::Root(Dimension::Other) => f.write_str("root.other"), + DynamicCalculation::Root(Dimension::Width) => f.write_str("root.width"), + DynamicCalculation::Root(Dimension::Height) => f.write_str("root.height"), } } } @@ -285,13 +290,18 @@ impl std::fmt::Display for DynamicCalculation { /// [Operator-precedence parser](https://en.wikipedia.org/wiki/Operator-precedence_parser#Precedence_climbing_method) struct DynamicCalculationEvaluator<'a> { calcs: Iter<'a, DynamicCalculation>, - parent_value: f32, - other_parent_value: f32, - root_value: f32, - other_root_value: f32, + dimension: EvalDimension, + parent_value: &'a Area, + root_value: &'a Area, current: Option<&'a DynamicCalculation>, } +#[derive(Copy, Clone, Debug, PartialEq)] +pub enum EvalDimension { + Width, + Height, +} + impl<'a> DynamicCalculationEvaluator<'a> { pub const fn new( calcs: Iter<'a, DynamicCalculation>, @@ -300,17 +310,15 @@ impl<'a> DynamicCalculationEvaluator<'a> { ) -> Self { pub fn new( calcs: Iter<'a, DynamicCalculation>, - parent_value: f32, - other_parent_value: f32, - root_value: f32, - other_root_value: f32, + dimension: EvalDimension, + parent_value: &'a Area, + root_value: &'a Area, ) -> Self { Self { calcs, parent_value, - other_parent_value, + dimension, root_value, - other_root_value, current: None, } } @@ -397,21 +405,53 @@ impl<'a> DynamicCalculationEvaluator<'a> { /// ` fn parse_value(&mut self) -> Option<(f32, bool)> { match self.current? { - DynamicCalculation::Percentage(Dimension::Current(value)) => { + DynamicCalculation::Root(Dimension::Current) => { self.current = self.calcs.next(); - Some(((self.parent_value / 100.0 * value).round(), false)) + match self.dimension { + EvalDimension::Width => Some((self.root_value.width(), true)), + EvalDimension::Height => Some((self.root_value.height(), true)), + } + } + DynamicCalculation::Root(Dimension::Other) => { + self.current = self.calcs.next(); + match self.dimension { + EvalDimension::Width => Some((self.root_value.height(), true)), + EvalDimension::Height => Some((self.root_value.width(), true)), + } + } + DynamicCalculation::Root(Dimension::Width) => { + self.current = self.calcs.next(); + Some((self.root_value.width(), true)) + } + DynamicCalculation::Root(Dimension::Height) => { + self.current = self.calcs.next(); + Some((self.root_value.height(), true)) + } + DynamicCalculation::Parent(Dimension::Current) => { + self.current = self.calcs.next(); + match self.dimension { + EvalDimension::Width => Some((self.parent_value.width(), true)), + EvalDimension::Height => Some((self.parent_value.height(), true)), + } + } + DynamicCalculation::Parent(Dimension::Other) => { + self.current = self.calcs.next(); + match self.dimension { + EvalDimension::Width => Some((self.parent_value.height(), true)), + EvalDimension::Height => Some((self.parent_value.width(), true)), + } } - DynamicCalculation::Percentage(Dimension::Other(value)) => { + DynamicCalculation::Parent(Dimension::Width) => { self.current = self.calcs.next(); - Some(((self.other_parent_value / 100.0 * value).round(), false)) + Some((self.parent_value.width(), true)) } - DynamicCalculation::RootPercentage(Dimension::Current(value)) => { + DynamicCalculation::Parent(Dimension::Height) => { self.current = self.calcs.next(); - Some(((self.root_value / 100.0 * value).round(), false)) + Some((self.parent_value.height(), true)) } - DynamicCalculation::RootPercentage(Dimension::Other(value)) => { + DynamicCalculation::ScalingFactor => { self.current = self.calcs.next(); - Some(((self.other_root_value / 100.0 * value).round(), false)) + Some((1.0, true)) } DynamicCalculation::Pixels(value) => { self.current = self.calcs.next(); @@ -496,17 +536,9 @@ impl<'a> DynamicCalculationEvaluator<'a> { /// This value could be for example the width of a node's parent area. pub fn run_calculations( calcs: &[DynamicCalculation], - parent_value: f32, - other_parent_value: f32, - root_value: f32, - other_root_value: f32, + parent_value: &Area, + root_value: &Area, + dimension: EvalDimension, ) -> Option { - DynamicCalculationEvaluator::new( - calcs.iter(), - parent_value, - other_parent_value, - root_value, - other_root_value, - ) - .evaluate() + DynamicCalculationEvaluator::new(calcs.iter(), dimension, parent_value, root_value).evaluate() } diff --git a/crates/torin/tests/size.rs b/crates/torin/tests/size.rs index f52371707..58805742a 100644 --- a/crates/torin/tests/size.rs +++ b/crates/torin/tests/size.rs @@ -781,458 +781,67 @@ pub fn inner_percentage() { #[test] pub fn test_calc() { - const PARENT_VALUE: f32 = 500.0; - const OTHER_PARENT_VALUE: f32 = 100.0; - - assert_eq!( - run_calculations( - &[DynamicCalculation::Pixels(10.0)], - PARENT_VALUE, - OTHER_PARENT_VALUE, - PARENT_VALUE, - OTHER_PARENT_VALUE, - ), - Some(10.0) - ); - - assert_eq!( - run_calculations( - &[DynamicCalculation::Percentage(Dimension::Current(87.5))], - PARENT_VALUE, - OTHER_PARENT_VALUE, - PARENT_VALUE, - OTHER_PARENT_VALUE, - ), - Some((87.5 / 100.0 * PARENT_VALUE).round()) - ); - - assert_eq!( - run_calculations( - &[DynamicCalculation::Percentage(Dimension::Other(87.5))], - PARENT_VALUE, - OTHER_PARENT_VALUE, - PARENT_VALUE, - OTHER_PARENT_VALUE, - ), - Some((87.5 / 100.0 * OTHER_PARENT_VALUE).round()) - ); - - assert_eq!( - run_calculations( - &[DynamicCalculation::RootPercentage(Dimension::Current(87.5))], - PARENT_VALUE, - OTHER_PARENT_VALUE, - PARENT_VALUE, - OTHER_PARENT_VALUE, - ), - Some((87.5 / 100.0 * PARENT_VALUE).round()) - ); - - assert_eq!( - run_calculations( - &[DynamicCalculation::RootPercentage(Dimension::Other(87.5))], - PARENT_VALUE, - OTHER_PARENT_VALUE, - PARENT_VALUE, - OTHER_PARENT_VALUE, - ), - Some((87.5 / 100.0 * OTHER_PARENT_VALUE).round()) - ); - - assert_eq!( - run_calculations( - &[ - DynamicCalculation::Pixels(10.0), - DynamicCalculation::Add, - DynamicCalculation::Pixels(20.0), - DynamicCalculation::Mul, - DynamicCalculation::Percentage(Dimension::Current(50.0)) - ], - PARENT_VALUE, - OTHER_PARENT_VALUE, - PARENT_VALUE, - OTHER_PARENT_VALUE, - ), - Some(10.0 + 20.0 * (50.0 / 100.0 * PARENT_VALUE).round()) - ); - - assert_eq!( - run_calculations( - &[ - DynamicCalculation::Pixels(10.0), - DynamicCalculation::Add, - DynamicCalculation::Percentage(Dimension::Current(10.0)), - DynamicCalculation::Add, - DynamicCalculation::Pixels(30.0), - DynamicCalculation::Mul, - DynamicCalculation::Pixels(10.0), - DynamicCalculation::Add, - DynamicCalculation::Pixels(75.0), - DynamicCalculation::Mul, - DynamicCalculation::Pixels(2.0) - ], - PARENT_VALUE, - OTHER_PARENT_VALUE, - PARENT_VALUE, - OTHER_PARENT_VALUE, - ), - Some(10.0 + (10.0 / 100.0 * PARENT_VALUE).round() + 30.0 * 10.0 + 75.0 * 2.0) - ); - - assert_eq!( - run_calculations( - &[ - DynamicCalculation::Pixels(10.0), - DynamicCalculation::Pixels(20.0) - ], - PARENT_VALUE, - OTHER_PARENT_VALUE, - PARENT_VALUE, - OTHER_PARENT_VALUE, - ), - None - ); - - assert_eq!( - run_calculations( - &[DynamicCalculation::Pixels(10.0), DynamicCalculation::Add], - PARENT_VALUE, - OTHER_PARENT_VALUE, - PARENT_VALUE, - OTHER_PARENT_VALUE, - ), - None - ); - - assert_eq!( - run_calculations( - &[DynamicCalculation::Add, DynamicCalculation::Pixels(10.0)], - PARENT_VALUE, - OTHER_PARENT_VALUE, - PARENT_VALUE, - OTHER_PARENT_VALUE, - ), - // Because +10 is just 10 - Some(10.0) - ); - - assert_eq!( - run_calculations( - &[ - DynamicCalculation::Pixels(10.0), - DynamicCalculation::Add, - // counts as a prefix - DynamicCalculation::Add, - DynamicCalculation::Pixels(10.0) - ], - PARENT_VALUE, - OTHER_PARENT_VALUE, - PARENT_VALUE, - OTHER_PARENT_VALUE, - ), - Some(20.0) - ); - - assert_eq!( - run_calculations( - &[ - DynamicCalculation::Percentage(Dimension::Current(50.0)), - DynamicCalculation::Sub, - DynamicCalculation::RootPercentage(Dimension::Current(20.0)) - ], - PARENT_VALUE, - OTHER_PARENT_VALUE, - PARENT_VALUE, - OTHER_PARENT_VALUE, - ), - Some((PARENT_VALUE * 0.5) - (PARENT_VALUE * 0.20)) - ); + const PARENT_VALUE: &Area = &Area::new(Point2D::new(0.0, 0.0), Size2D::new(500.0, 100.0)); assert_eq!( run_calculations( + // so many features... i wish nobody else has to test so many things ever again + // if you want to make this test better, do it... i date you + // represents `calc((10)(5) * scale(2 * (5 + min(root, parent.other)))` &[ DynamicCalculation::OpenParenthesis, DynamicCalculation::Pixels(10.0), - DynamicCalculation::ClosedParenthesis - ], - PARENT_VALUE, - OTHER_PARENT_VALUE, - PARENT_VALUE, - OTHER_PARENT_VALUE, - ), - Some(10.0) - ); - - assert_eq!( - run_calculations( - &[ - DynamicCalculation::Pixels(10.0), - DynamicCalculation::OpenParenthesis, - DynamicCalculation::Pixels(10.0), - DynamicCalculation::Add, - DynamicCalculation::Pixels(20.0), DynamicCalculation::ClosedParenthesis, - DynamicCalculation::Pixels(10.0), - DynamicCalculation::Add, - DynamicCalculation::Pixels(10.0), DynamicCalculation::OpenParenthesis, - DynamicCalculation::Pixels(10.0), + DynamicCalculation::Pixels(5.0), DynamicCalculation::ClosedParenthesis, - DynamicCalculation::Pixels(10.0) - ], - PARENT_VALUE, - OTHER_PARENT_VALUE, - PARENT_VALUE, - OTHER_PARENT_VALUE, - ), - Some((10.0 * (10.0 + 20.0) * 10.0) + (10.0 * (10.0) * 10.0)) - ); - - assert_eq!( - run_calculations( - &[ - DynamicCalculation::Sub, + DynamicCalculation::Mul, + DynamicCalculation::ScalingFactor, DynamicCalculation::OpenParenthesis, - DynamicCalculation::Pixels(10.0), - DynamicCalculation::ClosedParenthesis, - DynamicCalculation::Pixels(20.0) - ], - PARENT_VALUE, - OTHER_PARENT_VALUE, - PARENT_VALUE, - OTHER_PARENT_VALUE, - ), - Some(-1.0 * 10.0 * 20.0) - ); - - assert_eq!( - run_calculations( - &[ + DynamicCalculation::Pixels(2.0), + DynamicCalculation::Mul, DynamicCalculation::OpenParenthesis, - DynamicCalculation::Pixels(10.0) - ], - PARENT_VALUE, - OTHER_PARENT_VALUE, - PARENT_VALUE, - OTHER_PARENT_VALUE, - ), - None - ); - - assert_eq!( - run_calculations( - &[ + DynamicCalculation::Pixels(5.0), + DynamicCalculation::Add, DynamicCalculation::Function(LexFunction::Min), DynamicCalculation::OpenParenthesis, - DynamicCalculation::Pixels(10.0), + DynamicCalculation::Root(Dimension::Current), DynamicCalculation::FunctionSeparator, - DynamicCalculation::Pixels(20.0), + DynamicCalculation::Parent(Dimension::Other), DynamicCalculation::ClosedParenthesis, - ], - PARENT_VALUE, - OTHER_PARENT_VALUE, - PARENT_VALUE, - OTHER_PARENT_VALUE, - ), - Some(10.0) - ); - - assert_eq!( - run_calculations( - &[ - DynamicCalculation::Function(LexFunction::Max), - DynamicCalculation::OpenParenthesis, - DynamicCalculation::Pixels(10.0), - DynamicCalculation::FunctionSeparator, - DynamicCalculation::Pixels(30.0), - DynamicCalculation::FunctionSeparator, - DynamicCalculation::Pixels(20.0), - DynamicCalculation::ClosedParenthesis, - ], - PARENT_VALUE, - OTHER_PARENT_VALUE, - PARENT_VALUE, - OTHER_PARENT_VALUE, - ), - Some(30.0) - ); - - assert_eq!( - run_calculations( - &[ - DynamicCalculation::Function(LexFunction::Max), - DynamicCalculation::OpenParenthesis, - DynamicCalculation::Pixels(10.0), - DynamicCalculation::FunctionSeparator, DynamicCalculation::ClosedParenthesis, - ], - PARENT_VALUE, - OTHER_PARENT_VALUE, - PARENT_VALUE, - OTHER_PARENT_VALUE, - ), - None - ); - - assert_eq!( - run_calculations( - &[ - DynamicCalculation::Function(LexFunction::Max), - DynamicCalculation::OpenParenthesis, - DynamicCalculation::Pixels(10.0), - ], - PARENT_VALUE, - OTHER_PARENT_VALUE, - PARENT_VALUE, - OTHER_PARENT_VALUE, - ), - None - ); - - assert_eq!( - run_calculations( - &[ - DynamicCalculation::Function(LexFunction::Clamp), - DynamicCalculation::OpenParenthesis, - DynamicCalculation::Pixels(10.0), - DynamicCalculation::FunctionSeparator, - DynamicCalculation::Pixels(30.0), - DynamicCalculation::FunctionSeparator, - DynamicCalculation::Pixels(20.0), DynamicCalculation::ClosedParenthesis, ], PARENT_VALUE, - OTHER_PARENT_VALUE, PARENT_VALUE, - OTHER_PARENT_VALUE, + EvalDimension::Width, ), - Some(20.0) - ); - - assert_eq!( - run_calculations( - &[ - DynamicCalculation::Function(LexFunction::Clamp), - DynamicCalculation::OpenParenthesis, - DynamicCalculation::Pixels(10.0), - DynamicCalculation::FunctionSeparator, - DynamicCalculation::Pixels(5.0), - DynamicCalculation::FunctionSeparator, - DynamicCalculation::Pixels(20.0), - DynamicCalculation::ClosedParenthesis, - ], - PARENT_VALUE, - OTHER_PARENT_VALUE, - PARENT_VALUE, - OTHER_PARENT_VALUE, - ), - Some(10.0) - ); - - assert_eq!( - run_calculations( - &[ - DynamicCalculation::Function(LexFunction::Clamp), - DynamicCalculation::OpenParenthesis, - DynamicCalculation::Pixels(10.0), - DynamicCalculation::FunctionSeparator, - DynamicCalculation::Pixels(30.0), - DynamicCalculation::ClosedParenthesis, - ], - PARENT_VALUE, - OTHER_PARENT_VALUE, - PARENT_VALUE, - OTHER_PARENT_VALUE, - ), - None + Some(10.0 * 5.0 * 1.0 * (2.0 * (5.0 + PARENT_VALUE.width().min(PARENT_VALUE.height())))) ); } #[test] pub fn test_scaling_factor() { - const PARENT_VALUE: f32 = 500.0; - const OTHER_PARENT_VALUE: f32 = 500.0; - - assert_eq!( - { - let mut size = - Size::DynamicCalculations(Box::new(vec![DynamicCalculation::Pixels(10.0)])); - size.scale(1.5); - size - } - .eval( - PARENT_VALUE, - OTHER_PARENT_VALUE, - PARENT_VALUE, - 0.0, - PARENT_VALUE, - OTHER_PARENT_VALUE, - Phase::Initial - ), - Some((10.0) * 1.5) - ); + const PARENT_VALUE: &Area = &Area::new(Point2D::new(0.0, 0.0), Size2D::new(500.0, 500.0)); assert_eq!( { let mut size = Size::DynamicCalculations(Box::new(vec![ DynamicCalculation::Pixels(10.0), - DynamicCalculation::OpenParenthesis, - DynamicCalculation::Pixels(10.0), - DynamicCalculation::Add, - DynamicCalculation::Pixels(20.0), - DynamicCalculation::ClosedParenthesis, - DynamicCalculation::Pixels(10.0), - DynamicCalculation::Add, - DynamicCalculation::Pixels(10.0), - DynamicCalculation::OpenParenthesis, - DynamicCalculation::Pixels(10.0), - DynamicCalculation::ClosedParenthesis, - DynamicCalculation::Pixels(10.0), + DynamicCalculation::Mul, + DynamicCalculation::ScalingFactor, ])); size.scale(1.5); size } .eval( + EvalDimension::Width, PARENT_VALUE, - OTHER_PARENT_VALUE, - PARENT_VALUE, - 0.0, - PARENT_VALUE, - OTHER_PARENT_VALUE, - Phase::Initial - ), - Some(((10.0 * (10.0 + 20.0) * 10.0) + (10.0 * (10.0) * 10.0)) * 1.5) - ); - - assert_eq!( - { - let mut size = Size::DynamicCalculations(Box::new(vec![ - DynamicCalculation::Pixels(10.0), - DynamicCalculation::OpenParenthesis, - DynamicCalculation::Pixels(10.0), - DynamicCalculation::Add, - DynamicCalculation::Pixels(20.0), - DynamicCalculation::ClosedParenthesis, - DynamicCalculation::Pixels(10.0), - DynamicCalculation::Add, - DynamicCalculation::Pixels(10.0), - DynamicCalculation::OpenParenthesis, - DynamicCalculation::Pixels(10.0), - DynamicCalculation::ClosedParenthesis, - DynamicCalculation::Pixels(10.0), - ])); - size.scale(1.2); - size - } - .eval( - PARENT_VALUE, - OTHER_PARENT_VALUE, - PARENT_VALUE, + PARENT_VALUE.width(), 0.0, PARENT_VALUE, - OTHER_PARENT_VALUE, Phase::Initial ), - Some(((10.0 * (10.0 + 20.0) * 10.0) + (10.0 * (10.0) * 10.0)) * 1.2) + Some((10.0) * 1.5) ); } From faf87085eb9f519da9ca8a5dce8d22bad34d7192 Mon Sep 17 00:00:00 2001 From: RobertasJ Date: Fri, 20 Dec 2024 19:07:50 +0100 Subject: [PATCH 4/4] rename .other to .cross --- crates/state/src/values/size.rs | 14 +++++++------- crates/state/tests/parse_size.rs | 6 +++--- crates/torin/src/values/size.rs | 10 +++++----- crates/torin/tests/size.rs | 2 +- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/crates/state/src/values/size.rs b/crates/state/src/values/size.rs index 51948aa67..dae7d345b 100644 --- a/crates/state/src/values/size.rs +++ b/crates/state/src/values/size.rs @@ -100,23 +100,23 @@ pub fn parse_calc(mut value: &str) -> Result, ParseError map(tag("parent.height"), |_| { DynamicCalculation::Parent(Dimension::Height) }), - map(tag("parent.other"), |_| { - DynamicCalculation::Parent(Dimension::Other) + map(tag("parent.cross"), |_| { + DynamicCalculation::Parent(Dimension::Cross) }), map(tag("parent"), |_| { DynamicCalculation::Parent(Dimension::Current) }), map(tag("root.width"), |_| { - DynamicCalculation::Parent(Dimension::Width) + DynamicCalculation::Root(Dimension::Width) }), map(tag("root.height"), |_| { - DynamicCalculation::Parent(Dimension::Height) + DynamicCalculation::Root(Dimension::Height) }), - map(tag("root.other"), |_| { - DynamicCalculation::Parent(Dimension::Other) + map(tag("root.cross"), |_| { + DynamicCalculation::Root(Dimension::Cross) }), map(tag("root"), |_| { - DynamicCalculation::Parent(Dimension::Current) + DynamicCalculation::Root(Dimension::Current) }), map(tag("+"), |_| DynamicCalculation::Add), map(tag("-"), |_| DynamicCalculation::Sub), diff --git a/crates/state/tests/parse_size.rs b/crates/state/tests/parse_size.rs index aaa49d888..df681f07b 100644 --- a/crates/state/tests/parse_size.rs +++ b/crates/state/tests/parse_size.rs @@ -29,7 +29,7 @@ fn parse_auto_size() { #[test] fn parse_calc_size() { - let size = Size::parse("calc(min(max(clamp(1, 2, 3), 4), parent.width + root.height - root.other * 2 / 1, scale, parent, root, parent.height, parent.other, +5.0, -3.0))"); + let size = Size::parse("calc(min(max(clamp(1, 2, 3), 4), parent.width + root.height - root.cross * 2 / 1, scale, parent, root, parent.height, parent.cross, +5.0, -3.0))"); assert_eq!( size, Ok(Size::DynamicCalculations(Box::new(vec![ @@ -53,7 +53,7 @@ fn parse_calc_size() { DynamicCalculation::Add, DynamicCalculation::Root(Dimension::Height), DynamicCalculation::Sub, - DynamicCalculation::Root(Dimension::Other), + DynamicCalculation::Root(Dimension::Cross), DynamicCalculation::Mul, DynamicCalculation::Pixels(2.0), DynamicCalculation::Div, @@ -67,7 +67,7 @@ fn parse_calc_size() { DynamicCalculation::FunctionSeparator, DynamicCalculation::Parent(Dimension::Height), DynamicCalculation::FunctionSeparator, - DynamicCalculation::Parent(Dimension::Other), + DynamicCalculation::Parent(Dimension::Cross), DynamicCalculation::FunctionSeparator, DynamicCalculation::Add, DynamicCalculation::Pixels(5.0), diff --git a/crates/torin/src/values/size.rs b/crates/torin/src/values/size.rs index ba0b61a9a..c5661b9d8 100644 --- a/crates/torin/src/values/size.rs +++ b/crates/torin/src/values/size.rs @@ -238,7 +238,7 @@ pub enum LexFunction { #[derive(Copy, Clone, Debug, PartialEq)] pub enum Dimension { Current, - Other, + Cross, Width, Height, } @@ -276,11 +276,11 @@ impl std::fmt::Display for DynamicCalculation { DynamicCalculation::Function(LexFunction::Clamp) => f.write_str("clamp"), DynamicCalculation::ScalingFactor => f.write_str("scale"), DynamicCalculation::Parent(Dimension::Current) => f.write_str("parent"), - DynamicCalculation::Parent(Dimension::Other) => f.write_str("parent.other"), + DynamicCalculation::Parent(Dimension::Cross) => f.write_str("parent.other"), DynamicCalculation::Parent(Dimension::Width) => f.write_str("parent.width"), DynamicCalculation::Parent(Dimension::Height) => f.write_str("parent.height"), DynamicCalculation::Root(Dimension::Current) => f.write_str("root"), - DynamicCalculation::Root(Dimension::Other) => f.write_str("root.other"), + DynamicCalculation::Root(Dimension::Cross) => f.write_str("root.other"), DynamicCalculation::Root(Dimension::Width) => f.write_str("root.width"), DynamicCalculation::Root(Dimension::Height) => f.write_str("root.height"), } @@ -412,7 +412,7 @@ impl<'a> DynamicCalculationEvaluator<'a> { EvalDimension::Height => Some((self.root_value.height(), true)), } } - DynamicCalculation::Root(Dimension::Other) => { + DynamicCalculation::Root(Dimension::Cross) => { self.current = self.calcs.next(); match self.dimension { EvalDimension::Width => Some((self.root_value.height(), true)), @@ -434,7 +434,7 @@ impl<'a> DynamicCalculationEvaluator<'a> { EvalDimension::Height => Some((self.parent_value.height(), true)), } } - DynamicCalculation::Parent(Dimension::Other) => { + DynamicCalculation::Parent(Dimension::Cross) => { self.current = self.calcs.next(); match self.dimension { EvalDimension::Width => Some((self.parent_value.height(), true)), diff --git a/crates/torin/tests/size.rs b/crates/torin/tests/size.rs index 58805742a..191373fe7 100644 --- a/crates/torin/tests/size.rs +++ b/crates/torin/tests/size.rs @@ -807,7 +807,7 @@ pub fn test_calc() { DynamicCalculation::OpenParenthesis, DynamicCalculation::Root(Dimension::Current), DynamicCalculation::FunctionSeparator, - DynamicCalculation::Parent(Dimension::Other), + DynamicCalculation::Parent(Dimension::Cross), DynamicCalculation::ClosedParenthesis, DynamicCalculation::ClosedParenthesis, DynamicCalculation::ClosedParenthesis,