From a06fc4ff114f42c85f79f14ad13602d6b2bd74a5 Mon Sep 17 00:00:00 2001 From: Alexandre Antonio Juca Date: Sat, 11 Jan 2025 23:45:21 +0100 Subject: [PATCH] feat: ensure text, files and URLs can be drag and dropped to terminal window --- .../Sources/Ghostty/SurfaceView_AppKit.swift | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/macos/Sources/Ghostty/SurfaceView_AppKit.swift b/macos/Sources/Ghostty/SurfaceView_AppKit.swift index 14143313e..67154a3af 100644 --- a/macos/Sources/Ghostty/SurfaceView_AppKit.swift +++ b/macos/Sources/Ghostty/SurfaceView_AppKit.swift @@ -230,6 +230,8 @@ extension Ghostty { ghostty_surface_set_color_scheme(surface, scheme) } + + registerForDraggedTypes([.string, .fileURL, .URL]) } required init?(coder: NSCoder) { @@ -389,6 +391,68 @@ extension Ghostty { self?.title = title } } + + // MARK: - Drag and Drop + + override func draggingEntered(_ sender: any NSDraggingInfo) -> NSDragOperation { + if let _ = sender.draggingPasteboard.string(forType: .string) { + return .generic + } + + if let _ = sender.draggingPasteboard.string(forType: .URL) { + return .generic + } + + if let _ = sender.draggingPasteboard.string(forType: .fileURL) { + return .generic + } + return [] + } + + override func performDragOperation(_ sender: any NSDraggingInfo) -> Bool { + if let droppedText = sender.draggingPasteboard.string(forType: .string) { + let content = Shell.escape(droppedText) + + DispatchQueue.main.async { + self.insertText( + content, + replacementRange: NSMakeRange(0, 0) + ) + } + + return true + } + + if let droppedURL = sender.draggingPasteboard.string(forType: .URL) { + let content = Shell.escape(droppedURL) + + DispatchQueue.main.async { + self.insertText( + content, + replacementRange: NSMakeRange(0, 0) + ) + } + + return true + } + + if let droppedFileURL = sender.draggingPasteboard.string(forType: .fileURL) { + guard let urlPath = URL(string: droppedFileURL)?.path(percentEncoded: false) else { + return false + } + + DispatchQueue.main.async { + self.insertText( + urlPath, + replacementRange: NSMakeRange(0, 0) + ) + } + + return true + } + + return false + } // MARK: Local Events