Files
wehub-resource-sync 1d1286fadb
Build / Build and test (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:18:38 +08:00

39 lines
1.2 KiB
Swift

import AppKit
import Foundation
@MainActor
struct PasteboardSnapshot {
private let items: [[NSPasteboard.PasteboardType: Data]]
static func capture(from pasteboard: NSPasteboard) -> PasteboardSnapshot {
let capturedItems = (pasteboard.pasteboardItems ?? []).map { item in
var capturedTypes: [NSPasteboard.PasteboardType: Data] = [:]
for type in item.types {
guard let data = item.data(forType: type) else { continue }
capturedTypes[type] = data
}
return capturedTypes
}
return PasteboardSnapshot(items: capturedItems)
}
@discardableResult
func restore(to pasteboard: NSPasteboard) -> Bool {
pasteboard.clearContents()
guard !items.isEmpty else { return true }
var didRestoreAllData = true
let restoredItems = items.map { capturedTypes in
let item = NSPasteboardItem()
for (type, data) in capturedTypes {
if !item.setData(data, forType: type) {
didRestoreAllData = false
}
}
return item
}
return didRestoreAllData && pasteboard.writeObjects(restoredItems)
}
}