cotis-layout 0.1.0-alpha.1

Flexbox-style layout engine for Cotis
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
437
438
439
440
441
442
443
444
445
446
447
448
use crate::layout_algorithm::axis_utils::Axis;
use crate::layout_algorithm::sizing::{get_child_axis_max, get_children_axis_total};
use crate::layout_struct::info_types::{ClipElement, ClipElementConfig, FloatingElementStyle};
use cotis::utils::ElementId;
use cotis_defaults::element_configs::style::sizing::{AxisSizing, DoubleAxisSizing, Sizing};
use cotis_defaults::element_configs::style::types::{
    Alignment, LayoutAlignmentX, LayoutAlignmentY, LayoutDirection, Padding,
};
use cotis_utils::math::{BoundingBox, Vector2};
use std::fmt::Debug;

#[derive(Clone, Debug)]
pub struct LayoutElement {
    pub id: ElementId,
    pub name: String,
    pub info: LayoutElementInfo,
    pub config: LayoutElementConfig,
}

impl LayoutElement {
    pub(crate) fn new(id: ElementId, name: String) -> Self {
        Self {
            id,
            name,
            info: LayoutElementInfo::NotInitialized,
            config: Default::default(),
        }
    }

    pub(crate) fn calc_start_dim(&self) -> element_info_states::SelfMinSize {
        let min_w = Self::min_axis_from_sizing(&self.config.layout.sizing, true);
        let min_h = Self::min_axis_from_sizing(&self.config.layout.sizing, false);
        element_info_states::SelfMinSize {
            min_size_width: min_w,
            min_size_height: min_h,
            preferred_size_width: self.internal_space_width().max(min_w),
            preferred_size_height: self.internal_space_height().max(min_h),
        }
    }

    /// Minimum size along one axis from declared layout sizing (matches [`crate::layout_algorithm::axis_utils`]).
    fn min_axis_from_sizing(sizing: &Sizing, width: bool) -> f32 {
        match sizing {
            Sizing::DoubleAxis(axis_sizing) => {
                let axis = if width {
                    axis_sizing.width
                } else {
                    axis_sizing.height
                };
                match axis {
                    AxisSizing::Fit(min, max) | AxisSizing::Grow(min, max) => {
                        debug_assert!(min <= max);
                        min
                    }
                    AxisSizing::Fixed(v) => v,
                    AxisSizing::Percent(_) | AxisSizing::InnerPercent(_) => 0.0,
                }
            }
        }
    }

    /// Declared maximum along one axis from layout sizing (unbounded axes use `f32::MAX`).
    pub(crate) fn declared_max_axis(&self, width: bool) -> f32 {
        match &self.config.layout.sizing {
            Sizing::DoubleAxis(axis_sizing) => {
                let axis = if width {
                    axis_sizing.width
                } else {
                    axis_sizing.height
                };
                match axis {
                    AxisSizing::Fit(_, max) | AxisSizing::Grow(_, max) => max,
                    AxisSizing::Fixed(v) => v,
                    AxisSizing::Percent(_) | AxisSizing::InnerPercent(_) => f32::MAX,
                }
            }
        }
    }

    pub(crate) fn internal_space_width(&self) -> f32 {
        self.config.layout.padding.left + self.config.layout.padding.right
    }
    pub(crate) fn internal_space_height(&self) -> f32 {
        self.config.layout.padding.top + self.config.layout.padding.bottom
    }
}

