Repository Public
1import AppKit2import SwiftTerm34/// SwiftTerm's terminal view with the hooks SP Term needs: guarded paste, file drops,5/// and a host-provided context menu.6@MainActor7public final class SPTerminalView: TerminalView {8 /// Called before a paste with the clipboard text. Return `true` to let the paste proceed now;9 /// return `false` to take over (show a confirmation, then call `pasteApproved`).10 public var pasteInterceptor: ((String) -> Bool)?11 /// File URLs dropped on the view.12 public var onFileDrop: (([URL]) -> Void)?13 /// Plain text dropped on the view (goes through the same checks as a paste). The point is the14 /// drop location normalized to 0…1 with the origin at the top-left corner of the view.15 public var onTextDrop: ((String, CGPoint) -> Void)?16 public var contextMenuProvider: (() -> NSMenu?)?1718 public override init(frame: CGRect, font: NSFont?) {19 super.init(frame: frame, font: font)20 registerForDraggedTypes([.fileURL, .string])21 }2223 public override init(frame: CGRect, font: NSFont? = nil, options: TerminalOptions) {24 super.init(frame: frame, font: font, options: options)25 registerForDraggedTypes([.fileURL, .string])26 }2728 public required init?(coder: NSCoder) { fatalError("init(coder:) is not supported") }2930 // MARK: User input tracking3132 /// Sends text as if the user had typed it (counts as user input for broadcast).33 public func typeText(_ text: String) {34 UserInputScope.run { send(txt: text) }35 }3637 // MARK: Paste3839 public override func paste(_ sender: Any) {40 let text = NSPasteboard.general.string(forType: .string) ?? ""41 if let pasteInterceptor, !text.isEmpty, !pasteInterceptor(text) { return }42 pasteApproved()43 }4445 /// Performs the paste through SwiftTerm (bracketed-paste aware, control bytes filtered).46 public func pasteApproved() {47 UserInputScope.run { super.paste(self) }48 }4950 // MARK: Drag & drop5152 public override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation {53 let board = sender.draggingPasteboard54 if board.canReadObject(forClasses: [NSURL.self], options: [.urlReadingFileURLsOnly: true]) { return .copy }55 if board.string(forType: .string) != nil { return .copy }56 return []57 }5859 public override func performDragOperation(_ sender: NSDraggingInfo) -> Bool {60 let board = sender.draggingPasteboard61 if let urls = board.readObjects(forClasses: [NSURL.self], options: [.urlReadingFileURLsOnly: true]) as? [URL], !urls.isEmpty {62 onFileDrop?(urls)63 window?.makeFirstResponder(self)64 return true65 }66 if let text = board.string(forType: .string), !text.isEmpty {67 let local = convert(sender.draggingLocation, from: nil)68 let x = bounds.width > 0 ? local.x / bounds.width : 0.569 let y = bounds.height > 0 ? local.y / bounds.height : 0.570 onTextDrop?(text, CGPoint(x: min(max(x, 0), 1), y: min(max(isFlipped ? y : 1 - y, 0), 1)))71 window?.makeFirstResponder(self)72 return true73 }74 return false75 }7677 // MARK: Menu7879 public override func menu(for event: NSEvent) -> NSMenu? {80 contextMenuProvider?() ?? super.menu(for: event)81 }82}8384/// Marks the dynamic extent in which bytes sent to a PTY originate from the user (a key event,85/// a paste, a drop) rather than from the terminal emulator answering a program (device reports,86/// cursor position replies). The app's window wraps key-event dispatch in `run`; "broadcast87/// input" mirrors only bytes sent inside such a scope, so automatic replies are never mirrored.88@MainActor89public enum UserInputScope {90 public private(set) static var depth = 091 public static var isActive: Bool { depth > 0 }9293 public static func run<T>(_ body: () throws -> T) rethrows -> T {94 depth += 195 defer { depth -= 1 }96 return try body()97 }98}99