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
#![doc=include_str!("../README.md")]
#![deny(clippy::missing_const_for_fn)]

use layout::Vec2;

pub mod align;
pub mod flexbox;
pub mod grid;
pub mod layout;
pub use align::Alignment;

#[derive(Debug, Clone, Copy, PartialEq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Orientation {
    Vertical,
    Horizontal,
}

/// A layout trait
///
/// It uses height to width management like
/// [Gtk](https://docs.gtk.org/gtk4/class.Widget.html#height-for-width-geometry-management)
pub trait Layout {
    /// Given a height, how much width would this like to have at
    /// minimum.
    fn width_for_height(&self, height: usize) -> usize;
    /// Given a width, how much height would this like to have at
    /// minimum.
    fn height_for_width(&self, width: usize) -> usize;

    /// What would this widget like to have in an ideal world.
    ///
    /// This should be the minimum which isn't too small
    fn prefered_size(&self) -> Vec2;

    /// How would this widget choose to allocate itself given the parent's size
    fn prefered_size_of_container(&self, container: Vec2) -> Vec2 {
        let mut size = self.prefered_size();
        if size.x > container.x {
            size.x = container.x;
            size.y = self.height_for_width(size.x);
        }
        if size.y > container.y {
            size.y = container.y;
            size.x = self.width_for_height(size.y);
        }
        size
    }
}

impl<T: Layout> Layout for &T {
    fn width_for_height(&self, height: usize) -> usize {
        (*self).width_for_height(height)
    }
    fn height_for_width(&self, width: usize) -> usize {
        (*self).height_for_width(width)
    }
    fn prefered_size(&self) -> Vec2 {
        (*self).prefered_size()
    }
}