Skip to content

fix: re-measure and re-render BlueprintView after its window resizes - #633

Open
robmaceachern wants to merge 2 commits into
mainfrom
robmaceachern/blueprintview-window-size-invalidation
Open

robmaceachern wants to merge 2 commits into
mainfrom
robmaceachern/blueprintview-window-size-invalidation

Conversation

@robmaceachern

@robmaceachern robmaceachern commented Nov 18, 2026 •

Copy link
Copy Markdown
Member
🤖 Agent details

Contribution: Implementation and PR description

Agent: Claude Code · Claude Opus 5.5

BlueprintView sets Environment.windowSize from its window, so elements can size and lay out from the window. Its caches didn't account for that. After the window resized, a view whose own bounds stayed the same kept results computed for the old window size:

  • sizeThatFits(_:) returned the cached size for the same SizeConstraint, because only setNeedsViewHierarchyUpdate() clears sizesThatFit. That runs when the element, environment, layout mode or safe area changes, or when the view moves to a window. A window resize does none of these.
  • updateViewHierarchyIfNeeded() skipped the update unless needsViewHierarchyUpdate was set or bounds had changed, so a layout pass rendered nothing new.

A fixed-width alert is a typical case: it is proposed the same width and height before and after its window narrows, so it keeps the width it measured in the first window.

How it works

  • sizeThatFits(_ constraint: SizeConstraint) records the window size its cached measurements were made with (sizesThatFitWindowSize). When window?.bounds.size differs, it clears sizesThatFit before the cache lookup. A cache hit now also reads window?.bounds.size once; nothing else changes on that path.
  • updateViewHierarchyIfNeeded() records the window size of its last update (lastViewHierarchyUpdateWindowSize) and updates when it differs, as it already does for a bounds change. A different window size also calls invalidateIntrinsicContentSize(), because Auto Layout may still hold an intrinsic size measured in the old window. That holds even when an update was already pending: Auto Layout can read the intrinsic size between an element change and the render that follows it. On a view's first layout in a window, this invalidation also makes Auto Layout measure again a view it measured before the view had a width. A view whose height depends on its width, such as wrapping text, then renders a second time in the same layout pass, at its full height; before this change it kept the height measured without a width. If Auto Layout already measured the view at its width in that window, its second query is a cache hit.
  • An off-window view reports a nil window size. didMoveToWindow() already invalidates both caches, and the base environment (which supplies windowSize off-window) invalidates them when it is set.

Views that UIKit doesn't lay out

UIKit doesn't call layoutSubviews() on a view whose own bounds, safe area and traits stay the same while its window resizes. A BlueprintView like that is measured again whenever its host asks for a size, but it renders again only on its next layout pass. This PR documents that limit on BlueprintView (a "Window size changes" section) and on Environment.windowSize. When an element in such a view reads windowSize, its host calls setNeedsLayout() on the view, for example from viewWillTransition(to:with:). The checks above make that one call enough: it re-renders and invalidates the intrinsic size. Before this change, a host had to reassign element to get the same result.

Observing the window's size from every BlueprintView was considered and not shipped:

  • UIKit doesn't document UIWindow.frame or bounds as key-value observable, and scene-driven resizes aren't guaranteed to go through their setters.
  • UIWindowScene.effectiveGeometry is observable, but only on iOS 16 and later (Blueprint supports iOS 15). It also describes the scene, not the window. Every BlueprintView would observe it and invalidate on each step of a live resize, including the many views whose bounds change anyway and whose elements never read windowSize.

