From 4c9fc452b6a2053f708617e1c95abe44199e6615 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 21 Jan 2024 17:07:24 -0800 Subject: [PATCH] macos: register that we accept/send text types for services --- macos/Sources/App/macOS/AppDelegate.swift | 5 ++- .../Sources/Ghostty/SurfaceView_AppKit.swift | 39 +++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/macos/Sources/App/macOS/AppDelegate.swift b/macos/Sources/App/macOS/AppDelegate.swift index f4dd9f1cb..d3d3e1f4b 100644 --- a/macos/Sources/App/macOS/AppDelegate.swift +++ b/macos/Sources/App/macOS/AppDelegate.swift @@ -109,9 +109,10 @@ class AppDelegate: NSObject, // Initial config loading configDidReload(ghostty) - // Register our service provider. This must happen after everything - // else is initialized. + // Register our service provider. This must happen after everything is initialized. NSApp.servicesProvider = ServiceProvider() + + // This registers the Ghostty => Services menu to exist. NSApp.servicesMenu = menuServices // Configure user notifications diff --git a/macos/Sources/Ghostty/SurfaceView_AppKit.swift b/macos/Sources/Ghostty/SurfaceView_AppKit.swift index fb1915fc1..1938ffecd 100644 --- a/macos/Sources/Ghostty/SurfaceView_AppKit.swift +++ b/macos/Sources/Ghostty/SurfaceView_AppKit.swift @@ -953,3 +953,42 @@ extension Ghostty.SurfaceView: NSTextInputClient { print("SEL: \(selector)") } } + +// MARK: Services + +// https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/SysServices/Articles/using.html +extension Ghostty.SurfaceView: NSServicesMenuRequestor { + override func validRequestor( + forSendType sendType: NSPasteboard.PasteboardType?, + returnType: NSPasteboard.PasteboardType? + ) -> Any? { + // Types that we accept sent to us + let accepted: [NSPasteboard.PasteboardType] = [.string, .init("public.utf8-plain-text")] + + // We can always receive the accepted types + if (returnType == nil || accepted.contains(returnType!)) { + return self + } + + // If we have a selection we can send the accepted types too + // TODO selection + if (sendType == nil || accepted.contains(sendType!)) { + return self + } + + return super.validRequestor(forSendType: sendType, returnType: returnType) + } + + func writeSelection( + to pboard: NSPasteboard, + types: [NSPasteboard.PasteboardType] + ) -> Bool { + // TODO + return false + } + + func readSelection(from pboard: NSPasteboard) -> Bool { + // TODO + return false + } +}