Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

calc easier to extend syntax #1007

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 38 additions & 10 deletions crates/state/src/values/size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ use nom::{
combinator::map,
multi::many1,
number::complete::float,
sequence::{
preceded,
tuple,
},
sequence::preceded,
IResult,
};
use torin::{
geometry::Length,
size::{
Dimension,
DynamicCalculation,
LexFunction,
Size,
},
};
Expand Down Expand Up @@ -85,18 +84,47 @@ pub fn parse_calc(mut value: &str) -> Result<Vec<DynamicCalculation>, 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("scale"), |_| DynamicCalculation::ScalingFactor),
map(tag("parent.width"), |_| {
DynamicCalculation::Parent(Dimension::Width)
}),
map(tag("parent.height"), |_| {
DynamicCalculation::Parent(Dimension::Height)
}),
map(tag("parent.cross"), |_| {
DynamicCalculation::Parent(Dimension::Cross)
}),
map(tag("parent"), |_| {
DynamicCalculation::Parent(Dimension::Current)
}),
map(tag("root.width"), |_| {
DynamicCalculation::Root(Dimension::Width)
}),
map(tag("root.height"), |_| {
DynamicCalculation::Root(Dimension::Height)
}),
map(tag("root.cross"), |_| {
DynamicCalculation::Root(Dimension::Cross)
}),
map(tag("root"), |_| {
DynamicCalculation::Root(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(tuple((float, tag("%"))), |(v, _)| {
DynamicCalculation::Percentage(v)
}),
map(tuple((float, tag("v"))), |(v, _)| {
DynamicCalculation::RootPercentage(v)
}),
map(tag(","), |_| DynamicCalculation::FunctionSeparator),
map(float, DynamicCalculation::Pixels),
)),
))(value)
Expand Down
48 changes: 40 additions & 8 deletions crates/state/tests/parse_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use freya_node_state::Parse;
use torin::{
geometry::Length,
size::{
Dimension,
DynamicCalculation,
LexFunction,
Size,
},
};
Expand All @@ -27,21 +29,51 @@ fn parse_auto_size() {

#[test]
fn parse_calc_size() {
let size = Size::parse("calc(90%- 5%* 123.6/ 50v(5 + 6))");
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![
DynamicCalculation::Percentage(90.0),
DynamicCalculation::Function(LexFunction::Min),
DynamicCalculation::OpenParenthesis,
DynamicCalculation::Function(LexFunction::Max),
DynamicCalculation::OpenParenthesis,
DynamicCalculation::Function(LexFunction::Clamp),
DynamicCalculation::OpenParenthesis,
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::Percentage(5.0),
DynamicCalculation::Root(Dimension::Cross),
DynamicCalculation::Mul,
DynamicCalculation::Pixels(123.6),
DynamicCalculation::Pixels(2.0),
DynamicCalculation::Div,
DynamicCalculation::RootPercentage(50.0),
DynamicCalculation::OpenParenthesis,
DynamicCalculation::Pixels(5.0),
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::Cross),
DynamicCalculation::FunctionSeparator,
DynamicCalculation::Add,
DynamicCalculation::Pixels(6.0),
DynamicCalculation::Pixels(5.0),
DynamicCalculation::FunctionSeparator,
DynamicCalculation::Sub,
DynamicCalculation::Pixels(3.0),
DynamicCalculation::ClosedParenthesis,
])))
);
Expand Down
3 changes: 2 additions & 1 deletion crates/torin/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,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 torin;
Expand All @@ -15,6 +15,7 @@ pub mod prelude {
dom_adapter::*,
gaps::*,
geometry::*,
measure::*,
node::*,
scaled::*,
torin::*,
Expand Down
39 changes: 23 additions & 16 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,24 +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,
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,
phase,
);
area_size.height = node.height.min_max(
area_size.height,
parent_area.size.height,
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,
phase,
);

Expand All @@ -124,26 +127,28 @@ where
if node.width.inner_sized() {
area_size.width = node.width.min_max(
custom_size.width,
parent_area.size.width,
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,
phase,
);
}
if node.height.inner_sized() {
area_size.height = node.height.min_max(
custom_size.height,
parent_area.size.height,
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,
phase,
);
}
Expand Down Expand Up @@ -173,26 +178,28 @@ where
if node.width.inner_sized() {
inner_size.width = node.width.min_max(
available_parent_area.width(),
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,
phase,
);
}
if node.height.inner_sized() {
inner_size.height = node.height.min_max(
available_parent_area.height(),
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,
phase,
);
}
Expand Down
Loading
Loading