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

Correct ImageView handling of flex=1 sizing #2276

Merged
merged 4 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all 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 changes/2275.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ImageViews with ``flex=1`` will now shrink to fit if the image is larger than the available space.
8 changes: 4 additions & 4 deletions core/src/toga/widgets/imageview.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,22 @@ def rehint_imageview(image, style, scale=1):
width = int(style.width * scale)
height = int(style.width * scale / aspect_ratio)
if style.flex:
height = at_least(height)
height = at_least(0)
elif style.height != NONE:
# Explicit height, implicit width. Preserve aspect ratio.
aspect_ratio = image.width / image.height
width = int(style.height * scale * aspect_ratio)
height = int(style.height * scale)
if style.flex:
width = at_least(width)
width = at_least(0)
else:
# Use the image's actual size.
aspect_ratio = image.width / image.height
width = int(image.width * scale)
height = int(image.height * scale)
if style.flex:
width = at_least(width)
height = at_least(height)
width = at_least(0)
height = at_least(0)
else:
# No image. Hinted size is 0.
width = 0
Expand Down
12 changes: 6 additions & 6 deletions core/tests/widgets/test_imageview.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,14 @@ def test_set_image_none(app):
(dict(style=Pack(width=37, height=42)), 37, 42, None),
(dict(style=Pack(width=37, height=42), scale=2), 74, 84, None),
# Intrinsic image size, flex widget
(dict(style=Pack(flex=1)), at_least(144), at_least(72), 2),
(dict(style=Pack(flex=1), scale=2), at_least(288), at_least(144), 2),
(dict(style=Pack(flex=1)), at_least(0), at_least(0), 2),
(dict(style=Pack(flex=1), scale=2), at_least(0), at_least(0), 2),
# Fixed width, flex widget
(dict(style=Pack(width=150, flex=1)), 150, at_least(75), 2),
(dict(style=Pack(width=150, flex=1), scale=2), 300, at_least(150), 2),
(dict(style=Pack(width=150, flex=1)), 150, at_least(0), 2),
(dict(style=Pack(width=150, flex=1), scale=2), 300, at_least(0), 2),
# Fixed height, flex widget
(dict(style=Pack(height=80, flex=1)), at_least(160), 80, 2),
(dict(style=Pack(height=80, flex=1), scale=2), at_least(320), 160, 2),
(dict(style=Pack(height=80, flex=1)), at_least(0), 80, 2),
(dict(style=Pack(height=80, flex=1), scale=2), at_least(0), 160, 2),
# Explicit image size, flex widget
(dict(style=Pack(width=37, height=42, flex=1)), 37, 42, None),
(dict(style=Pack(width=37, height=42, flex=1), scale=2), 74, 84, None),
Expand Down
7 changes: 3 additions & 4 deletions docs/reference/api/widgets/imageview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,10 @@ Notes

* If an ImageView is given a style of ``flex=1``, and doesn't have an explicit size set
along its container's main axis, it will be allowed to expand and contract along that
axis.
axis, with the size determined by the flex allocation.

* If the cross axis size is unspecified, it will be determined by the image's aspect
ratio, with a minimum size in the main axis matching the size of the image in the
main axis.
* If the cross axis size is unspecified, it will be determined by applying the image's
aspect ratio to the size allocated on the main axis.

* If the cross axis has an explicit size, the image will be scaled to fill the
available space so that the entire image can be seen, while preserving its aspect
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/style/pack.rst
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ The mapping that can be used to establish the reference implementation is:
the HTML reference document. The rendering area of the browser window becomes
the view area that Pack will fill.

* Images map to ``<img>`` elements. The ``<img>`` element has an additional style of
* ImageViews map to ``<img>`` elements. The ``<img>`` element has an additional style of
``object-fit: contain`` unless *both* ``height`` and ``width`` are defined.

* All other elements in the DOM tree are mapped to ``<div>`` elements.
Expand Down