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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
use std::borrow::Borrow;

use fltk::group::Group;
use fltk::prelude::*;

use crate::WrapperFactory;

use super::{LayoutElement, Size};

mod builder;

pub use builder::{CellBuilder, GridBuilder, StripeBuilder};

#[derive(Debug, Clone, Copy)]
pub enum CellAlign {
    Start,
    Center,
    End,
    Stretch,
}

pub struct Grid<G: GroupExt + Clone = Group> {
    props: GridProperties<G>,
    stretch_rows: Vec<usize>,
    stretch_cols: Vec<usize>,
    min_size: Size,
}

struct GridProperties<G: GroupExt + Clone = Group> {
    group: G,
    padding: Padding,
    row_spacing: i32,
    col_spacing: i32,
    cells: Vec<Cell>,
    spans: Vec<Cell>,
    groups: Vec<StripeProperties>,
    rows: Vec<Stripe>,
    cols: Vec<Stripe>,
}

#[derive(Debug, Default, Clone, Copy)]
struct Padding {
    left: i32,
    top: i32,
    right: i32,
    bottom: i32,
}

struct Cell {
    element: Box<dyn LayoutElement>,
    min_size: Size,
    props: CellProperties,
}

struct CellProperties {
    row: usize,
    col: usize,
    row_span: usize,
    col_span: usize,
    padding: Padding,
    horz_align: CellAlign,
    vert_align: CellAlign,
}

#[derive(Debug, Clone, Copy)]
struct StripeProperties {
    stretch: u8,
    min_size: i32,
}

struct Stripe {
    cells: Vec<StripeCell>,
    group_idx: usize,
}

#[derive(Debug, Clone, Copy)]
enum StripeCell {
    Free,
    Skipped,
    Cell(usize),
    Span,
}

impl StripeCell {
    fn cell_idx(&self) -> Option<usize> {
        if let Self::Cell(idx) = self {
            Some(*idx)
        } else {
            None
        }
    }
}

impl<G: GroupExt + Clone> LayoutElement for Grid<G> {
    fn min_size(&self) -> Size {
        self.min_size
    }

    fn layout(&self, x: i32, y: i32, width: i32, height: i32) {
        self.props.group.clone().resize(x, y, width, height);
        self.layout_children()
    }
}

impl Grid {
    pub fn builder() -> GridBuilder<Group, WrapperFactory> {
        GridBuilder::new(Group::default_fill())
    }

    pub fn builder_with_factory<F: Borrow<WrapperFactory>>(factory: F) -> GridBuilder<Group, F> {
        GridBuilder::with_factory(Group::default_fill(), factory)
    }
}

impl<G: GroupExt + Clone> Grid<G> {
    pub fn group(&self) -> G {
        self.props.group.clone()
    }

    pub fn layout_children(&self) {
        let x = self.props.group.x() + self.props.padding.left;
        let y = self.props.group.y() + self.props.padding.top;
        let width = self.props.group.width() - (self.props.padding.left + self.props.padding.right);
        let height =
            self.props.group.height() - (self.props.padding.top + self.props.padding.bottom);

        // TODO: Eliminate unnecessary allocation
        let col_bounds = calc_stripe_bounds(
            width,
            &self.props.cols,
            &self.props.groups,
            &self.stretch_cols,
            self.props.col_spacing,
        );
        let row_bounds = calc_stripe_bounds(
            height,
            &self.props.rows,
            &self.props.groups,
            &self.stretch_rows,
            self.props.row_spacing,
        );

        for cell in self.props.cells.iter() {
            let (cell_x, cell_width) = col_bounds[cell.props.col];
            let (cell_y, cell_height) = row_bounds[cell.props.row];
            let (widget_x, widget_width) = calc_widget_bounds(
                x,
                cell_x,
                cell_width,
                cell.min_size.width,
                cell.props.padding.left,
                cell.props.padding.right,
                cell.props.horz_align,
            );
            let (widget_y, widget_height) = calc_widget_bounds(
                y,
                cell_y,
                cell_height,
                cell.min_size.height,
                cell.props.padding.top,
                cell.props.padding.bottom,
                cell.props.vert_align,
            );
            cell.element
                .layout(widget_x, widget_y, widget_width, widget_height);
        }

        for span in self.props.spans.iter() {
            let left_col = span.props.col;
            let right_col = left_col + span.props.col_span - 1;
            let span_x = col_bounds[left_col].0;
            let span_width = col_bounds[right_col].0 + col_bounds[right_col].1 - span_x;

            let top_row = span.props.row;
            let bottom_row = top_row + span.props.row_span - 1;
            let span_y = row_bounds[top_row].0;
            let span_height = row_bounds[bottom_row].0 + row_bounds[bottom_row].1 - span_y;

            let (widget_x, widget_width) = calc_widget_bounds(
                x,
                span_x,
                span_width,
                span.min_size.width,
                span.props.padding.left,
                span.props.padding.right,
                span.props.horz_align,
            );
            let (widget_y, widget_height) = calc_widget_bounds(
                y,
                span_y,
                span_height,
                span.min_size.height,
                span.props.padding.top,
                span.props.padding.bottom,
                span.props.vert_align,
            );
            span.element
                .layout(widget_x, widget_y, widget_width, widget_height);
        }
    }

