Skip to content

fix(swiftui): map Image.fit with CSS object-fit semantics, as a2ui_flutter does - #4097

Merged
pinieb merged 5 commits into
a2ui-project:mainfrom
karolkulesza:fix/swiftui-image-fit
Nov 12, 2026
Merged

pinieb merged 5 commits into
a2ui-project:mainfrom
karolkulesza:fix/swiftui-image-fit

Conversation

@karolkulesza

@karolkulesza karolkulesza commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Description

A2UIImage mapped fit: "fill" (the catalog default) to aspect-fill, which crops instead of stretching. none took the same path, and scaleDown scaled small images up.

This PR adds an A2UIImage.Fit enum (package access), which reads the property and lays the image out with CSS object-fit rules, as a2ui_flutter does with BoxFit:

  1. fill stretches the image to the frame.
  2. contain scales it to fit, keeping the aspect ratio.
  3. cover scales it to cover the frame and clips the overflow.
  4. none keeps the intrinsic size, centered and clipped.
  5. scaleDown uses none or contain, whichever is smaller.

The variant frames are unchanged. The smallFeature, mediumFeature and largeFeature variants now size the frame from the image's aspect ratio, like an <img> with max-width: 100%; height: auto, so fill doesn't stretch an image to the full width when a scroll view leaves the height open, and an image smaller than the frame isn't scaled up. One visible change: when the height is capped (for example largeFeature at 400 points with a tall image), the image keeps its proportions and is letterboxed instead of cropped, as an <img> is on the web.

avatar and header now honor an explicit fit, and header crops a wide image to the container's width instead of widening it. Without an explicit fit they still default to cover (Flutter does the same for header, but uses fill for avatar). An unknown value falls back to the default.

Tests: the new A2UIImageTests cover every catalog value, the defaults per variant, unknown values, the laid-out size of each fit for an image smaller and larger than its frame, and the size of the header, mediumFeature and largeFeature variants. The full swift test run passes, and the sample app builds for the iOS Simulator.

Fixes #4096

Pre-launch Checklist

One time:

For this PR:

  • I have updated the relevant CHANGELOG.md file. (The Swift packages have no CHANGELOG.md.)
  • I updated/added relevant documentation.
  • My code changes (if any) have tests.
  • If my branch is on a fork, I have verified that scripts/e2e_test.sh passes.

If you need help, consider asking for advice on the discussion board.

…utter does

The catalog default fit "fill" mapped to aspect-fill, which crops
instead of stretching, "none" took the same path, and "scaleDown"
scaled small images up. Add A2UIImage.Fit, which resolves the property
and lays the image out with object-fit rules.

The feature variants size their frame from the image's aspect ratio,
like an <img> with an automatic height, so "fill" does not stretch an
image when a scroll view leaves the height open. The avatar and header
variants now honor an explicit fit and still default to cover.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new Fit enum to A2UIImage to support CSS object-fit semantics (contain, cover, fill, none, scaleDown) and adds corresponding unit tests. Feedback on the changes highlights two key issues: first, the need to re-apply .clipped() to several feature variants to prevent overflow bleeding; second, a layout constraint issue in aspectBox that breaks cover and fill behaviors when the frame height is capped, which can be resolved by conditionally applying the aspect-ratio container.

@github-actions github-actions Bot added the status: needs-triage auto-managed: https://github.com/a2ui-project/a2ui/blob/main/scripts/triage.mjs label Nov 10, 2026
@pinieb pinieb added status: waiting-for-author-response lang: swift Affects SDKs or apps in the Swift programming language component: SwiftUI adapter Swift UI framework adapter and removed status: needs-triage auto-managed: https://github.com/a2ui-project/a2ui/blob/main/scripts/triage.mjs labels Nov 10, 2026
…s upscaling

The header variant reported the fitted image's width, so a wide banner
with cover or none pushed its column wider than the container. It now
sizes a 200-point slot with Color.clear and fits the image in an overlay.

The feature variants sized their aspect-ratio box with an aspect-fit
image, which scales a small image up to the full width. The box now uses
scaleDown, like max-width: 100%; height: auto on the web.

Fit and applyVariantSizing are package instead of public, the fallback
in Fit.init is a guard and a switch, and the tests lay out each variant.
@github-actions github-actions Bot added status: needs-triage auto-managed: https://github.com/a2ui-project/a2ui/blob/main/scripts/triage.mjs and removed status: waiting-for-author-response labels Nov 10, 2026

