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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
use std::collections::HashMap;

use cassowary::strength::*;
use cassowary::WeightedRelation::*;
use cassowary::{Variable, Constraint};

use super::{LayoutId, LayoutVars, Layout, LayoutContainer};
use super::constraint::*;

#[derive(Debug, Copy, Clone,PartialEq)]
pub enum Spacing {
    /// Equal spacing before, after and between all elements
    Around,
    /// Equal spacing between elements, with no extra space on the ends
    Between,
    /// Elements packed together at the start, leaving space at the end
    End,
    /// Elements packed together at the end, leaving space at the start
    Start,
}

#[derive(Debug, Copy, Clone)]
pub enum ItemAlignment {
    /// No constraints are added, items can be aligned individually
    None,
    /// Item width/height matched layout parent
    Fill,
    /// Items centered within layout parent width/height
    Center,
    /// For a vertical layout, align items to the parent's left bound.
    /// Do not use in a horizontal layout
    Left,
    /// For a vertical layout, align items to the parent's right bound.
    /// Do not use in a horizontal layout
    Right,
    /// For a horizontal layout, align items to the parent's top bound.
    /// Do not use in a vertical layout
    Top,
    /// For a horizontal layout, align items to the parent's bottom bound.
    /// Do not use in a vertical layout
    Bottom,
}

#[derive(Debug, Copy, Clone)]
pub struct LinearLayoutSettings {
    /// Horizontal or vertical orientation of the current layout
    pub orientation: Orientation,
    /// Specifies the extra space between elements along the primary axis
    pub spacing: Spacing,
    /// Specifies alignment of items perpendicular to the primary axis
    pub item_align: ItemAlignment,
    /// Constrain items to have equal size along primary axis, and fill container
    pub fill_equal: bool,
    /// Minimum spacing between items
    pub padding: f32,
}

impl LinearLayoutSettings {

    /// Creates a default `LinearLayoutSettings`, aligned to the
    /// end of the parent container
    pub fn new(orientation: Orientation) -> Self {
        LinearLayoutSettings {
            orientation: orientation,
            spacing: Spacing::End,
            item_align: ItemAlignment::None,
            fill_equal: false,
            padding: 0.0,
        }
    }
}

#[derive(Debug, Copy, Clone)]
pub enum Orientation {
    Horizontal,
    Vertical,
}

#[derive(Debug, Clone)]
struct WidgetData {
    start: Variable,
    end: Variable,
    prev: Option<LayoutId>,
    next: Option<LayoutId>,
    end_constraint: Option<Constraint>,
}

pub struct LinearLayout {
    settings: LinearLayoutSettings,
    start: Variable,
    end: Variable,
    space: Variable,
    size: Option<Variable>,
    widgets: HashMap<LayoutId, WidgetData>,
    last_widget: Option<LayoutId>,
}

impl LinearLayout {
    pub fn new(parent: &mut Layout, settings: LinearLayoutSettings) -> Self {
        let start = Variable::new();
        let end = Variable::new();
        parent.add_associated_var(start, "linear_layout_start");
        parent.add_associated_var(end, "linear_layout_end");
        let space = Variable::new();
        parent.add_associated_var(space, "linear_layout_space");
        match settings.spacing {
            Spacing::Between | Spacing::Around => parent.add(space | GE(REQUIRED) | settings.padding),
            _ => parent.add(space | EQ(REQUIRED) | settings.padding)
        };
        let parent_start = beginning(settings.orientation, &parent.vars);
        let parent_end = ending(settings.orientation, &parent.vars);
        match settings.spacing {
            Spacing::Around => {
                parent.add(constraints![
                    start | EQ(REQUIRED) | parent_start + space,
                    end | EQ(REQUIRED) | parent_end - space,
                ]);
            },
            _ => {
                parent.add(constraints![
                    start | EQ(REQUIRED) | parent_start,
                    end | EQ(REQUIRED) | parent_end,
                ]);
            }
        }

        let size = if settings.fill_equal {
            let size = Variable::new();
            parent.add_associated_var(size, "linear_layout_size");
            let parent_size = axis_length(settings.orientation, &parent.vars);
            parent.add(parent_size | EQ(STRONG) | size);
            Some(size)
        } else {
            None
        };
        LinearLayout {
            settings: settings,
            start: start,
            end: end,
            space: space,
            size: size,
            widgets: HashMap::new(),
            last_widget: None,
        }
    }
}

