From 413d11b00bcee4686a9b33214a0a8deeed4bee80 Mon Sep 17 00:00:00 2001 From: Beingpax Date: Mon, 3 Mar 2025 14:30:53 +0545 Subject: [PATCH] Improve clipboard handling and reorder paste operations --- VoiceInk/CursorPaster.swift | 18 +++++++++++------- VoiceInk/MediaController.swift | 2 +- VoiceInk/WhisperState.swift | 20 +++++++++++--------- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/VoiceInk/CursorPaster.swift b/VoiceInk/CursorPaster.swift index 0ced9b0..3faa140 100644 --- a/VoiceInk/CursorPaster.swift +++ b/VoiceInk/CursorPaster.swift @@ -35,13 +35,17 @@ class CursorPaster { vUp?.post(tap: .cghidEventTap) cmdUp?.post(tap: .cghidEventTap) - // Restore the original pasteboard contents after a delay - // Use a background queue to not block the main thread - DispatchQueue.global(qos: .userInitiated).asyncAfter(deadline: .now() + pasteCompletionDelay) { - if let oldContents = oldContents { - pasteboard.clearContents() - pasteboard.setString(oldContents, forType: .string) + // First clear the clipboard of our pasted content + DispatchQueue.global(qos: .userInitiated).asyncAfter(deadline: .now() + 0.1) { + pasteboard.clearContents() + + // Then restore the original content after a short delay + DispatchQueue.global(qos: .userInitiated).asyncAfter(deadline: .now() + 0.2) { + if let oldContents = oldContents { + pasteboard.clearContents() + pasteboard.setString(oldContents, forType: .string) + } } } } -} +} \ No newline at end of file diff --git a/VoiceInk/MediaController.swift b/VoiceInk/MediaController.swift index 5132bbc..82c0208 100644 --- a/VoiceInk/MediaController.swift +++ b/VoiceInk/MediaController.swift @@ -26,7 +26,7 @@ class MediaController: ObservableObject { // Additional function pointers for direct control private var mrSendCommand: (@convention(c) (Int, [String: Any]?) -> Bool)? - // MediaRemote command constants + // MediaRemote command constantst private let kMRPlay = 0 private let kMRPause = 1 private let kMRTogglePlayPause = 2 diff --git a/VoiceInk/WhisperState.swift b/VoiceInk/WhisperState.swift index efe8ebd..b850b79 100644 --- a/VoiceInk/WhisperState.swift +++ b/VoiceInk/WhisperState.swift @@ -552,6 +552,17 @@ class WhisperState: NSObject, ObservableObject, AVAudioRecorderDelegate { // Play stop sound when transcription is complete SoundManager.shared.playStopSound() + // First try to paste if accessibility permissions are granted + if AXIsProcessTrusted() { + // For notch recorder, paste right after animation starts (animation takes 0.3s) + DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { + CursorPaster.pasteAtCursor(text) + } + } else { + messageLog += "Accessibility permissions not granted. Transcription not pasted automatically.\n" + } + + // Then copy to clipboard if enabled (as a backup) if isAutoCopyEnabled { let success = ClipboardManager.copyToClipboard(text) if success { @@ -562,15 +573,6 @@ class WhisperState: NSObject, ObservableObject, AVAudioRecorderDelegate { } } - if AXIsProcessTrusted() { - // For notch recorder, paste right after animation starts (animation takes 0.3s) - DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { - CursorPaster.pasteAtCursor(text) - } - } else { - messageLog += "Accessibility permissions not granted. Transcription not pasted automatically.\n" - } - await cleanupResources() // Don't set processing states to false here