Bucksley / app / Apps / Mac / Support.swift
| 1 | import SwiftUI |
| 2 | import AppKit |
| 3 | import BucksleyKit |
| 4 | import BucksleyUI |
| 5 | |
| 6 | /// The Mac "support Bucksley" card (Standard Settings, ported from Jotley via Glimmer): after five |
| 7 | /// core actions it returns at most once a week until every action has been opened. "Later" waits a |
| 8 | /// week; "Don't show again" stops it for good. |
| 9 | extension SupportPrompt { |
| 10 | static func recordCoreAction() { recordCoreAction(in: .standard) } |
| 11 | |
| 12 | struct Presenter: ViewModifier { |
| 13 | @Environment(\.scenePhase) private var phase |
| 14 | @State private var showing = false |
| 15 | @State private var tick = 0 |
| 16 | |
| 17 | func body(content: Content) -> some View { |
| 18 | content |
| 19 | .task(id: tick) { |
| 20 | // Check every few minutes; only show while the app is active and idle with no sheet in front. |
| 21 | do { try await Task.sleep(for: .seconds(tick == 0 ? 20 : 240)) } catch { return } |
| 22 | if NSApp.isActive, let window = NSApp.keyWindow, !window.isSheet, window.attachedSheet == nil, |
| 23 | SupportPrompt.claim() { showing = true } |
| 24 | tick += 1 |
| 25 | } |
| 26 | .sheet(isPresented: $showing) { SupportSheet(isReminder: true) } |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | /// Feedback, share, support and more apps, each ticked once opened. |
| 32 | struct SupportSheet: View { |
| 33 | let isReminder: Bool |
| 34 | @Environment(\.openURL) private var openURL |
| 35 | @Environment(\.dismiss) private var dismiss |
| 36 | @Environment(\.palette) private var palette |
| 37 | @AppStorage(SupportPrompt.actionsKey) private var clicks = "" |
| 38 | @State private var sharePicker = SupportSharePicker() |
| 39 | |
| 40 | var body: some View { |
| 41 | VStack(alignment: .leading, spacing: 16) { |
| 42 | HStack(spacing: 16) { |
| 43 | Image(BucksleyTheme(preference: BucksleyPreferences.store.string(forKey: BucksleyTheme.preferenceKey)).iconName ?? "AppIconImage") |
| 44 | .resizable().frame(width: 58, height: 58).clipShape(RoundedRectangle(cornerRadius: 13, style: .continuous)) |
| 45 | VStack(alignment: .leading, spacing: 4) { |
| 46 | Text("Free software.\nSupported by people.").font(palette.title(20)).accessibilityAddTraits(.isHeader) |
| 47 | Text("Help more people discover Bucksley and tell us what we can improve.") |
| 48 | .foregroundStyle(palette.muted).fixedSize(horizontal: false, vertical: true) |
| 49 | } |
| 50 | } |
| 51 | VStack(spacing: 8) { |
| 52 | row("Send feedback", detail: "Tell us what’s working and what could be better", symbol: "bubble.left.and.bubble.right.fill", tint: palette.color(1), action: .feedback) { |
| 53 | visit(.feedback, url: SupportPrompt.Links.feedback) |
| 54 | } |
| 55 | row("Share Bucksley", detail: "Send someone a link to the app", symbol: "square.and.arrow.up.fill", tint: palette.accent, action: .share) { |
| 56 | sharePicker.show { if SupportPrompt.record(.share), isReminder { dismiss() } } |
| 57 | } |
| 58 | .background(SupportShareAnchor(view: sharePicker.anchor).allowsHitTesting(false)) |
| 59 | row("Open a Support Ticket", detail: "Free help from a real person", symbol: "headphones", tint: palette.color(2), action: .support) { |
| 60 | visit(.support, url: SupportPrompt.Links.support) |
| 61 | } |
| 62 | row("More Free Apps from Project ATOM", detail: "Explore more free apps from Project ATOM", symbol: "square.grid.2x2.fill", tint: palette.color(4), action: .apps) { |
| 63 | visit(.apps, url: SupportPrompt.Links.protomotaApps) |
| 64 | } |
| 65 | } |
| 66 | Text("Checkmarks show which buttons you’ve opened.").font(.caption).foregroundStyle(palette.muted) |
| 67 | HStack { |
| 68 | if isReminder { |
| 69 | Button("Don’t show again") { SupportPrompt.dismissForGood(); dismiss() } |
| 70 | .buttonStyle(.plain).font(.system(size: 12)).foregroundStyle(palette.muted) |
| 71 | .accessibilityIdentifier("support.neverShow") |
| 72 | } |
| 73 | Spacer() |
| 74 | Button(isReminder ? "Later" : "Done") { dismiss() } |
| 75 | .buttonStyle(PrimaryButtonStyle()).keyboardShortcut(.cancelAction) |
| 76 | .accessibilityIdentifier("support.later") |
| 77 | } |
| 78 | } |
| 79 | .padding(24) |
| 80 | .frame(width: 520) |
| 81 | .background(palette.canvas) |
| 82 | .accessibilityIdentifier("supportReminder") |
| 83 | } |
| 84 | |
| 85 | private func visit(_ action: SupportPrompt.Action, url: URL) { |
| 86 | let finished = SupportPrompt.record(action) |
| 87 | openURL(url) |
| 88 | if finished, isReminder { dismiss() } |
| 89 | } |
| 90 | |
| 91 | private func row(_ title: String, detail: String, symbol: String, tint: Color, action: SupportPrompt.Action, perform: @escaping () -> Void) -> some View { |
| 92 | let clicked = SupportPrompt.clicked(clicks).contains(action) |
| 93 | return Button(action: perform) { |
| 94 | HStack(spacing: 12) { |
| 95 | Image(systemName: symbol).foregroundStyle(tint).frame(width: 30, height: 30).background(tint.opacity(0.15), in: RoundedRectangle(cornerRadius: 8)) |
| 96 | VStack(alignment: .leading, spacing: 1) { |
| 97 | Text(title).font(.system(size: 13, weight: .semibold)) |
| 98 | Text(detail).font(.system(size: 11)).foregroundStyle(palette.muted) |
| 99 | } |
| 100 | Spacer() |
| 101 | Image(systemName: clicked ? "checkmark.circle.fill" : "arrow.up.right").foregroundStyle(clicked ? palette.gain : palette.muted) |
| 102 | } |
| 103 | .padding(10).background(palette.card, in: RoundedRectangle(cornerRadius: 10)).contentShape(Rectangle()) |
| 104 | } |
| 105 | .buttonStyle(.plain) |
| 106 | .accessibilityValue(clicked ? "Previously opened" : "") |
| 107 | .accessibilityIdentifier("support.\(action.rawValue)") |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | private struct SupportShareAnchor: NSViewRepresentable { |
| 112 | let view: NSView |
| 113 | func makeNSView(context: Context) -> NSView { view } |
| 114 | func updateNSView(_ nsView: NSView, context: Context) {} |
| 115 | } |
| 116 | |
| 117 | @MainActor |
| 118 | final class SupportSharePicker: NSObject, NSSharingServicePickerDelegate { |
| 119 | let anchor = NSView() |
| 120 | private var picker: NSSharingServicePicker? |
| 121 | private var finished: (() -> Void)? |
| 122 | |
| 123 | func show(finished: @escaping () -> Void) { |
| 124 | guard picker == nil, anchor.window != nil else { return } |
| 125 | let picker = NSSharingServicePicker(items: [SupportPrompt.Links.website]) |
| 126 | self.picker = picker |
| 127 | self.finished = finished |
| 128 | picker.delegate = self |
| 129 | picker.show(relativeTo: anchor.bounds, of: anchor, preferredEdge: .minY) |
| 130 | } |
| 131 | |
| 132 | nonisolated func sharingServicePicker(_ sharingServicePicker: NSSharingServicePicker, didChoose service: NSSharingService?) { |
| 133 | Task { @MainActor in self.complete() } |
| 134 | } |
| 135 | |
| 136 | private func complete() { |
| 137 | let completion = finished |
| 138 | finished = nil |
| 139 | picker = nil |
| 140 | completion?() |
| 141 | } |
| 142 | } |