use std::time::Instant;
use ui_layout::*;
fn node(display: Display) -> LayoutNode {
LayoutNode::new(Style {
display,
spacing: Spacing {
margin_left: Length::Px(2.0),
margin_right: Length::Px(2.0),
padding_left: Length::Px(1.0),
padding_right: Length::Px(1.0),
border_left: Length::Px(1.0),
border_right: Length::Px(1.0),
..Default::default()
},
size: SizeStyle {
min_width: Length::Px(0.0),
max_width: Length::Px(10_000.0),
..Default::default()
},
..Default::default()
})
}
fn make_flex_chain(depth: usize, max_depth: usize) -> LayoutNode {
let mut root = node(Display::Flex {
flex_direction: FlexDirection::Row,
});
if depth >= max_depth {
root.children.push(node(Display::Block));
return root;
}
let mut block = node(Display::Block);
block.children.push(make_flex_chain(depth + 1, max_depth));
root.children.push(block);
root
}
fn make_branch(depth: usize, max_depth: usize) -> LayoutNode {
let mut flex = node(Display::Flex {
flex_direction: FlexDirection::Column,
});
flex.children.push(make_flex_chain(depth, max_depth));
if depth % 3 == 0 {
let mut side = node(Display::Block);
side.children.push(node(Display::Block));
flex.children.push(side);
}
flex
}
fn make_tree() -> LayoutNode {
let mut root = node(Display::Block);
root.children.push(node(Display::Block));
root.children.push(make_branch(0, 20));
root
}
fn main() {
println!("flex-chain layout benchmark");
println!("----------------------------");
let mut root = make_tree();
let t1 = {
let start = Instant::now();
LayoutEngine::layout(&mut root, 800.0, 600.0);
start.elapsed()
};
println!("1st layout: {:?}", t1);
let t2 = {
let start = Instant::now();
LayoutEngine::layout(&mut root, 800.0, 600.0);
start.elapsed()
};
println!("2nd layout: {:?}", t2);
}