1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
pub mod default_layout_engine;

use crate::widget::{unit::WidgetUnit, utils::Rect, WidgetId};
use std::collections::HashMap;

pub trait LayoutEngine<E> {
    fn layout(&mut self, ui_space: Rect, tree: &WidgetUnit) -> Result<Layout, E>;
}

#[derive(Debug, Default, Clone)]
pub struct Layout {
    pub ui_space: Rect,
    pub items: HashMap<WidgetId, LayoutItem>,
}

#[derive(Debug, Default, Clone)]
pub struct LayoutNode {
    pub id: WidgetId,
    pub local_space: Rect,
    pub children: Vec<LayoutNode>,
}

impl LayoutNode {
    pub fn count(&self) -> usize {
        1 + self.children.iter().map(Self::count).sum::<usize>()
    }
}

#[derive(Debug, Default, Copy, Clone)]
pub struct LayoutItem {
    pub local_space: Rect,
    pub ui_space: Rect,
}

impl LayoutEngine<()> for () {
    fn layout(&mut self, ui_space: Rect, _: &WidgetUnit) -> Result<Layout, ()> {
        Ok(Layout {
            ui_space,
            items: Default::default(),
        })
    }
}