Fix clipboard auto-copy logic

This commit is contained in:
Beingpax 2025-06-16 00:40:21 +05:45
parent 7c833b7be9
commit bced13315d
2 changed files with 12 additions and 17 deletions

View File

@ -4,7 +4,7 @@ import AppKit
class CursorPaster {
private static let pasteCompletionDelay: TimeInterval = 0.3
static func pasteAtCursor(_ text: String) {
static func pasteAtCursor(_ text: String, shouldPreserveClipboard: Bool = true) {
guard AXIsProcessTrusted() else {
return
}
@ -12,12 +12,15 @@ class CursorPaster {
let pasteboard = NSPasteboard.general
var savedContents: [(NSPasteboard.PasteboardType, Data)] = []
let currentItems = pasteboard.pasteboardItems ?? []
for item in currentItems {
for type in item.types {
if let data = item.data(forType: type) {
savedContents.append((type, data))
if shouldPreserveClipboard {
let currentItems = pasteboard.pasteboardItems ?? []
for item in currentItems {
for type in item.types {
if let data = item.data(forType: type) {
savedContents.append((type, data))
}
}
}
}
@ -31,8 +34,8 @@ class CursorPaster {
pasteUsingCommandV()
}
DispatchQueue.global(qos: .userInitiated).asyncAfter(deadline: .now() + pasteCompletionDelay) {
if !savedContents.isEmpty {
if shouldPreserveClipboard && !savedContents.isEmpty {
DispatchQueue.global(qos: .userInitiated).asyncAfter(deadline: .now() + pasteCompletionDelay) {
pasteboard.clearContents()
for (type, data) in savedContents {
pasteboard.setData(data, forType: type)

View File

@ -362,15 +362,7 @@ class WhisperState: NSObject, ObservableObject, AVAudioRecorderDelegate {
SoundManager.shared.playStopSound()
if AXIsProcessTrusted() {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) {
CursorPaster.pasteAtCursor(text)
}
}
if isAutoCopyEnabled {
let success = ClipboardManager.copyToClipboard(text)
if success {
clipboardMessage = "Transcription copied to clipboard"
} else {
clipboardMessage = "Failed to copy to clipboard"
CursorPaster.pasteAtCursor(text, shouldPreserveClipboard: !self.isAutoCopyEnabled)
}
}
try? FileManager.default.removeItem(at: url)