Skip to content

fix(ZoomContentControl): re-fit/center when AdditionalMargin changes - #1611

Merged
nickrandolph merged 4 commits into
mainfrom
dev/dr/zoomcontentcontrol-refit-on-additionalmargin
Jul 14, 2026
Merged

nickrandolph merged 4 commits into
mainfrom
dev/dr/zoomcontentcontrol-refit-on-additionalmargin

Conversation

@DerekRaven

Copy link
Copy Markdown
Contributor

GitHub Issue (If applicable): #

PR Type

  • Bugfix

What is the current behavior?

ZoomContentControl.AdditionalMargin insets the usable viewport — it is subtracted from the viewport in FitToCanvas and participates in the centering math (ClippedViewportSize, PaddedScaledContentSize). Every other input that affects the fitted/centered layout (viewport size, content size, content reload) re-runs the auto fit/center pass via UpdateScrollDetails(), but a change to AdditionalMargin does not — OnAdditionalMarginChanged only refreshed the scrollbars:

private void OnAdditionalMarginChanged()
{
    UpdateScrollBars();
    ViewportSizeChanged?.Invoke(this, EventArgs.Empty);
}

As a result, with AutoFitToCanvas="True" (and/or AutoCenterContent="True"), setting/changing AdditionalMargin after layout leaves the content fitted/centered against the previous margin. When the new margin is larger, the content can extend into the region the margin was meant to reserve; a later viewport/content/zoom change corrects it (the tell-tale "fixes itself on the next resize").

What is the new behavior?

A change to AdditionalMargin now re-runs the auto fit/center pass, consistent with a viewport-size change (a margin change is effectively a change to the usable viewport). OnAdditionalMarginChanged mirrors OnViewportSizeChanged:

private void OnAdditionalMarginChanged()
{
    UpdateScrollDetails();               // applies AutoFitToCanvas / AutoCenterContent, then scrollbars
    ViewportSizeChanged?.Invoke(this, EventArgs.Empty);
}

UpdateScrollDetails() already gates the fit/center on AutoFitToCanvas / AutoCenterContent, so the opt-in contract is preserved (no re-fit is forced when those are false), no new state is introduced, and the single existing "re-apply layout" path is reused. Scrollbars still update and ViewportSizeChanged is still raised.

Runtime tests (ZoomContentControlAdditionalMarginTests, red/fix/green):

  • When_AdditionalMarginIncreases_AndAutoFit_ThenRefitsToSmallerZoom — repro/regression guard: fit a small content in a fixed viewport, then set a large AdditionalMargin; the fitted ZoomLevel must decrease. Verified red (fails on the previous code — margin change did not re-fit) → green (passes with the fix), run headless via the Material sample under xvfb.
  • When_AdditionalMarginChanges_AndAutoFitDisabled_ThenZoomUnchanged — guard: with AutoFitToCanvas="False", a margin change does not alter ZoomLevel.

Design spec: specs/zoomcontentcontrol-refit-on-additionalmargin/spec.md.

PR Checklist

Please check if your PR fulfills the following requirements:

  • Tested code with current supported SDKs
  • Tested the changes where applicable:
    • WinUI (skia-desktop, runtime tests headless via the Material sample)
  • Updated the documentation as needed:
  • Runtime Tests and/or UI Tests for the changes have been added (for bug fixes / features) (if applicable)
  • Contains NO breaking changes
  • Associated with an issue (GitHub or internal) — self-contained toolkit behavioral fix; no external issue
  • Commits must be following the Conventional Commits specification.

Other information

  • Release build is clean (0 warnings / 0 errors under TreatWarningsAsErrors); Debug build clean.
  • No public API change — AdditionalMargin, AutoFitToCanvas, AutoCenterContent are unchanged; only when the fit/center re-runs.
  • Draft while the full multi-platform runtime matrix is pending (verified on skia-desktop).

- `AdditionalMargin` insets the usable viewport and participates in
  `FitToCanvas`/`CenterContent`, but `OnAdditionalMarginChanged` only refreshed
  the scrollbars, so content stayed fitted/centered against the previous margin
  until an unrelated size/zoom change re-fit it.
- Mirror `OnViewportSizeChanged`: call `UpdateScrollDetails()` so the auto
  fit/center pass re-runs (gated on `AutoFitToCanvas`/`AutoCenterContent`).
- Add runtime tests (red/fix/green): re-fit on margin increase; no forced
  re-fit when `AutoFitToCanvas` is disabled.
@github-actions

Copy link
Copy Markdown

@DerekRaven
DerekRaven marked this pull request as ready for review July 13, 2026 13:59
Copilot AI review requested due to automatic review settings July 13, 2026 13:59

Copilot AI 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.

Pull request overview

This PR fixes ZoomContentControl layout consistency by treating AdditionalMargin changes as a usable-viewport change: updating the margin now re-runs the same auto fit/center pipeline used for viewport size changes, preventing “it fixes itself on next resize” behavior.

Changes:

  • Call UpdateScrollDetails() from OnAdditionalMarginChanged() so AutoFitToCanvas / AutoCenterContent are re-applied when the margin changes.
  • Add runtime tests covering re-fit behavior when AdditionalMargin changes (with AutoFit enabled and disabled).
  • Update the control documentation (and add a small spec) to reflect the behavioral contract.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
src/Uno.Toolkit.UI/Controls/ZoomContentControl/ZoomContentControl.cs Re-run auto fit/center + scrollbar update when AdditionalMargin changes.
src/Uno.Toolkit.RuntimeTests/Tests/ZoomContentControlAdditionalMarginTests.cs Adds regression/guard runtime tests for AdditionalMargin triggering (or not triggering) refit.
specs/zoomcontentcontrol-refit-on-additionalmargin/spec.md Documents expected behavior and test requirements for the fix.
doc/controls/ZoomContentControl.md Clarifies AdditionalMargin semantics and that it triggers auto fit/center when enabled.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@unodevops

Copy link
Copy Markdown
Contributor

⚠️⚠️ The build 222039 has failed on Uno Toolkit - CI.

@nickrandolph
nickrandolph enabled auto-merge July 14, 2026 13:01
Fixes spellcheck failures on the ZoomContentControl AdditionalMargin
re-fit spec (lines 14 and 58).
@github-actions

Copy link
Copy Markdown

@nickrandolph
nickrandolph merged commit 030026c into main Jul 14, 2026
23 checks passed
@nickrandolph
nickrandolph deleted the dev/dr/zoomcontentcontrol-refit-on-additionalmargin branch July 14, 2026 18:17
@welcome

welcome Bot commented Jul 14, 2026

Copy link
Copy Markdown

Congrats on your first pull-request! We ❤ the people who are involved in this project, and we’d love to pay it forward by sending you some swag. If you have any feedback (or ideas how to improve Uno as a open-source project) please open a feedback issue.
giphy
Merci beaucoup 👋

@kazo0

kazo0 commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

/unobot prepare-release --commit-message-pattern "ci: Set version to '{0}'"

@unodevops

Copy link
Copy Markdown
Contributor

The release branch release/stable/9.0 has been created from 7aed779 and the PR #1613 has been created for the version bump.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants