macos: add GhosttyDelegate for global operations

This commit is contained in:
Mitchell Hashimoto
2025-12-28 07:23:14 -08:00
parent a54c557565
commit e2b45659f3
4 changed files with 37 additions and 12 deletions

View File

@@ -66,6 +66,7 @@
isa = PBXFileSystemSynchronizedBuildFileExceptionSet;
membershipExceptions = (
App/macOS/AppDelegate.swift,
"App/macOS/AppDelegate+Ghostty.swift",
App/macOS/main.swift,
App/macOS/MainMenu.xib,
Features/About/About.xib,

View File

@@ -0,0 +1,18 @@
import AppKit
extension AppDelegate: Ghostty.Delegate {
func ghosttySurface(id: UUID) -> Ghostty.SurfaceView? {
for window in NSApp.windows {
guard let controller = window.windowController as? BaseTerminalController else {
continue
}
for surface in controller.surfaceTree {
if surface.id == id {
return surface
}
}
}
return nil
}
}

View File

@@ -0,0 +1,8 @@
extension Ghostty {
/// This is a delegate that should be applied to your global app delegate for GhosttyKit
/// to perform app-global operations.
protocol Delegate {
/// Look up a surface within the application by ID.
func ghosttySurface(id: UUID) -> SurfaceView?
}
}

View File

@@ -4,6 +4,7 @@ import AppKit
import CoreTransferable
import UniformTypeIdentifiers
/// Conformance to `Transferable` enables drag-and-drop.
extension Ghostty.SurfaceView: Transferable {
static var transferRepresentation: some TransferRepresentation {
DataRepresentation(contentType: .ghosttySurfaceId) { surface in
@@ -32,22 +33,19 @@ extension Ghostty.SurfaceView: Transferable {
@MainActor
static func find(uuid: UUID) -> Self? {
#if canImport(AppKit)
for window in NSApp.windows {
guard let controller = window.windowController as? BaseTerminalController else {
continue
}
for surface in controller.surfaceTree {
if surface.id == uuid {
return surface as? Self
}
}
}
#endif
guard let del = NSApp.delegate as? Ghostty.Delegate else { return nil }
return del.ghosttySurface(id: uuid) as? Self
#elseif canImport(UIKit)
// We should be able to use UIApplication here.
return nil
#else
return nil
#endif
}
}
extension UTType {
/// A format that encodes the bare UUID only for the surface. This can be used if you have
/// a way to look up a surface by ID.
static let ghosttySurfaceId = UTType(exportedAs: "com.mitchellh.ghosttySurfaceId")
}