Skip to content

Commit

Permalink
Fix js bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
dflemstr committed Dec 16, 2019
1 parent e7ae3b0 commit 1b40f4a
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 74 deletions.
2 changes: 1 addition & 1 deletion bindings/js/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ default = ["console_error_panic_hook"]
[dependencies]
wasm-bindgen = "0.2"
js-sys = "0.3"
stretch = "0.3.2"
stretch = { version = "0.3.2", path = "../.." }

# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
Expand Down
12 changes: 7 additions & 5 deletions bindings/js/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ impl Node {
pub fn new(allocator: &Allocator, style: &JsValue) -> Node {
Node {
allocator: allocator.clone(),
node: allocator.stretch.borrow_mut().new_node(parse_style(&style), vec![]).unwrap(),
node: allocator.stretch.borrow_mut().new_node(parse_style(&style), &[]).unwrap(),
style: style.clone(),
childCount: 0,
}
Expand All @@ -432,7 +432,9 @@ impl Node {
.borrow_mut()
.set_measure(
self.node,
Some(Box::new(move |constraints| {
Some(stretch::node::MeasureFunc::Boxed(Box::new(move |constraints| {
use stretch::number::OrElse;

let widthConstraint = if let stretch::number::Number::Defined(val) = constraints.width {
val.into()
} else {
Expand All @@ -450,12 +452,12 @@ impl Node {
let height = get_f32(&result, "height");

if width.is_some() && height.is_some() {
return Ok(stretch::geometry::Size { width: width.unwrap(), height: height.unwrap() });
return stretch::geometry::Size { width: width.unwrap(), height: height.unwrap() };
}
}

Err(Box::new("Failed in javascript"))
})),
constraints.map(|v| v.or_else(0.0))
}))),
)
.unwrap();
}
Expand Down
7 changes: 6 additions & 1 deletion src/algo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use core::f32;

use crate::forest::{Forest, NodeData};
use crate::id::NodeId;
use crate::node::MeasureFunc;
use crate::result;
use crate::style::*;
use crate::sys;
Expand Down Expand Up @@ -195,7 +196,11 @@ impl Forest {
}

if let Some(ref measure) = self.nodes[node].measure {
let result = ComputeResult { size: measure(node_size) };
let result = match measure {
MeasureFunc::Raw(measure) => ComputeResult { size: measure(node_size) },
#[cfg(any(feature = "std", feature = "alloc"))]
MeasureFunc::Boxed(measure) => ComputeResult { size: measure(node_size) },
};
self.nodes[node].layout_cache =
Some(result::Cache { node_size, parent_size, perform_layout, result: result.clone() });
return result;
Expand Down
2 changes: 1 addition & 1 deletion src/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl Size<()> {
}

impl<T> Size<T> {
pub(crate) fn map<R, F>(self, f: F) -> Size<R>
pub fn map<R, F>(self, f: F) -> Size<R>
where
F: Fn(T) -> R,
{
Expand Down
6 changes: 5 additions & 1 deletion src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ use crate::style::*;
use crate::sys;
use crate::Error;

pub type MeasureFunc = fn(Size<Number>) -> Size<f32>;
pub enum MeasureFunc {
Raw(fn(Size<Number>) -> Size<f32>),
#[cfg(any(feature = "std", feature = "alloc"))]
Boxed(sys::Box<dyn Fn(Size<Number>) -> Size<f32>>),
}

lazy_static! {
/// Global stretch instance id allocator.
Expand Down
2 changes: 2 additions & 0 deletions src/sys.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#[cfg(feature = "std")]
mod std {
pub type Box<A> = ::std::boxed::Box<A>;
pub type Map<K, V> = ::std::collections::HashMap<K, V>;
pub type Vec<A> = ::std::vec::Vec<A>;
pub type ChildrenVec<A> = ::std::vec::Vec<A>;
Expand All @@ -19,6 +20,7 @@ mod std {

#[cfg(feature = "alloc")]
mod alloc {
pub type Box<A> = ::alloc::boxed::Box<A>;
pub type Map<K, V> = ::hashbrown::HashMap<K, V>;
pub type Vec<A> = ::alloc::vec::Vec<A>;
pub type ChildrenVec<A> = ::alloc::vec::Vec<A>;
Expand Down
156 changes: 94 additions & 62 deletions tests/measure.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
#[cfg(test)]
mod measure {
use stretch::node::MeasureFunc;
use stretch::number::OrElse;

#[test]
fn measure_root() {
let mut stretch = stretch::node::Stretch::new();
let node = stretch
.new_leaf(stretch::style::Style { ..Default::default() }, |constraint| stretch::geometry::Size {
width: constraint.width.or_else(100.0),
height: constraint.height.or_else(100.0),
})
.new_leaf(
stretch::style::Style { ..Default::default() },
MeasureFunc::Raw(|constraint| stretch::geometry::Size {
width: constraint.width.or_else(100.0),
height: constraint.height.or_else(100.0),
}),
)
.unwrap();

stretch.compute_layout(node, stretch::geometry::Size::undefined()).unwrap();
Expand All @@ -23,10 +27,13 @@ mod measure {
let mut stretch = stretch::node::Stretch::new();

let child = stretch
.new_leaf(stretch::style::Style { ..Default::default() }, |constraint| stretch::geometry::Size {
width: constraint.width.or_else(100.0),
height: constraint.height.or_else(100.0),
})
.new_leaf(
stretch::style::Style { ..Default::default() },
MeasureFunc::Raw(|constraint| stretch::geometry::Size {
width: constraint.width.or_else(100.0),
height: constraint.height.or_else(100.0),
}),
)
.unwrap();

let node = stretch.new_node(stretch::style::Style { ..Default::default() }, &[child]).unwrap();
Expand All @@ -43,10 +50,13 @@ mod measure {
fn measure_child_constraint() {
let mut stretch = stretch::node::Stretch::new();
let child = stretch
.new_leaf(stretch::style::Style { ..Default::default() }, |constraint| stretch::geometry::Size {
width: constraint.width.or_else(100.0),
height: constraint.height.or_else(100.0),
})
.new_leaf(
stretch::style::Style { ..Default::default() },
MeasureFunc::Raw(|constraint| stretch::geometry::Size {
width: constraint.width.or_else(100.0),
height: constraint.height.or_else(100.0),
}),
)
.unwrap();

let node = stretch
Expand Down Expand Up @@ -75,10 +85,13 @@ mod measure {
fn measure_child_constraint_padding_parent() {
let mut stretch = stretch::node::Stretch::new();
let child = stretch
.new_leaf(stretch::style::Style { ..Default::default() }, |constraint| stretch::geometry::Size {
width: constraint.width.or_else(100.0),
height: constraint.height.or_else(100.0),
})
.new_leaf(
stretch::style::Style { ..Default::default() },
MeasureFunc::Raw(|constraint| stretch::geometry::Size {
width: constraint.width.or_else(100.0),
height: constraint.height.or_else(100.0),
}),
)
.unwrap();

let node = stretch
Expand Down Expand Up @@ -125,12 +138,13 @@ mod measure {
.unwrap();

let child1 = stretch
.new_leaf(stretch::style::Style { flex_grow: 1.0, ..Default::default() }, |constraint| {
stretch::geometry::Size {
.new_leaf(
stretch::style::Style { flex_grow: 1.0, ..Default::default() },
MeasureFunc::Raw(|constraint| stretch::geometry::Size {
width: constraint.width.or_else(10.0),
height: constraint.height.or_else(50.0),
}
})
}),
)
.unwrap();

let node = stretch
Expand Down Expand Up @@ -170,10 +184,13 @@ mod measure {
.unwrap();

let child1 = stretch
.new_leaf(stretch::style::Style { ..Default::default() }, |constraint| stretch::geometry::Size {
width: constraint.width.or_else(100.0),
height: constraint.height.or_else(50.0),
})
.new_leaf(
stretch::style::Style { ..Default::default() },
MeasureFunc::Raw(|constraint| stretch::geometry::Size {
width: constraint.width.or_else(100.0),
height: constraint.height.or_else(50.0),
}),
)
.unwrap();

let node = stretch
Expand Down Expand Up @@ -212,11 +229,14 @@ mod measure {
.unwrap();

let child1 = stretch
.new_leaf(stretch::style::Style { flex_grow: 1.0, ..Default::default() }, |constraint| {
let width = constraint.width.or_else(10.0);
let height = constraint.height.or_else(width * 2.0);
stretch::geometry::Size { width, height }
})
.new_leaf(
stretch::style::Style { flex_grow: 1.0, ..Default::default() },
MeasureFunc::Raw(|constraint| {
let width = constraint.width.or_else(10.0);
let height = constraint.height.or_else(width * 2.0);
stretch::geometry::Size { width, height }
}),
)
.unwrap();

let node = stretch
Expand Down Expand Up @@ -258,11 +278,14 @@ mod measure {
.unwrap();

let child1 = stretch
.new_leaf(stretch::style::Style { ..Default::default() }, |constraint| {
let width = constraint.width.or_else(100.0);
let height = constraint.height.or_else(width * 2.0);
stretch::geometry::Size { width, height }
})
.new_leaf(
stretch::style::Style { ..Default::default() },
MeasureFunc::Raw(|constraint| {
let width = constraint.width.or_else(100.0);
let height = constraint.height.or_else(width * 2.0);
stretch::geometry::Size { width, height }
}),
)
.unwrap();

let node = stretch
Expand Down Expand Up @@ -290,11 +313,14 @@ mod measure {
let mut stretch = stretch::node::Stretch::new();

let child = stretch
.new_leaf(stretch::style::Style { ..Default::default() }, |constraint| {
let height = constraint.height.or_else(50.0);
let width = constraint.width.or_else(height);
stretch::geometry::Size { width, height }
})
.new_leaf(
stretch::style::Style { ..Default::default() },
MeasureFunc::Raw(|constraint| {
let height = constraint.height.or_else(50.0);
let width = constraint.width.or_else(height);
stretch::geometry::Size { width, height }
}),
)
.unwrap();

let node = stretch
Expand Down Expand Up @@ -328,10 +354,10 @@ mod measure {
},
..Default::default()
},
|constraint| stretch::geometry::Size {
MeasureFunc::Raw(|constraint| stretch::geometry::Size {
width: constraint.width.or_else(100.0),
height: constraint.height.or_else(100.0),
},
}),
)
.unwrap();

Expand All @@ -354,10 +380,10 @@ mod measure {
},
..Default::default()
},
|constraint| stretch::geometry::Size {
MeasureFunc::Raw(|constraint| stretch::geometry::Size {
width: constraint.width.or_else(100.0),
height: constraint.height.or_else(100.0),
},
}),
)
.unwrap();

Expand Down Expand Up @@ -389,10 +415,10 @@ mod measure {
flex_grow: 1.0,
..Default::default()
},
|constraint| stretch::geometry::Size {
MeasureFunc::Raw(|constraint| stretch::geometry::Size {
width: constraint.width.or_else(100.0),
height: constraint.height.or_else(100.0),
},
}),
)
.unwrap();

Expand Down Expand Up @@ -421,10 +447,13 @@ mod measure {
fn stretch_overrides_measure() {
let mut stretch = stretch::node::Stretch::new();
let child = stretch
.new_leaf(stretch::style::Style { ..Default::default() }, |constraint| stretch::geometry::Size {
width: constraint.width.or_else(50.0),
height: constraint.height.or_else(50.0),
})
.new_leaf(
stretch::style::Style { ..Default::default() },
MeasureFunc::Raw(|constraint| stretch::geometry::Size {
width: constraint.width.or_else(50.0),
height: constraint.height.or_else(50.0),
}),
)
.unwrap();

let node = stretch
Expand Down Expand Up @@ -452,10 +481,10 @@ mod measure {
let child = stretch
.new_leaf(
stretch::style::Style { position_type: stretch::style::PositionType::Absolute, ..Default::default() },
|constraint| stretch::geometry::Size {
MeasureFunc::Raw(|constraint| stretch::geometry::Size {
width: constraint.width.or_else(50.0),
height: constraint.height.or_else(50.0),
},
}),
)
.unwrap();

Expand All @@ -482,10 +511,10 @@ mod measure {
fn ignore_invalid_measure() {
let mut stretch = stretch::node::Stretch::new();
let child = stretch
.new_leaf(stretch::style::Style { flex_grow: 1.0, ..Default::default() }, |_| stretch::geometry::Size {
width: 200.0,
height: 200.0,
})
.new_leaf(
stretch::style::Style { flex_grow: 1.0, ..Default::default() },
MeasureFunc::Raw(|_| stretch::geometry::Size { width: 200.0, height: 200.0 }),
)
.unwrap();

let node = stretch
Expand Down Expand Up @@ -515,13 +544,16 @@ mod measure {
static NUM_MEASURES: atomic::AtomicU32 = atomic::AtomicU32::new(0);

let grandchild = stretch
.new_leaf(stretch::style::Style { ..Default::default() }, |constraint| {
NUM_MEASURES.fetch_add(1, atomic::Ordering::Relaxed);
stretch::geometry::Size {
width: constraint.width.or_else(50.0),
height: constraint.height.or_else(50.0),
}
})
.new_leaf(
stretch::style::Style { ..Default::default() },
MeasureFunc::Raw(|constraint| {
NUM_MEASURES.fetch_add(1, atomic::Ordering::Relaxed);
stretch::geometry::Size {
width: constraint.width.or_else(50.0),
height: constraint.height.or_else(50.0),
}
}),
)
.unwrap();

let child = stretch.new_node(stretch::style::Style { ..Default::default() }, &[grandchild]).unwrap();
Expand Down
Loading

0 comments on commit 1b40f4a

Please sign in to comment.