clipboard: pasteboard watcher/writer and received-file staging

Pasteboard.swift is the macOS FormHelper (FormHelper.cs:234-330) plus the
writing half of Clipboard.cs:1101-1294, polled on one private serial queue.
ReceivedFile.swift ports ReceivedDestinationFile.cs and Common.GetMyStorageDir.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
This commit is contained in:
lucioandClaude Opus 5 committed 2026-09-16 04:38:19 -07:00
1 parent 8105f5f24f
commit 9ab4956551
2 files changed
+295

No files matched your search

+109
View File
@@ -0,0 +1,109 @@
// Where a file that arrives over the bulk channel lands, and how it gets there
// without ever leaving a half-written file in its place.
//
// Ported from App/Core/ReceivedDestinationFile.cs (the staging file and its
// commit/delete pair), App/Core/Common.cs GetMyStorageDir (1291-1330) and
// App/Core/Clipboard.cs (669-711, 787-798, 840-848).
//
// The staging file lives in the destination directory so the commit is a rename
// on one volume, which is what makes it atomic.
import AppKit
import Foundation
/// Why a received file could not be put where it belongs.
public enum ReceivedFileError: Error, Sendable, Equatable {
/// The Desktop folder could not be found or the download folder could not
/// be created under it.
case destinationUnavailable(String)
/// The rename into place failed.
case commitFailed(String)
}
/// The download folder and the staged-file dance around it.
///
/// Every method is a plain function of its arguments: no shared state, so this
/// is safe to call from the bulk channel's queue and from the Engine alike.
public enum ReceivedFile {
/// The folder Windows drops received files into, under the user's Desktop
/// (`Clipboard.cs:679`, `Common.cs:1321-1327`).
public static let folderName = "MouseWithoutBorders"
/// `~/Desktop/MouseWithoutBorders/`, created if it is not there yet.
public static func destinationDirectory() throws -> URL {
let manager = FileManager.default
guard
let desktop = manager.urls(for: .desktopDirectory, in: .userDomainMask).first
else {
throw ReceivedFileError.destinationUnavailable("no Desktop folder")
}
let folder = desktop.appendingPathComponent(folderName, isDirectory: true)
if !manager.fileExists(atPath: folder.path) {
do {
try manager.createDirectory(at: folder, withIntermediateDirectories: true)
} catch {
throw ReceivedFileError.destinationUnavailable("\(error)")
}
}
return folder
}
/// A hidden, unique, empty file in `directory` to stream into
/// (`ReceivedDestinationFile.cs:24-27`: `.{guid}.partial`).
public static func stagingURL(in directory: URL) -> URL {
let name = ".\(UUID().uuidString.replacingOccurrences(of: "-", with: "")).partial"
return directory.appendingPathComponent(name, isDirectory: false)
}
/// Moves a completed staging file onto its final name, replacing whatever
/// was there (`File.Move(..., overwrite: true)`, `Clipboard.cs:542`).
///
/// The existing file is only disturbed at this point, so a transfer that
/// fails half way leaves the previous copy alone.
@discardableResult
public static func commit(staged: URL, toFileNamed name: String, in directory: URL) throws
-> URL
{
let destination = directory.appendingPathComponent(name, isDirectory: false)
let manager = FileManager.default
do {
if manager.fileExists(atPath: destination.path) {
_ = try manager.replaceItemAt(destination, withItemAt: staged)
} else {
try manager.moveItem(at: staged, to: destination)
}
} catch {
throw ReceivedFileError.commitFailed("\(error)")
}
return destination
}
/// Deletes an abandoned staging file (`ReceivedDestinationFile.cs:41-46`).
/// A failure here is not worth reporting: the file is hidden and unique.
public static func discard(_ staged: URL) {
try? FileManager.default.removeItem(at: staged)
}
/// Shows the folder with the new file selected (`Clipboard.cs:840-848`
/// starts Explorer on the saving folder).
@MainActor
public static func reveal(_ file: URL) {
NSWorkspace.shared.activateFileViewerSelecting([file])
}
/// The size of a file to be sent, or `nil` when the path is not a readable
/// regular file (`SocketStuff.cs:1690-1702`).
public static func size(of file: URL) -> Int64? {
let values = try? file.resourceValues(forKeys: [.fileSizeKey, .isRegularFileKey])
guard values?.isRegularFile == true, let size = values?.fileSize else { return nil }
return Int64(size)
}
/// Whether a path names a directory, which Windows refuses to transfer
/// (`SocketStuff.cs:1693`).
public static func isDirectory(_ url: URL) -> Bool {
var directory: ObjCBool = false
let exists = FileManager.default.fileExists(atPath: url.path, isDirectory: &directory)
return exists && directory.boolValue
}
}