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

feat: Support RootPercentage in calc() #907

Merged
merged 2 commits into from
Sep 28, 2024
Merged
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
4 changes: 4 additions & 0 deletions crates/state/src/values/size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ pub fn parse_calc(mut value: &str) -> Result<Vec<DynamicCalculation>, ParseError
calcs.push(DynamicCalculation::Percentage(
val.replace('%', "").parse().map_err(|_| ParseError)?,
));
} else if val.contains('v') {
calcs.push(DynamicCalculation::RootPercentage(
val.replace('v', "").parse().map_err(|_| ParseError)?,
));
} else if val == "+" {
calcs.push(DynamicCalculation::Add);
} else if val == "-" {
Expand Down
6 changes: 4 additions & 2 deletions crates/state/tests/parse_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,17 @@ fn parse_auto_size() {

#[test]
fn parse_calc_size() {
let size = Size::parse("calc(90% - 5% * 123.6)");
let size = Size::parse("calc(90% - 5% * 123.6 / 50v)");
assert_eq!(
size,
Ok(Size::DynamicCalculations(Box::new(vec![
DynamicCalculation::Percentage(90.0),
DynamicCalculation::Sub,
DynamicCalculation::Percentage(5.0),
DynamicCalculation::Mul,
DynamicCalculation::Pixels(123.6)
DynamicCalculation::Pixels(123.6),
DynamicCalculation::Div,
DynamicCalculation::RootPercentage(50.0)
])))
);
}
24 changes: 18 additions & 6 deletions crates/torin/src/values/size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ impl Size {
match self {
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).unwrap_or(0.0))
}
Size::DynamicCalculations(calculations) => Some(
run_calculations(calculations.deref(), parent_value, root_value).unwrap_or(0.0),
),
Size::Fill => Some(available_parent_value),
Size::FillMinimum => {
if phase == Phase::Initial {
Expand Down Expand Up @@ -172,6 +172,7 @@ pub enum DynamicCalculation {
Div,
Add,
Percentage(f32),
RootPercentage(f32),
Pixels(f32),
}

Expand All @@ -191,6 +192,7 @@ impl std::fmt::Display for DynamicCalculation {
DynamicCalculation::Div => f.write_str("/"),
DynamicCalculation::Add => f.write_str("+"),
DynamicCalculation::Percentage(p) => f.write_fmt(format_args!("{p}%")),
DynamicCalculation::RootPercentage(p) => f.write_fmt(format_args!("{p}v")),
DynamicCalculation::Pixels(s) => f.write_fmt(format_args!("{s}")),
}
}
Expand All @@ -200,14 +202,16 @@ impl std::fmt::Display for DynamicCalculation {
struct DynamicCalculationEvaluator<'a> {
calcs: Iter<'a, DynamicCalculation>,
parent_value: f32,
root_value: f32,
current: Option<&'a DynamicCalculation>,
}

impl<'a> DynamicCalculationEvaluator<'a> {
pub fn new(calcs: Iter<'a, DynamicCalculation>, parent_value: f32) -> Self {
pub fn new(calcs: Iter<'a, DynamicCalculation>, parent_value: f32, root_value: f32) -> Self {
Self {
calcs,
parent_value,
root_value,
current: None,
}
}
Expand Down Expand Up @@ -275,6 +279,10 @@ impl<'a> DynamicCalculationEvaluator<'a> {
self.current = self.calcs.next();
Some((self.parent_value / 100.0 * value).round())
}
DynamicCalculation::RootPercentage(value) => {
self.current = self.calcs.next();
Some((self.root_value / 100.0 * value).round())
}
DynamicCalculation::Pixels(value) => {
self.current = self.calcs.next();
Some(*value)
Expand All @@ -295,6 +303,10 @@ impl<'a> DynamicCalculationEvaluator<'a> {

/// Calculate dynamic expression with operator precedence.
/// This value could be for example the width of a node's parent area.
pub fn run_calculations(calcs: &[DynamicCalculation], value: f32) -> Option<f32> {
DynamicCalculationEvaluator::new(calcs.iter(), value).evaluate()
pub fn run_calculations(
calcs: &[DynamicCalculation],
parent_value: f32,
root_value: f32,
) -> Option<f32> {
DynamicCalculationEvaluator::new(calcs.iter(), parent_value, root_value).evaluate()
}
31 changes: 29 additions & 2 deletions crates/torin/tests/size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -783,12 +783,20 @@ pub fn test_calc() {
const PARENT_VALUE: f32 = 500.0;

assert_eq!(
run_calculations(&vec![DynamicCalculation::Pixels(10.0)], PARENT_VALUE),
run_calculations(
&vec![DynamicCalculation::Pixels(10.0)],
PARENT_VALUE,
PARENT_VALUE
),
Some(10.0)
);

assert_eq!(
run_calculations(&vec![DynamicCalculation::Percentage(87.5)], PARENT_VALUE),
run_calculations(
&vec![DynamicCalculation::Percentage(87.5)],
PARENT_VALUE,
PARENT_VALUE
),
Some((87.5 / 100.0 * PARENT_VALUE).round())
);

Expand All @@ -801,6 +809,7 @@ pub fn test_calc() {
DynamicCalculation::Mul,
DynamicCalculation::Percentage(50.0),
],
PARENT_VALUE,
PARENT_VALUE
),
Some(10.0 + 20.0 * (50.0 / 100.0 * PARENT_VALUE).round())
Expand All @@ -821,6 +830,7 @@ pub fn test_calc() {
DynamicCalculation::Mul,
DynamicCalculation::Pixels(2.0),
],
PARENT_VALUE,
PARENT_VALUE
),
Some(10.0 + (10.0 / 100.0 * PARENT_VALUE).round() + 30.0 * 10.0 + 75.0 * 2.0)
Expand All @@ -832,6 +842,7 @@ pub fn test_calc() {
DynamicCalculation::Pixels(10.0),
DynamicCalculation::Pixels(20.0),
],
PARENT_VALUE,
PARENT_VALUE
),
None
Expand All @@ -840,6 +851,7 @@ pub fn test_calc() {
assert_eq!(
run_calculations(
&vec![DynamicCalculation::Pixels(10.0), DynamicCalculation::Add],
PARENT_VALUE,
PARENT_VALUE
),
None
Expand All @@ -848,6 +860,7 @@ pub fn test_calc() {
assert_eq!(
run_calculations(
&vec![DynamicCalculation::Add, DynamicCalculation::Pixels(10.0)],
PARENT_VALUE,
PARENT_VALUE
),
None
Expand All @@ -861,8 +874,22 @@ pub fn test_calc() {
DynamicCalculation::Add,
DynamicCalculation::Pixels(10.0)
],
PARENT_VALUE,
PARENT_VALUE
),
None
);

assert_eq!(
run_calculations(
&vec![
DynamicCalculation::Percentage(50.0),
DynamicCalculation::Sub,
DynamicCalculation::RootPercentage(20.0)
],
PARENT_VALUE,
PARENT_VALUE
),
Some((PARENT_VALUE * 0.5) - (PARENT_VALUE * 0.20))
);
}
Loading