Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix layout of image when either width or height is bounded, but not the other #1189

Merged
merged 23 commits into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ Leopold Luley
Andrey Kabylin
Garrett Risley
Robert Wittams
Jaap Aarts
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ You can find its changes [documented below](#060---2020-06-01).

### Fixed

- `widget::Imge` now computes the layout correctly when unbound in one direction. ([#1189] by [@JAicewizard])
JAicewizard marked this conversation as resolved.
Show resolved Hide resolved
- macOS: Timers not firing during modal loop. ([#1028] by [@xStrom])
- GTK: Directory selection now properly ignores file filters. ([#957] by [@xStrom])
- GTK: Don't crash when receiving an external command while a file dialog is visible. ([#1043] by [@jneem])
Expand Down Expand Up @@ -278,7 +279,7 @@ Last release without a changelog :(
[@sysint64]: https://github.com/sysint64
[@justinmoon]: https://github.com/justinmoon
[@rjwittams]: https://github.com/rjwittams

[@JAicewizard]: https://github.com/JAicewizard
JAicewizard marked this conversation as resolved.
Show resolved Hide resolved
[#599]: https://github.com/linebender/druid/pull/599
[#611]: https://github.com/linebender/druid/pull/611
[#695]: https://github.com/linebender/druid/pull/695
Expand Down Expand Up @@ -412,7 +413,7 @@ Last release without a changelog :(
[#1171]: https://github.com/linebender/druid/pull/1171
[#1172]: https://github.com/linebender/druid/pull/1172
[#1173]: https://github.com/linebender/druid/pull/1173

[#1186]: https://github.com/linebender/druid/pull/1186
JAicewizard marked this conversation as resolved.
Show resolved Hide resolved
JAicewizard marked this conversation as resolved.
Show resolved Hide resolved
[Unreleased]: https://github.com/linebender/druid/compare/v0.6.0...master
[0.6.0]: https://github.com/linebender/druid/compare/v0.5.0...v0.6.0
[0.5.0]: https://github.com/linebender/druid/compare/v0.4.0...v0.5.0
Expand Down
115 changes: 113 additions & 2 deletions druid/src/widget/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,19 @@ impl<T: Data> Widget<T> for Image {
) -> Size {
bc.debug_check("Image");

if bc.is_width_bounded() {
bc.max()
// If either the width or height is constrained calculate a value so that the image fits
// in the size exactly. If it is unconstrained by both width and height take the size of
// the image.
if !bc.is_height_bounded() && bc.is_width_bounded() {
let mut size = bc.max();
let ratio = size.width / self.image_data.x_pixels as f64;
size.height = ratio * self.image_data.y_pixels as f64;
size
ForLoveOfCats marked this conversation as resolved.
Show resolved Hide resolved
} else if !bc.is_width_bounded() && bc.is_height_bounded() {
let mut size = bc.max();
let ratio = size.height / self.image_data.y_pixels as f64;
size.width = ratio * self.image_data.x_pixels as f64;
size
} else {
bc.constrain(self.image_data.get_size())
}
Expand Down Expand Up @@ -438,4 +449,104 @@ mod tests {
},
);
}

#[test]
fn width_bound_paint() {
use crate::{tests::harness::Harness, widget::Scroll, WidgetId};
let _id_1 = WidgetId::next();
let image_data = ImageData {
pixels: vec![255, 255, 255, 0, 0, 0, 0, 0, 0, 255, 255, 255],
x_pixels: 2,
y_pixels: 2,
format: ImageFormat::Rgb,
};

let image_widget =
Image::new(image_data).interpolation_mode(InterpolationMode::NearestNeighbor);

Harness::create_with_render(
true,
Scroll::new(image_widget).vertical(),
Size::new(400., 400.),
|harness| {
harness.send_initial_events();
harness.just_layout();
harness.paint();
},
|target| {
// the width should be calculated to be 400.
let width = 400;
let raw_pixels = target.into_raw();
assert_eq!(raw_pixels.len(), 400 * width * 4);

// Being a height bound widget every row will have no padding at the start and end._id_1

// The image starts at (0,0), so 200 white and then 200 black.
JAicewizard marked this conversation as resolved.
Show resolved Hide resolved
let expecting: Vec<u8> = [
vec![255, 255, 255, 255].repeat(200),
vec![0, 0, 0, 255].repeat(200),
]
.concat();
assert_eq!(raw_pixels[199 * width * 4..200 * width * 4], expecting[..]);

// The final row of 400 pixels is 200 white and then 200 black.
JAicewizard marked this conversation as resolved.
Show resolved Hide resolved
let expecting: Vec<u8> = [
vec![0, 0, 0, 255].repeat(200),
vec![255, 255, 255, 255].repeat(200),
]
.concat();
assert_eq!(raw_pixels[200 * width * 4..201 * width * 4], expecting[..]);
},
);
}

#[test]
fn height_bound_paint() {
use crate::{tests::harness::Harness, widget::Scroll, WidgetId};
let _id_1 = WidgetId::next();
let image_data = ImageData {
pixels: vec![255, 255, 255, 0, 0, 0, 0, 0, 0, 255, 255, 255],
x_pixels: 2,
y_pixels: 2,
format: ImageFormat::Rgb,
};

let image_widget =
Image::new(image_data).interpolation_mode(InterpolationMode::NearestNeighbor);

Harness::create_with_render(
true,
Scroll::new(image_widget).horizontal(),
Size::new(400., 400.),
|harness| {
harness.send_initial_events();
harness.just_layout();
harness.paint();
},
|target| {
// the width should be calculated to be 400.
let width = 400;
let raw_pixels = target.into_raw();
assert_eq!(raw_pixels.len(), 400 * width * 4);

// Being a height bound widget every row will have no padding at the start and end._id_1

// The image starts at (0,0), so 200 black and then 200 white.
JAicewizard marked this conversation as resolved.
Show resolved Hide resolved
let expecting: Vec<u8> = [
vec![255, 255, 255, 255].repeat(200),
vec![0, 0, 0, 255].repeat(200),
]
.concat();
assert_eq!(raw_pixels[199 * width * 4..200 * width * 4], expecting[..]);

// The final row of 400 pixels is 200 white and then 200 black.
JAicewizard marked this conversation as resolved.
Show resolved Hide resolved
let expecting: Vec<u8> = [
vec![0, 0, 0, 255].repeat(200),
vec![255, 255, 255, 255].repeat(200),
]
.concat();
assert_eq!(raw_pixels[200 * width * 4..201 * width * 4], expecting[..]);
},
);
}
}