use std::ops::Deref;
use super::LayoutBox;
use super::boxes::{LayoutBoxIterMut};
use style::{StyledNode};
pub struct LayoutBuffer(Box<[LayoutBox]>);
impl Deref for LayoutBuffer {
type Target = [LayoutBox];
fn deref<'a>(&'a self) -> &'a [LayoutBox] {
self.0.deref()
}
}
impl LayoutBuffer {
pub fn new(style_tree: &StyledNode) -> LayoutBuffer {
let size = style_tree.tree_size();
let mut buffer = Vec::with_capacity(size);
let res = LayoutBuffer::fill_buffer(&mut buffer, style_tree, true);
assert_eq!(res as usize, size);
LayoutBuffer(buffer.into_boxed_slice())
}
pub fn compute_layout(&mut self, max_width: f32, max_height: f32) {
for root in LayoutBoxIterMut::new(&mut self.0) {
root.compute_layout_defaut_width(max_width);
}
for root in LayoutBoxIterMut::new(&mut self.0) {
root.compute_layout_auto_width(max_width);
}
for root in LayoutBoxIterMut::new(&mut self.0) {
root.compute_layout_height_and_position(max_height);
}
}
fn fill_buffer(
vec: &mut Vec<LayoutBox>,
style_tree: &StyledNode,
last_child: bool) -> isize
{
let index = vec.len();
let mut next_sibling: isize = 1;
let mut kids = style_tree.kids.len();
unsafe {
if kids > 0 {
vec.push(LayoutBox::new(style_tree, -1));
} else {
vec.push(LayoutBox::new(style_tree, 0));
}
}
for kid in &style_tree.kids {
kids -= 1;
next_sibling += LayoutBuffer::fill_buffer(vec, kid, kids == 0);
}
if !last_child {
unsafe {
vec.get_unchecked_mut(index).set_next_sibling(next_sibling);
}
}
next_sibling
}
}