Skip to content

Commit

Permalink
Merge pull request #70 from adamnemecek/pr3
Browse files Browse the repository at this point in the history
refactoring
  • Loading branch information
Emil Sjölander authored May 22, 2020
2 parents 28e0047 + 06110e5 commit 6879b9a
Show file tree
Hide file tree
Showing 13 changed files with 248 additions and 236 deletions.
8 changes: 4 additions & 4 deletions bindings/js/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,8 +396,8 @@ pub struct Allocator {
#[wasm_bindgen]
impl Allocator {
#[wasm_bindgen(constructor)]
pub fn new() -> Allocator {
Allocator { stretch: Rc::new(RefCell::new(stretch::node::Stretch::new())) }
pub fn new() -> Self {
Self { stretch: Rc::new(RefCell::new(stretch::node::Stretch::new())) }
}
}

Expand All @@ -414,8 +414,8 @@ pub struct Node {
#[wasm_bindgen]
impl Node {
#[wasm_bindgen(constructor)]
pub fn new(allocator: &Allocator, style: &JsValue) -> Node {
Node {
pub fn new(allocator: &Allocator, style: &JsValue) -> Self {
Self {
allocator: allocator.clone(),
node: allocator.stretch.borrow_mut().new_node(parse_style(&style), &[]).unwrap(),
style: style.clone(),
Expand Down
2 changes: 1 addition & 1 deletion bindings/swift/StretchCore/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ pub unsafe extern "C" fn stretch_node_set_measure(
*node,
Some(stretch::node::MeasureFunc::Boxed(Box::new(move |constraint| {
let size = measure(swift_ptr, constraint.width.or_else(f32::NAN), constraint.height.or_else(f32::NAN));
Size { width: size.width, height: size.height }
size
}))),
)
.unwrap();
Expand Down
7 changes: 3 additions & 4 deletions examples/basic.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use stretch::geometry::Size;
use stretch::style::*;
use stretch::prelude::*;

fn main() -> Result<(), stretch::Error> {
let mut stretch = stretch::node::Stretch::new();
fn main() -> Result<(), Error> {
let mut stretch = Stretch::new();
let child = stretch.new_node(
Style { size: Size { width: Dimension::Percent(0.5), height: Dimension::Auto }, ..Default::default() },
&[],
Expand Down
108 changes: 38 additions & 70 deletions src/algo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,7 @@ impl Forest {
|| style.max_size.height.is_defined();

let result = if has_root_min_max {
let first_pass = self.compute_internal(
root,
Size { width: style.size.width.resolve(size.width), height: style.size.height.resolve(size.height) },
size,
false,
);
let first_pass = self.compute_internal(root, style.size.resolve(size), size, false);

self.compute_internal(
root,
Expand All @@ -78,31 +73,22 @@ impl Forest {
.width
.maybe_max(style.min_size.width.resolve(size.width))
.maybe_min(style.max_size.width.resolve(size.width))
.to_number(),
.into(),
height: first_pass
.size
.height
.maybe_max(style.min_size.height.resolve(size.height))
.maybe_min(style.max_size.height.resolve(size.height))
.to_number(),
.into(),
},
size,
true,
)
} else {
self.compute_internal(
root,
Size { width: style.size.width.resolve(size.width), height: style.size.height.resolve(size.height) },
size,
true,
)
self.compute_internal(root, style.size.resolve(size), size, true)
};

self.nodes[root].layout = result::Layout {
order: 0,
size: Size { width: result.size.width, height: result.size.height },
location: Point { x: 0.0, y: 0.0 },
};
self.nodes[root].layout = result::Layout { order: 0, size: result.size, location: Point::zero() };

Self::round_layout(&mut self.nodes, &self.children, root, 0.0, 0.0);
}
Expand Down Expand Up @@ -186,8 +172,8 @@ impl Forest {
height: node_size.height - padding_border.vertical(),
};

let mut container_size = Size { width: 0.0, height: 0.0 };
let mut inner_container_size = Size { width: 0.0, height: 0.0 };
let mut container_size = Size::zero();
let mut inner_container_size = Size::zero();

// If this is a leaf node we can skip a lot of this function in some cases
if self.children[node].is_empty() {
Expand Down Expand Up @@ -238,21 +224,9 @@ impl Forest {
.filter(|(_, style)| style.display != Display::None)
.map(|(child, child_style)| FlexItem {
node: *child,

size: Size {
width: child_style.size.width.resolve(node_inner_size.width),
height: child_style.size.height.resolve(node_inner_size.height),
},

min_size: Size {
width: child_style.min_size.width.resolve(node_inner_size.width),
height: child_style.min_size.height.resolve(node_inner_size.height),
},

max_size: Size {
width: child_style.max_size.width.resolve(node_inner_size.width),
height: child_style.max_size.height.resolve(node_inner_size.height),
},
size: child_style.size.resolve(node_inner_size),
min_size: child_style.min_size.resolve(node_inner_size),
max_size: child_style.max_size.resolve(node_inner_size),

position: child_style.position.map(|p| p.resolve(node_inner_size.width)),
margin: child_style.margin.map(|m| m.resolve(node_inner_size.width).or_else(0.0)),
Expand All @@ -264,10 +238,10 @@ impl Forest {
violation: 0.0,
frozen: false,

hypothetical_inner_size: Size { width: 0.0, height: 0.0 },
hypothetical_outer_size: Size { width: 0.0, height: 0.0 },
target_size: Size { width: 0.0, height: 0.0 },
outer_target_size: Size { width: 0.0, height: 0.0 },
hypothetical_inner_size: Size::zero(),
hypothetical_outer_size: Size::zero(),
target_size: Size::zero(),
outer_target_size: Size::zero(),

baseline: 0.0,

Expand Down Expand Up @@ -375,12 +349,12 @@ impl Forest {
// webkit handled various scenarios. Can probably be solved better by passing in
// min-content max-content constraints from the top
let min_main = self
.compute_internal(child.node, Size { width: Undefined, height: Undefined }, available_space, false)
.compute_internal(child.node, Size::undefined(), available_space, false)
.size
.main(dir)
.maybe_max(child.min_size.main(dir))
.maybe_min(child.size.main(dir))
.to_number();
.into();

child
.hypothetical_inner_size
Expand Down Expand Up @@ -620,17 +594,12 @@ impl Forest {
// min-content max-content constraints from the top. Need to figure out correct thing to do here as
// just piling on more conditionals.
let min_main = if is_row && self.nodes[child.node].measure.is_none() {
self.compute_internal(
child.node,
Size { width: Undefined, height: Undefined },
available_space,
false,
)
.size
.width
.maybe_min(child.size.width)
.maybe_max(child.min_size.width)
.to_number()
self.compute_internal(child.node, Size::undefined(), available_space, false)
.size
.width
.maybe_min(child.size.width)
.maybe_max(child.min_size.width)
.into()
} else {
child.min_size.main(dir)
};
Expand Down Expand Up @@ -699,12 +668,12 @@ impl Forest {
self.compute_internal(
child.node,
Size {
width: if is_row { child.target_size.width.to_number() } else { child_cross },
height: if is_row { child_cross } else { child.target_size.height.to_number() },
width: if is_row { child.target_size.width.into() } else { child_cross },
height: if is_row { child_cross } else { child.target_size.height.into() },
},
Size {
width: if is_row { container_size.main(dir).to_number() } else { available_space.width },
height: if is_row { available_space.height } else { container_size.main(dir).to_number() },
width: if is_row { container_size.main(dir).into() } else { available_space.width },
height: if is_row { available_space.height } else { container_size.main(dir).into() },
},
false,
)
Expand Down Expand Up @@ -739,19 +708,19 @@ impl Forest {
child.node,
Size {
width: if is_row {
child.target_size.width.to_number()
child.target_size.width.into()
} else {
child.hypothetical_inner_size.width.to_number()
child.hypothetical_inner_size.width.into()
},
height: if is_row {
child.hypothetical_inner_size.height.to_number()
child.hypothetical_inner_size.height.into()
} else {
child.target_size.height.to_number()
child.target_size.height.into()
},
},
Size {
width: if is_row { container_size.width.to_number() } else { node_size.width },
height: if is_row { node_size.height } else { container_size.height.to_number() },
width: if is_row { container_size.width.into() } else { node_size.width },
height: if is_row { node_size.height } else { container_size.height.into() },
},
true,
);
Expand All @@ -762,7 +731,7 @@ impl Forest {
&result::Layout {
order: self.children[node].iter().position(|n| *n == child.node).unwrap() as u32,
size: result.size,
location: Point { x: 0.0, y: 0.0 },
location: Point::zero(),
},
);
}
Expand Down Expand Up @@ -1149,8 +1118,8 @@ impl Forest {
let layout_item = |child: &mut FlexItem| {
let result = self.compute_internal(
child.node,
child.target_size.map(|s| s.to_number()),
container_size.map(|s| s.to_number()),
child.target_size.map(|s| s.into()),
container_size.map(|s| s.into()),
true,
);

Expand Down Expand Up @@ -1204,8 +1173,8 @@ impl Forest {
.collect::<sys::Vec<_>>();

for (order, child) in candidates {
let container_width = container_size.width.to_number();
let container_height = container_size.height.to_number();
let container_width = container_size.width.into();
let container_height = container_size.height.into();

let child_style = self.nodes[child].style;

Expand Down Expand Up @@ -1325,8 +1294,7 @@ impl Forest {
}

fn hidden_layout(nodes: &mut [NodeData], children: &[sys::ChildrenVec<NodeId>], node: NodeId, order: u32) {
nodes[node].layout =
result::Layout { order, size: Size { width: 0.0, height: 0.0 }, location: Point { x: 0.0, y: 0.0 } };
nodes[node].layout = result::Layout { order, size: Size::zero(), location: Point::zero() };

for (order, child) in children[node].iter().enumerate() {
hidden_layout(nodes, children, *child, order as _);
Expand Down
6 changes: 3 additions & 3 deletions src/forest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ pub(crate) struct NodeData {

impl NodeData {
fn new_leaf(style: Style, measure: MeasureFunc) -> Self {
NodeData { style, measure: Some(measure), layout_cache: None, layout: Layout::new(), is_dirty: true }
Self { style, measure: Some(measure), layout_cache: None, layout: Layout::new(), is_dirty: true }
}

fn new(style: Style) -> Self {
NodeData { style, measure: None, layout_cache: None, layout: Layout::new(), is_dirty: true }
Self { style, measure: None, layout_cache: None, layout: Layout::new(), is_dirty: true }
}
}

Expand All @@ -35,7 +35,7 @@ pub(crate) struct Forest {

impl Forest {
pub fn with_capacity(capacity: usize) -> Self {
Forest {
Self {
nodes: sys::new_vec_with_capacity(capacity),
children: sys::new_vec_with_capacity(capacity),
parents: sys::new_vec_with_capacity(capacity),
Expand Down
Loading

0 comments on commit 6879b9a

Please sign in to comment.