Skip to content

Commit 94ad9c6

Browse files
committed
Fix Fill options for non Fixed width options
1 parent 0c0851f commit 94ad9c6

File tree

1 file changed

+8
-15
lines changed

1 file changed

+8
-15
lines changed

druid/src/widget/image.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -190,19 +190,14 @@ impl<T: Data> Widget<T> for Image {
190190
bc.debug_check("Image");
191191

192192
// If either the width or height is constrained calculate a value so that the image fits
193-
// in the size exactly. If it is unconstrained by both width and height take the size of
194-
// the image.
193+
// in the size exactly. If it is unconstrained by both width and height then use the fill
194+
// strategy to determine the size.
195195
let max = bc.max();
196196
let image_size = self.image_data.size();
197-
let size = if bc.is_width_bounded() && !bc.is_height_bounded() {
198-
let ratio = max.width / image_size.width;
199-
Size::new(max.width, ratio * image_size.height)
200-
} else if bc.is_height_bounded() && !bc.is_width_bounded() {
201-
let ratio = max.height / image_size.height;
202-
Size::new(ratio * image_size.width, max.height)
203-
} else {
204-
bc.constrain(self.image_data.size())
205-
};
197+
let affine = self.fill.affine_to_fill(max, image_size).as_coeffs();
198+
// The first and forth elements of the affine are the x and y scale factor.
199+
// So just multiply them by the original size to get the ideal area based on `self.fill`.
200+
let size = Size::new(affine[0] * image_size.width, affine[3] * image_size.height);
206201
trace!("Computed size: {}", size);
207202
size
208203
}
@@ -367,20 +362,18 @@ mod tests {
367362

368363
// A middle row of 600 pixels is 100 padding 200 black, 200 white and then 100 padding.
369364
let expecting: Vec<u8> = [
370-
vec![41, 41, 41, 255].repeat(100),
371365
vec![255, 255, 255, 255].repeat(200),
372366
vec![0, 0, 0, 255].repeat(200),
373-
vec![41, 41, 41, 255].repeat(100),
367+
vec![41, 41, 41, 255].repeat(200),
374368
]
375369
.concat();
376370
assert_eq!(raw_pixels[199 * 600 * 4..200 * 600 * 4], expecting[..]);
377371

378372
// The final row of 600 pixels is 100 padding 200 black, 200 white and then 100 padding.
379373
let expecting: Vec<u8> = [
380-
vec![41, 41, 41, 255].repeat(100),
381374
vec![0, 0, 0, 255].repeat(200),
382375
vec![255, 255, 255, 255].repeat(200),
383-
vec![41, 41, 41, 255].repeat(100),
376+
vec![41, 41, 41, 255].repeat(200),
384377
]
385378
.concat();
386379
assert_eq!(raw_pixels[399 * 600 * 4..400 * 600 * 4], expecting[..]);

0 commit comments

Comments
 (0)