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
135 changes: 135 additions & 0 deletions crates/state/src/values/size.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
use nom::{
branch::alt,
bytes::complete::tag,
character::complete::multispace0,
combinator::map,
multi::many1,
number::complete::float,
sequence::preceded,
IResult,
};
use torin::{
geometry::Length,
size::{
Dimension,
DynamicCalculation,
LexFunction,
Size,
},
};

use crate::{
Parse,
ParseError,
};

impl Parse for Size {
fn parse(value: &str) -> Result<Self, ParseError> {
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::<f32>()
.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::<f32>()
.map_err(|_| ParseError)?,
)))
} else if value.contains('v') {
Ok(Size::RootPercentage(Length::new(
value
.replace('v', "")
.parse::<f32>()
.map_err(|_| ParseError)?,
)))
} else if value.contains('a') {
Ok(Size::InnerPercentage(Length::new(
value
.replace('a', "")
.parse::<f32>()
.map_err(|_| ParseError)?,
)))
} else {
Ok(Size::Pixels(Length::new(
value.parse::<f32>().map_err(|_| ParseError)?,
)))
}
}
}

pub fn parse_calc(mut value: &str) -> Result<Vec<DynamicCalculation>, 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<DynamicCalculation>> {
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(tag(","), |_| DynamicCalculation::FunctionSeparator),
map(float, DynamicCalculation::Pixels),
)),
))(value)
}
let tokens = inner_parse(value).map_err(|_| ParseError)?.1;

Ok(tokens)
}
80 changes: 80 additions & 0 deletions crates/state/tests/parse_size.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
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(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::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::Root(Dimension::Cross),
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::Cross),
DynamicCalculation::FunctionSeparator,
DynamicCalculation::Add,
DynamicCalculation::Pixels(5.0),
DynamicCalculation::FunctionSeparator,
DynamicCalculation::Sub,
DynamicCalculation::Pixels(3.0),
DynamicCalculation::ClosedParenthesis,
])))
);
}
3 changes: 2 additions & 1 deletion crates/torin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,6 +26,7 @@ pub mod prelude {
dom_adapter::*,
gaps::*,
geometry::*,
measure::*,
node::*,
scaled::*,
sendanymap::*,
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 @@ -83,24 +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,
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 @@ -123,26 +126,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 @@ -172,26 +177,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