Validation

  • New tests in BlueprintViewTests, each changing only the size of the view's window:
    • test_sizeThatFits_remeasuresWhenOnlyTheWindowResizes: the same fitting size returns the new measurement, and a repeat measurement in the same window still hits the cache.
    • test_layout_rendersWithTheNewWindowSizeWhenOnlyTheWindowResizes: setNeedsLayout() re-renders with the new windowSize.
    • test_intrinsicContentSize_updatesWhenOnlyTheWindowResizes: a view with a fixed-width constraint gets its new height from Auto Layout after setNeedsLayout().
    • test_intrinsicContentSize_updatesWhenTheWindowResizesBeforeAPendingRender: the same, when Auto Layout read the intrinsic size after an element change and the window resized before the view rendered.
    • test_rotation_rendersOnceInTheRotatedWindow_whenTheViewResizesWithTheWindow and test_rotation_rendersOnceInTheRotatedWindow_whenTheViewKeepsItsSize: a 390 × 844 window rotates to 844 × 390, once with the view resizing with it and once with the view keeping its size while its host calls setNeedsLayout(). Each rotation renders the view once with the rotated window's size, another layout pass renders nothing, and the view measures once in the rotated window, then reuses that measurement.
  • Each new test fails on unchanged main with the old window's value (350 instead of 250 points wide, tag 700 instead of 500, 70 instead of 50 points tall, and after a rotation 195 instead of 422 points wide). On main, the view that keeps its size doesn't render after the rotation. The first intrinsic-size test also fails when only the invalidateIntrinsicContentSize() call is removed. The pending-render test fails when that call is skipped while an update is pending.
  • All 211 BlueprintUI-Tests pass through the Tuist UnitTests scheme on iPhone SE (3rd generation), iOS 17.5, and on iPhone 17 Pro, iOS 26.5 (Xcode 26.5). Pinned SwiftFormat 0.50.8 reports no changes needed. BlueprintUICommonControls tests weren't run locally.
  • Rotated a sample app screen (not part of this PR) on iPad Pro 13-inch (M5) and iPhone 17 Pro simulators, iOS 26.5. The screen hosts BlueprintViews with Auto Layout, each sized by its intrinsic height. A is full width, with a bar whose height is an eighth of the window's width. B and C are 300 points wide with the same bar; B's view controller calls setNeedsLayout() from viewWillTransition(to:with:), and C's doesn't. D is full width, with wrapping text that doesn't read windowSize.
    • On iPad, where rotation keeps the size classes, A and D render once per rotation at their new heights. B renders twice per rotation, 1–2 ms apart inside the rotation animation: first with the new window size at its old height, then at its new height. Nothing renders after the animation, and nothing snaps. C keeps the old window size, as documented. On main, A renders once but keeps its old height, so its label truncates, and B and C don't render.
    • On its first layout, D renders twice and shows all its text. On main, it renders once at the one-line height Auto Layout measured before the view had a width, and its text stays truncated, also after a rotation.
    • On iPhone, A, B and C render once per rotation inside the animation, at their new sizes, both on main and with this change. iPhone rotation already updated these views without this change.

This response was drafted with AI assistance.

🤖 Generated with Claude Code

robmaceachern and others added 2 commits November 18, 2026 10:41
BlueprintView passes its window's size to elements as
Environment.windowSize, but its measurement cache and its update check
ignored that size. When only the window resized, sizeThatFits returned
the size cached for the same constraint, and a layout pass with
unchanged bounds rendered nothing new.

Record the window size that the cached measurements and the last update
used, and treat a different size as invalid. An update with a different
window size also invalidates the intrinsic content size, because Auto
Layout may hold one measured in the old window, even when an element
change had already scheduled the update.

UIKit does not lay out a view whose own size stays the same while its
window resizes. Document that hosts of such a view call setNeedsLayout()
when an element reads windowSize.

---
*This response was drafted with AI assistance.*

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Rotate a 390 x 844 window to 844 x 390 under a view whose element reads
Environment.windowSize, once with the view resizing with the window and
once with the view keeping its size while its host calls
setNeedsLayout(). Each rotation renders the view once with the rotated
window's size, another layout pass in that window renders nothing, and
the view measures once in the rotated window, then reuses the cached
measurement.

---
*This response was drafted with AI assistance.*

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@robmaceachern
robmaceachern marked this pull request as ready for review November 18, 2026 17:11
@robmaceachern
robmaceachern requested a review from a team as a code owner November 18, 2026 17:11
@robmaceachern
robmaceachern enabled auto-merge (squash) November 18, 2026 17:12

This branch has not been deployed

No deployments
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.

1 participant