@pinieb pinieb left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR!

@pinieb
pinieb merged commit eb1fd7f into a2ui-project:main Nov 12, 2026
48 checks passed
pinieb added a commit that referenced this pull request Nov 13, 2026
# Description

Backfills missing `## Unreleased` entries in
[`swift/CHANGELOG.md`](https://github.com/a2ui-project/a2ui/blob/b52b2cda6/swift/CHANGELOG.md?plain=1#L5-L58)
for Swift pull requests that merged since
[`v0.10.0+swift`](https://github.com/a2ui-project/a2ui/releases/tag/v0.10.0%2Bswift)
(`ae2b6baae`) while
[#4133](#4133) was open or
shortly after it landed:

1. [#4140](#4140),
[#4037](#4037), and
[#4044](#4044) (**BREAKING
CHANGE**): Gate component `metadata` and `catalogId` envelope handling
to v1.0 and later surfaces, and support `@catalogId`, `@metadata`,
`@accessibility`, and `@checks` on v1.0 components with dual-read
fallback for undeclared bare keys.
2. [#4022](#4022) (**BREAKING
CHANGE**): Migrate `A2UIAudioPlayerModel` and `A2UIVideoPlayerModel`
from `ObservableObject` to `@Observable`, take direct model instances in
`A2UIAudioPlayer.init(node:model:)` and `A2UIVideo.init(node:model:)`,
skip redundant `AVPlayer` seeks during `A2UIAudioPlayer` playback, and
adopt `ContentUnavailableView` and a poster play button accessibility
label in `A2UIVideo`.
3. [#4030](#4030): Inherit the
enclosing function's resolved `catalogId` for catalogless calls inside
v1.0 `FormatStringFunction` `${...}` template expressions, add
`DataContext.activeFunctionCatalogID` and
`FunctionHandler.resolvedFunctionCatalogID(forCallCatalogID:)`, and
scope `RPCHandler` `callRendererFunction` evaluation to the target
catalog.
4. [#4092](#4092): Include
`$defs.anyComponent` and `$defs.anyFunction` unions in v1.0 or later
inline catalogs emitted by
`MessageProcessor.getRendererCapabilities(options:)`.
5. [#4097](#4097): Map
`A2UIImage` `fit` values (`contain`, `cover`, `fill`, `none`,
`scaleDown`) to CSS `object-fit` semantics and fix variant sizing in
`BasicCatalogSwiftUI`.
6. [#4099](#4099): Drop `theme`
from v1.0 `createSurface` payloads when
`ValidationConfig.allowUnknownElements` is `true` and add
`allowUnknownElements` overloads to `VersionAdapter` and
`BaseVersionAdapter`.

## Pre-launch Checklist

One time:

- [x] I signed the [CLA].
- [x] I read the [Contributors Guide].
- [x] I read the [Style Guide].
- [x] I have built and run at least one [sample].

For this PR:

- [x] I have updated the relevant CHANGELOG.md file.
- [x] I updated/added relevant documentation.
- [x] My code changes (if any) have tests.
- [x] If my branch is on a fork, I have verified that
[scripts/e2e_test.sh](https://github.com/a2ui-project/a2ui/blob/main/scripts/e2e_test.sh)
passes.

If you need help, consider asking for advice on the [discussion board].

<!-- Links -->

[CLA]: https://cla.developers.google.com/
[Contributors Guide]:
https://github.com/a2ui-project/a2ui/blob/main/CONTRIBUTING.md
[discussion board]: https://github.com/a2ui-project/a2ui/discussions
[Style Guide]:
https://github.com/a2ui-project/a2ui/blob/main/CONTRIBUTING.md#coding-style
[sample]:
https://github.com/a2ui-project/a2ui/blob/main/samples/README.md#samples

---------

Signed-off-by: Pete Biencourt <piebie@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: SwiftUI adapter Swift UI framework adapter lang: swift Affects SDKs or apps in the Swift programming language status: needs-triage auto-managed: https://github.com/a2ui-project/a2ui/blob/main/scripts/triage.mjs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: SwiftUI Image crops for fit "fill" and "none" instead of following CSS object-fit

2 participants