    fn new(props: GridProperties<G>) -> Self {
        let stretch_rows = collect_stretch_stripes(&props.rows, &props.groups);
        let stretch_cols = collect_stretch_stripes(&props.cols, &props.groups);

        let mut grid = Self {
            props,
            stretch_rows,
            stretch_cols,
            min_size: Default::default(),
        };

        grid.cache_min_sizes();

        sort_stretch_stripes(&grid.props.rows, &grid.props.groups, &mut grid.stretch_rows);
        sort_stretch_stripes(&grid.props.cols, &grid.props.groups, &mut grid.stretch_cols);

        grid
    }

    fn cache_min_sizes(&mut self) {
        self.cache_cell_min_sizes();
        self.cache_span_min_sizes();

        self.min_size.width =
            span_size(&self.props.cols, &self.props.groups, self.props.col_spacing)
                + self.props.padding.left
                + self.props.padding.right;
        self.min_size.height =
            span_size(&self.props.rows, &self.props.groups, self.props.row_spacing)
                + self.props.padding.top
                + self.props.padding.bottom;
    }

    fn cache_cell_min_sizes(&mut self) {
        for cell in self.props.cells.iter_mut() {
            cell.cache_min_size();
        }
        for col in self.props.cols.iter_mut() {
            self.props.groups[col.group_idx].min_size = col
                .cells
                .iter()
                .filter_map(StripeCell::cell_idx)
                .map(|idx| self.props.cells[idx].min_size.width)
                .fold(self.props.groups[col.group_idx].min_size, std::cmp::max);
        }
        for row in self.props.rows.iter_mut() {
            self.props.groups[row.group_idx].min_size = row
                .cells
                .iter()
                .filter_map(StripeCell::cell_idx)
                .map(|idx| self.props.cells[idx].min_size.height)
                .fold(self.props.groups[row.group_idx].min_size, std::cmp::max);
        }
    }

    fn cache_span_min_sizes(&mut self) {
        for span in self.props.spans.iter_mut() {
            span.cache_min_size();

            let top = span.props.row;
            let bottom = top + span.props.row_span;
            let left = span.props.col;
            let right = left + span.props.col_span;

            adjust_span_stripes(
                span.min_size.width,
                &self.props.cols[left..right],
                &mut self.props.groups,
                self.props.col_spacing,
            );
            adjust_span_stripes(
                span.min_size.height,
                &self.props.rows[top..bottom],
                &mut self.props.groups,
                self.props.row_spacing,
            );
        }
    }
}

impl Cell {
    fn cache_min_size(&mut self) {
        self.min_size = self.element.min_size();
        self.min_size.width += self.props.padding.left + self.props.padding.right;
        self.min_size.height += self.props.padding.top + self.props.padding.bottom;
    }
}

fn collect_stretch_stripes(stripes: &[Stripe], groups: &[StripeProperties]) -> Vec<usize> {
    stripes
        .iter()
        .enumerate()
        .filter_map(
            |(idx, stripe)| {
                if groups[stripe.group_idx].stretch > 0 {
                    Some(idx)
                } else {
                    None
                }
            },
        )
        .collect()
}

