Skip to content

Commit 1b40f4a

Browse files
committed
Fix js bindings
1 parent e7ae3b0 commit 1b40f4a

File tree

8 files changed

+120
-74
lines changed

8 files changed

+120
-74
lines changed

bindings/js/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ default = ["console_error_panic_hook"]
1313
[dependencies]
1414
wasm-bindgen = "0.2"
1515
js-sys = "0.3"
16-
stretch = "0.3.2"
16+
stretch = { version = "0.3.2", path = "../.." }
1717

1818
# The `console_error_panic_hook` crate provides better debugging of panics by
1919
# logging them with `console.error`. This is great for development, but requires

bindings/js/src/lib.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ impl Node {
417417
pub fn new(allocator: &Allocator, style: &JsValue) -> Node {
418418
Node {
419419
allocator: allocator.clone(),
420-
node: allocator.stretch.borrow_mut().new_node(parse_style(&style), vec![]).unwrap(),
420+
node: allocator.stretch.borrow_mut().new_node(parse_style(&style), &[]).unwrap(),
421421
style: style.clone(),
422422
childCount: 0,
423423
}
@@ -432,7 +432,9 @@ impl Node {
432432
.borrow_mut()
433433
.set_measure(
434434
self.node,
435-
Some(Box::new(move |constraints| {
435+
Some(stretch::node::MeasureFunc::Boxed(Box::new(move |constraints| {
436+
use stretch::number::OrElse;
437+
436438
let widthConstraint = if let stretch::number::Number::Defined(val) = constraints.width {
437439
val.into()
438440
} else {
@@ -450,12 +452,12 @@ impl Node {
450452
let height = get_f32(&result, "height");
451453

452454
if width.is_some() && height.is_some() {
453-
return Ok(stretch::geometry::Size { width: width.unwrap(), height: height.unwrap() });
455+
return stretch::geometry::Size { width: width.unwrap(), height: height.unwrap() };
454456
}
455457
}
456458

457-
Err(Box::new("Failed in javascript"))
458-
})),
459+
constraints.map(|v| v.or_else(0.0))
460+
}))),
459461
)
460462
.unwrap();
461463
}

src/algo.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use core::f32;
22

33
use crate::forest::{Forest, NodeData};
44
use crate::id::NodeId;
5+
use crate::node::MeasureFunc;
56
use crate::result;
67
use crate::style::*;
78
use crate::sys;
@@ -195,7 +196,11 @@ impl Forest {
195196
}
196197

197198
if let Some(ref measure) = self.nodes[node].measure {
198-
let result = ComputeResult { size: measure(node_size) };
199+
let result = match measure {
200+
MeasureFunc::Raw(measure) => ComputeResult { size: measure(node_size) },
201+
#[cfg(any(feature = "std", feature = "alloc"))]
202+
MeasureFunc::Boxed(measure) => ComputeResult { size: measure(node_size) },
203+
};
199204
self.nodes[node].layout_cache =
200205
Some(result::Cache { node_size, parent_size, perform_layout, result: result.clone() });
201206
return result;

src/geometry.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ impl Size<()> {
101101
}
102102

103103
impl<T> Size<T> {
104-
pub(crate) fn map<R, F>(self, f: F) -> Size<R>
104+
pub fn map<R, F>(self, f: F) -> Size<R>
105105
where
106106
F: Fn(T) -> R,
107107
{

src/node.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ use crate::style::*;
99
use crate::sys;
1010
use crate::Error;
1111

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

1418
lazy_static! {
1519
/// Global stretch instance id allocator.

src/sys.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#[cfg(feature = "std")]
22
mod std {
3+
pub type Box<A> = ::std::boxed::Box<A>;
34
pub type Map<K, V> = ::std::collections::HashMap<K, V>;
45
pub type Vec<A> = ::std::vec::Vec<A>;
56
pub type ChildrenVec<A> = ::std::vec::Vec<A>;
@@ -19,6 +20,7 @@ mod std {
1920

2021
#[cfg(feature = "alloc")]
2122
mod alloc {
23+
pub type Box<A> = ::alloc::boxed::Box<A>;
2224
pub type Map<K, V> = ::hashbrown::HashMap<K, V>;
2325
pub type Vec<A> = ::alloc::vec::Vec<A>;
2426
pub type ChildrenVec<A> = ::alloc::vec::Vec<A>;

tests/measure.rs

Lines changed: 94 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
#[cfg(test)]
22
mod measure {
3+
use stretch::node::MeasureFunc;
34
use stretch::number::OrElse;
45

56
#[test]
67
fn measure_root() {
78
let mut stretch = stretch::node::Stretch::new();
89
let node = stretch
9-
.new_leaf(stretch::style::Style { ..Default::default() }, |constraint| stretch::geometry::Size {
10-
width: constraint.width.or_else(100.0),
11-
height: constraint.height.or_else(100.0),
12-
})
10+
.new_leaf(
11+
stretch::style::Style { ..Default::default() },
12+
MeasureFunc::Raw(|constraint| stretch::geometry::Size {
13+
width: constraint.width.or_else(100.0),
14+
height: constraint.height.or_else(100.0),
15+
}),
16+
)
1317
.unwrap();
1418

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

2529
let child = stretch
26-
.new_leaf(stretch::style::Style { ..Default::default() }, |constraint| stretch::geometry::Size {
27-
width: constraint.width.or_else(100.0),
28-
height: constraint.height.or_else(100.0),
29-
})
30+
.new_leaf(
31+
stretch::style::Style { ..Default::default() },
32+
MeasureFunc::Raw(|constraint| stretch::geometry::Size {
33+
width: constraint.width.or_else(100.0),
34+
height: constraint.height.or_else(100.0),
35+
}),
36+
)
3037
.unwrap();
3138

3239
let node = stretch.new_node(stretch::style::Style { ..Default::default() }, &[child]).unwrap();
@@ -43,10 +50,13 @@ mod measure {
4350
fn measure_child_constraint() {
4451
let mut stretch = stretch::node::Stretch::new();
4552
let child = stretch
46-
.new_leaf(stretch::style::Style { ..Default::default() }, |constraint| stretch::geometry::Size {
47-
width: constraint.width.or_else(100.0),
48-
height: constraint.height.or_else(100.0),
49-
})
53+
.new_leaf(
54+
stretch::style::Style { ..Default::default() },
55+
MeasureFunc::Raw(|constraint| stretch::geometry::Size {
56+
width: constraint.width.or_else(100.0),
57+
height: constraint.height.or_else(100.0),
58+
}),
59+
)
5060
.unwrap();
5161

5262
let node = stretch
@@ -75,10 +85,13 @@ mod measure {
7585
fn measure_child_constraint_padding_parent() {
7686
let mut stretch = stretch::node::Stretch::new();
7787
let child = stretch
78-
.new_leaf(stretch::style::Style { ..Default::default() }, |constraint| stretch::geometry::Size {
79-
width: constraint.width.or_else(100.0),
80-
height: constraint.height.or_else(100.0),
81-
})
88+
.new_leaf(
89+
stretch::style::Style { ..Default::default() },
90+
MeasureFunc::Raw(|constraint| stretch::geometry::Size {
91+
width: constraint.width.or_else(100.0),
92+
height: constraint.height.or_else(100.0),
93+
}),
94+
)
8295
.unwrap();
8396

8497
let node = stretch
@@ -125,12 +138,13 @@ mod measure {
125138
.unwrap();
126139

127140
let child1 = stretch
128-
.new_leaf(stretch::style::Style { flex_grow: 1.0, ..Default::default() }, |constraint| {
129-
stretch::geometry::Size {
141+
.new_leaf(
142+
stretch::style::Style { flex_grow: 1.0, ..Default::default() },
143+
MeasureFunc::Raw(|constraint| stretch::geometry::Size {
130144
width: constraint.width.or_else(10.0),
131145
height: constraint.height.or_else(50.0),
132-
}
133-
})
146+
}),
147+
)
134148
.unwrap();
135149

136150
let node = stretch
@@ -170,10 +184,13 @@ mod measure {
170184
.unwrap();
171185

172186
let child1 = stretch
173-
.new_leaf(stretch::style::Style { ..Default::default() }, |constraint| stretch::geometry::Size {
174-
width: constraint.width.or_else(100.0),
175-
height: constraint.height.or_else(50.0),
176-
})
187+
.new_leaf(
188+
stretch::style::Style { ..Default::default() },
189+
MeasureFunc::Raw(|constraint| stretch::geometry::Size {
190+
width: constraint.width.or_else(100.0),
191+
height: constraint.height.or_else(50.0),
192+
}),
193+
)
177194
.unwrap();
178195

179196
let node = stretch
@@ -212,11 +229,14 @@ mod measure {
212229
.unwrap();
213230

214231
let child1 = stretch
215-
.new_leaf(stretch::style::Style { flex_grow: 1.0, ..Default::default() }, |constraint| {
216-
let width = constraint.width.or_else(10.0);
217-
let height = constraint.height.or_else(width * 2.0);
218-
stretch::geometry::Size { width, height }
219-
})
232+
.new_leaf(
233+
stretch::style::Style { flex_grow: 1.0, ..Default::default() },
234+
MeasureFunc::Raw(|constraint| {
235+
let width = constraint.width.or_else(10.0);
236+
let height = constraint.height.or_else(width * 2.0);
237+
stretch::geometry::Size { width, height }
238+
}),
239+
)
220240
.unwrap();
221241

222242
let node = stretch
@@ -258,11 +278,14 @@ mod measure {
258278
.unwrap();
259279

260280
let child1 = stretch
261-
.new_leaf(stretch::style::Style { ..Default::default() }, |constraint| {
262-
let width = constraint.width.or_else(100.0);
263-
let height = constraint.height.or_else(width * 2.0);
264-
stretch::geometry::Size { width, height }
265-
})
281+
.new_leaf(
282+
stretch::style::Style { ..Default::default() },
283+
MeasureFunc::Raw(|constraint| {
284+
let width = constraint.width.or_else(100.0);
285+
let height = constraint.height.or_else(width * 2.0);
286+
stretch::geometry::Size { width, height }
287+
}),
288+
)
266289
.unwrap();
267290

268291
let node = stretch
@@ -290,11 +313,14 @@ mod measure {
290313
let mut stretch = stretch::node::Stretch::new();
291314

292315
let child = stretch
293-
.new_leaf(stretch::style::Style { ..Default::default() }, |constraint| {
294-
let height = constraint.height.or_else(50.0);
295-
let width = constraint.width.or_else(height);
296-
stretch::geometry::Size { width, height }
297-
})
316+
.new_leaf(
317+
stretch::style::Style { ..Default::default() },
318+
MeasureFunc::Raw(|constraint| {
319+
let height = constraint.height.or_else(50.0);
320+
let width = constraint.width.or_else(height);
321+
stretch::geometry::Size { width, height }
322+
}),
323+
)
298324
.unwrap();
299325

300326
let node = stretch
@@ -328,10 +354,10 @@ mod measure {
328354
},
329355
..Default::default()
330356
},
331-
|constraint| stretch::geometry::Size {
357+
MeasureFunc::Raw(|constraint| stretch::geometry::Size {
332358
width: constraint.width.or_else(100.0),
333359
height: constraint.height.or_else(100.0),
334-
},
360+
}),
335361
)
336362
.unwrap();
337363

@@ -354,10 +380,10 @@ mod measure {
354380
},
355381
..Default::default()
356382
},
357-
|constraint| stretch::geometry::Size {
383+
MeasureFunc::Raw(|constraint| stretch::geometry::Size {
358384
width: constraint.width.or_else(100.0),
359385
height: constraint.height.or_else(100.0),
360-
},
386+
}),
361387
)
362388
.unwrap();
363389

@@ -389,10 +415,10 @@ mod measure {
389415
flex_grow: 1.0,
390416
..Default::default()
391417
},
392-
|constraint| stretch::geometry::Size {
418+
MeasureFunc::Raw(|constraint| stretch::geometry::Size {
393419
width: constraint.width.or_else(100.0),
394420
height: constraint.height.or_else(100.0),
395-
},
421+
}),
396422
)
397423
.unwrap();
398424

@@ -421,10 +447,13 @@ mod measure {
421447
fn stretch_overrides_measure() {
422448
let mut stretch = stretch::node::Stretch::new();
423449
let child = stretch
424-
.new_leaf(stretch::style::Style { ..Default::default() }, |constraint| stretch::geometry::Size {
425-
width: constraint.width.or_else(50.0),
426-
height: constraint.height.or_else(50.0),
427-
})
450+
.new_leaf(
451+
stretch::style::Style { ..Default::default() },
452+
MeasureFunc::Raw(|constraint| stretch::geometry::Size {
453+
width: constraint.width.or_else(50.0),
454+
height: constraint.height.or_else(50.0),
455+
}),
456+
)
428457
.unwrap();
429458

430459
let node = stretch
@@ -452,10 +481,10 @@ mod measure {
452481
let child = stretch
453482
.new_leaf(
454483
stretch::style::Style { position_type: stretch::style::PositionType::Absolute, ..Default::default() },
455-
|constraint| stretch::geometry::Size {
484+
MeasureFunc::Raw(|constraint| stretch::geometry::Size {
456485
width: constraint.width.or_else(50.0),
457486
height: constraint.height.or_else(50.0),
458-
},
487+
}),
459488
)
460489
.unwrap();
461490

@@ -482,10 +511,10 @@ mod measure {
482511
fn ignore_invalid_measure() {
483512
let mut stretch = stretch::node::Stretch::new();
484513
let child = stretch
485-
.new_leaf(stretch::style::Style { flex_grow: 1.0, ..Default::default() }, |_| stretch::geometry::Size {
486-
width: 200.0,
487-
height: 200.0,
488-
})
514+
.new_leaf(
515+
stretch::style::Style { flex_grow: 1.0, ..Default::default() },
516+
MeasureFunc::Raw(|_| stretch::geometry::Size { width: 200.0, height: 200.0 }),
517+
)
489518
.unwrap();
490519

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

517546
let grandchild = stretch
518-
.new_leaf(stretch::style::Style { ..Default::default() }, |constraint| {
519-
NUM_MEASURES.fetch_add(1, atomic::Ordering::Relaxed);
520-
stretch::geometry::Size {
521-
width: constraint.width.or_else(50.0),
522-
height: constraint.height.or_else(50.0),
523-
}
524-
})
547+
.new_leaf(
548+
stretch::style::Style { ..Default::default() },
549+
MeasureFunc::Raw(|constraint| {
550+
NUM_MEASURES.fetch_add(1, atomic::Ordering::Relaxed);
551+
stretch::geometry::Size {
552+
width: constraint.width.or_else(50.0),
553+
height: constraint.height.or_else(50.0),
554+
}
555+
}),
556+
)
525557
.unwrap();
526558

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

0 commit comments

Comments
 (0)