sequoia-layout 0.1.0

Sequoia GUI framework — layout engine
Documentation
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
432
433
434
435
436
use crate::container::Align;
use crate::dock::sanitize_gap;

#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
#[derive(Default)]
pub enum Track {
    #[default]
    Fit,
    Px(f32),
    Percent(f32),
    Fill(f32),

    MinMax {
        min: f32,
        max: Box<Track>,
    },
}

impl Track {
    pub fn minmax(min: f32, max: Track) -> Self {
        let min = if min.is_finite() { min.max(0.0) } else { 0.0 };

        let max = match max {
            Self::MinMax { min: _, max: inner } => *inner,
            other => other,
        };
        Self::MinMax {
            min,
            max: Box::new(max),
        }
    }

    pub fn is_fill_like(&self) -> bool {
        match self {
            Self::Fill(w) => *w > 0.0,
            Self::MinMax { max, .. } => (**max).is_fill_like(),
            _ => false,
        }
    }

    pub fn fill_weight(&self) -> f32 {
        match self {
            Self::Fill(w) if w.is_finite() => w.max(0.0),
            Self::MinMax { max, .. } => (**max).fill_weight(),
            _ => 0.0,
        }
    }

    pub fn min_floor(&self) -> f32 {
        match self {
            Self::MinMax { min, .. } => *min,
            _ => 0.0,
        }
    }
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct GridPlacement {
    pub column: Option<usize>,
    pub row: Option<usize>,
    pub col_span: usize,
    pub row_span: usize,
}

impl GridPlacement {
    pub const AUTO: Self = Self {
        column: None,
        row: None,
        col_span: 1,
        row_span: 1,
    };

    pub fn new() -> Self {
        Self::AUTO
    }

    pub fn at(mut self, column: usize, row: usize) -> Self {
        self.column = Some(column);
        self.row = Some(row);
        self
    }

    pub fn column(mut self, column: usize) -> Self {
        self.column = Some(column);
        self
    }

    pub fn row(mut self, row: usize) -> Self {
        self.row = Some(row);
        self
    }

    pub fn span(mut self, col_span: usize, row_span: usize) -> Self {
        self.col_span = col_span.max(1);
        self.row_span = row_span.max(1);
        self
    }

    pub fn col_span(mut self, span: usize) -> Self {
        self.col_span = span.max(1);
        self
    }

    pub fn row_span(mut self, span: usize) -> Self {
        self.row_span = span.max(1);
        self
    }

    pub fn is_auto(self) -> bool {
        self.column.is_none() && self.row.is_none() && self.col_span <= 1 && self.row_span <= 1
    }
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum GridFlow {
    #[default]
    Row,
    Column,
}

#[derive(Clone, Debug, PartialEq)]
pub struct GridStyle {
    pub columns: Vec<Track>,
    pub rows: Vec<Track>,
    pub column_gap: f32,
    pub row_gap: f32,
    pub auto_flow: GridFlow,

    pub align_items: Align,
    pub justify_items: Align,
}

impl Default for GridStyle {
    fn default() -> Self {
        Self {
            columns: Vec::new(),
            rows: Vec::new(),
            column_gap: 0.0,
            row_gap: 0.0,
            auto_flow: GridFlow::Row,
            align_items: Align::Stretch,
            justify_items: Align::Stretch,
        }
    }
}

impl GridStyle {
    pub fn new(columns: impl IntoIterator<Item = Track>) -> Self {
        Self {
            columns: columns.into_iter().collect(),
            ..Self::default()
        }
    }

    pub fn rows(mut self, rows: impl IntoIterator<Item = Track>) -> Self {
        self.rows = rows.into_iter().collect();
        self
    }

    pub fn gap(mut self, gap: f32) -> Self {
        let gap = sanitize_gap(gap);
        self.column_gap = gap;
        self.row_gap = gap;
        self
    }

    pub fn column_gap(mut self, gap: f32) -> Self {
        self.column_gap = sanitize_gap(gap);
        self
    }

    pub fn row_gap(mut self, gap: f32) -> Self {
        self.row_gap = sanitize_gap(gap);
        self
    }

