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
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#![no_std]
extern crate alloc;
use alloc::vec::Vec;

use generational_arena::{Arena, Index};

pub type NodeIndex = Index;

pub struct LayoutStyle {
    pub direction: Direction,
}

impl Default for LayoutStyle {
    fn default() -> Self {
        LayoutStyle {
            direction: Direction::LeftRight,
        }
    }
}

pub enum Direction {
    TopBottom,
    LeftRight,
}

#[derive(Debug, PartialEq)]
pub struct Layout {
    pub x: f64,
    pub y: f64,
    pub w: f64,
    pub h: f64,
}

pub struct Node {
    pub layout: Option<Layout>,
    pub style: LayoutStyle,
    pub children: Vec<NodeIndex>,
}

pub struct Shoji {
    nodes: Arena<Node>,
}

pub struct LayoutSize {
    width: Option<f64>,
    height: Option<f64>,
}

impl LayoutSize {
    pub fn new(w: f64, h: f64) -> LayoutSize {
        LayoutSize {
            width: Some(w),
            height: Some(h),
        }
    }
}

impl Shoji {
    pub fn new() -> Self {
        Shoji {
            nodes: Arena::new(),
        }
    }

    pub fn new_node(
        &mut self,
        style: LayoutStyle,
        children: Vec<NodeIndex>,
    ) -> Result<NodeIndex, &'static str> {
        Ok(self.nodes.insert(Node {
            layout: None,
            style,
            children,
        }))
    }

    pub fn get_node(&mut self, node_index: NodeIndex) -> Result<&mut Node, &'static str> {
        Ok(&mut self.nodes[node_index])
    }

    pub fn get_layout(&self, i: NodeIndex) -> Result<&Layout, &'static str> {
        match &self.nodes[i].layout {
            Some(l) => Ok(l),
            None => Err("layout has not been calculated yet"),
        }
    }

    pub fn compute_layout(
        &mut self,
        node_index: NodeIndex,
        s: LayoutSize,
    ) -> Result<(), &'static str> {
        let node = self.get_node(node_index)?;
        node.layout = Some(Layout {
            x: 0.0,
            y: 0.0,
            w: s.width.ok_or("cannot create width from undefined value")?,
            h: s.height
                .ok_or("cannot create height from undefined value")?,
        });
        let children = node.children.clone();
        let num_children = children.len();
        if num_children == 0 {
            // do nothing
            Ok(())
        } else if num_children == 1 {
            self.compute_layout(children[0], s)?;
            let child_node = self.get_node(children[0])?;
            match child_node.layout.as_mut() {
                Some(l) => {
                    l.x = 0.0;
                    l.y = 0.0;
                    Ok(())
                }
                None => return Err("something went wrong"),
            }
        } else {
            match node.style.direction {
                Direction::LeftRight => {
                    let width = s.width;
                    let height = s.height;
                    match width {
                        Some(w) => {
                            let child_width = w / children.len() as f64;
                            for (i, c) in children.iter().enumerate() {
                                self.compute_layout(
                                    *c,
                                    LayoutSize {
                                        width: Some(child_width),
                                        height,
                                    },
                                )?;
                                let child_node = self.get_node(*c)?;
                                match child_node.layout.as_mut() {
                                    Some(l) => {
                                        l.x = i as f64 * child_width;
                                        l.y = 0.0;
                                    }
                                    None => return Err("something went wrong"),
                                }
                            }
                            Ok(())
                        }
                        None => Err("cannot compute layout of LeftRight without defined width"),
                    }
                }
                Direction::TopBottom => {
                    let width = s.width;
                    let height = s.height;
                    match height {
                        Some(h) => {
                            let child_height = h / children.len() as f64;
                            for (i, c) in children.iter().enumerate() {
                                self.compute_layout(
                                    *c,
                                    LayoutSize {
                                        width,
                                        height: Some(child_height),
                                    },
                                )?;
                                let child_node = self.get_node(*c)?;
                                match child_node.layout.as_mut() {
                                    Some(l) => {
                                        l.x = 0.0;
                                        l.y = i as f64 * child_height;
                                    }
                                    None => return Err("something went wrong"),
                                }
                            }
                            Ok(())
                        }
                        None => Err("cannot compute layout of TopBottom without defined width"),
                    }
                }
            }
        }
    }
}

#[cfg(test)]
mod shoji_test;