From b810afb089931dad1fef0841c5fb4971254e59f3 Mon Sep 17 00:00:00 2001 From: Janic Duplessis Date: Sat, 14 Nov 2026 20:48:43 -0500 Subject: [PATCH] A header's drawn bar items are drawn again when the scheme changes on iOS A bar item whose face is drawn (ChipFace, BadgeFace) is an image painted for one scheme, and the bar was projected again only after a batch. ExactView, which hears every scheme change, now calls NavigationHost.schemeChanged, which runs prepareRoutes on each live stack a turn later, once the header's views report the new traits. A stack still showing an ended route is left alone. --- .../Sources/ExactKit/IOS/ExactViewIOS.swift | 2 +- .../ExactKit/IOS/NavigationBarIOS.swift | 16 ++++++++++ .../NavigationBarSchemeIOSTests.swift | 31 +++++++++++++++++++ .../tests/fixtures/bar-face-scheme.contract | 15 +++++++++ host/apple/xctest.mjs | 2 ++ 5 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 host/apple/tests/ExactKitTests/NavigationBarSchemeIOSTests.swift create mode 100644 host/apple/tests/fixtures/bar-face-scheme.contract diff --git a/host/apple/Sources/ExactKit/IOS/ExactViewIOS.swift b/host/apple/Sources/ExactKit/IOS/ExactViewIOS.swift index 714c80ace..32e4fa36f 100644 --- a/host/apple/Sources/ExactKit/IOS/ExactViewIOS.swift +++ b/host/apple/Sources/ExactKit/IOS/ExactViewIOS.swift @@ -179,7 +179,7 @@ public final class ExactView: UIView { /// Paint motion resolves `light-dark()` by this view's appearance (LLP 1062). /// A system appearance change reaches the view as a trait change too: /// `prefers-color-scheme` is told again (LLP 1069.000 D1). - private func reportScheme() { fitAheadOfLayout(); session.scheme(dark: traitCollection.userInterfaceStyle == .dark); session.tellPreferences(); session.natives.scopesChanged() } + private func reportScheme() { fitAheadOfLayout(); session.scheme(dark: traitCollection.userInterfaceStyle == .dark); session.tellPreferences(); session.natives.scopesChanged(); session.presenter.navigation.schemeChanged() } /// An app or window tint changed: `AccentColor` is reported again (LLP 1095 D9). public override func tintColorDidChange() { super.tintColorDidChange(); fitAheadOfLayout(); session.reportColors() } diff --git a/host/apple/Sources/ExactKit/IOS/NavigationBarIOS.swift b/host/apple/Sources/ExactKit/IOS/NavigationBarIOS.swift index 385bbfcf4..641ce7f9b 100644 --- a/host/apple/Sources/ExactKit/IOS/NavigationBarIOS.swift +++ b/host/apple/Sources/ExactKit/IOS/NavigationBarIOS.swift @@ -516,6 +516,22 @@ extension NavigationHost { } } + /// A bar item's face and badge are images drawn for one scheme + /// (`ChipFace.dark`, `BadgeFace`): a new scheme projects each stack's + /// bars again, a turn later, once the header's views have the new traits + /// too. A stack still showing an ended route (a sheet animating away) is + /// left alone. + func schemeChanged() { + DispatchQueue.main.async { [weak self] in + guard let self else { return } + for nav in allNavigations { + let routes = nav.stackRoutes.compactMap { $0 as? RouteController } + guard !routes.isEmpty, routes.allSatisfy({ self.controllers[$0.node.id] === $0 }) else { continue } + prepareRoutes(routes, in: nav) + } + } + } + func makeNavigation(first: NodeView?) -> UINavigationController { let nav = UINavigationController(navigationBarClass: RouteNavigationBar.self, toolbarClass: nil) let stack = registerStack(nav) diff --git a/host/apple/tests/ExactKitTests/NavigationBarSchemeIOSTests.swift b/host/apple/tests/ExactKitTests/NavigationBarSchemeIOSTests.swift new file mode 100644 index 000000000..7038a7633 --- /dev/null +++ b/host/apple/tests/ExactKitTests/NavigationBarSchemeIOSTests.swift @@ -0,0 +1,31 @@ +#if os(iOS) +import UIKit +import XCTest +@testable import ExactKit + +extension NavigationBarIOSTests { + /// A drawn face is an image for one scheme: the scheme changing while + /// it shows draws it again in the other ink, though the app sends no batch. + func testADrawnBarItemIsDrawnAgainWhenTheSchemeChanges() throws { + let session = try fixture("scheme-face", module: false, plan: "BAR_FACE_SCHEME") + let nav = try XCTUnwrap(session.presenter.navigation.primaryNavigation) + let navigationItem = try XCTUnwrap(nav.topViewController?.navigationItem) + let title = { ((navigationItem.leftBarButtonItems ?? []) + (navigationItem.rightBarButtonItems ?? [])).first { $0.accessibilityIdentifier == "title-home" } } + // The brightest red among the face's opaque pixels: near 0 in black ink, near 255 in white. + let ink = { () -> UInt8? in + guard let image = title()?.image?.cgImage else { return nil } + var bytes = [UInt8](repeating: 0, count: image.width * image.height * 4) + guard let context = CGContext(data: &bytes, width: image.width, height: image.height, bitsPerComponent: 8, bytesPerRow: image.width * 4, + space: CGColorSpace(name: CGColorSpace.sRGB)!, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue) else { return nil } + context.draw(image, in: CGRect(x: 0, y: 0, width: image.width, height: image.height)) + return stride(from: 0, to: bytes.count, by: 4).filter { bytes[$0 + 3] > 200 }.map { bytes[$0] }.max() + } + let window = try XCTUnwrap(self.window) + window.overrideUserInterfaceStyle = .light + until("the title is drawn in the light ink") { ink().map { $0 < 60 } == true } + window.overrideUserInterfaceStyle = .dark + defer { window.overrideUserInterfaceStyle = .unspecified } + until("the title is drawn again in the dark ink") { ink().map { $0 > 200 } == true } + } +} +#endif diff --git a/host/apple/tests/fixtures/bar-face-scheme.contract b/host/apple/tests/fixtures/bar-face-scheme.contract new file mode 100644 index 000000000..4d0bc0246 --- /dev/null +++ b/host/apple/tests/fixtures/bar-face-scheme.contract @@ -0,0 +1,15 @@ +// A route whose header's title is a pressable in an authored font with a +// light and a dark ink (NavigationBarIOSTests): its bar item is a drawn face, +// and nothing in the app depends on the scheme, so switching it sends no batch. +component BarFaceScheme + state taps = 0 + action tap + taps = taps + 1 + view + main navigationKey="home" width="100%" height="100%" + column navigationKey="home" position="absolute" inset=0 display="flex" flex-direction="column" + header display="flex" align-items="center" padding=12 flex-shrink=0 + view role="button" press=tap aria-label="Title" testId="title-home" + text "Title" font-size=20 color="light-dark(#000000, #ffffff)" + text "" role="heading" aria-level=2 flex=1 + column flex=1 diff --git a/host/apple/xctest.mjs b/host/apple/xctest.mjs index 6908bffb6..94a88a76b 100644 --- a/host/apple/xctest.mjs +++ b/host/apple/xctest.mjs @@ -120,6 +120,8 @@ export function compileFixtures(root, run, env, dir, prefix, appId) { contract([resolve(root, 'host/apple/tests/fixtures/untitled-bar.contract'), '-o', env[`${prefix}_UNTITLED_BAR_PLAN`] = resolve(dir, 'untitled-bar.plan')]); // NavigationBarIOSTests': a header whose controls share and add a control. contract([resolve(root, 'host/apple/tests/fixtures/bar-share.contract'), '-o', env[`${prefix}_BAR_SHARE_PLAN`] = resolve(dir, 'bar-share.plan')]); + // NavigationBarIOSTests': a header title drawn as a face, in an ink per scheme. + contract([resolve(root, 'host/apple/tests/fixtures/bar-face-scheme.contract'), '-o', env[`${prefix}_BAR_FACE_SCHEME_PLAN`] = resolve(dir, 'bar-face-scheme.plan')]); contract([resolve(root, 'host/apple/tests/fixtures/dialog-sheet-reads.contract'), '-o', env[`${prefix}_DIALOG_SHEET_READS_PLAN`] = resolve(dir, 'dialog-sheet-reads.plan')]); contract([resolve(root, 'host/apple/tests/fixtures/route-backdrop.contract'), '-o', env[`${prefix}_ROUTE_BACKDROP_PLAN`] = resolve(dir, 'route-backdrop.plan')]); contract([resolve(root, 'host/apple/tests/fixtures/trailing-time.contract'), '-o', env[`${prefix}_TRAILING_TIME_PLAN`] = resolve(dir, 'trailing-time.plan')]);