Repository navigation
fix: re-measure and re-render BlueprintView after its window resizes - #633
Open
robmaceachern wants to merge 2 commits into
Open
robmaceachern wants to merge 2 commits into
robmaceachern wants to merge 2 commits into
Conversation
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
marked this pull request as ready for review
November 18, 2026 17:11
robmaceachern
enabled auto-merge (squash)
November 18, 2026 17:12
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🤖 Agent details
Contribution: Implementation and PR description
Agent: Claude Code · Claude Opus 5.5
BlueprintViewsetsEnvironment.windowSizefrom 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 sameSizeConstraint, because onlysetNeedsViewHierarchyUpdate()clearssizesThatFit. 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 unlessneedsViewHierarchyUpdatewas set orboundshad 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). Whenwindow?.bounds.sizediffers, it clearssizesThatFitbefore the cache lookup. A cache hit now also readswindow?.bounds.sizeonce; 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 callsinvalidateIntrinsicContentSize(), 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.nilwindow size.didMoveToWindow()already invalidates both caches, and the baseenvironment(which supplieswindowSizeoff-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. ABlueprintViewlike 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 onBlueprintView(a "Window size changes" section) and onEnvironment.windowSize. When an element in such a view readswindowSize, its host callssetNeedsLayout()on the view, for example fromviewWillTransition(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 reassignelementto get the same result.Observing the window's size from every
BlueprintViewwas considered and not shipped:UIWindow.frameorboundsas key-value observable, and scene-driven resizes aren't guaranteed to go through their setters.UIWindowScene.effectiveGeometryis observable, but only on iOS 16 and later (Blueprint supports iOS 15). It also describes the scene, not the window. EveryBlueprintViewwould observe it and invalidate on each step of a live resize, including the many views whose bounds change anyway and whose elements never readwindowSize.Validation
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 newwindowSize.test_intrinsicContentSize_updatesWhenOnlyTheWindowResizes: a view with a fixed-width constraint gets its new height from Auto Layout aftersetNeedsLayout().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_whenTheViewResizesWithTheWindowandtest_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 callssetNeedsLayout(). 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.mainwith 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). Onmain, the view that keeps its size doesn't render after the rotation. The first intrinsic-size test also fails when only theinvalidateIntrinsicContentSize()call is removed. The pending-render test fails when that call is skipped while an update is pending.BlueprintUI-Testspass through the TuistUnitTestsscheme 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.BlueprintUICommonControlstests weren't run locally.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 callssetNeedsLayout()fromviewWillTransition(to:with:), and C's doesn't. D is full width, with wrapping text that doesn't readwindowSize.main, A renders once but keeps its old height, so its label truncates, and B and C don't render.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.mainand with this change. iPhone rotation already updated these views without this change.This response was drafted with AI assistance.
🤖 Generated with Claude Code