Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415import ParcelConfigimport SwiftUI
struct TourSetupView: View { let controller: WindowLearningController @Bindable private var setup: TourSetupController @Environment(\.colorScheme) private var colorScheme @AccessibilityFocusState private var titleFocused: Bool @AccessibilityFocusState private var errorFocused: Bool @FocusState private var primaryFocused: Bool @ScaledMetric(relativeTo: .body) private var footerHeight = 64.0 @State private var measurements = TourContentMeasurements() nonisolated static let windowSize = CGSize(width: 560, height: 460)
init(controller: WindowLearningController) { self.controller = controller; setup = controller.setup }
var body: some View { VStack(spacing: 0) { content .fixedSize(horizontal: false, vertical: true) .onGeometryChange(for: CGSize.self) { $0.size } action: { measurements.content = $0 fitMeasuredContent() } .frame(maxHeight: .infinity, alignment: .top) .clipped() Divider() .onGeometryChange(for: CGFloat.self) { $0.size.height } action: { measurements.separatorHeight = $0 fitMeasuredContent() } VStack(spacing: 8) { HStack(alignment: .center, spacing: 14) { if setup.step != .welcome, setup.result == nil { TourBackButton(controller: controller).frame(width: 32, height: 32) } Button(action: exit) { Text(exitTitle).fixedSize(horizontal: false, vertical: true) } .buttonStyle(.link) .accessibilityIdentifier("tour.defer") Spacer(minLength: 0) if setup.step != .welcome || setup.result != nil { Button(action: primary) { Text(primaryTitle).fixedSize(horizontal: false, vertical: true) .multilineTextAlignment(.center) } .buttonStyle(.borderedProminent) .controlSize(.large) .keyboardShortcut(.defaultAction) .disabled(!primaryEnabled) .focused($primaryFocused) .accessibilityIdentifier( setup.step == .ready && setup.intent == .automatic ? "tour.tidy" : "tour.continue") } } if setup.isReplay && !needsAttention { Text(TourText.text("Closing the tour leaves automatic tiling off.")) .font(.callout) .fixedSize(horizontal: false, vertical: true) } } .padding(.horizontal, 40) .padding(.top, 20) .padding(.bottom, 40) .frame(minHeight: footerHeight) .fixedSize(horizontal: false, vertical: true) .accessibilityElement(children: .contain) .accessibilityIdentifier("tour.footer") .onGeometryChange(for: CGFloat.self) { $0.size.height } action: { measurements.footerHeight = $0 fitMeasuredContent() } } .frame(width: Self.windowSize.width) .onChange(of: setup.step, initial: true) { titleFocused = true primaryFocused = setup.step != .welcome controller.setupScreenChanged() } .onChange(of: setup.result) { if setup.result != nil { errorFocused = true } } .onChange(of: setup.errorMessage) { if setup.errorMessage != nil { errorFocused = true } } .onChange(of: setup.permission) { controller.announceTourPermission() } .onExitCommand { controller.closeSetup() } }
private func fitMeasuredContent() { if let size = measurements.fittingSize { controller.resizeSetupPanel(to: size) } }
private var content: some View { VStack(alignment: .leading, spacing: 24) { VStack(alignment: .leading, spacing: 12) { TourStepTabs( selected: setup.step.screenIndex, count: TourSetupStep.allCases.count, title: TourText.text(setup.step.title), configuration: controller.configuration.motion ) .padding(.bottom, 4) heading if setup.step == .welcome || showsReadyIntroduction { Text(summary).fixedSize(horizontal: false, vertical: true) } } switch setup.step { case .welcome: TourWelcomeView( configuration: controller.configuration, intent: setup.intent, isEnabled: setup.canContinue && setup.result == nil ) { intent in controller.runSetup { try await setup.chooseMode(intent) } } case .allow: TourAllowView(controller: controller) case .ready: if setup.result == nil { ready } } if let result = setup.result { VStack(alignment: .leading, spacing: 8) { Label(result.title, systemImage: result.completed ? "info.circle" : "exclamationmark.triangle") .font(.headline) .fixedSize(horizontal: false, vertical: true) .accessibilityIdentifier("tour.result") .accessibilityFocused($errorFocused) if let message = result.message, setup.progress.recovery != .recoveryRequired { Text(message).fixedSize(horizontal: false, vertical: true) .accessibilityIdentifier("tour.tidy.summary") } if setup.progress.recovery == .recoveryRequired { Text( TourText.text( "Keep your windows where they are, then continue setup. Automatic tiling stays off until setup finishes." ) ) .fixedSize(horizontal: false, vertical: true) .accessibilityIdentifier("tour.recovery.explanation") if let error = setup.errorMessage { Label(error, systemImage: "exclamationmark.triangle") .fixedSize(horizontal: false, vertical: true) .accessibilityIdentifier("tour.recovery.error") } } if !visibleResultDetails.isEmpty { DisclosureGroup(TourText.text("Details"), isExpanded: $setup.detailsExpanded) { ForEach(Array(visibleResultDetails.enumerated()), id: \.offset) { _, detail in Text(detail).textSelection(.enabled).fixedSize(horizontal: false, vertical: true) } } } } .padding(16) .frame(maxWidth: .infinity, alignment: .leading) .background(Color.primary.opacity(0.04), in: RoundedRectangle(cornerRadius: 12)) } else if let error = setup.errorMessage { Label(error, systemImage: "exclamationmark.triangle") .fixedSize(horizontal: false, vertical: true) .accessibilityIdentifier("tour.error") .accessibilityFocused($errorFocused) Button(TourText.text("Try again")) { controller.retryTourProgress() } .accessibilityIdentifier("tour.retry.progress") } } .frame(maxWidth: .infinity, alignment: .leading) .padding(.horizontal, setup.step == .welcome ? 32 : 40) .padding(.top, setup.step == .welcome ? 32 : 36) .padding(.bottom, setup.step == .welcome ? 32 : 20) }
var showsReadyCheckmark: Bool { setup.step == .ready && setup.canContinue && setup.result == nil && !needsAttention }
private var showsReadyIntroduction: Bool { setup.step == .ready && setup.result == nil && setup.errorMessage == nil && setup.progress.recovery == .none && setup.readyCheck == .ready }
private var needsAttention: Bool { setup.step == .ready && (setup.progress.recovery != .none || setup.errorMessage != nil || setup.result.map { !$0.completed } == true || (setup.result == nil && setup.readyCheck != .ready)) }
private var visibleResultDetails: [String] { guard let result = setup.result else { return [] } return result.details.filter { setup.progress.recovery != .recoveryRequired || $0 != setup.progress.recoveryNotice } }
private var heading: some View { Group { if showsReadyCheckmark { Label(TourText.text("You’re ready"), systemImage: "checkmark.circle.fill") .foregroundStyle(readyTitleColor) } else { Text(TourText.text(needsAttention ? "Finish setup" : setup.step.title)) } } .font(.title.bold()) .fixedSize(horizontal: false, vertical: true) .accessibilityAddTraits(.isHeader) .accessibilityIdentifier("tour.title") .accessibilityFocused($titleFocused) }
private var readyTitleColor: Color { colorScheme == .dark ? Color(red: 0.4, green: 0.85, blue: 0.5) : Color(red: 0.08, green: 0.42, blue: 0.18) }
private var summary: String { switch setup.step { case .welcome: TourText.text("Parcel tiles your windows automatically. No shortcuts needed.") case .allow: TourAllowView(controller: controller).accessibilityStatus case .ready: if setup.intent == .manual { TourText.text( "Parcel moves windows only when you ask. Choose Retile in the Parcel menu to arrange them.") } else if controller.state.displays.count > 1 { TourText.text("Tidy this screen, then keep windows arranged across your screens.") } else { TourText.text("Parcel will tidy your windows and keep them arranged automatically.") } } }
private var ready: some View { VStack(alignment: .leading, spacing: 12) { if setup.result == nil && !setup.isBusy && setup.readyCheck != .ready { VStack(alignment: .leading, spacing: 12) { Label(setup.readyCheck.statusLabel, systemImage: readySymbol) .fixedSize(horizontal: false, vertical: true) .accessibilityIdentifier("tour.ready.status") HStack(spacing: 12) { readyRecovery } } .padding(16) .frame(maxWidth: .infinity, alignment: .leading) .background(Color.primary.opacity(0.04), in: RoundedRectangle(cornerRadius: 12)) } if setup.intent == .automatic && showsReadyIntroduction { Text(TourText.text("Find Parcel in your menu bar anytime.")) .foregroundStyle(.secondary) } if setup.isBusy { Text(TourText.text(setup.intent == .automatic ? "Arranging your windows…" : "Checking setup…")) .accessibilityIdentifier("tour.working") } if setup.loginWasNotChanged && setup.result == nil { Text(TourText.text("Open at login wasn’t changed. You can set it later in Settings.")) .font(.callout) .foregroundStyle(.secondary) .fixedSize(horizontal: false, vertical: true) } if setup.progress.recovery != .none && !setup.isBusy && setup.result == nil { Text(TourText.text("Setup was interrupted. Automatic tiling is off.")) } if setup.intent == .manual && setup.result == nil { DisclosureGroup(TourText.text("See useful shortcuts"), isExpanded: $setup.referenceExpanded) { reference } .accessibilityIdentifier("tour.reference.disclosure") .onChange(of: setup.referenceExpanded) { if setup.referenceExpanded { controller.runSetup { try await setup.loadShortcuts() } } } } } }
private var readySymbol: String { switch setup.readyCheck { case .ready: "checkmark.circle.fill" case .checking: "clock" default: "exclamationmark.circle" } }
@ViewBuilder private var readyRecovery: some View { switch setup.readyCheck { case .ready, .checking: EmptyView() case .permissionMissing, .permissionUnknown: Button(TourText.text("Open System Settings")) { controller.changeSetup { try setup.openAccessibilitySettings() } } case .stageManager: Button(TourText.text("Open System Settings")) { controller.changeSetup { try setup.openSettings(for: .ready) } } case .configuration: Button(TourText.text("Review settings…")) { controller.changeSetup { try setup.reviewConfiguration() } } case .targetChanged, .unavailable: EmptyView() } if setup.readyCheck != .ready && setup.readyCheck != .checking { Button(TourText.text("Check again")) { controller.runSetup { try await setup.checkCurrentStep() } } .accessibilityIdentifier("tour.ready.retry") } }
@ViewBuilder private var reference: some View { VStack(alignment: .leading, spacing: 10) { if setup.checkingChecks.contains(.shortcuts) { Text(TourText.text("Loading shortcuts…")) } else if setup.checkErrors[.shortcuts] != nil { Text(TourText.text("Shortcuts couldn’t be loaded. You can use the Parcel menu.")) Button(TourText.text("Try again")) { controller.runSetup { try await setup.loadShortcuts() } } } else if let shortcuts = setup.shortcuts { if shortcuts.reference.isEmpty { Text(TourText.text("Use the Parcel menu, or set shortcuts in Settings.")) } else { ForEach(shortcuts.reference.prefix(4)) { row in HStack { Text(TourText.text(referenceTitle(row.command))) Spacer() ShortcutLabel(chord: row.chord) } } } } else { Text(TourText.text("Loading shortcuts…")) } Button(TourText.text("Edit shortcuts…")) { controller.openTourShortcuts() } .accessibilityIdentifier("tour.shortcuts.edit") } }
private func referenceTitle(_ command: ParcelConfig.ConfigCommand) -> String { switch command { case .retile: "Retile" case .windowNext: "Next window" case .floatToggle: "Float window" default: "Change layout" } }
private var exitTitle: String { if setup.progress.recovery == .recoveryRequired && !setup.isBusy { return TourText.text("Close") } return TourText.text( setup.isBusy ? "Cancel" : (setup.result?.completed == true ? "Try again" : (setup.result?.permitsRetry == false ? "Details" : "Set up later"))) } private var primaryTitle: String { if setup.progress.recovery == .recoveryRequired { return TourText.text("Keep current arrangement") } if let result = setup.result { return TourText.text(result.completed ? "Done" : result.permitsRetry ? "Try again" : "Close") } if setup.step == .allow { return TourText.text("Continue") } if setup.step == .ready { return TourText.text(setup.intent == .automatic ? "Tidy my workspace" : "Finish setup") } return TourText.text("Continue") } private var primaryEnabled: Bool { if setup.isBusy { return false } if setup.progress.recovery == .recoveryRequired { return setup.canKeepCurrentArrangement } if setup.result != nil { return true } return setup.canContinue } private func exit() { if setup.progress.recovery == .recoveryRequired { controller.closeSetup() return } if setup.result?.completed == true { controller.retrySetup() } else if setup.result?.permitsRetry == false { setup.detailsExpanded.toggle() } else { controller.closeSetup() } } private func primary() { if setup.progress.recovery == .recoveryRequired { controller.runSetup { try await setup.keepCurrentArrangement() } return } if let result = setup.result { if result.completed || !result.permitsRetry { controller.closeSetup() } else { controller.retrySetup() } return } switch setup.step { case .welcome: controller.runSetup { try await setup.next() } case .allow: controller.runSetup { try await setup.next() } case .ready: controller.runSetup { try await setup.startSetup() } } }}
extension TourSetupStep { var title: String { switch self { case .welcome: "Windows that arrange themselves." case .allow: "Allow Parcel to arrange windows" case .ready: "You’re ready" } }}