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.

Show live drag selection in macOS file views

sunzhaozhengd668639d20f5
diff --git a/macos/Sources/RustyFiles/FileList.swift b/macos/Sources/RustyFiles/FileList.swift
index 8fde901e..4e1571e2 100644
--- a/macos/Sources/RustyFiles/FileList.swift
+++ b/macos/Sources/RustyFiles/FileList.swift
@@ -579,6 +579,23 @@ final class FileTableView: NSTableView, NSServicesMenuRequestor {
if selectedRowIndexes.contains(r) { deselectRow(r) } else { selectRowIndexes([r], byExtendingSelection: true) }
return
}
+ if r < 0, event.clickCount == 1, allowsMultipleSelection,
+ !event.modifierFlags.contains(.control) {
+ let initial = selectedRowIndexes
+ let toggle = event.modifierFlags.contains(.command)
+ let extend = event.modifierFlags.contains(.shift)
+ let dragged = SelectionMarquee.track(from: event, in: self) { rect in
+ let rows = self.rows(in: rect)
+ let hits = rows.location == NSNotFound ? IndexSet() : IndexSet(integersIn: rows.location..<(rows.location + rows.length))
+ let selection = toggle ? initial.symmetricDifference(hits) : extend ? initial.union(hits) : hits
+ if selection != selectedRowIndexes { selectRowIndexes(selection, byExtendingSelection: false) }
+ }
+ if !dragged {
+ window?.makeFirstResponder(self)
+ if !toggle && !extend { deselectAll(nil) }
+ }
+ return
+ }
super.mouseDown(with: event)
}
diff --git a/macos/Sources/RustyFiles/SelectionMarquee.swift b/macos/Sources/RustyFiles/SelectionMarquee.swift
new file mode 100644
index 00000000..0d5dab8a
--- /dev/null
+++ b/macos/Sources/RustyFiles/SelectionMarquee.swift
@@ -0,0 +1,53 @@
+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)
+ let overlay = SelectionMarquee(frame: view.bounds)
+ overlay.autoresizingMask = [.width, .height]
+ 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)
+ view.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))
+ if lastRect != rect {
+ lastRect = rect
+ overlay.box = overlay.convert(rect, from: view)
+ overlay.needsDisplay = true
+ update(rect)
+ window.displayIfNeeded()
+ }
+ if next.type == .leftMouseUp || NSEvent.pressedMouseButtons & 1 == 0 { return true }
+ lastDrag = next
+ }
+ return lastDrag != nil
+ }
+}
diff --git a/macos/Sources/RustyFiles/Tiles.swift b/macos/Sources/RustyFiles/Tiles.swift
index bee5b20d..4d3a467d 100644
--- a/macos/Sources/RustyFiles/Tiles.swift
+++ b/macos/Sources/RustyFiles/Tiles.swift
@@ -280,6 +280,29 @@ final class TileCollectionView: NSCollectionView, NSServicesMenuRequestor {
}
override func mouseDown(with event: NSEvent) {
+ let point = convert(event.locationInWindow, from: nil)
+ if event.clickCount == 1, isSelectable, allowsMultipleSelection,
+ indexPathForItem(at: point) == nil,
+ !event.modifierFlags.contains(.control) {
+ let initial = selectionIndexPaths
+ let toggle = event.modifierFlags.contains(.command)
+ let extend = event.modifierFlags.contains(.shift)
+ let dragged = SelectionMarquee.track(from: event, in: self) { rect in
+ let hits = Set((collectionViewLayout?.layoutAttributesForElements(in: rect) ?? [])
+ .filter { $0.representedElementCategory == .item && $0.frame.intersects(rect) }
+ .compactMap(\.indexPath))
+ let selection = toggle ? initial.symmetricDifference(hits) : extend ? initial.union(hits) : hits
+ updateMarqueeSelection(selection)
+ }
+ if !dragged {
+ window?.makeFirstResponder(self)
+ if !toggle && !extend { updateMarqueeSelection([]) }
+ }
+ if initial != selectionIndexPaths {
+ delegate?.collectionView?(self, didSelectItemsAt: selectionIndexPaths)
+ }
+ return
+ }
super.mouseDown(with: event)
guard event.clickCount == 2 else { return }
if indexPathForItem(at: convert(event.locationInWindow, from: nil)) != nil {
@@ -290,6 +313,11 @@ final class TileCollectionView: NSCollectionView, NSServicesMenuRequestor {
}
}
+ private func updateMarqueeSelection(_ selection: Set<IndexPath>) {
+ guard selectionIndexPaths != selection else { return }
+ selectionIndexPaths = selection
+ }
+
override func menu(for event: NSEvent) -> NSMenu? {
let at = indexPathForItem(at: convert(event.locationInWindow, from: nil))
if let at, !selectionIndexPaths.contains(at) {