/// Scrollable content extent inside a clip container (direct in-flow children + padding + gaps).
pub(crate) fn clip_content_size(
    parent: &LayoutElement,
    children: &[&LayoutElement],
) -> Option<Vector2> {
    if !parent.config.clip.horizontal && !parent.config.clip.vertical {
        return None;
    }
    let pad = &parent.config.layout.padding;
    let gap = parent.config.layout.child_gap;
    let child_iter = children.iter().copied();

    let (children_w, children_h) = match parent.config.layout.layout_direction {
        LayoutDirection::LeftToRight => (
            get_children_axis_total(child_iter.clone(), Axis::Width, gap),
            get_child_axis_max(child_iter, Axis::Height),
        ),
        LayoutDirection::TopToBottom => (
            get_child_axis_max(child_iter.clone(), Axis::Width),
            get_children_axis_total(child_iter, Axis::Height, gap),
        ),
    };

    Some(Vector2 {
        x: pad.left + children_w + pad.right,
        y: pad.top + children_h + pad.bottom,
    })
}

pub mod element_info_states {
    #[derive(Debug, Clone)]
    pub struct SelfMinSize {
        pub min_size_width: f32,
        pub min_size_height: f32,
        pub preferred_size_width: f32,
        pub preferred_size_height: f32,
    }
    #[derive(Debug, Clone)]
    pub struct TotalMinSize {
        pub min_size_width: f32,
        pub min_size_height: f32,
        pub preferred_size_width: f32,
        pub preferred_size_height: f32,
    }
    #[derive(Debug, Clone)]
    pub struct TotalSizeWidthMinHeight {
        pub width: f32,
        pub min_height: f32,
        pub preferred_size_height: f32,
    }
    #[derive(Debug, Clone)]
    pub struct TextWrappedMinHeight {
        pub width: f32,
        pub min_height: f32,
        pub preferred_size_height: f32,
    }
    #[derive(Debug, Clone)]
    pub struct TotalSize {
        pub width: f32,
        pub height: f32,
    }
    #[derive(Debug, Clone)]
    pub struct TotalSizedAndPosition {
        pub x: f32,
        pub y: f32,
        pub width: f32,
        pub height: f32,
    }

    impl SelfMinSize {
        pub fn into_total_min_size(self) -> TotalMinSize {
            TotalMinSize {
                min_size_width: self.min_size_width,
                min_size_height: self.min_size_height,
                preferred_size_width: self.preferred_size_width,
                preferred_size_height: self.preferred_size_height,
            }
        }
    }
}

#[derive(Debug, Clone)]
pub enum LayoutElementInfo {
    NotInitialized,
    SelfMinSize(element_info_states::SelfMinSize),
    TotalMinSize(element_info_states::TotalMinSize),
    TotalSizeWidthMinHeight(element_info_states::TotalSizeWidthMinHeight),
    TextWrappedMinHeight(element_info_states::TextWrappedMinHeight),
    TotalSize(element_info_states::TotalSize),
    TotalSizedAndPosition(element_info_states::TotalSizedAndPosition),
}

impl LayoutElementInfo {
    pub(crate) fn upgrade_width(&mut self) {
        match self {
            LayoutElementInfo::SelfMinSize(old) => {
                *self = LayoutElementInfo::TotalSizeWidthMinHeight(
                    element_info_states::TotalSizeWidthMinHeight {
                        width: old.preferred_size_width.max(old.min_size_width),
                        min_height: old.min_size_height,
                        preferred_size_height: old.preferred_size_height,
                    },
                )
            }
            LayoutElementInfo::TotalMinSize(old) => {
                *self = LayoutElementInfo::TotalSizeWidthMinHeight(
                    element_info_states::TotalSizeWidthMinHeight {
                        width: old.preferred_size_width.max(old.min_size_width),
                        min_height: old.min_size_height,
                        preferred_size_height: old.preferred_size_height,
                    },
                )
            }
            LayoutElementInfo::NotInitialized => panic!("Cant upgrade none init"),
            _ => {}
        }
    }

