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.

4.4 KB
Raw Blame
import AppKit

/// A scroll view whose two-finger swipe left or right goes back or forward, as in Finder and
/// Safari (Windows has the mouse's back and forward buttons; issue 430). It follows the
/// system's Trackpad › "Swipe between pages" (NSEvent.isSwipeTrackingFromScrollEventsEnabled)
/// and Settings › Behaviours › "Swipe to go back and forward". A list that can still scroll
/// sideways in that direction scrolls instead; the swipe works once it is at its edge.
@MainActor
final class SwipeScrollView: NSScrollView {
    /// Back (true) or forward (false), and whether there is somewhere to go.
    var onSwipe: ((Bool) -> Void)?
    var canSwipe: ((Bool) -> Bool)?
    /// The folder's own menu, for a right click on the room left under the last row (the
    /// content inset below the document), which the document view does not get.
    var emptyMenu: (() -> NSMenu?)?

    /// The two-finger gesture under way: how far the fingers have moved, and where the list
    /// stood sideways when it began.
    private var travel: (x: CGFloat, y: CGFloat)?
    private var startX: CGFloat = 0

    /// How far the fingers must move sideways, in points, for a swipe to count.
    private static let distance: CGFloat = 90

    /// The gesture is measured here rather than handed to AppKit's swipe tracking
    /// (trackSwipeEvent). On macOS 26 a swipe's first event often carries no movement, and
    /// tracking started on a later event does nothing at all, so most swipes were lost
    /// (issue 430, in 1.0.4 and again in 1.0.6). The scroll events go on to the list as
    /// usual; when the fingers lift (.ended, or .cancelled as macOS 26 often reports it) a
    /// gesture that went far enough, mostly sideways, goes back or forward.
    override func scrollWheel(with event: NSEvent) {
        super.scrollWheel(with: event)
        // momentum after the fingers lift is not part of the gesture
        guard event.momentumPhase == [] else { return }
        switch event.phase {
        case .began:
            travel = (event.scrollingDeltaX, event.scrollingDeltaY)
            startX = contentView.bounds.minX
        case .changed:
            if let t = travel { travel = (t.x + event.scrollingDeltaX, t.y + event.scrollingDeltaY) }
        case .ended, .cancelled:
            guard let t = travel else { return }
            travel = nil
            finish(t, event)
        default:
            break
        }
    }

    private func finish(_ t: (x: CGFloat, y: CGFloat), _ event: NSEvent) {
        guard AppSettings.swipeNavigation, NSEvent.isSwipeTrackingFromScrollEventsEnabled, onSwipe != nil,
            abs(t.x) >= Self.distance, abs(t.x) > 2 * abs(t.y)
        else { return }
        // which way the fingers went: with natural scrolling the deltas follow the fingers,
        // without it they are reversed. Fingers to the right go back, as in Safari and Finder.
        let fingersRight = event.isDirectionInvertedFromDevice ? t.x > 0 : t.x < 0
        let back = fingersRight
        // a list that scrolled sideways during the gesture, or still can that way, keeps it
        guard canSwipe?(back) ?? false, abs(contentView.bounds.minX - startX) < 1,
            !canScrollSideways(towardsStart: back)
        else { return }
        onSwipe?(back)
    }

    override func hitTest(_ point: NSPoint) -> NSView? {
        let hit = super.hitTest(point)
        // Blank viewport space outside the document still belongs to the file pane.
        // Leave scrollers, controls, and ordinary item clicks on their native paths.
        if hit === contentView || hit === self,
           contentView.frame.contains(convert(point, from: superview)),
           NSApp.currentEvent?.type == .leftMouseDown,
           let documentView, documentView is FileTableView || documentView is TileCollectionView {
            return documentView
        }
        return hit
    }

    override func menu(for event: NSEvent) -> NSMenu? {
        emptyMenu?() ?? super.menu(for: event)
    }

    /// The document can still move sideways towards its start (back) or its end (forward).
    private func canScrollSideways(towardsStart: Bool) -> Bool {
        guard let doc = documentView else { return false }
        let visible = contentView.bounds, width = doc.frame.width
        guard width > visible.width + 1 else { return false }
        return towardsStart ? visible.minX > 0.5 : visible.maxX < width - 0.5
    }
}