ori_core/layout/
space.rs

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
use std::{
    hash::{Hash, Hasher},
    ops::{Add, AddAssign, BitAnd, BitAndAssign, Sub, SubAssign},
};

use super::Size;

/// Space available to lay out a view.
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Space {
    /// Minimum size the view can be.
    pub min: Size,
    /// Maximum size the view can be.
    pub max: Size,
}

impl Default for Space {
    fn default() -> Self {
        Self::UNBOUNDED
    }
}

impl Space {
    /// The zero space.
    pub const ZERO: Self = Self {
        min: Size::ZERO,
        max: Size::ZERO,
    };

    /// The unbounded space.
    pub const UNBOUNDED: Self = Self {
        min: Size::ZERO,
        max: Size::UNBOUNDED,
    };

    /// The infinite space.
    pub const FILL: Self = Self::new(Size::FILL, Size::FILL);

    /// Create a new space.
    pub const fn new(min: Size, max: Size) -> Self {
        Self { min, max }
    }

    /// Create a new space from a maximum size.
    pub const fn max(max: Size) -> Self {
        Self::new(Size::ZERO, max)
    }

    /// Create a new space with the same minimum and maximum size.
    pub fn from_size(size: Size) -> Self {
        Self::new(size, size)
    }

    /// Shrink the space by `size`.
    pub fn shrink(self, size: Size) -> Self {
        let min = self.min - size;
        let max = self.max - size;

        Self::new(min.max(Size::ZERO), max.max(Size::ZERO))
    }

    /// Expand the space by `size`.
    pub fn expand(self, size: Size) -> Self {
        Self::new(self.min + size, self.max + size)
    }

    /// Loosen the space, setting the minimum size to zero.
    pub fn loosen(self) -> Self {
        Self::new(Size::ZERO, self.max)
    }

    /// Loosen the width, setting the minimum width to zero.
    pub fn loosen_width(mut self) -> Self {
        self.min.width = 0.0;
        self
    }

    /// Loosen the height, setting the minimum height to zero.
    pub fn loosen_height(mut self) -> Self {
        self.min.height = 0.0;
        self
    }

    /// Get the most constraning space between `self` and `other
    pub fn constrain(self, other: Self) -> Self {
        let min = self.min.max(other.min);
        let max = self.max.min(other.max);

        Self::new(min.min(max), max)
    }

    /// Clamp a size to the space.
    pub fn fit(self, size: Size) -> Size {
        let width = if self.min.width.is_finite() {
            size.width.max(self.min.width)
        } else {
            size.width
        };

        let height = if self.min.height.is_finite() {
            size.height.max(self.min.height)
        } else {
            size.height
        };

        Size::new(width.min(self.max.width), height.min(self.max.height))
    }

    /// Get whether the space is finite.
    pub fn is_finite(self) -> bool {
        self.min.is_finite() && self.max.is_finite()
    }

    /// Get whether the space is infinite.
    pub fn is_infinite(self) -> bool {
        self.min.is_infinite() && self.max.is_infinite()
    }
}

impl From<Size> for Space {
    fn from(size: Size) -> Self {
        Self::new(size, size)
    }
}

impl BitAnd for Space {
    type Output = Self;

    fn bitand(self, rhs: Self) -> Self::Output {
        self.constrain(rhs)
    }
}

impl BitAndAssign for Space {
    fn bitand_assign(&mut self, rhs: Self) {
        *self = *self & rhs;
    }
}

impl Add<Size> for Space {
    type Output = Self;

    fn add(self, rhs: Size) -> Self::Output {
        self.expand(rhs)
    }
}

impl AddAssign<Size> for Space {
    fn add_assign(&mut self, rhs: Size) {
        *self = *self + rhs;
    }
}

impl Sub<Size> for Space {
    type Output = Self;

    fn sub(self, rhs: Size) -> Self::Output {
        self.shrink(rhs)
    }
}

impl SubAssign<Size> for Space {
    fn sub_assign(&mut self, rhs: Size) {
        *self = *self - rhs;
    }
}

impl Hash for Space {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.min.hash(state);
        self.max.hash(state);
    }
}