fn sort_stretch_stripes(
    stripes: &[Stripe],
    groups: &[StripeProperties],
    stretch_stripes: &mut [usize],
) {
    stretch_stripes.sort_by(|lidx, ridx| {
        groups[stripes[*ridx].group_idx]
            .min_size
            .cmp(&groups[stripes[*lidx].group_idx].min_size)
    });
}

fn span_size(stripes: &[Stripe], groups: &[StripeProperties], spacing: i32) -> i32 {
    if stripes.len() == 0 {
        return 0;
    }

    let mut size = stripes
        .iter()
        .map(|stripe| groups[stripe.group_idx].min_size)
        .sum();
    size += (stripes.len() as i32 - 1) * spacing;
    size
}

fn adjust_span_stripes(
    min_size: i32,
    stripes: &[Stripe],
    groups: &mut [StripeProperties],
    spacing: i32,
) {
    let current_size = span_size(stripes, groups, spacing);
    if current_size >= min_size {
        return;
    }

    let mut stretch_stripes = collect_stretch_stripes(stripes, groups);
    if stretch_stripes.len() > 0 {
        sort_stretch_stripes(stripes, groups, &mut stretch_stripes);
        let bounds = calc_stripe_bounds(min_size, stripes, groups, &stretch_stripes, spacing);
        for idx in stretch_stripes {
            groups[stripes[idx].group_idx].min_size = bounds[idx].1;
        }
    } else {
        groups[stripes[0].group_idx].min_size += min_size - current_size;
    }
}

fn calc_stripe_bounds(
    total_size: i32,
    stripes: &[Stripe],
    groups: &[StripeProperties],
    stretch_stripes: &[usize],
    spacing: i32,
) -> Vec<(i32, i32)> {
    let mut bounds = Vec::with_capacity(stripes.len());

    let mut stretch_budget = total_size - (stripes.len() - 1) as i32 * spacing;
    let mut stretch_count: i32 = 0;
    for stripe in stripes.iter() {
        let group = &groups[stripe.group_idx];
        if group.stretch == 0 {
            stretch_budget -= group.min_size;
        } else {
            stretch_count += group.stretch as i32;
        }
        bounds.push((0, group.min_size));
    }
    stretch_budget = std::cmp::max(0, stretch_budget);

    let mut stretch_unit = if stretch_count > 0 { stretch_budget / stretch_count } else { 0 };
    for &stripe_idx in stretch_stripes.iter() {
        let stripe = &stripes[stripe_idx];
        let group = &groups[stripe.group_idx];

        let factor = group.stretch as i32;
        stretch_count -= factor;
        let stripe_size = if stretch_count > 0 { stretch_unit * factor } else { stretch_budget };

        if stripe_size < group.min_size {
            stretch_budget -= group.min_size;
            if stretch_count > 0 {
                stretch_unit = stretch_budget / stretch_count;
            }
        } else {
            stretch_budget -= stripe_size;
            bounds[stripe_idx].1 = stripe_size;
        }
    }

    let mut start = 0;
    for stripe_bounds in bounds.iter_mut() {
        stripe_bounds.0 = start;
        start += stripe_bounds.1 + spacing;
    }

    bounds
}

fn calc_widget_bounds(
    group_start: i32,
    cell_start: i32,
    cell_size: i32,
    min_size: i32,
    pad_start: i32,
    pad_end: i32,
    align: CellAlign,
) -> (i32, i32) {
    let widget_size = match align {
        CellAlign::Stretch => cell_size,
        _ => min_size,
    };

    let widget_size = widget_size - pad_start - pad_end;
    let cell_size = cell_size - pad_start - pad_end;

    let widget_start = match align {
        CellAlign::Start => 0,
        CellAlign::Center => (cell_size - widget_size) / 2,
        CellAlign::End => cell_size - widget_size,
        CellAlign::Stretch => 0,
    };
    let widget_start = group_start + pad_start + cell_start + widget_start;

    (widget_start, widget_size)
}