    pub(crate) fn upgrade_height(&mut self) {
        match self {
            LayoutElementInfo::TotalSizeWidthMinHeight(old) => {
                *self = LayoutElementInfo::TotalSize(element_info_states::TotalSize {
                    width: old.width,
                    height: old.min_height.max(old.preferred_size_height),
                });
            }
            LayoutElementInfo::TextWrappedMinHeight(old) => {
                *self = LayoutElementInfo::TotalSize(element_info_states::TotalSize {
                    width: old.width,
                    height: old.min_height.max(old.preferred_size_height),
                });
            }
            LayoutElementInfo::TotalSize(_) => {}
            LayoutElementInfo::TotalSizedAndPosition(_) => {}
            _ => panic!("Cant upgrade not final width"),
        }
    }
}

impl LayoutElementInfo {
    pub(crate) fn temp_width(&self) -> Option<f32> {
        match self {
            LayoutElementInfo::SelfMinSize(tmp) => Some(tmp.preferred_size_width),
            LayoutElementInfo::TotalMinSize(tmp) => Some(tmp.preferred_size_width),
            _ => None,
        }
    }
    pub(crate) fn min_width(&self) -> Option<f32> {
        match self {
            LayoutElementInfo::SelfMinSize(tmp) => Some(tmp.min_size_width),
            LayoutElementInfo::TotalMinSize(tmp) => Some(tmp.min_size_width),
            _ => None,
        }
    }

    pub(crate) fn temp_height(&self) -> Option<f32> {
        match self {
            LayoutElementInfo::SelfMinSize(tmp) => Some(tmp.preferred_size_height),
            LayoutElementInfo::TotalMinSize(tmp) => Some(tmp.preferred_size_height),
            LayoutElementInfo::TotalSizeWidthMinHeight(tmp) => Some(tmp.preferred_size_height),
            LayoutElementInfo::TextWrappedMinHeight(tmp) => Some(tmp.preferred_size_height),
            _ => None,
        }
    }
    pub(crate) fn min_height(&self) -> Option<f32> {
        match self {
            LayoutElementInfo::SelfMinSize(tmp) => Some(tmp.min_size_height),
            LayoutElementInfo::TotalMinSize(tmp) => Some(tmp.min_size_height),
            LayoutElementInfo::TotalSizeWidthMinHeight(tmp) => Some(tmp.min_height),
            LayoutElementInfo::TextWrappedMinHeight(tmp) => Some(tmp.min_height),
            _ => None,
        }
    }

    pub fn get_final_width(&self) -> Option<f32> {
        match self {
            LayoutElementInfo::TotalSizeWidthMinHeight(old) => Some(old.width),
            LayoutElementInfo::TextWrappedMinHeight(old) => Some(old.width),
            LayoutElementInfo::TotalSize(old) => Some(old.width),
            LayoutElementInfo::TotalSizedAndPosition(old) => Some(old.width),
            _ => None,
        }
    }

    pub fn get_final_height(&self) -> Option<f32> {
        match self {
            LayoutElementInfo::TotalSize(old) => Some(old.height),
            LayoutElementInfo::TotalSizedAndPosition(old) => Some(old.height),
            _ => None,
        }
    }
}

impl LayoutElementInfo {
    pub fn set_x(&mut self, x: f32) {
        match self {
            LayoutElementInfo::TotalSize(old) => {
                *self = LayoutElementInfo::TotalSizedAndPosition(
                    element_info_states::TotalSizedAndPosition {
                        x,
                        y: 0.0,
                        width: old.width,
                        height: old.height,
                    },
                )
            }
            LayoutElementInfo::TotalSizedAndPosition(old) => {
                old.x = x;
            }
            _ => {
                panic!("tried setting x for not sized info")
            }
        }
    }

    pub fn set_y(&mut self, y: f32) {
        match self {
            LayoutElementInfo::TotalSize(old) => {
                *self = LayoutElementInfo::TotalSizedAndPosition(
                    element_info_states::TotalSizedAndPosition {
                        x: 0.0,
                        y,
                        width: old.width,
                        height: old.height,
                    },
                )
            }
            LayoutElementInfo::TotalSizedAndPosition(old) => {
                old.y = y;
            }
            _ => {
                panic!("tried setting y for not sized info")
            }
        }
    }

