Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173import AppKitimport SwiftUI
@MainActorfinal class SupportController { private var window: NSWindow?
func show(_ kind: SupportKind, collect: (@MainActor () async throws -> SupportRuntimeDiagnostics)?) { window?.close() let draft = SupportDraft( kind: kind, recipient: Bundle.main.object(forInfoDictionaryKey: "ParcelSupportEmail") as? String ?? "", diagnostics: .current(), issuesURL: Bundle.main.object(forInfoDictionaryKey: "ParcelIssuesURL") as? String ?? "https://tangled.org/vinnymac.dev/parcel/issues") let window = NSWindow( contentViewController: NSHostingController(rootView: SupportView(draft: draft, collect: collect))) window.title = SupportText.text("Send Feedback") window.setAccessibilityLabel(window.title) window.styleMask = [.titled, .closable, .resizable] window.isReleasedWhenClosed = false AppWindowPresentation.configure(window) window.setContentSize(NSSize(width: 560, height: 520)) window.center() self.window = window NSApplication.shared.activate() window.makeKeyAndOrderFront(nil) }
func close() { window?.close() window = nil }}
private struct SupportDiagnosticsRequest: Equatable { let included: Bool let attempt: Int}
struct SupportView: View { @State private var draft: SupportDraft let collect: (@MainActor () async throws -> SupportRuntimeDiagnostics)? @State private var error: String? @State private var diagnosticsFailed = false @State private var diagnosticsAttempt = 0
init(draft: SupportDraft, collect: (@MainActor () async throws -> SupportRuntimeDiagnostics)?) { _draft = State(initialValue: draft) self.collect = collect }
var body: some View { VStack(alignment: .leading, spacing: 16) { Text(SupportText.text(draft.usesEmail ? "Review your email" : "Review your report")) .font(.title2.bold()) .accessibilityAddTraits(.isHeader) Text( SupportText.text( draft.usesEmail ? "Your email app opens a draft. Nothing is sent until you send it." : "The exact message below is copied to your clipboard, then the project’s issue page opens. Paste it into a new issue. Nothing is posted automatically." )) Picker(SupportText.text("Feedback Type"), selection: $draft.kind) { ForEach([SupportKind.problem, .feature], id: \.self) { kind in Text(kind.choiceTitle).tag(kind) } } .pickerStyle(.segmented) .accessibilityLabel(SupportText.text("Feedback Type")) .accessibilityIdentifier("support.kind") Toggle(SupportText.text("Include diagnostics"), isOn: $draft.includeDiagnostics) .accessibilityIdentifier("support.includeDiagnostics") Text( SupportText.text( "Diagnostics include version numbers, permission states, and anonymous counts of apps, displays, and capability checks. They never include window titles, home paths, or other app identifiers." ) ) .font(.callout) if draft.usesEmail && !SupportDraft.validAddress(draft.recipient) { let warning = SupportText.text( "The support email address is invalid. Please contact the Parcel maintainer.") Label(warning, systemImage: "exclamationmark.triangle") .labelStyle(.titleAndIcon) .foregroundStyle(.red) .accessibilityElement(children: .ignore) .accessibilityLabel(warning) .accessibilityIdentifier("support.invalidAddress") } ScrollView { VStack(alignment: .leading, spacing: 12) { if draft.usesEmail { LabeledContent(SupportText.text("To")) { Text(draft.recipient).textSelection(.enabled) } } else { LabeledContent(SupportText.text("Issue page")) { Text(draft.issuesURL).textSelection(.enabled) } } LabeledContent(SupportText.text("Subject")) { Text(draft.kind.subject).textSelection(.enabled) } VStack(alignment: .leading, spacing: 4) { Text(SupportText.text("Message")).font(.headline) if draft.includeDiagnostics && draft.diagnostics.runtime == nil { if collect == nil { Text(SupportText.text("Diagnostics aren’t available. Turn them off to continue.")) } else if diagnosticsFailed { Text( SupportText.text("Couldn’t collect diagnostics. Turn diagnostics off or try again.") ) Button(SupportText.text("Try Again")) { diagnosticsAttempt += 1 } } else { Text(SupportText.text("Collecting diagnostics…")) } } else { Text(verbatim: draft.body).textSelection(.enabled) .frame(maxWidth: .infinity, alignment: .leading) } } } .padding(12) .frame(maxWidth: .infinity, alignment: .leading) } .background(.quaternary, in: RoundedRectangle(cornerRadius: 8)) HStack { Spacer() Button(draft.submissionTitle) { do { try draft.submission().perform( open: { NSWorkspace.shared.open($0) }, copy: { text in NSPasteboard.general.clearContents() return NSPasteboard.general.setString(text, forType: .string) }) } catch { self.error = error.localizedDescription } } .accessibilityIdentifier("support.submit") .disabled(!draft.readyToSubmit) } } .task(id: SupportDiagnosticsRequest(included: draft.includeDiagnostics, attempt: diagnosticsAttempt)) { draft.diagnostics.runtime = nil diagnosticsFailed = false guard draft.includeDiagnostics else { return } guard let collect else { diagnosticsFailed = true; return } do { let runtime = try await collect() guard !Task.isCancelled else { return } draft.diagnostics.runtime = runtime } catch { guard !Task.isCancelled else { return } diagnosticsFailed = true } } .padding(24) .frame(minWidth: 420, minHeight: 400) .alert( SupportText.text("Unable to continue"), isPresented: Binding( get: { error != nil }, set: { if !$0 { error = nil } } ) ) { Button(SupportText.text("OK")) { error = nil } } message: { Text(error ?? "") } }}