-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathCopyButton.swift
More file actions
40 lines (37 loc) · 1.24 KB
/
CopyButton.swift
File metadata and controls
40 lines (37 loc) · 1.24 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
import AppKit
import SwiftUI
public struct CopyButton: View {
public var copy: () -> Void
@State var isCopied = false
private var foregroundColor: Color?
private var fontWeight: Font.Weight?
public init(copy: @escaping () -> Void, foregroundColor: Color? = nil, fontWeight: Font.Weight? = nil) {
self.copy = copy
self.foregroundColor = foregroundColor
self.fontWeight = fontWeight
}
public var body: some View {
Button(action: {
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
}
}
}) {
Image(systemName: isCopied ? "checkmark.circle" : "doc.on.doc")
.resizable()
.scaledToFit()
.scaledPadding(2)
.scaledFrame(width: 16, height: 16)
.foregroundColor(foregroundColor ?? .secondary)
.conditionalFontWeight(fontWeight)
}
.buttonStyle(HoverButtonStyle(padding: 0))
.help("Copy")
}
}