Files
museion/Museion/FileDropTextView.swift
T
AlexKucera 46e87e678c fix(editor): editor owns file drops on macOS 27.2
Dropping an image file on the prose editor pasted the file path
instead of ingesting it (#141 follow-up, unticketed; dogfood on
macOS 27.2.1). #141 was field-verified on 27.0.1, where the
window-level SwiftUI dropDestination starved every deeper
destination; 27.2 routes a drag to the deepest destination, and a
live NSTextView registers itself for drag types, so its stock
performDragOperation pasted the path and the window destination
never fired — the same for a markdown file dropped on the editor.

The editor text views now share a FileDropTextView base that owns
the drops landing on them: URL-bearing drags route (never super) —
the prose editor through the one new AppGateState.routeFileDrop
(Cold Turkey guard, markdown → guest, anything else → ingestDrop),
which the window destination also uses for drops landing outside
the editor; the outline rows bounce; text-only drags keep the stock
drop; the pasteboard probe is memoized per changeCount. A web URL
refuses with the type strip, a vanished file refuses as missing.

+9 tests (a real NSDraggingInfo mock drives the overrides; the
router's guest/lock/ingest branches pinned), all flip-verified.
Finish-audit: three reviewers; the text-drag regression and the
outline rows' identical exposure fixed, two pre-existing ceilings
deferred in the devlog. Rail 729/729, swiftformat 0/153, swiftlint 0.
2026-12-03 16:36:47 +01:00

90 lines
3.2 KiB
Swift

//
// 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)?) {}
}