    pub fn auto_flow(mut self, flow: GridFlow) -> Self {
        self.auto_flow = flow;
        self
    }

    pub fn column_flow(mut self) -> Self {
        self.auto_flow = GridFlow::Column;
        self
    }

    pub fn align_items(mut self, align: Align) -> Self {
        self.align_items = align;
        self
    }

    pub fn justify_items(mut self, align: Align) -> Self {
        self.justify_items = align;
        self
    }

    pub fn place_items(mut self, align: Align) -> Self {
        self.align_items = align;
        self.justify_items = align;
        self
    }
}

fn resolve_single(track: &Track, content: f32, available: f32) -> (f32, f32) {
    let content = if content.is_finite() {
        content.max(0.0)
    } else {
        0.0
    };
    match track {
        Track::Px(v) => (if v.is_finite() { v.max(0.0) } else { content }, 0.0),
        Track::Percent(p) => {
            if available.is_finite() && p.is_finite() {
                ((available * p / 100.0).max(0.0), 0.0)
            } else {
                (content, 0.0)
            }
        }
        Track::Fit => (content, 0.0),
        Track::Fill(w) => (0.0, if w.is_finite() { w.max(0.0) } else { 0.0 }),
        Track::MinMax { min, max } => {
            let (fixed, fill) = resolve_single(max, content, available);

            if fill > 0.0 {
                (min.max(fixed).max(0.0), fill)
            } else {
                (fixed.max(*min).max(0.0), 0.0)
            }
        }
    }
}

pub fn resolve_tracks(tracks: &[Track], content: &[f32], available: f32, gap: f32) -> Vec<f32> {
    resolve_tracks_clamped(tracks, content, available, gap, None, None)
}

pub fn resolve_tracks_clamped(
    tracks: &[Track],
    content: &[f32],
    available: f32,
    gap: f32,
    mins: Option<&[f32]>,
    maxs: Option<&[f32]>,
) -> Vec<f32> {
    let gap = sanitize_gap(gap);
    let gaps = gap * tracks.len().saturating_sub(1) as f32;
    let mut sizes = vec![0.0; tracks.len()];
    let mut fill_weight_sum = 0.0;

    for (i, track) in tracks.iter().enumerate() {
        let c = content.get(i).copied().unwrap_or(0.0);
        let (fixed, fill) = resolve_single(track, c, available);

        let floor = track.min_floor();
        let mut size = fixed.max(floor);
        if let Track::MinMax { min, .. } = track {
            size = size.max(*min).max(0.0);

            let _ = c;
        }
        if let Some(mns) = mins {
            let mn = mns.get(i).copied().unwrap_or(0.0);
            let mn = if mn.is_finite() { mn.max(0.0) } else { 0.0 };
            size = size.max(mn);
        }
        if let Some(mxs) = maxs {
            let mx = mxs.get(i).copied().unwrap_or(f32::INFINITY);
            let mx = if mx.is_nan() {
                f32::INFINITY
            } else if mx.is_infinite() {
                if mx > 0.0 { f32::INFINITY } else { 0.0 }
            } else {
                mx.max(0.0)
            };
            size = size.min(mx);
        }
        sizes[i] = size.max(0.0);
        fill_weight_sum += fill;
    }

    if fill_weight_sum > 0.0 {
        let max_of = |i: usize| -> Option<f32> {
            maxs.and_then(|m| m.get(i).copied()).map(|mx| {
                if mx.is_nan() {
                    f32::INFINITY
                } else if mx.is_infinite() {
                    if mx > 0.0 { f32::INFINITY } else { 0.0 }
                } else {
                    mx.max(0.0)
                }
            })
        };
        let capped = |i: usize, size: f32| max_of(i).is_some_and(|mx| size >= mx - 0.005);

        if available.is_finite() {
            // Fill tracks share the free space; a track that reaches its max
            // stops taking space so the rest is handed to its siblings.
            for _ in 0..tracks.len().max(1) {
                let used: f32 = sizes.iter().sum::<f32>() + gaps;
                let free = available - used;
                if free <= 0.005 {
                    break;
                }
                let active: f32 = tracks
                    .iter()
                    .enumerate()
                    .filter(|(i, t)| t.fill_weight() > 0.0 && !capped(*i, sizes[*i]))
                    .map(|(_, t)| t.fill_weight())
                    .sum();
                if active <= 0.0 {
                    break;
                }
                for (i, track) in tracks.iter().enumerate() {
                    let w = track.fill_weight();
                    if w <= 0.0 || capped(i, sizes[i]) {
                        continue;
                    }
                    let floor = sizes[i];
                    let mut v = floor + free * w / active;
                    if let Some(mx) = max_of(i) {
                        v = v.min(mx.max(floor));
                    }
                    sizes[i] = v.max(floor).max(0.0);
                }
            }
        } else {
            for (i, track) in tracks.iter().enumerate() {
                let w = track.fill_weight();
                if w > 0.0 {
                    let floor = sizes[i];
                    let extra = content.get(i).copied().unwrap_or(0.0).max(0.0).max(floor) - floor;
                    sizes[i] = (floor + extra.max(0.0)).max(floor).max(0.0);
                }
            }
        }
    }

    sizes
}

pub fn auto_cells(count: usize, ncols: usize, flow: GridFlow) -> Vec<(usize, usize)> {
    let ncols = ncols.max(1);
    (0..count)
        .map(|i| match flow {
            GridFlow::Row => (i % ncols, i / ncols),
            GridFlow::Column => {
                let nrows = count.div_ceil(ncols).max(1);
                (i / nrows, i % nrows)
            }
        })
        .collect()
}

pub fn track_offsets(sizes: &[f32], gap: f32) -> Vec<f32> {
    let mut offsets = Vec::with_capacity(sizes.len());
    let mut cursor = 0.0;
    for &size in sizes {
        offsets.push(cursor);
        cursor += size + gap;
    }
    offsets
}

#[cfg(test)]
mod tests {
    use super::*;

