// // FileDropTextView.swift // Museion // // #141 (macOS 27.2.1): a drag goes to the DEEPEST destination, and a live // NSTextView registers itself for drag types — so a file drag landing on // an editor text view is delivered to the view, not to the window-level // SwiftUI .dropDestination (the inverse of the 27.0.1 ruling #141 was // verified on). Stock NSTextView handling pastes the dropped file's PATH // (`readSelection` on NSFilenamesPboardType). This base replaces that for // URL-bearing drags and leaves text-only drags to the stock behavior. The // prose editor routes through the gate's one router (`AppGateState. // routeFileDrop`); the outline rows have no route and bounce. // import AppKit class FileDropTextView: NSTextView { /// Where a URL-bearing drop routes. Nil = the view claims nothing /// (outline rows: images don't ingest there, so the drag bounces /// instead of pasting a path). var onFileDrop: (([URL]) -> Bool)? /// One classification per pasteboard state — `draggingUpdated` fires on /// every mouse-move and the read is a pasteboard round-trip (a hostile /// 20k-URL drag measured 137 ms per move uncached). private var dropProbe: (changeCount: Int, kind: DropKind)? enum DropKind: Equatable { /// File URLs (Finder) or web URLs (browser) — routed, never pasted. case urls([URL]) /// Text only (a selection drag, text from another app) — the stock /// NSTextView drop, which inserts text and nothing else. case text /// Raw image data, file promises — anything URL-less and text-less. case none } /// The drag's URLs — or nil when it carries none. static func droppedURLs(from pasteboard: NSPasteboard) -> [URL]? { let urls = pasteboard.readObjects(forClasses: [NSURL.self], options: nil) as? [URL] ?? [] return urls.isEmpty ? nil : urls } static func classify(_ pasteboard: NSPasteboard) -> DropKind { if let urls = droppedURLs(from: pasteboard) { return .urls(urls) } return pasteboard.types?.contains(.string) == true ? .text : .none } /// `classify`, memoized on the pasteboard's changeCount. func dropKind(of pasteboard: NSPasteboard) -> DropKind { if let dropProbe, dropProbe.changeCount == pasteboard.changeCount { return dropProbe.kind } let kind = Self.classify(pasteboard) dropProbe = (pasteboard.changeCount, kind) return kind } override func draggingEntered(_ sender: any NSDraggingInfo) -> NSDragOperation { switch dropKind(of: sender.draggingPasteboard) { case .urls: onFileDrop != nil ? .copy : [] // accept so a refusal stays LOUD case .text: super.draggingEntered(sender) case .none: [] } } override func draggingUpdated(_ sender: any NSDraggingInfo) -> NSDragOperation { switch dropKind(of: sender.draggingPasteboard) { case .urls: onFileDrop != nil ? .copy : [] case .text: super.draggingUpdated(sender) case .none: [] } } override func performDragOperation(_ sender: any NSDraggingInfo) -> Bool { switch dropKind(of: sender.draggingPasteboard) { case let .urls(urls): onFileDrop?(urls) ?? false case .text: super.performDragOperation(sender) case .none: false } } /// Nothing to conclude — the ingest leaves the scroll stack as a /// keystroke would. override func concludeDragOperation(_: (any NSDraggingInfo)?) {} }