feat: add extension to normalize OSPasteboard string interface

pull/12712/head
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/CrossKit.swift,
"Helpers/Extensions/NSImage+Extension.swift", "Helpers/Extensions/NSImage+Extension.swift",
"Helpers/Extensions/OSColor+Extension.swift", "Helpers/Extensions/OSColor+Extension.swift",
"Helpers/Extensions/OSPasteboard+Extension.swift",
); );
target = 8193244C2F24E6C000A9ED8F /* DockTilePlugin */; target = 8193244C2F24E6C000A9ED8F /* DockTilePlugin */;
}; };

View File

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

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 typealias StartSearch = Ghostty.Action.StartSearch
/// A unique pasteboard for each test case prevents flakiness. /// A unique pasteboard for each test case prevents flakiness.
let pasteboard = OSPasteboard( let pasteboard = OSPasteboard.withUniqueName()
name: OSPasteboard.Name(rawValue: UUID().uuidString)
)
init() { init() {
pasteboard.setString("pb", forType: .string) pasteboard.setString("pb", forType: .string)