Skip to content

Commit 630d038

Browse files
committed
Add DraggableCopyButton
1 parent 213c277 commit 630d038

1 file changed

Lines changed: 47 additions & 28 deletions

File tree

Tool/Sources/SharedUIComponents/CopyButton.swift

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,59 @@ import SwiftUI
44
public struct CopyButton: View {
55
public var copy: () -> Void
66
@State var isCopied = false
7-
7+
88
public init(copy: @escaping () -> Void) {
99
self.copy = copy
1010
}
11-
11+
1212
public var body: some View {
13-
Button(action: {
14-
withAnimation(.linear(duration: 0.1)) {
15-
isCopied = true
16-
}
17-
copy()
18-
Task {
19-
try await Task.sleep(nanoseconds: 1_000_000_000)
20-
withAnimation(.linear(duration: 0.1)) {
21-
isCopied = false
22-
}
13+
Image(systemName: isCopied ? "checkmark.circle.fill" : "doc.on.doc.fill")
14+
.resizable()
15+
.aspectRatio(contentMode: .fit)
16+
.frame(width: 14, height: 14)
17+
.frame(width: 20, height: 20, alignment: .center)
18+
.foregroundColor(.secondary)
19+
.background(
20+
.regularMaterial,
21+
in: RoundedRectangle(cornerRadius: 4, style: .circular)
22+
)
23+
.background {
24+
RoundedRectangle(cornerRadius: 4, style: .circular)
25+
.fill(Color.primary.opacity(0.1))
2326
}
24-
}) {
25-
Image(systemName: isCopied ? "checkmark.circle.fill" : "doc.on.doc.fill")
26-
.resizable()
27-
.aspectRatio(contentMode: .fit)
28-
.frame(width: 14, height: 14)
29-
.frame(width: 20, height: 20, alignment: .center)
30-
.foregroundColor(.secondary)
31-
.background(
32-
.regularMaterial,
33-
in: RoundedRectangle(cornerRadius: 4, style: .circular)
34-
)
35-
.background {
36-
RoundedRectangle(cornerRadius: 4, style: .circular)
37-
.fill(Color.primary.opacity(0.1))
27+
.padding(4)
28+
.simultaneousGesture(
29+
TapGesture().onEnded { _ in
30+
withAnimation(.linear(duration: 0.1)) {
31+
isCopied = true
32+
}
33+
copy()
34+
Task {
35+
try await Task.sleep(nanoseconds: 1_000_000_000)
36+
withAnimation(.linear(duration: 0.1)) {
37+
isCopied = false
38+
}
39+
}
3840
}
39-
.padding(4)
41+
)
42+
}
43+
}
44+
45+
public struct DraggableCopyButton: View {
46+
public var content: () -> String
47+
48+
public init(content: @escaping () -> String) {
49+
self.content = content
50+
}
51+
52+
public var body: some View {
53+
CopyButton {
54+
NSPasteboard.general.clearContents()
55+
NSPasteboard.general.setString(content(), forType: .string)
56+
}
57+
.onDrag {
58+
NSItemProvider(object: content() as NSString)
4059
}
41-
.buttonStyle(.borderless)
4260
}
4361
}
62+

0 commit comments

Comments
 (0)