Skip to content

A fast file manager for Windows and macOS that never freezes: tabs, dual panes, archives as folders, Quick Look-style previews. A File Explorer and Finder alternative written in Rust.

2.8 KB
Raw Blame
import AppKit

/// Tracks a background drag in document coordinates, independent of AppKit's deferred selection.
@MainActor
final class SelectionMarquee: NSView {
    private var box: NSRect = .zero

    override func hitTest(_ point: NSPoint) -> NSView? { nil }

    override func draw(_ dirtyRect: NSRect) {
        NSColor.controlAccentColor.withAlphaComponent(0.15).setFill()
        box.fill()
        NSColor.controlAccentColor.withAlphaComponent(0.8).setStroke()
        NSBezierPath(rect: box.insetBy(dx: 0.5, dy: 0.5)).stroke()
    }

    /// Returns false for a plain click, so the caller can preserve native click handling.
    static func track(from event: NSEvent, in view: NSView, update: (NSRect) -> Void) -> Bool {
        guard let window = view.window else { return false }
        let origin = view.convert(event.locationInWindow, from: nil)
        // The visible clip can extend past the document, including the empty area
        // where a drag starts. Draw across the viewport, not just over the items.
        let host = view.enclosingScrollView?.contentView ?? view
        let overlay = SelectionMarquee(frame: .zero)
        var lastDrag: NSEvent?
        var lastRect: NSRect?
        defer { overlay.removeFromSuperview() }

        while let next = window.nextEvent(matching: [.leftMouseDragged, .leftMouseUp],
                                          until: lastDrag == nil ? .distantFuture : Date(timeIntervalSinceNow: 0.05),
                                          inMode: .eventTracking, dequeue: true) ?? lastDrag {
            if next.type == .leftMouseUp, lastDrag == nil { return false }
            let point = view.convert(next.locationInWindow, from: nil)
            if lastDrag == nil {
                guard hypot(point.x - origin.x, point.y - origin.y) >= 3 else { continue }
                window.makeFirstResponder(view)
                overlay.frame = host.bounds
                host.addSubview(overlay, positioned: .above, relativeTo: nil)
            }
            if next.type == .leftMouseDragged { _ = view.autoscroll(with: next) }
            let end = view.convert(next.locationInWindow, from: nil)
            let rect = NSRect(x: min(origin.x, end.x), y: min(origin.y, end.y),
                              width: max(abs(end.x - origin.x), 1), height: max(abs(end.y - origin.y), 1))
            overlay.frame = host.bounds
            let box = overlay.convert(rect, from: view)
            if overlay.box != box {
                overlay.box = box
                overlay.needsDisplay = true
            }
            if lastRect != rect {
                lastRect = rect
                update(rect)
            }
            window.displayIfNeeded()
            if next.type == .leftMouseUp || NSEvent.pressedMouseButtons & 1 == 0 { return true }
            lastDrag = next
        }
        return lastDrag != nil
    }
}