diff --git a/macos/Sources/WeChatBridgeApp/Onboarding/OnboardingFlow.swift b/macos/Sources/WeChatBridgeApp/Onboarding/OnboardingFlow.swift index 67b82a2..5b2912e 100644 --- a/macos/Sources/WeChatBridgeApp/Onboarding/OnboardingFlow.swift +++ b/macos/Sources/WeChatBridgeApp/Onboarding/OnboardingFlow.swift @@ -50,6 +50,30 @@ struct OnboardingFlow: View { } var body: some View { + OnboardingFrame(width: Metrics.onboardingWidth, minHeight: Metrics.onboardingHeight) { + content + } + .background(Theme.raised) + .onAppear { + authorization.refresh() + probe.refresh() + preferences.onboardingStep = step.rawValue + } + .onChange(of: step) { _, new in preferences.onboardingStep = new.rawValue } + // The permission is granted, and the entries can be switched, in System + // Settings — in another process, while this window waits. Both have to + // be re-read the moment the user comes back. + .onReceive(NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification)) { _ in + authorization.refresh() + probe.refresh() + } + .animation(Motion.reduced(.easeInOut(duration: 0.22)), value: step) + } + + /// The guide itself, at the design's width. Its height is the step's own: + /// the entry roster has grown past the height the design was drawn at, and + /// a fixed one clipped the footer button off the bottom of step one. + private var content: some View { VStack(spacing: 0) { StepBar(steps: Step.allCases.map(\.title), current: step.rawValue) .frame(height: 52) @@ -75,27 +99,6 @@ struct OnboardingFlow: View { .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading) } } - // The design's width, and only its width: the height is whatever the - // step showing asks for. It has to be — the entry roster has grown past - // the height the design was drawn at, and a fixed one clipped the - // footer button off the bottom of step one. The window follows this - // view's own size. - .frame(width: Metrics.onboardingWidth) - .background(Theme.raised) - .onAppear { - authorization.refresh() - probe.refresh() - preferences.onboardingStep = step.rawValue - } - .onChange(of: step) { _, new in preferences.onboardingStep = new.rawValue } - // The permission is granted, and the entries can be switched, in System - // Settings — in another process, while this window waits. Both have to - // be re-read the moment the user comes back. - .onReceive(NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification)) { _ in - authorization.refresh() - probe.refresh() - } - .animation(Motion.reduced(.easeInOut(duration: 0.22)), value: step) } // MARK: - Art column @@ -341,3 +344,39 @@ private struct PermissionArt: View { .accessibilityHidden(true) } } + +// MARK: - Window sizing + +/// Sizes the guide at `width` × max(`minHeight`, the content's own ideal +/// height), whatever size it is offered. +/// +/// The window follows this view's preferred size (`.preferredContentSize`, so +/// a step taller than the design is not clipped — macOS 15). The view must not +/// follow the window back: the panes stretch with `maxHeight: .infinity`, so a +/// size read from the offer feeds the window's height back into itself. On +/// macOS 14 that loop ends in AppKit's "more Update Constraints in Window +/// passes than there are views" exception and the app exits at launch (issue +/// #54). Measuring at an unspecified height makes the answer depend on the +/// step alone. +struct OnboardingFrame: Layout { + var width: CGFloat + var minHeight: CGFloat + + func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize { + CGSize(width: width, height: height(of: subviews)) + } + + func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) { + let size = ProposedViewSize(width: width, height: height(of: subviews)) + for subview in subviews { + subview.place(at: bounds.origin, anchor: .topLeading, proposal: size) + } + } + + func height(of subviews: Subviews) -> CGFloat { + let ideal = subviews + .map { $0.sizeThatFits(ProposedViewSize(width: width, height: nil)).height } + .max() ?? 0 + return max(minHeight, ideal) + } +} diff --git a/macos/Sources/WeChatBridgeApp/Onboarding/OnboardingWindowController.swift b/macos/Sources/WeChatBridgeApp/Onboarding/OnboardingWindowController.swift index 83208b4..51af7a6 100644 --- a/macos/Sources/WeChatBridgeApp/Onboarding/OnboardingWindowController.swift +++ b/macos/Sources/WeChatBridgeApp/Onboarding/OnboardingWindowController.swift @@ -38,6 +38,8 @@ final class OnboardingWindowController: NSObject, NSWindowDelegate { // The width is part of the layout; the height is the current step's // intrinsic height. Without `.preferredContentSize`, macOS 15 leaves // this fixed at the starting frame and clips step one's footer. + // `OnboardingFrame` keeps that size independent of the window's, which + // is what stops macOS 14 looping on it at launch (issue #54). controller.sizingOptions = [.preferredContentSize] let window = NSWindow(contentViewController: controller) window.title = "" diff --git a/macos/Tests/WeChatBridgeAppTests/OnboardingFrameTests.swift b/macos/Tests/WeChatBridgeAppTests/OnboardingFrameTests.swift new file mode 100644 index 0000000..43c63c0 --- /dev/null +++ b/macos/Tests/WeChatBridgeAppTests/OnboardingFrameTests.swift @@ -0,0 +1,51 @@ +import AppKit +import SwiftUI +import XCTest +@testable import WeChatBridgeApp + +/// Issue #54: on macOS 14 the first-run guide's window and its content fed +/// each other's height until AppKit threw and the app exited at launch. The +/// guide's size must depend on the step showing, never on the size offered. +final class OnboardingFrameTests: XCTestCase { + /// A stand-in with the guide's shape: content of a fixed height inside a + /// pane that stretches with `maxHeight: .infinity`. + private func stretchingContent(height: CGFloat) -> some View { + VStack(spacing: 0) { + Color.clear.frame(height: height) + Spacer(minLength: 0) + } + .frame(maxHeight: .infinity) + } + + @MainActor + func testTallStepKeepsItsOwnHeightWhateverItIsOffered() { + let host = NSHostingController(rootView: OnboardingFrame(width: 920, minHeight: 600) { + stretchingContent(height: 740) + }) + for offered in [300.0, 600, 740, 2000] { + let size = host.sizeThatFits(in: CGSize(width: 920, height: offered)) + XCTAssertEqual(size.width, 920, "offered \(offered)") + XCTAssertEqual(size.height, 740, "offered \(offered)") + } + } + + @MainActor + func testShortStepFillsTheDesignHeight() { + let host = NSHostingController(rootView: OnboardingFrame(width: 920, minHeight: 600) { + stretchingContent(height: 420) + }) + for offered in [300.0, 600, 2000] { + XCTAssertEqual(host.sizeThatFits(in: CGSize(width: 920, height: offered)).height, 600, "offered \(offered)") + } + } + + /// The shape the guide had before: its height follows the offer, which is + /// the feedback the window was looping on. + @MainActor + func testTheOldFrameFollowedTheOfferedHeight() { + let host = NSHostingController(rootView: stretchingContent(height: 420).frame(width: 920)) + let small = host.sizeThatFits(in: CGSize(width: 920, height: 600)).height + let large = host.sizeThatFits(in: CGSize(width: 920, height: 2000)).height + XCTAssertNotEqual(small, large) + } +}