Skip to content

Commit b38e873

Browse files
authored
Fix layout of image when either width or height is bounded, but not the other (#1189)
1 parent afdcb0c commit b38e873

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed

AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ Leopold Luley
1313
Andrey Kabylin
1414
Garrett Risley
1515
Robert Wittams
16+
Jaap Aarts

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ You can find its changes [documented below](#060---2020-06-01).
9797
- Multi-click on Windows, partial fix for #859 ([#1157] by [@raphlinus])
9898
- Windows: fix crash on resize from incompatible resources ([#1191 by [@raphlinus]])
9999
- GTK: Related dependencies are now optional, facilitating a pure X11 build. ([#1241] by [@finnerale])
100+
- `widget::Image` now computes the layout correctly when unbound in one direction. ([#1189] by [@JAicewizard])
100101

101102
### Visual
102103

@@ -322,6 +323,7 @@ Last release without a changelog :(
322323
[@rhzk]: https://github.com/rhzk
323324
[@koutoftimer]: https://github.com/koutoftimer
324325
[@tay64]: https://github.com/tay64
326+
[@JAicewizard]: https://github.com/JAicewizard
325327

326328
[#599]: https://github.com/linebender/druid/pull/599
327329
[#611]: https://github.com/linebender/druid/pull/611
@@ -465,6 +467,7 @@ Last release without a changelog :(
465467
[#1185]: https://github.com/linebender/druid/pull/1185
466468
[#1191]: https://github.com/linebender/druid/pull/1191
467469
[#1092]: https://github.com/linebender/druid/pull/1092
470+
[#1189]: https://github.com/linebender/druid/pull/1189
468471
[#1195]: https://github.com/linebender/druid/pull/1195
469472
[#1204]: https://github.com/linebender/druid/pull/1204
470473
[#1205]: https://github.com/linebender/druid/pull/1205

druid/src/widget/image.rs

+67-2
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,17 @@ impl<T: Data> Widget<T> for Image {
145145
) -> Size {
146146
bc.debug_check("Image");
147147

148-
if bc.is_width_bounded() {
149-
bc.max()
148+
// If either the width or height is constrained calculate a value so that the image fits
149+
// in the size exactly. If it is unconstrained by both width and height take the size of
150+
// the image.
151+
let max = bc.max();
152+
let image_size = self.image_data.size();
153+
if bc.is_width_bounded() && !bc.is_height_bounded() {
154+
let ratio = max.width / image_size.width;
155+
Size::new(max.width, ratio * image_size.height)
156+
} else if bc.is_height_bounded() && !bc.is_width_bounded() {
157+
let ratio = max.height / image_size.height;
158+
Size::new(ratio * image_size.width, max.height)
150159
} else {
151160
bc.constrain(self.image_data.size())
152161
}
@@ -317,4 +326,60 @@ mod tests {
317326
},
318327
);
319328
}
329+
330+
#[test]
331+
fn width_bound_layout() {
332+
use crate::{
333+
tests::harness::Harness,
334+
widget::{Container, Scroll},
335+
WidgetExt, WidgetId,
336+
};
337+
use float_cmp::approx_eq;
338+
339+
let id_1 = WidgetId::next();
340+
let image_data = ImageBuf::from_raw(
341+
vec![255, 255, 255, 0, 0, 0, 0, 0, 0, 255, 255, 255],
342+
ImageFormat::Rgb,
343+
2,
344+
2,
345+
);
346+
347+
let image_widget =
348+
Scroll::new(Container::new(Image::new(image_data)).with_id(id_1)).vertical();
349+
350+
Harness::create_simple(true, image_widget, |harness| {
351+
harness.send_initial_events();
352+
harness.just_layout();
353+
let state = harness.get_state(id_1);
354+
assert!(approx_eq!(f64, state.layout_rect().x1, 400.0));
355+
})
356+
}
357+
358+
#[test]
359+
fn height_bound_layout() {
360+
use crate::{
361+
tests::harness::Harness,
362+
widget::{Container, Scroll},
363+
WidgetExt, WidgetId,
364+
};
365+
use float_cmp::approx_eq;
366+
367+
let id_1 = WidgetId::next();
368+
let image_data = ImageBuf::from_raw(
369+
vec![255, 255, 255, 0, 0, 0, 0, 0, 0, 255, 255, 255],
370+
ImageFormat::Rgb,
371+
2,
372+
2,
373+
);
374+
375+
let image_widget =
376+
Scroll::new(Container::new(Image::new(image_data)).with_id(id_1)).horizontal();
377+
378+
Harness::create_simple(true, image_widget, |harness| {
379+
harness.send_initial_events();
380+
harness.just_layout();
381+
let state = harness.get_state(id_1);
382+
assert!(approx_eq!(f64, state.layout_rect().x1, 400.0));
383+
})
384+
}
320385
}

0 commit comments

Comments
 (0)