From 6b5617785ffca2966c4104159ff807aab5848541 Mon Sep 17 00:00:00 2001 From: ide Date: Tue, 1 Dec 2026 03:59:01 -0800 Subject: [PATCH] iOS: a route a stack is given borrows its insets until UIKit places it Why === On an open iPhone Duo, choosing UI Gallery's Text colors beside Typography for the first time drew its first frame with its paragraphs broken across the 84 pt side strip, then wrapped inside it a frame later. The new route's content was laid out before UIKit placed its controller, with no local insets: its sections pad their scrollers with `var(--exact-safe-area-inset-*)` (LLP 1123 D9), and `reportLocalInsets` reports a route's only once its view is in a window. The split writes the detail only after the route's header is lifted into the bar, a covers pass later, and UIKit gives the route its safe area as it places it, so the kernel laid the content out at the wrong width first, painted its text there, and laid it out again; the text's rasters for the right width came a frame after the boxes moved. How === `prepareRoutes` records on each route the stack the router gave it (`RouteController.arriving`). Until UIKit places it there, `reportLocalInsets` gives the route the local insets of the route that stack shows, which are its column's and the safe area UIKit gives it, so the content lays out where it will rest. `LocalInsetsReport.borrowed` keeps those apart from a report of UIKit's own, so the route's first measure still takes its landing safe area, as a route with no report does. Test Plan ========= On the open iPhone Duo simulator (iOS 27.1), UI Gallery driven with `bun scripts/agent.mjs ios --app ui-gallery --chrome platform --timing platform`, Typography then Text colors, filmed with `simctl io recordVideo` in three runs: the first frame of Text colors has its paragraphs and its swatches' samples where they rest, and nothing in the detail moves after it. A probe build showed the content laid out at its final width in the covers pass before UIKit places the route, its text painted once at that width as it appears, and no width change from UIKit's measure. `perf during "tap sidebar-textcolors"` counts 814 node layouts where it counted about 1,400 to 1,540, and 7 kernel layouts, as before. `bun host/apple/build.mjs --test --ios --only LocalInsetsIOSTests,SplitIOSTests,ScrollInsetsIOSTests` passes but for `SplitIOSTests.testAnInstallTheRunnerCannotAnswerAsksAgain`, which fails on main too. `bun scripts/gate.mjs` passes. No test of its own: a hosted XCTest loses the Duo's hinge, and a window larger than the screen gives the detail other insets than the open Duo does, so the case is verified on the simulator as above. --- host/apple/Sources/ExactKit/LocalInsets.swift | 3 +++ .../IOS/NavigationBarIOS.swift | 1 + .../IOS/NavigationCoversIOS.swift | 24 +++++++++++++++---- .../IOS/RouteControllerIOS.swift | 4 ++++ 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/host/apple/Sources/ExactKit/LocalInsets.swift b/host/apple/Sources/ExactKit/LocalInsets.swift index 8bda8d57fc..3a98160d26 100644 --- a/host/apple/Sources/ExactKit/LocalInsets.swift +++ b/host/apple/Sources/ExactKit/LocalInsets.swift @@ -11,6 +11,9 @@ import CoreGraphics package struct LocalInsetsReport { /// Top, right, bottom, left, in points. package var wanted: [UInt32?: [CGFloat]] = [:] + /// The owners whose insets are another's until their container gives them their own: a route UIKit has yet to + /// place, which holds those of the route its stack shows. + package var borrowed: Set = [] private var delivered: [UInt32?: [CGFloat]] = [:] package init() {} diff --git a/host/apple/Sources/ExactNavigation/IOS/NavigationBarIOS.swift b/host/apple/Sources/ExactNavigation/IOS/NavigationBarIOS.swift index d767e0a077..d9b1d9bebf 100644 --- a/host/apple/Sources/ExactNavigation/IOS/NavigationBarIOS.swift +++ b/host/apple/Sources/ExactNavigation/IOS/NavigationBarIOS.swift @@ -449,6 +449,7 @@ extension NavigationHost { let columns = nav.flatMap { n in (split?.detail === n ? split : nil) ?? tabSplits.values.first { $0.detail === n } } let detail = columns != nil for (index, c) in routes.enumerated() { + c.arriving = nav // The bar is each route's (LLP 1115.000 D5.2, D5.3): its header, // the system Back alone on a pushed route with none of its own // (no header, a hidden one, one that is not a bar's), or no bar diff --git a/host/apple/Sources/ExactNavigation/IOS/NavigationCoversIOS.swift b/host/apple/Sources/ExactNavigation/IOS/NavigationCoversIOS.swift index 342385e90c..4fafac16b1 100644 --- a/host/apple/Sources/ExactNavigation/IOS/NavigationCoversIOS.swift +++ b/host/apple/Sources/ExactNavigation/IOS/NavigationCoversIOS.swift @@ -686,15 +686,25 @@ extension NavigationHost { /// take the whole view, where `env()` is 0 but the screen's insets are not. /// Whether any changed. func reportLocalInsets() -> Bool { - var wanted: [UInt32?: [CGFloat]] = [:] + var wanted: [UInt32?: [CGFloat]] = [:], borrowed: Set = [] for c in controllers.values where presenter.views[c.node.id] === c.node { - let last = localInsets.wanted[c.node.id] + // What UIKit last gave the route: nothing while it holds insets it borrowed arriving (`arrivingInsets`) + let last = localInsets.borrowed.contains(c.node.id) ? nil : localInsets.wanted[c.node.id] let stack = stackTransition(for: c) let moving = stack != nil || c.navigationController?.transitionCoordinator != nil let presenting = c.inPresentation && stack == nil let view = c.surface guard view.window != nil, c.node.window != nil, !c.safeAreaNeedsLayout, last == nil || !moving || c.appearing || presenting else { - wanted[c.node.id] = last + // A route written to a stack UIKit has yet to place it in borrows the insets of the route that stack + // shows, the safe area UIKit gives it there (`RouteController.arriving`): its content lays out where + // it will rest, and UIKit's first measure once it places the route changes nothing + if last == nil, view.window == nil, let insets = arrivingInsets(c) { + wanted[c.node.id] = insets + borrowed.insert(c.node.id) + } else { + wanted[c.node.id] = localInsets.wanted[c.node.id] + if localInsets.borrowed.contains(c.node.id) { borrowed.insert(c.node.id) } + } continue } let box = c.node.convert(c.node.bounds, to: view) @@ -719,10 +729,16 @@ extension NavigationHost { let safe = ExactView.agentSize == nil ? view.safeAreaInsets : .zero wanted[nil] = [safe.top, safe.right, safe.bottom, safe.left] } - defer { localInsets.wanted = wanted } + defer { localInsets.wanted = wanted; localInsets.borrowed = borrowed } return localInsets.wanted != wanted } + /// The local insets of the route shown in the stack `route` is written to (`RouteController.arriving`). + private func arrivingInsets(_ route: RouteController) -> [CGFloat]? { + guard let shown = route.arriving?.stackRoutes.last as? RouteController, shown !== route else { return nil } + return localInsets.wanted[shown.node.id] + } + func padsTop(_ route: RouteController) -> Bool { guard case .edges(let edges)? = covers[route.node.id] else { return false } return edges.top > 0.5 && route.collapseScroll == nil diff --git a/host/apple/Sources/ExactNavigation/IOS/RouteControllerIOS.swift b/host/apple/Sources/ExactNavigation/IOS/RouteControllerIOS.swift index 2039b3ae65..0a080a7e9c 100644 --- a/host/apple/Sources/ExactNavigation/IOS/RouteControllerIOS.swift +++ b/host/apple/Sources/ExactNavigation/IOS/RouteControllerIOS.swift @@ -574,6 +574,10 @@ final class RouteController: UIViewController, UIScrollViewDelegate { /// for that Exact has not written yet. var held = false weak var lastStack: UINavigationController? + /// The stack the router last gave the route (`NavigationHost.prepareRoutes`), which UIKit places it in when it is + /// written there: until then the route borrows the local insets of the route that stack shows + /// (`reportLocalInsets`). + weak var arriving: UINavigationController? /// The route's node, left in place after a batch removed the route while /// UIKit still shows it (`NavigationHost.prepare`), until UIKit takes the