Skip to content

Fastest and only macOS Dictation app with on-device STT and custom trained AI enhancement model. Windows, iOS testflight! Android, Linux waitlist open! A local Wispr Flow alternative. Follow https://x.com/fluidvoiceapp for more

fix(clipboard): preserve filenames when oversized payloads are skipped

altic-deva3c896a7614b
diff --git a/Sources/Fluid/Services/PasteDeliveryCoordinator.swift b/Sources/Fluid/Services/PasteDeliveryCoordinator.swift
index dffb80e3..54d93466 100644
--- a/Sources/Fluid/Services/PasteDeliveryCoordinator.swift
+++ b/Sources/Fluid/Services/PasteDeliveryCoordinator.swift
@@ -167,6 +167,7 @@ final class SystemPasteboardManager: PasteboardManaging {
}
var representations: [PasteboardSnapshot.Item.Representation] = []
+ var skippedOversizedRepresentation = false
representations.reserveCapacity(sourceTypes.count)
for type in sourceTypes {
guard let data = sourceItem.data(forType: type) else {
@@ -174,6 +175,7 @@ final class SystemPasteboardManager: PasteboardManaging {
break itemLoop
}
guard data.count <= Self.maximumRepresentationBytes else {
+ skippedOversizedRepresentation = true
skippedBytes += data.count
skippedTypes.append(type.rawValue)
continue
@@ -182,9 +184,22 @@ final class SystemPasteboardManager: PasteboardManaging {
totalBytes += data.count
totalRepresentations += 1
}
+ // File URLs are small references, not file contents. Preserve them and
+ // offer a plain filename when a large accompanying payload was omitted.
+ if skippedOversizedRepresentation,
+ !representations.contains(where: { $0.type == .string }),
+ let fileData = representations.first(where: { $0.type == .fileURL })?.data,
+ let url = URL(dataRepresentation: fileData, relativeTo: nil, isAbsolute: true),
+ url.isFileURL, !url.lastPathComponent.isEmpty
+ {
+ let nameData = Data(url.lastPathComponent.utf8)
+ representations.append(.init(type: .string, data: nameData))
+ totalBytes += nameData.count
+ totalRepresentations += 1
+ }
guard !representations.isEmpty else { continue }
let item = PasteboardSnapshot.Item(representations: representations)
- let portableImage = Self.portableImageRepresentation(for: item)
+ let portableImage = skippedOversizedRepresentation ? nil : Self.portableImageRepresentation(for: item)
totalPortableImageBytes += portableImage?.data.count ?? 0
items.append(
.init(
@@ -551,7 +566,9 @@ final class SystemPasteboardManager: PasteboardManaging {
@MainActor
final class SystemPasteCommandPoster: PasteCommandPosting {
func postGlobalPasteCommand() async -> Bool {
- guard AXIsProcessTrusted(), let events = Self.makePasteEvents() else { return false }
+ guard AXIsProcessTrusted() else { return false }
+ let events = Self.makePasteEvents()
+ guard !events.isEmpty else { return false }
// The coordinator verifies pasteboard ownership before reaching this point,
// so the temporary item is ready to consume without an additional delay.
@@ -564,7 +581,7 @@ final class SystemPasteCommandPoster: PasteCommandPosting {
/// Command down, V down, V up, Command up. Pressing and releasing Command
/// explicitly matters: setting the flag on V alone leaves the HID state
/// reporting Command as held, which later blocks the send key.
- nonisolated static func makePasteEvents() -> [CGEvent]? {
+ nonisolated static func makePasteEvents() -> [CGEvent] {
let pasteKeyCode = TypingService.pasteVirtualKeyCode
let commandKeyCode = CGKeyCode(kVK_Command)
let source = CGEventSource(stateID: .combinedSessionState)
@@ -573,7 +590,7 @@ final class SystemPasteCommandPoster: PasteCommandPosting {
let vUp = CGEvent(keyboardEventSource: source, virtualKey: pasteKeyCode, keyDown: false),
let cmdUp = CGEvent(keyboardEventSource: source, virtualKey: commandKeyCode, keyDown: false)
else {
- return nil
+ return []
}
cmdDown.flags = .maskCommand
diff --git a/Tests/ClipboardBackupReproductionTests.swift b/Tests/ClipboardBackupReproductionTests.swift
index a34b0fa4..ceda4ea7 100644
--- a/Tests/ClipboardBackupReproductionTests.swift
+++ b/Tests/ClipboardBackupReproductionTests.swift
@@ -9,6 +9,7 @@ enum ClipboardBackupReproductionTests {
static func main() async {
var failures = 0
var cases = 0
+ self.testOversizedFileFallback()
for keepBackup in [false, true] {
for scenario in Scenario.allCases {
cases += 1
@@ -63,6 +64,31 @@ enum ClipboardBackupReproductionTests {
exit(failures == 0 ? 0 : 1)
}
+ static func testOversizedFileFallback() {
+ for existingText in [nil, "original label"] as [String?] {
+ for oversized in [false, true] {
+ let board = NSPasteboard(name: .init("fluidvoice.filename-fallback.\(UUID().uuidString)"))
+ defer { board.releaseGlobally() }
+ let fileURL = URL(fileURLWithPath: "/tmp/Report with spaces.pdf")
+ let payloadType = NSPasteboard.PasteboardType("com.fluidvoice.tests.payload")
+ let item = NSPasteboardItem()
+ precondition(item.setString(fileURL.absoluteString, forType: .fileURL))
+ precondition(item.setData(Data(count: oversized ? SystemPasteboardManager.maximumRepresentationBytes + 1 : 16), forType: payloadType))
+ if let existingText { precondition(item.setString(existingText, forType: .string)) }
+ precondition(board.writeObjects([item]))
+ let manager = SystemPasteboardManager(pasteboard: board)
+ guard let snapshot = manager.captureSnapshot() else { fatalError("Snapshot missing") }
+ precondition(manager.writeTemporaryText("dictated", sessionID: "filename-test"))
+ precondition(manager.restore(snapshot))
+ precondition(board.string(forType: .fileURL).flatMap(URL.init(string:)) == fileURL)
+ precondition(board.string(forType: .string) == (existingText ?? (oversized ? fileURL.lastPathComponent : nil)))
+ precondition((board.data(forType: payloadType) == nil) == oversized)
+ if oversized { precondition(snapshot.items.first?.portableImage == nil) }
+ }
+ }
+ print("PASS: 4 filename fallback cases; file URLs, existing text and small payloads preserved")
+ }
+
static func testPreparation() async -> (cases: Int, failures: Int) {
var failures = 0
var cases = 0
diff --git a/Tests/FluidDictationIntegrationTests/SpokenSendTests.swift b/Tests/FluidDictationIntegrationTests/SpokenSendTests.swift
index 82796e80..ac65e6e0 100644
--- a/Tests/FluidDictationIntegrationTests/SpokenSendTests.swift
+++ b/Tests/FluidDictationIntegrationTests/SpokenSendTests.swift
@@ -340,7 +340,7 @@ final class SpokenSendTests: XCTestCase {
// MARK: - Adversarial: paste key sequence
func testPasteSequencePressesAndReleasesCommandExplicitly() throws {
- let events = try XCTUnwrap(SystemPasteCommandPoster.makePasteEvents())
+ let events = SystemPasteCommandPoster.makePasteEvents()
XCTAssertEqual(events.count, 4)
let keyCodes = events.map { CGKeyCode($0.getIntegerValueField(.keyboardEventKeycode)) }
XCTAssertEqual(keyCodes, [
@@ -355,7 +355,8 @@ final class SpokenSendTests: XCTestCase {
}
func testPasteSequenceIsInvisibleToTheHotkeyTap() throws {
- let events = try XCTUnwrap(SystemPasteCommandPoster.makePasteEvents())
+ let events = SystemPasteCommandPoster.makePasteEvents()
+ XCTAssertEqual(events.count, 4)
for event in events {
XCTAssertTrue(GlobalHotkeyManager.isSynthesizedTypingEvent(event))
}