Skip to content

fix(wasm): Redispatch resize once viewport settles after rotation - #24216

Merged
ajpinedam merged 12 commits into
unoplatform:masterfrom
ajpinedam:dev/anpi/fix.wasm.controls.dimension
Aug 28, 2026
Merged

ajpinedam merged 12 commits into
unoplatform:masterfrom
ajpinedam:dev/anpi/fix.wasm.controls.dimension

Conversation

@ajpinedam

Copy link
Copy Markdown
Contributor

GitHub Issue: n/a — customer-reported, tracked internally.

PR Type:

🐞 Bugfix

What changed? 🚀

Current behavior: in a WebAssembly app using the Skia renderer, rotating the device between portrait and landscape can leave the app rendered at the wrong dimensions (controls squished / at the wrong scale). Some mobile browsers — observed with Chrome on iPad, while Safari is fine — fire resize / orientationchange before the layout viewport (document.documentElement) has actually reflowed to the new size, and never fire a corrective resize afterward. Since WebAssemblyWindowWrapper sizes the window (and thus the Skia surface) from documentElement.getBoundingClientRect() inside that early resize handler, it keeps the stale pre-rotation dimensions until some other event forces a re-measure. Because rendering and input mapping both use the stale size, taps also land offset from the visuals.

This PR installs a ResizeObserver on document.documentElement — the exact element resize() measures from — in the Skia WASM host. ResizeObserver fires after layout, i.e. precisely when the element has actually settled to its post-rotation size. When the observed size differs from the last size the host measured, it re-dispatches a synthetic window.resize, restoring the corrective event the browser should have fired; the existing resize path then re-measures against the settled dimensions. The host records the last measured size on every resize(), so when a native resize already handled the change the observer callback no-ops — no redundant dispatches, no loops (the observer only fires on actual size changes). Browsers without ResizeObserver keep the previous behavior.

The fix is host-level and benefits app code too: application-level window.resize listeners were equally stale, and now receive the corrective event. Dispatching the global event (rather than re-measuring internally) is deliberate — BrowserRenderer sizes the canvas from its own window.resize listener, so an internal-only re-measure would fix the window bounds but leave the canvas stale.

