summaryrefslogtreecommitdiffstats
path: root/src/tuice/context.rs
blob: 0de909cb23a8c10d82f603f26388ccaafceb2e39 (plain)
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
use tui::layout::Rect;

use super::LayoutNode;

pub struct DrawContext<'a> {
    current_node: &'a LayoutNode,
    current_offset: (u16, u16),
}

impl<'a> DrawContext<'_> {
    /// Creates a new [`DrawContext`], with the offset set to `(0, 0)`.
    pub(crate) fn root(root: &'a LayoutNode) -> DrawContext<'a> {
        DrawContext {
            current_node: root,
            current_offset: (0, 0),
        }
    }

    pub(crate) fn rect(&self) -> Rect {
        let mut rect = self.current_node.rect;
        rect.x += self.current_offset.0;
        rect.y += self.current_offset.1;

        rect
    }

    pub(crate) fn children(&self) -> impl Iterator<Item = DrawContext<'_>> {
        let new_offset = (
            self.current_offset.0 + self.current_node.rect.x,
            self.current_offset.1 + self.current_node.rect.y,
        );

        self.current_node
            .children
            .iter()
            .map(move |layout_node| DrawContext {
                current_node: layout_node,
                current_offset: new_offset,
            })
    }
}