Improved cursorpaster with clipboard restoration

This commit is contained in:
Beingpax 2025-08-08 20:56:54 +05:45
parent bf31cfcc5b
commit 0c517c728b
3 changed files with 17 additions and 17 deletions

View File

@ -2,21 +2,18 @@ import Foundation
import AppKit
class CursorPaster {
private static let pasteCompletionDelay: TimeInterval = 0.6
static func pasteAtCursor(_ text: String, shouldPreserveClipboard: Bool = true) {
static func pasteAtCursor(_ text: String) {
let pasteboard = NSPasteboard.general
var savedContents: [(NSPasteboard.PasteboardType, 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))
}
let currentItems = pasteboard.pasteboardItems ?? []
for item in currentItems {
for type in item.types {
if let data = item.data(forType: type) {
savedContents.append((type, data))
}
}
}
@ -30,11 +27,14 @@ class CursorPaster {
pasteUsingCommandV()
}
if shouldPreserveClipboard && !savedContents.isEmpty {
DispatchQueue.global(qos: .userInitiated).asyncAfter(deadline: .now() + pasteCompletionDelay) {
pasteboard.clearContents()
for (type, data) in savedContents {
pasteboard.setData(data, forType: type)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.075) {
pasteboard.clearContents()
if !savedContents.isEmpty {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
for (type, data) in savedContents {
pasteboard.setData(data, forType: type)
}
}
}
}

View File

@ -67,7 +67,7 @@ class LastTranscriptionService: ObservableObject {
// Delay to give the user time to release modifier keys (especially Control)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.15) {
CursorPaster.pasteAtCursor(textToPaste + " ", shouldPreserveClipboard: true)
CursorPaster.pasteAtCursor(textToPaste + " ")
}
}

View File

@ -359,7 +359,7 @@ class WhisperState: NSObject, ObservableObject {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) {
CursorPaster.pasteAtCursor(text, shouldPreserveClipboard: true)
CursorPaster.pasteAtCursor(text)
let powerMode = PowerModeManager.shared
if let activeConfig = powerMode.currentActiveConfiguration, activeConfig.isAutoSendEnabled {