impl LayoutContainer for LinearLayout {
    fn add_child(&mut self, parent: &mut Layout, child: &mut Layout) {

        let child_start = beginning(self.settings.orientation, &child.vars);
        let child_end = ending(self.settings.orientation, &child.vars);

        parent.add(child_start | GE(REQUIRED) | self.start);
        parent.add(child_end | LE(REQUIRED) | self.end);

        if let Some(last_id) = self.last_widget {
            let last_widget = self.widgets.get_mut(&last_id).unwrap();
            parent.add(child_start | EQ(REQUIRED) | last_widget.end + self.space);
            last_widget.next = Some(child.id);
        } else {
            if self.settings.spacing != Spacing::Start {
                parent.add(child_start | EQ(REQUIRED) | self.start);
            }
        }
        let end_constraint = {
            if self.settings.spacing != Spacing::End {
                if let Some(last_id) = self.last_widget {
                    let last_widget = self.widgets.get_mut(&last_id).unwrap();
                    parent.remove_constraint(last_widget.end_constraint.take().unwrap());
                }
                let end_constraint = child_end | EQ(REQUIRED) | self.end;
                parent.add(end_constraint.clone());
                Some(end_constraint)
            } else {
                None
            }
        };
        self.widgets.insert(child.id, WidgetData {
            start: child_start,
            end: child_end,
            prev: self.last_widget,
            next: None,
            end_constraint: end_constraint,
        });
        self.last_widget = Some(child.id);

        if self.settings.fill_equal {
            let child_size = axis_length(self.settings.orientation, &child.vars);
            parent.add(child_size | EQ(REQUIRED) | self.size.unwrap());
        }
        match self.settings.orientation {
            Orientation::Horizontal => {
                match self.settings.item_align {
                    ItemAlignment::Fill => {
                        child.add(constraints![
                            align_top(parent),
                            align_bottom(parent),
                        ]);
                    },
                    ItemAlignment::Center => {
                        child.add(constraints![
                            center_vertical(parent),
                            bound_top(parent),
                            bound_bottom(parent),
                        ]);
                    },
                    ItemAlignment::Top => {
                        child.add(constraints![
                            align_top(parent),
                            bound_bottom(parent),
                        ]);
                    },
                    ItemAlignment::Bottom => {
                        child.add(constraints![
                            bound_top(parent),
                            align_bottom(parent),
                        ]);
                    },
                    ItemAlignment::None => {
                        child.add(constraints![
                            bound_top(parent),
                            bound_bottom(parent),
                        ]);
                    },
                    _ => panic!("Invalid linear layout settings"),
                }
            },
            Orientation::Vertical => {
                match self.settings.item_align {
                    ItemAlignment::Fill => {
                        child.add(constraints![
                            align_left(parent),
                            align_right(parent),
                        ]);
                    },
                    ItemAlignment::Center => {
                        child.add(constraints![
                            center_horizontal(parent),
                            bound_left(parent),
                            bound_right(parent),
                        ]);
                    },
                    ItemAlignment::Left => {
                        child.add(constraints![
                            align_left(parent),
                            bound_right(parent),
                        ]);
                    },
                    ItemAlignment::Right => {
                        child.add(constraints![
                            bound_left(parent),
                            align_right(parent),
                        ]);
                    },
                    ItemAlignment::None => {
                        child.add(constraints![
                            bound_left(parent),
                            bound_right(parent),
                        ]);
                    },
                    _ => panic!("Invalid linear layout settings"),
                }
            }
        }
    }

    fn remove_child(&mut self, parent: &mut Layout, child: &mut Layout) {
        if let Some(widget_data) = self.widgets.remove(&child.id) {
            if let Some(prev) = widget_data.prev {
                let next_start = widget_data.next.map(|next_id| self.widgets[&next_id].start);
                let prev = self.widgets.get_mut(&prev).unwrap();
                if let Some(next_start) = next_start {
                    parent.add(next_start | EQ(REQUIRED) | prev.end + self.space);
                } else {
                    if self.settings.spacing != Spacing::End {
                        let end_constraint = prev.end | EQ(REQUIRED) | self.end;
                        parent.add(end_constraint.clone());
                        prev.end_constraint = Some(end_constraint);
                    }
                }
                prev.next = widget_data.next;
            } else if let Some(next) = widget_data.next {
                if self.settings.spacing != Spacing::Start {
                    let next_start = self.widgets[&next].start;
                    parent.add(next_start | EQ(REQUIRED) | self.start);
                }
            }
            if let Some(next) = widget_data.next {
                self.widgets.get_mut(&next).unwrap().prev = widget_data.prev;
            }

            if let Some(last_id) = self.last_widget {
                if last_id == child.id {
                    self.last_widget = widget_data.prev;
                }
            }
        }
    }
}

fn beginning(orientation: Orientation, layout: &LayoutVars) -> Variable {
    match orientation {
        Orientation::Horizontal => layout.left,
        Orientation::Vertical => layout.top,
    }
}
fn ending(orientation: Orientation, layout: &LayoutVars) -> Variable {
    match orientation {
        Orientation::Horizontal => layout.right,
        Orientation::Vertical => layout.bottom,
    }
}
fn axis_length(orientation: Orientation, layout: &LayoutVars) -> Variable {
    match orientation {
        Orientation::Horizontal => layout.width,
        Orientation::Vertical => layout.height,
    }
}