summaryrefslogtreecommitdiff
path: root/src/layout/stack.rs
blob: 7d1d7a12761520f4171948ed1ebfb60c99a6abf8 (plain) (blame)
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
use super::*;

/// A node that stacks its children.
#[derive(Debug, Clone, PartialEq)]
pub struct NodeStack {
    /// The `main` and `cross` directions of this stack.
    ///
    /// The children are stacked along the `main` direction. The `cross`
    /// direction is required for aligning the children.
    pub dirs: LayoutDirs,
    /// How to align this stack in _its_ parent.
    pub align: ChildAlign,
    /// Whether to expand the axes to fill the area or to fit the content.
    pub expand: Spec<Expansion>,
    /// The nodes to be stacked.
    pub children: Vec<Node>,
}

impl Layout for NodeStack {
    fn layout(&self, ctx: &mut LayoutContext, areas: &Areas) -> Layouted {
        let mut layouter = StackLayouter::new(self, areas.clone());
        for child in &self.children {
            match child.layout(ctx, &layouter.areas) {
                Layouted::Spacing(spacing) => layouter.push_spacing(spacing),
                Layouted::Frame(frame, align) => layouter.push_frame(frame, align),
                Layouted::Frames(frames, align) => {
                    for frame in frames {
                        layouter.push_frame(frame, align);
                    }
                }
            }
        }
        Layouted::Frames(layouter.finish(), self.align)
    }
}

impl From<NodeStack> for NodeAny {
    fn from(stack: NodeStack) -> Self {
        Self::new(stack)
    }
}

struct StackLayouter<'a> {
    stack: &'a NodeStack,
    main: SpecAxis,
    dirs: LayoutDirs,
    areas: Areas,
    finished: Vec<Frame>,
    frames: Vec<(Length, Frame, ChildAlign)>,
    used: Gen<Length>,
    ruler: Align,
}

impl<'a> StackLayouter<'a> {
    fn new(stack: &'a NodeStack, areas: Areas) -> Self {
        Self {
            stack,
            main: stack.dirs.main.axis(),
            dirs: stack.dirs,
            areas,
            finished: vec![],
            frames: vec![],
            used: Gen::ZERO,
            ruler: Align::Start,
        }
    }

    fn push_spacing(&mut self, amount: Length) {
        let main_rest = self.areas.current.get_mut(self.main);
        let capped = amount.min(*main_rest);
        *main_rest -= capped;
        self.used.main += capped;
    }

    fn push_frame(&mut self, frame: Frame, align: ChildAlign) {
        if self.ruler > align.main {
            self.finish_area();
        }

        while !self.areas.current.fits(frame.size) {
            if self.areas.in_full_last() {
                // TODO: Diagnose once the necessary spans exist.
                break;
            } else {
                self.finish_area();
            }
        }

        let size = frame.size.switch(self.dirs);
        self.frames.push((self.used.main, frame, align));

        *self.areas.current.get_mut(self.main) -= size.main;
        self.used.main += size.main;
        self.used.cross = self.used.cross.max(size.cross);
        self.ruler = align.main;
    }

    fn finish_area(&mut self) {
        let full_size = {
            let expand = self.stack.expand.switch(self.dirs);
            let full = self.areas.full.switch(self.dirs);
            Gen::new(
                expand.main.resolve(self.used.main.min(full.main), full.main),
                expand.cross.resolve(self.used.cross.min(full.cross), full.cross),
            )
        };

        let mut output = Frame::new(full_size.switch(self.dirs).to_size());

        for (before, frame, align) in std::mem::take(&mut self.frames) {
            let child_size = frame.size.switch(self.dirs);

            // Align along the main axis.
            let main = align.main.resolve(if self.dirs.main.is_positive() {
                let after_with_self = self.used.main - before;
                before .. full_size.main - after_with_self
            } else {
                let before_with_self = before + child_size.main;
                let after = self.used.main - (before + child_size.main);
                full_size.main - before_with_self .. after
            });

            // Align along the cross axis.
            let cross = align.cross.resolve(if self.dirs.cross.is_positive() {
                Length::ZERO .. full_size.cross - child_size.cross
            } else {
                full_size.cross - child_size.cross .. Length::ZERO
            });

            let pos = Gen::new(main, cross).switch(self.dirs).to_point();
            output.push_frame(pos, frame);
        }

        self.finished.push(output);

        self.areas.next();
        self.used = Gen::ZERO;
        self.ruler = Align::Start;
    }

    fn finish(mut self) -> Vec<Frame> {
        self.finish_area();
        self.finished
    }
}