import ParcelAnimation import ParcelConfig import SwiftUI struct TourView: View { let controller: WindowLearningController @Environment(\.accessibilityReduceMotion) private var reduceMotion @Environment(\.colorSchemeContrast) private var contrast @State private var winScale = 1.0 var body: some View { Group { if controller.setup.isPresented { TourSetupView(controller: controller) .ignoresSafeArea(.container, edges: .top) .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top) } else { VStack(alignment: .leading, spacing: 16) { HStack { Text(UIStrings.text("A little tour, your way.")).font(.title2.bold()) Spacer() Button(UIStrings.text("Later")) { controller.pause() } .disabled(controller.setup.isBusy) } learningContent } .padding(24) .frame(width: 440, alignment: .topLeading) .fixedSize(horizontal: false, vertical: true) } } .onGeometryChange(for: CGSize.self) { $0.size } action: { controller.resizePanel(to: $0) } .background(Color(nsColor: .windowBackgroundColor)) .modifier(TourWindowChrome(setupWindow: controller.setup.isPresented, contrast: contrast)) .animation(phaseAnimation, value: controller.tour.progress.phase) .task( id: WinRequest( phase: controller.tour.progress.phase, motion: controller.configuration.motion, reduceMotion: reduceMotion) ) { await celebrate() } } @ViewBuilder private var learningContent: some View { switch controller.tour.progress.phase { case .offered, .paused, .completed: Text( UIStrings.text( controller.tour.progress.phase == .completed ? "You've got this. Want another go?" : "Try a few things with your own windows. Nothing moves until you do.")) if !controller.active && controller.tour.progress.phase == .paused { Text(controller.instruction.detail) } HStack { Button(UIStrings.text("Keyboard track")) { if controller.tour.progress.phase == .paused { controller.begin(.keyboard) } else { controller.beginSetup(.keyboard) } } Button(UIStrings.text("Mouse track")) { if controller.tour.progress.phase == .paused { controller.begin(.mouse) } else { controller.beginSetup(.mouse) } } } if controller.tour.progress.phase == .paused { Text(UIStrings.text("We'll pick up where you left off on the same track.")).font(.caption) Button(TourText.text("Continue")) { controller.beginSetup(controller.tour.progress.track) } } case .running: TourStepTabs( selected: controller.tour.progress.step, count: TourStep.allCases.count, title: controller.instruction.title, configuration: controller.configuration.motion) Text(controller.instruction.title).font(.headline) Text(controller.instruction.detail) .fixedSize(horizontal: false, vertical: true) .accessibilityLabel(controller.instruction.detailAccessibilityLabel ?? controller.instruction.detail) if controller.instruction.shortcut != nil, let command = controller.tour.command(state: controller.state, configuration: controller.configuration), let chord = controller.configuration.keybindings[command] { ShortcutLabel(chord: chord) } if controller.tour.progress.track == .mouse && !controller.instruction.ready { Text(UIStrings.text("Open two tiled windows to give this a try.")).font(.caption) } Button(UIStrings.text("Skip this step")) { controller.skip() } case .celebrating: TourStepTabs( selected: controller.tour.progress.step, count: TourStep.allCases.count, title: controller.instruction.title, configuration: controller.configuration.motion) HStack { Image(systemName: "checkmark.circle.fill") .font(.system(size: 28, weight: contrast == .increased ? .bold : .regular)) .scaleEffect(winScale) .accessibilityHidden(true) Text(UIStrings.text("Nicely done.")).font(.headline) } Button(UIStrings.text("Keep going")) { controller.next() } Button(UIStrings.text("Skip this step")) { controller.skip() } } } private var phaseAnimation: Animation? { let motion = controller.configuration.motion guard motion.enabled else { return nil } let fade = MotionToken.fade.spec(settings: motion.uiMotionSettings) return .easeInOut(duration: reduceMotion ? 0.12 : fade.duration) } private func celebrate() async { var transaction = Transaction() transaction.disablesAnimations = true withTransaction(transaction) { winScale = 1 } guard controller.tour.progress.phase == .celebrating else { return } let configuration = controller.configuration.motion let behavior = TourInstruction.celebrationMotion( configuration: configuration, reduceMotion: reduceMotion, step: controller.tour.step) guard behavior == .animate else { return } let pop = MotionToken.pop.spec(settings: configuration.uiMotionSettings) let drop = MotionToken.drop.spec(settings: configuration.uiMotionSettings) withAnimation(.spring(duration: pop.duration, bounce: pop.bounce)) { winScale = 1.08 } do { try await Task.sleep(for: .seconds(pop.duration)) } catch is CancellationError { return } catch { preconditionFailure("Unexpected tour clock failure: \(error)") } withAnimation(.spring(duration: drop.duration, bounce: drop.bounce)) { winScale = 1 } } } private struct WinRequest: Equatable { let phase: TourPhase let motion: ParcelConfig.MotionConfiguration let reduceMotion: Bool }