Design notes / scope:

  • The window.resize listener stays the primary size source; the observer is only the corrective for the mis-ordered case. The native listener uniquely covers DPR-only resizes (browser zoom fires resize without changing documentElement's CSS size, which a ResizeObserver cannot see).
  • No feedback-loop risk: uno.css pins html/body and the canvas to position: fixed; width/height: 100%; overflow: hidden, so the viewport drives documentElement — never the canvas — and resize() records the last measured size, making the observer a no-op whenever a native resize already handled the change.
  • The native (non-Skia) WebAssembly target has the same measure-on-resize pattern in WindowManager.ts and may exhibit the same staleness, but per the Skia-first policy it is maintenance-only and intentionally out of scope here.
  • DPR-only staleness (RasterizationScale on zoom/monitor moves) is a pre-existing, unrelated behavior and is unchanged.

PR Checklist ✅

  • 🧪 Added Runtime tests, UI tests, or a manual test sample (for bug fixes / features, if applicable)
    • The race requires physically rotating a device and a browser that mis-orders the events, so it is not runtime-testable. Added the manual sample Wasm_ViewportOrientationTelemetry (Wasm category), which live-compares XamlRoot.Size against documentElement.getBoundingClientRect() with a STALE/OK banner and raw resize/orientationchange counters, so the symptom and the fix are directly observable on a device.
    • Manual validation on a physical iPad in Chrome (the affected combination): without the fix the stale size reproduces after rotation and sticks; with the fix (validated both as the patched host in the published SamplesApp.Skia.WebAssembly.Browser served over LAN, and as the equivalent logic A/B-toggled in a telemetry repro app) every rotation converges to the settled viewport size. Safari behavior is unchanged.
  • 📚 Docs have been added/updated following the documentation template (for bug fixes / features)
  • 🖼️ Validated PR Screenshots Compare Test Run results.
  • ❗ Contains NO breaking changes

Credit: the root-cause analysis and the proposed ResizeObserver approach come from the reporting customer's investigation.

Copilot AI lite review requested due to automatic review settings August 26, 2026 21:46
@github-actions github-actions Bot added platform/wasm 🌐 Categorizes an issue or PR as relevant to the WebAssembly platform area/automation Categorizes an issue or PR as relevant to project automation labels Aug 26, 2026

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 addresses a WebAssembly Skia host sizing issue where some mobile browsers can fire window.resize before document.documentElement has reflowed after rotation, leaving the Skia surface (and input mapping) stuck with stale dimensions. It adds a ResizeObserver-based corrective path in the WASM host and includes a manual SamplesApp telemetry page to validate behavior on real devices/browsers.

Changes:

  • Add a ResizeObserver on document.documentElement to re-dispatch a synthetic window.resize once the viewport has settled after rotation.
  • Harden the managed JS-exported resize callback to prevent exceptions from escaping into the JS DOM-event callback.
  • Add a manual telemetry sample to compare XamlRoot.Size vs DOM viewport size and show “STALE/OK”.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
src/Uno.UI.Runtime.Skia.WebAssembly.Browser/UI/Xaml/Window/WebAssemblyWindowWrapper.cs Wraps JS-exported resize callback with try/catch and routes managed exceptions to recoverable handling.
src/Uno.UI.Runtime.Skia.WebAssembly.Browser/ts/Runtime/WebAssemblyWindowWrapper.ts Adds ResizeObserver on documentElement to re-dispatch resize after post-rotation layout settles.
src/SamplesApp/SamplesApp.Samples/Wasm/Wasm_ViewportOrientationTelemetry.xaml.cs Adds manual telemetry page that polls DOM viewport rect + event counters and compares to XamlRoot.Size.
src/SamplesApp/SamplesApp.Samples/Wasm/Wasm_ViewportOrientationTelemetry.xaml UI for the telemetry page (status banner + reported sizes/event counters).

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

@unodevops

Copy link
Copy Markdown
Contributor

Copilot AI review requested due to automatic review settings August 26, 2026 22:29

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

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.

@ajpinedam

Copy link
Copy Markdown
Contributor Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@unodevops

Copy link
Copy Markdown
Contributor

Copilot AI review requested due to automatic review settings August 28, 2026 01:27

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

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

Suppressed comments (2)

Previously missed (1) — in code that hasn't changed since the last review.

src/Uno.UI.Runtime.Skia.WebAssembly.Browser/UI/Xaml/Window/WebAssemblyWindowWrapper.cs:107

  • In this JSExport callback, the null-safe exception helper logs using sender.GetType() when Application.Current is null. Passing typeof(WebAssemblyWindowWrapper) means the log category becomes System.RuntimeType, which makes startup exceptions harder to triage. Capture the actual WebAssemblyWindowWrapper instance (when available) and pass it to RaiseRecoverableUnhandledExceptionOrLog; fall back to typeof(WebAssemblyWindowWrapper) only when no instance was created.

This issue also appears on line 133 of the same file.

		{
			// A managed exception must not cross back into the JS DOM-event callback.
			// Null-safe: the initial resize runs before Application.Start.
			Application.Current.RaiseRecoverableUnhandledExceptionOrLog(e, typeof(WebAssemblyWindowWrapper));
		}

src/Uno.UI.Runtime.Skia.WebAssembly.Browser/UI/Xaml/Window/WebAssemblyWindowWrapper.cs:137

  • Same as OnResize: when Application.Current is null, passing typeof(WebAssemblyWindowWrapper) to RaiseRecoverableUnhandledExceptionOrLog logs under System.RuntimeType. Preserve the real wrapper instance (when available) so early-startup exceptions are logged under the correct type.
		{
			// A managed exception must not cross back into the JS DOM-event callback.
			// Null-safe: visualViewport events can fire before Application.Start.
			Application.Current.RaiseRecoverableUnhandledExceptionOrLog(e, typeof(WebAssemblyWindowWrapper));
		}

@unodevops

Copy link
Copy Markdown
Contributor

@unodevops

Copy link
Copy Markdown
Contributor

⚠️⚠️ The build 230693 has failed on Uno.UI - CI.

Copilot AI review requested due to automatic review settings August 28, 2026 05:33

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

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.

@unodevops

Copy link
Copy Markdown
Contributor

@unodevops

Copy link
Copy Markdown
Contributor

⚠️⚠️ The build 230704 has failed on Uno.UI - CI.

@mergify

mergify Bot commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

@ajpinedam
ajpinedam merged commit cf65a28 into unoplatform:master Aug 28, 2026
48 checks passed
@ajpinedam
ajpinedam deleted the dev/anpi/fix.wasm.controls.dimension branch August 28, 2026 14:56
@ajpinedam

Copy link
Copy Markdown
Contributor Author

@Mergifyio backport release/stable/6.7

@mergify

mergify Bot commented Aug 28, 2026 •

Copy link
Copy Markdown
Contributor

backport release/stable/6.7

✅ Backports have been created

Details

kazo0 added a commit that referenced this pull request Aug 29, 2026
…6.7/pr-24216

fix(wasm): Redispatch resize once viewport settles after rotation (backport #24216)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/automation Categorizes an issue or PR as relevant to project automation platform/wasm 🌐 Categorizes an issue or PR as relevant to the WebAssembly platform

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants