- Centralize clipboard operations through ClipboardManager - Remove iOS-specific code - Improve error handling and user feedback - Make clipboard restoration non-blocking - Add proper success/failure status for clipboard operations - Increase error message visibility duration - Use background queue for clipboard restoration
51 lines
1.5 KiB
Swift
51 lines
1.5 KiB
Swift
import SwiftUI
|
|
import AppKit
|
|
|
|
struct ClipboardManager {
|
|
enum ClipboardError: Error {
|
|
case copyFailed
|
|
case accessDenied
|
|
}
|
|
|
|
static func copyToClipboard(_ text: String) -> Bool {
|
|
let pasteboard = NSPasteboard.general
|
|
pasteboard.clearContents()
|
|
return pasteboard.setString(text, forType: .string)
|
|
}
|
|
|
|
static func getClipboardContent() -> String? {
|
|
return NSPasteboard.general.string(forType: .string)
|
|
}
|
|
}
|
|
|
|
struct ClipboardMessageModifier: ViewModifier {
|
|
@Binding var message: String
|
|
|
|
func body(content: Content) -> some View {
|
|
content
|
|
.overlay(
|
|
Group {
|
|
if !message.isEmpty {
|
|
Text(message)
|
|
.font(.caption)
|
|
.foregroundColor(.green)
|
|
.padding(.horizontal, 8)
|
|
.padding(.vertical, 4)
|
|
.background(Color.green.opacity(0.1))
|
|
.cornerRadius(4)
|
|
.transition(.opacity)
|
|
.animation(.easeInOut, value: message)
|
|
}
|
|
}
|
|
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topTrailing)
|
|
.padding()
|
|
)
|
|
}
|
|
}
|
|
|
|
extension View {
|
|
func clipboardMessage(_ message: Binding<String>) -> some View {
|
|
self.modifier(ClipboardMessageModifier(message: message))
|
|
}
|
|
}
|