Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 60 additions & 21 deletions macos/Sources/WeChatBridgeApp/Onboarding/OnboardingFlow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand Down
51 changes: 51 additions & 0 deletions macos/Tests/WeChatBridgeAppTests/OnboardingFrameTests.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
Loading