Skip to content

Commit

Permalink
new stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertasJ committed Dec 20, 2024
1 parent d2ec180 commit c607144
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 571 deletions.
55 changes: 27 additions & 28 deletions crates/state/src/values/size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -99,33 +93,38 @@ pub fn parse_calc(mut value: &str) -> Result<Vec<DynamicCalculation>, 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),
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),
)),
))(value)
Expand Down
59 changes: 33 additions & 26 deletions crates/state/tests/parse_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
])))
);
Expand Down
51 changes: 23 additions & 28 deletions crates/torin/src/measure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::{
Length,
Torin,
},
size::EvalDimension,
};

/// Some layout strategies require two-phase measurements
Expand Down Expand Up @@ -84,28 +85,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,
);

Expand All @@ -128,30 +127,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,
);
}
Expand Down Expand Up @@ -181,30 +178,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,
);
}
Expand Down
Loading

0 comments on commit c607144

Please sign in to comment.