summaryrefslogtreecommitdiff
path: root/src/model/collapse.rs
blob: 258f577ebf4cc8ac6247b01e6db6ccd5c71758e0 (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
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
use super::{StyleChain, StyleVec, StyleVecBuilder};

/// A wrapper around a [`StyleVecBuilder`] that allows to collapse items.
pub struct CollapsingBuilder<'a, T> {
    /// The internal builder.
    builder: StyleVecBuilder<'a, T>,
    /// Staged weak and ignorant items that we can't yet commit to the builder.
    /// The option is `Some(_)` for weak items and `None` for ignorant items.
    staged: Vec<(T, StyleChain<'a>, Option<u8>)>,
    /// What the last non-ignorant item was.
    last: Last,
}

/// What the last non-ignorant item was.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
enum Last {
    Weak,
    Destructive,
    Supportive,
}

impl<'a, T> CollapsingBuilder<'a, T> {
    /// Create a new style-vec builder.
    pub fn new() -> Self {
        Self {
            builder: StyleVecBuilder::new(),
            staged: vec![],
            last: Last::Destructive,
        }
    }

    /// Whether the builder is empty.
    pub fn is_empty(&self) -> bool {
        self.builder.is_empty() && self.staged.is_empty()
    }

    /// Can only exist when there is at least one supportive item to its left
    /// and to its right, with no destructive items in between. There may be
    /// ignorant items in between in both directions.
    ///
    /// Between weak items, there may be at least one per layer and among the
    /// candidates the strongest one (smallest `weakness`) wins. When tied,
    /// the one that compares larger through `PartialOrd` wins.
    pub fn weak(&mut self, item: T, styles: StyleChain<'a>, weakness: u8)
    where
        T: PartialOrd,
    {
        if self.last == Last::Destructive {
            return;
        }

        if self.last == Last::Weak {
            if let Some(i) =
                self.staged.iter().position(|(prev_item, _, prev_weakness)| {
                    prev_weakness.map_or(false, |prev_weakness| {
                        weakness < prev_weakness
                            || (weakness == prev_weakness && item > *prev_item)
                    })
                })
            {
                self.staged.remove(i);
            } else {
                return;
            }
        }

        self.staged.push((item, styles, Some(weakness)));
        self.last = Last::Weak;
    }

    /// Forces nearby weak items to collapse.
    pub fn destructive(&mut self, item: T, styles: StyleChain<'a>) {
        self.flush(false);
        self.builder.push(item, styles);
        self.last = Last::Destructive;
    }

    /// Allows nearby weak items to exist.
    pub fn supportive(&mut self, item: T, styles: StyleChain<'a>) {
        self.flush(true);
        self.builder.push(item, styles);
        self.last = Last::Supportive;
    }

    /// Has no influence on other items.
    pub fn ignorant(&mut self, item: T, styles: StyleChain<'a>) {
        self.staged.push((item, styles, None));
    }

    /// Iterate over the contained items.
    pub fn items(&self) -> impl DoubleEndedIterator<Item = &T> {
        self.builder.items().chain(self.staged.iter().map(|(item, ..)| item))
    }

    /// Return the finish style vec and the common prefix chain.
    pub fn finish(mut self) -> (StyleVec<T>, StyleChain<'a>) {
        self.flush(false);
        self.builder.finish()
    }

    /// Push the staged items, filtering out weak items if `supportive` is
    /// false.
    fn flush(&mut self, supportive: bool) {
        for (item, styles, meta) in self.staged.drain(..) {
            if supportive || meta.is_none() {
                self.builder.push(item, styles);
            }
        }
    }
}

impl<'a, T> Default for CollapsingBuilder<'a, T> {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::library::layout::FlowChild;
    use crate::library::prelude::*;

    #[track_caller]
    fn test<T>(builder: CollapsingBuilder<T>, expected: &[T])
    where
        T: Debug + PartialEq,
    {
        let result = builder.finish().0;
        let items: Vec<_> = result.items().collect();
        let expected: Vec<_> = expected.iter().collect();
        assert_eq!(items, expected);
    }

    fn node() -> FlowChild {
        FlowChild::Node(Content::Text("Hi".into()).pack())
    }

    fn abs(pt: f64) -> FlowChild {
        FlowChild::Spacing(Length::pt(pt).into())
    }

    #[test]
    fn test_collapsing_weak() {
        let mut builder = CollapsingBuilder::new();
        let styles = StyleChain::default();
        builder.weak(FlowChild::Colbreak, styles, 0);
        builder.supportive(node(), styles);
        builder.weak(abs(10.0), styles, 0);
        builder.ignorant(FlowChild::Colbreak, styles);
        builder.weak(abs(20.0), styles, 0);
        builder.supportive(node(), styles);
        builder.weak(abs(10.0), styles, 0);
        builder.weak(abs(20.0), styles, 1);
        builder.supportive(node(), styles);
        test(builder, &[
            node(),
            FlowChild::Colbreak,
            abs(20.0),
            node(),
            abs(10.0),
            node(),
        ]);
    }

    #[test]
    fn test_collapsing_destructive() {
        let mut builder = CollapsingBuilder::new();
        let styles = StyleChain::default();
        builder.supportive(node(), styles);
        builder.weak(abs(10.0), styles, 0);
        builder.destructive(FlowChild::Colbreak, styles);
        builder.weak(abs(20.0), styles, 0);
        builder.supportive(node(), styles);
        test(builder, &[node(), FlowChild::Colbreak, node()]);
    }
}