    fn approx(a: f32, b: f32) -> bool {
        (a - b).abs() < 0.01
    }

    #[test]
    fn px_and_auto_tracks_take_fixed_and_content() {
        let sizes = resolve_tracks(
            &[Track::Px(100.0), Track::Fit, Track::Px(50.0)],
            &[0.0, 70.0, 0.0],
            400.0,
            0.0,
        );
        assert_eq!(sizes.len(), 3);
        assert!(approx(sizes[0], 100.0));
        assert!(approx(sizes[1], 70.0));
        assert!(approx(sizes[2], 50.0));
    }

    #[test]
    fn fill_tracks_split_remaining_space() {
        let sizes = resolve_tracks(
            &[Track::Px(100.0), Track::Fill(1.0), Track::Fill(3.0)],
            &[0.0; 3],
            400.0,
            0.0,
        );
        assert!(approx(sizes[0], 100.0));
        assert!(approx(sizes[1], 75.0));
        assert!(approx(sizes[2], 225.0));
    }

    #[test]
    fn fill_respects_gaps_and_overflow_clamps_to_zero() {
        let with_gap = resolve_tracks(
            &[Track::Fill(1.0), Track::Fill(1.0)],
            &[0.0; 2],
            110.0,
            10.0,
        );
        assert!(approx(with_gap[0], 50.0));
        assert!(approx(with_gap[1], 50.0));

        let overflow = resolve_tracks(&[Track::Px(300.0), Track::Fill(2.0)], &[0.0; 2], 100.0, 0.0);
        assert!(approx(overflow[0], 300.0));
        assert!(approx(overflow[1], 0.0));
    }

    #[test]
    fn unbounded_axis_falls_back_to_content() {
        let sizes = resolve_tracks(
            &[Track::Percent(50.0), Track::Fill(1.0), Track::Px(20.0)],
            &[80.0, 40.0, 0.0],
            f32::INFINITY,
            0.0,
        );
        assert!(approx(sizes[0], 80.0));
        assert!(approx(sizes[1], 40.0));
        assert!(approx(sizes[2], 20.0));
    }

    #[test]
    fn track_offsets_place_tracks_after_gaps() {
        let offsets = track_offsets(&[100.0, 50.0, 30.0], 10.0);
        assert!(approx(offsets[0], 0.0));
        assert!(approx(offsets[1], 110.0));
        assert!(approx(offsets[2], 170.0));
    }
}