cranpose_ui_layout/
constraints.rs1#[derive(Clone, Copy, Debug, PartialEq)]
5pub struct Constraints {
6 pub min_width: f32,
7 pub max_width: f32,
8 pub min_height: f32,
9 pub max_height: f32,
10}
11
12impl Constraints {
13 pub fn tight(width: f32, height: f32) -> Self {
15 Self {
16 min_width: width,
17 max_width: width,
18 min_height: height,
19 max_height: height,
20 }
21 }
22
23 pub fn loose(max_width: f32, max_height: f32) -> Self {
25 Self {
26 min_width: 0.0,
27 max_width,
28 min_height: 0.0,
29 max_height,
30 }
31 }
32
33 pub fn is_tight(&self) -> bool {
35 self.min_width == self.max_width && self.min_height == self.max_height
36 }
37
38 pub fn is_bounded(&self) -> bool {
40 self.max_width.is_finite() && self.max_height.is_finite()
41 }
42
43 pub fn constrain(&self, width: f32, height: f32) -> (f32, f32) {
45 (
46 width.clamp(self.min_width, self.max_width),
47 height.clamp(self.min_height, self.max_height),
48 )
49 }
50
51 #[inline]
53 pub fn has_bounded_width(&self) -> bool {
54 self.max_width.is_finite()
55 }
56
57 #[inline]
59 pub fn has_bounded_height(&self) -> bool {
60 self.max_height.is_finite()
61 }
62
63 #[inline]
65 pub fn has_tight_width(&self) -> bool {
66 self.min_width == self.max_width
67 }
68
69 #[inline]
71 pub fn has_tight_height(&self) -> bool {
72 self.min_height == self.max_height
73 }
74
75 pub fn tighten_width(self, width: f32) -> Self {
77 Self {
78 min_width: width,
79 max_width: width,
80 ..self
81 }
82 }
83
84 pub fn tighten_height(self, height: f32) -> Self {
86 Self {
87 min_height: height,
88 max_height: height,
89 ..self
90 }
91 }
92
93 pub fn copy_with_width(self, min_width: f32, max_width: f32) -> Self {
95 Self {
96 min_width,
97 max_width,
98 ..self
99 }
100 }
101
102 pub fn copy_with_height(self, min_height: f32, max_height: f32) -> Self {
104 Self {
105 min_height,
106 max_height,
107 ..self
108 }
109 }
110
111 pub fn deflate(self, horizontal: f32, vertical: f32) -> Self {
114 Self {
115 min_width: (self.min_width - horizontal).max(0.0),
116 max_width: (self.max_width - horizontal).max(0.0),
117 min_height: (self.min_height - vertical).max(0.0),
118 max_height: (self.max_height - vertical).max(0.0),
119 }
120 }
121
122 pub fn loosen(self) -> Self {
124 Self {
125 min_width: 0.0,
126 min_height: 0.0,
127 ..self
128 }
129 }
130
131 pub fn enforce(self, width: f32, height: f32) -> Self {
133 Self {
134 min_width: width.clamp(self.min_width, self.max_width),
135 max_width: width.clamp(self.min_width, self.max_width),
136 min_height: height.clamp(self.min_height, self.max_height),
137 max_height: height.clamp(self.min_height, self.max_height),
138 }
139 }
140}