forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopyButton.swift
More file actions
63 lines (56 loc) · 1.86 KB
/
Copy pathCopyButton.swift
File metadata and controls
63 lines (56 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import AppKit
import SwiftUI
public struct CopyButton: View {
public var copy: () -> Void
@State var isCopied = false
public init(copy: @escaping () -> Void) {
self.copy = copy
}
public var body: some View {
Image(systemName: isCopied ? "checkmark.circle.fill" : "doc.on.doc.fill")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 14, height: 14)
.frame(width: 20, height: 20, alignment: .center)
.foregroundColor(.secondary)
.background(
.regularMaterial,
in: RoundedRectangle(cornerRadius: 4, style: .circular)
)
.background {
RoundedRectangle(cornerRadius: 4, style: .circular)
.fill(Color.primary.opacity(0.1))
}
.padding(4)
.simultaneousGesture(
TapGesture()
.onEnded { _ in
withAnimation(.linear(duration: 0.1)) {
isCopied = true
}
copy()
Task {
try await Task.sleep(nanoseconds: 1_000_000_000)
withAnimation(.linear(duration: 0.1)) {
isCopied = false
}
}
}
)
}
}
public struct DraggableCopyButton: View {
public var content: () -> String
public init(content: @escaping () -> String) {
self.content = content
}
public var body: some View {
CopyButton {
NSPasteboard.general.clearContents()
NSPasteboard.general.setString(content(), forType: .string)
}
.onDrag {
NSItemProvider(object: content() as NSString)
}
}
}