mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-12-31 10:42:12 +00:00
macos: add GhosttyDelegate for global operations
This commit is contained in:
@@ -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,
|
||||
|
||||
18
macos/Sources/App/macOS/AppDelegate+Ghostty.swift
Normal file
18
macos/Sources/App/macOS/AppDelegate+Ghostty.swift
Normal 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
|
||||
}
|
||||
}
|
||||
8
macos/Sources/Ghostty/GhosttyDelegate.swift
Normal file
8
macos/Sources/Ghostty/GhosttyDelegate.swift
Normal 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?
|
||||
}
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user