@@ -8,9 +8,10 @@ use vello::kurbo::Size;
8
8
/// The layout strategy for Masonry is strongly inspired by Flutter,
9
9
/// and this struct is similar to the [Flutter BoxConstraints] class.
10
10
///
11
- /// At the moment, it represents simply a minimum and maximum size.
11
+ /// At the moment, it represents only a maximum size.
12
12
/// A widget's [`layout`] method should choose an appropriate size that
13
- /// meets these constraints.
13
+ /// meets that constraint.
14
+ /// The algorithm is supposed to use a minimum constraint, but we're phasing that out.
14
15
///
15
16
/// Further, a container widget should compute appropriate constraints
16
17
/// for each of its child widgets, and pass those down when recursing.
@@ -23,7 +24,6 @@ use vello::kurbo::Size;
23
24
/// [rounded away from zero]: Size::expand
24
25
#[ derive( Clone , Copy , Debug , PartialEq ) ]
25
26
pub struct BoxConstraints {
26
- min : Size ,
27
27
max : Size ,
28
28
}
29
29
@@ -32,7 +32,6 @@ impl BoxConstraints {
32
32
///
33
33
/// Can be satisfied by any nonnegative size.
34
34
pub const UNBOUNDED : BoxConstraints = BoxConstraints {
35
- min : Size :: ZERO ,
36
35
max : Size :: new ( f64:: INFINITY , f64:: INFINITY ) ,
37
36
} ;
38
37
@@ -44,37 +43,8 @@ impl BoxConstraints {
44
43
/// so that the layout is aligned to integers.
45
44
///
46
45
/// [rounded away from zero]: Size::expand
47
- pub fn new ( min : Size , max : Size ) -> BoxConstraints {
48
- BoxConstraints {
49
- min : min. expand ( ) ,
50
- max : max. expand ( ) ,
51
- }
52
- }
53
-
54
- /// Create a "tight" box constraints object.
55
- ///
56
- /// A "tight" constraint can only be satisfied by a single size.
57
- ///
58
- /// The given size is also [rounded away from zero],
59
- /// so that the layout is aligned to integers.
60
- ///
61
- /// [rounded away from zero]: Size::expand
62
- pub fn tight ( size : Size ) -> BoxConstraints {
63
- let size = size. expand ( ) ;
64
- BoxConstraints {
65
- min : size,
66
- max : size,
67
- }
68
- }
69
-
70
- /// Create a "loose" version of the constraints.
71
- ///
72
- /// Make a version with zero minimum size, but the same maximum size.
73
- pub fn loosen ( & self ) -> BoxConstraints {
74
- BoxConstraints {
75
- min : Size :: ZERO ,
76
- max : self . max ,
77
- }
46
+ pub fn new ( max : Size ) -> BoxConstraints {
47
+ BoxConstraints { max : max. expand ( ) }
78
48
}
79
49
80
50
/// Clamp a given size so that it fits within the constraints.
@@ -84,19 +54,14 @@ impl BoxConstraints {
84
54
///
85
55
/// [rounded away from zero]: Size::expand
86
56
pub fn constrain ( & self , size : impl Into < Size > ) -> Size {
87
- size. into ( ) . expand ( ) . clamp ( self . min , self . max )
57
+ size. into ( ) . expand ( ) . clamp ( Size :: ZERO , self . max )
88
58
}
89
59
90
60
/// Returns the max size of these constraints.
91
61
pub fn max ( & self ) -> Size {
92
62
self . max
93
63
}
94
64
95
- /// Returns the min size of these constraints.
96
- pub fn min ( & self ) -> Size {
97
- self . min
98
- }
99
-
100
65
/// Whether there is an upper bound on the width.
101
66
pub fn is_width_bounded ( & self ) -> bool {
102
67
self . max . width . is_finite ( )
@@ -115,62 +80,38 @@ impl BoxConstraints {
115
80
return ;
116
81
}
117
82
118
- if self . min . width . is_nan ( ) {
119
- debug_panic ! ( "Minimum width constraint passed to {name} is NaN" ) ;
120
- }
121
- if self . min . height . is_nan ( ) {
122
- debug_panic ! ( "Minimum height constraint passed to {name} is NaN" ) ;
123
- }
124
83
if self . max . width . is_nan ( ) {
125
84
debug_panic ! ( "Maximum width constraint passed to {name} is NaN" ) ;
126
85
}
127
86
if self . max . height . is_nan ( ) {
128
87
debug_panic ! ( "Maximum height constraint passed to {name} is NaN" ) ;
129
88
}
130
89
131
- if self . min . width . is_infinite ( ) {
132
- debug_panic ! ( "Infinite minimum width constraint passed to {name}" ) ;
133
- }
134
- if self . min . height . is_infinite ( ) {
135
- debug_panic ! ( "Infinite minimum height constraint passed to {name}" ) ;
136
- }
137
-
138
- if !( 0.0 <= self . min . width
139
- && self . min . width <= self . max . width
140
- && 0.0 <= self . min . height
141
- && self . min . height <= self . max . height
142
- && self . min . expand ( ) == self . min
143
- && self . max . expand ( ) == self . max )
144
- {
90
+ if !( 0.0 <= self . max . width && 0.0 <= self . max . height && self . max . expand ( ) == self . max ) {
145
91
debug_panic ! ( "Bad BoxConstraints passed to {name}: {self:?}" , ) ;
146
92
}
147
93
}
148
94
149
- /// Shrink min and max constraints by size
95
+ /// Shrink max constraint by size
150
96
///
151
97
/// The given size is also [rounded away from zero],
152
98
/// so that the layout is aligned to integers.
153
99
///
154
100
/// [rounded away from zero]: Size::expand
155
101
pub fn shrink ( & self , diff : impl Into < Size > ) -> BoxConstraints {
156
102
let diff = diff. into ( ) . expand ( ) ;
157
- let min = Size :: new (
158
- ( self . min ( ) . width - diff. width ) . max ( 0. ) ,
159
- ( self . min ( ) . height - diff. height ) . max ( 0. ) ,
160
- ) ;
161
103
let max = Size :: new (
162
104
( self . max ( ) . width - diff. width ) . max ( 0. ) ,
163
105
( self . max ( ) . height - diff. height ) . max ( 0. ) ,
164
106
) ;
165
107
166
- BoxConstraints :: new ( min , max)
108
+ BoxConstraints :: new ( max)
167
109
}
168
110
169
111
/// Test whether these constraints contain the given `Size`.
170
112
pub fn contains ( & self , size : impl Into < Size > ) -> bool {
171
113
let size = size. into ( ) ;
172
- ( self . min . width <= size. width && size. width <= self . max . width )
173
- && ( self . min . height <= size. height && size. height <= self . max . height )
114
+ ( size. width <= self . max . width ) && ( size. height <= self . max . height )
174
115
}
175
116
176
117
/// Find the `Size` within these `BoxConstraint`s that minimises the difference between the
@@ -203,9 +144,9 @@ impl BoxConstraints {
203
144
204
145
// Then we check if any `Size`s with our desired aspect ratio are inside the constraints.
205
146
// TODO this currently outputs garbage when things are < 0 - See https://github.com/linebender/xilem/issues/377
206
- let min_w_min_h = self . min . height / self . min . width ;
207
- let max_w_min_h = self . min . height / self . max . width ;
208
- let min_w_max_h = self . max . height / self . min . width ;
147
+ let min_w_min_h = 0.0 / 0.0 ;
148
+ let max_w_min_h = 0.0 / self . max . width ;
149
+ let min_w_max_h = self . max . height / 0.0 ;
209
150
let max_w_max_h = self . max . height / self . max . width ;
210
151
211
152
// When the aspect ratio line crosses the constraints, the closest point must be one of the
@@ -219,22 +160,22 @@ impl BoxConstraints {
219
160
if aspect_ratio > min_w_max_h {
220
161
// outside max height min width
221
162
Size {
222
- width : self . min . width ,
163
+ width : 0.0 ,
223
164
height : self . max . height ,
224
165
}
225
166
} else if aspect_ratio < max_w_min_h {
226
167
// outside min height max width
227
168
Size {
228
169
width : self . max . width ,
229
- height : self . min . height ,
170
+ height : 0.0 ,
230
171
}
231
172
} else if aspect_ratio > min_w_min_h {
232
173
// hits the constraints on the min width line
233
- if width < self . min . width {
174
+ if width < 0.0 {
234
175
// we take the point on the min width
235
176
Size {
236
- width : self . min . width ,
237
- height : self . min . width * aspect_ratio,
177
+ width : 0.0 ,
178
+ height : 0.0 * aspect_ratio,
238
179
}
239
180
} else if aspect_ratio < max_w_max_h {
240
181
// exits through max.width
@@ -251,11 +192,11 @@ impl BoxConstraints {
251
192
}
252
193
} else {
253
194
// final case is where we hit constraints on the min height line
254
- if width < self . min . width {
195
+ if width < 0.0 {
255
196
// take the point on the min height
256
197
Size {
257
- width : self . min . height * aspect_ratio. recip ( ) ,
258
- height : self . min . height ,
198
+ width : 0.0 * aspect_ratio. recip ( ) ,
199
+ height : 0.0 ,
259
200
}
260
201
} else if aspect_ratio > max_w_max_h {
261
202
// exit thru max height
@@ -278,13 +219,11 @@ impl BoxConstraints {
278
219
mod tests {
279
220
use super :: * ;
280
221
281
- fn bc ( min_width : f64 , min_height : f64 , max_width : f64 , max_height : f64 ) -> BoxConstraints {
282
- BoxConstraints :: new (
283
- Size :: new ( min_width, min_height) ,
284
- Size :: new ( max_width, max_height) ,
285
- )
222
+ fn bc ( _min_width : f64 , _min_height : f64 , max_width : f64 , max_height : f64 ) -> BoxConstraints {
223
+ BoxConstraints :: new ( Size :: new ( max_width, max_height) )
286
224
}
287
225
226
+ // TODO - Fix this test
288
227
#[ test]
289
228
fn constrain_aspect_ratio ( ) {
290
229
for ( bc, aspect_ratio, width, output) in [
@@ -373,7 +312,5 @@ mod tests {
373
312
fn unbounded ( ) {
374
313
assert ! ( !BoxConstraints :: UNBOUNDED . is_width_bounded( ) ) ;
375
314
assert ! ( !BoxConstraints :: UNBOUNDED . is_height_bounded( ) ) ;
376
-
377
- assert_eq ! ( BoxConstraints :: UNBOUNDED . min( ) , Size :: ZERO ) ;
378
315
}
379
316
}
0 commit comments