Sources/Orgstar/ShareSubtreeSheet.swift

9945f06859a5feee9b6af3496e944cb6b395715b
orgstar/Sources/Orgstar/ShareSubtreeSheet.swift

94 lines · 3418 bytes

 1import AppKit
 2import OrgApp
 3import SwiftUI
 4
 5/// Share Subtree…: the entry at the caret as an org file, with its attachments if chosen, or as
 6/// text, through the sharing services (AirDrop, Mail, Messages).
 7struct ShareSubtreeSheet: View {
 8    let share: SubtreeShare
 9    let report: (String) -> Void
10    @State private var asFile = true
11    @State private var withAttachments = false
12    @Environment(\.dismiss) private var dismiss
13
14    var body: some View {
15        VStack(alignment: .leading, spacing: 12) {
16            Text("Share Subtree").font(.headline)
17            Picker("Share as", selection: $asFile) {
18                Text("Org file (\(share.fileName))").tag(true)
19                Text("Text").tag(false)
20            }
21            .pickerStyle(.radioGroup)
22            if asFile, !share.attachments.isEmpty {
23                Toggle("Include \(share.attachments.count) attachments", isOn: $withAttachments)
24            }
25            HStack {
26                Spacer()
27                Button("Cancel") { dismiss() }.keyboardShortcut(.cancelAction)
28                SharingButton(title: String(localized: "Share…"), items: items) { dismiss() }
29                    .fixedSize()
30            }
31        }
32        .padding(20)
33        .frame(width: 380)
34    }
35
36    /// The text, or the written file and the attachments chosen.
37    private func items() -> [Any] {
38        guard asFile else { return [share.text] }
39        do {
40            return share.files(file: try share.writeFile(), withAttachments: withAttachments)
41        } catch {
42            report(String(localized: "Can't share \(share.fileName): \(error.localizedDescription)"))
43            return []
44        }
45    }
46}
47
48/// A button that opens the sharing services picker under itself with `items`; `chose` runs once
49/// a service is chosen.
50struct SharingButton: NSViewRepresentable {
51    let title: String
52    let items: () -> [Any]
53    let chose: () -> Void
54
55    func makeCoordinator() -> Coordinator { Coordinator(self) }
56
57    func makeNSView(context: Context) -> NSButton {
58        let button = NSButton(title: title, target: context.coordinator, action: #selector(Coordinator.click(_:)))
59        button.bezelStyle = .push
60        button.keyEquivalent = "\r"
61        return button
62    }
63
64    func updateNSView(_ button: NSButton, context: Context) {
65        context.coordinator.parent = self
66        button.title = title
67    }
68
69    @MainActor
70    final class Coordinator: NSObject, NSSharingServicePickerDelegate, NSSharingServiceDelegate {
71        var parent: SharingButton
72
73        init(_ parent: SharingButton) {
74            self.parent = parent
75        }
76
77        @objc func click(_ sender: NSButton) {
78            let items = parent.items()
79            guard !items.isEmpty else { return }
80            let picker = NSSharingServicePicker(items: items)
81            picker.delegate = self
82            picker.show(relativeTo: sender.bounds, of: sender, preferredEdge: .minY)
83        }
84
85        /// The sheet stays while a service such as AirDrop shows its own sheet on it.
86        nonisolated func sharingServicePicker(_ picker: NSSharingServicePicker, delegateFor sharingService: NSSharingService) -> (any NSSharingServiceDelegate)? {
87            self
88        }
89
90        nonisolated func sharingService(_ sharingService: NSSharingService, didShareItems items: [Any]) {
91            MainActor.assumeIsolated { parent.chose() }
92        }
93    }
94}
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form