feat: add extension to normalize OSPasteboard string interface

This commit is contained in:
Nolin McFarland
2026-05-18 10:12:26 -04:00
parent ed52160612
commit bf716a0c39
4 changed files with 33 additions and 7 deletions

View File

@@ -109,6 +109,7 @@
Helpers/CrossKit.swift,
"Helpers/Extensions/NSImage+Extension.swift",
"Helpers/Extensions/OSColor+Extension.swift",
"Helpers/Extensions/OSPasteboard+Extension.swift",
);
target = 8193244C2F24E6C000A9ED8F /* DockTilePlugin */;
};

View File

@@ -131,7 +131,7 @@ extension Ghostty.OSSurfaceView {
init(
from startSearch: Ghostty.Action.StartSearch,
pasteboard: OSPasteboard = OSPasteboard(name: .find)
pasteboard: OSPasteboard = OSPasteboard.find
) {
self.pasteboard = pasteboard
if let needle = startSearch.needle, !needle.isEmpty {
@@ -143,7 +143,7 @@ extension Ghostty.OSSurfaceView {
}
func readPasteboardNeedle() {
let pasteboardNeedle = pasteboard.string(forType: .string)
let pasteboardNeedle = pasteboard.string
if let pasteboardNeedle, pasteboardNeedle != needle {
needle = pasteboardNeedle
needleSelection = needle.startIndex..<needle.endIndex
@@ -151,8 +151,7 @@ extension Ghostty.OSSurfaceView {
}
func writePasteboardNeedle() {
pasteboard.clearContents()
pasteboard.setString(needle, forType: .string)
pasteboard.string = needle
}
}

View File

@@ -0,0 +1,28 @@
#if canImport(AppKit)
/// Normalizes the interface between NSPasteboard and UIPasteboard for working with pasteboard
/// strings.
extension OSPasteboard {
@MainActor static let find = OSPasteboard(name: .find)
/// The pasteboard's current string value.
@MainActor var string: String? {
get {
string(forType: .string)
}
set {
clearContents()
if let newValue {
setString(newValue, forType: .string)
}
}
}
}
#elseif canImport(UIKit)
extension OSPasteboard {
static let find = OSPasteboard.withUniqueName()
}
#endif

View File

@@ -8,9 +8,7 @@ import Testing
typealias StartSearch = Ghostty.Action.StartSearch
/// A unique pasteboard for each test case prevents flakiness.
let pasteboard = OSPasteboard(
name: OSPasteboard.Name(rawValue: UUID().uuidString)
)
let pasteboard = OSPasteboard.withUniqueName()
init() {
pasteboard.setString("pb", forType: .string)