    pub fn to_bounding_box(&self) -> BoundingBox {
        match self {
            LayoutElementInfo::TotalSizedAndPosition(info) => BoundingBox {
                x: info.x,
                y: info.y,
                width: info.width,
                height: info.height,
            },
            _ => {
                panic!("Tried to get bounding box for not positioned info")
            }
        }
    }

    pub fn set_tmp_width(&mut self, width: f32) {
        match self {
            LayoutElementInfo::SelfMinSize(tmp) => {
                tmp.preferred_size_width = width.max(tmp.min_size_width)
            }
            LayoutElementInfo::TotalMinSize(tmp) => {
                tmp.preferred_size_width = width.max(tmp.min_size_width)
            }
            _ => {
                panic!("tried to set tmp width of final width")
            }
        }
    }
    pub fn set_tmp_height(&mut self, height: f32) {
        match self {
            LayoutElementInfo::SelfMinSize(tmp) => {
                tmp.preferred_size_height = height.max(tmp.min_size_height)
            }
            LayoutElementInfo::TotalMinSize(tmp) => {
                tmp.preferred_size_height = height.max(tmp.min_size_height)
            }
            LayoutElementInfo::TotalSizeWidthMinHeight(tmp) => {
                tmp.preferred_size_height = height.max(tmp.min_height)
            }
            LayoutElementInfo::TextWrappedMinHeight(tmp) => {
                tmp.preferred_size_height = height.max(tmp.min_height)
            }
            _ => {
                panic!("tried to set tmp width of final width")
            }
        }
    }

    pub fn set_min_height(&mut self, height: f32) {
        match self {
            LayoutElementInfo::SelfMinSize(tmp) => {
                tmp.min_size_height = height.max(tmp.min_size_height)
            }
            LayoutElementInfo::TotalMinSize(tmp) => {
                tmp.min_size_height = height.max(tmp.min_size_height)
            }
            LayoutElementInfo::TotalSizeWidthMinHeight(tmp) => {
                tmp.min_height = height.max(tmp.min_height)
            }
            LayoutElementInfo::TextWrappedMinHeight(tmp) => {
                tmp.min_height = height.max(tmp.min_height)
            }
            _ => {
                panic!("tried to set tmp width of final width")
            }
        }
    }
}

#[derive(Debug, Clone)]
pub struct LayoutElementConfig {
    pub layout: LayoutElementStyle,
    pub floating: FloatingElementStyle,
    pub clip: ClipElementConfig,
}

impl Default for LayoutElementConfig {
    fn default() -> Self {
        Self {
            layout: LayoutElementStyle {
                sizing: Sizing::DoubleAxis(DoubleAxisSizing {
                    width: AxisSizing::Fit(0.0, f32::MAX),
                    height: AxisSizing::Fit(0.0, f32::MAX),
                }),
                padding: Padding {
                    top: 0.0,
                    bottom: 0.0,
                    right: 0.0,
                    left: 0.0,
                },
                child_gap: 0.0,
                child_alignment: Alignment {
                    x: LayoutAlignmentX::Left,
                    y: LayoutAlignmentY::Top,
                },
                layout_direction: LayoutDirection::LeftToRight,
                wrapping: ChildWrapping::None,
            },
            floating: FloatingElementStyle {
                config: None,
                z_index: 0,
            },
            clip: ClipElement {
                horizontal: false,
                vertical: false,
                offset: Default::default(),
            },
        }
    }
}

#[derive(Debug, Clone)]
pub struct LayoutElementStyle {
    pub sizing: Sizing,
    pub padding: Padding,
    pub child_gap: f32,
    pub child_alignment: Alignment,
    pub layout_direction: LayoutDirection,
    pub wrapping: ChildWrapping,
}

#[derive(Debug, Clone, PartialEq)]
pub enum ChildWrapping {
    None,
    Text,
}