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.

15.8 KB
Raw Blame
import AppKit

/// What a tab strip needs to draw a tab.
struct TabInfo {
    let title: String
    let icon: String
    /// The folder's path (with ~), for the tooltip and Settings › The folder's path on tabs.
    var path: String?
    /// The folder above that tells this tab from another of the same name.
    var hint: String?
}

/// The tabs, drawn as in the Windows app (workspace.rs `tab_strip`): one width for all,
/// `clamp((strip - 4) / n - 4, 72, 220)`, 32 high on the bar's bottom edge, top corners
/// rounded 8; the active one stands on a glass lens (laid out by the title bar, under the
/// strip) and its icon is in the accent colour.
/// Clicks on empty space drag the window; a double click zooms it.
@MainActor
final class TabStripView: HoverView, NSViewToolTipOwner {
    var tabs: [TabInfo] = [] { didSet { needsDisplay = true; superview?.needsLayout = true; needsLayout = true } }

    /// For each tab's folder, the name of a folder above it that tells it apart from the other
    /// tabs' folders of the same name (workspace.rs `tab_hints`): the nearest level whose name
    /// differs, up to five levels; nil when no other tab has its name.
    static func hints(_ urls: [URL?]) -> [String?] {
        func level(_ u: URL, _ up: Int) -> String? {
            var d = u
            for _ in 0..<up {
                let p = d.deletingLastPathComponent()
                if p.path == d.path { return nil }
                d = p
            }
            return d.path == "/" ? "/" : d.lastPathComponent
        }
        return urls.map { me in
            guard let me else { return nil }
            let mine = me.lastPathComponent.lowercased()
            let others = urls.compactMap { $0 }.filter { $0.lastPathComponent.lowercased() == mine && $0.standardizedFileURL != me.standardizedFileURL }
            guard !others.isEmpty else { return nil }
            for up in 1...5 {
                guard let here = level(me, up) else { return nil }
                if others.allSatisfy({ level($0, up)?.lowercased() != here.lowercased() }) { return here }
            }
            return nil
        }
    }

    override func layout() {
        super.layout()
        removeAllToolTips()
        for i in tabs.indices where tabs[i].path != nil { addToolTip(tabRect(i), owner: self, userData: UnsafeMutableRawPointer(bitPattern: i + 1)) }
    }

    nonisolated func view(_ view: NSView, stringForToolTip tag: NSView.ToolTipTag, point: NSPoint, userData data: UnsafeMutableRawPointer?) -> String {
        let i = Int(bitPattern: data) - 1
        return MainActor.assumeIsolated { tabs.indices.contains(i) ? (tabs[i].path ?? "") : "" }
    }
    var active = 0 { didSet { needsDisplay = true; superview?.needsLayout = true } }
    var onSelect: ((Int) -> Void)?
    var onClose: ((Int) -> Void)?
    /// The tab menu for the tab at this index (workspace.rs `tab_menu`).
    var menuFor: ((Int) -> NSMenu?)?
    /// A tab dragged to another place in the strip.
    var onMove: ((Int, Int) -> Void)?
    /// A tab dragged out of the strip: a window of its own where it was let go (screen point).
    var onTearOff: ((Int, NSPoint) -> Void)?
    /// Files dropped on a tab go into its folder; nil: that tab takes none.
    var dropFolder: ((Int) -> URL?)?
    var onDropFiles: ((Int, NSDraggingInfo) -> Bool)?

    private var pressedClose: Int?
    /// A tab being dragged: which one, where the press was, and whether it has left the strip.
    private var dragging: (index: Int, start: NSPoint, out: Bool)?
    /// The tab files are held over.
    private var dropTab: Int? { didSet { if dropTab != oldValue { needsDisplay = true } } }

    private static let minW: CGFloat = 72
    private static let maxW: CGFloat = 220
    private static var height: CGFloat { Metrics.compact ? 28 : 32 }

    override var mouseDownCanMoveWindow: Bool { false }

    /// Width every tab gets, and the strip's total width (the "+" button sits right after it).
    var tabWidth: CGFloat {
        let n = CGFloat(max(tabs.count, 1))
        let strip = max(bounds.width, 120)
        return min(max((strip - 4) / n - 4, Self.minW), Self.maxW)
    }

    var usedWidth: CGFloat { 4 + CGFloat(tabs.count) * (tabWidth + 4) }

    var activeTabRect: NSRect { tabs.indices.contains(active) ? tabRect(active) : .zero }

    /// Tabs stand on the bar's bottom edge, joined to the row under them, as on Windows.
    func tabRect(_ i: Int) -> NSRect {
        NSRect(x: 4 + CGFloat(i) * (tabWidth + 4), y: bounds.height - Self.height, width: tabWidth, height: Self.height)
    }

    private func closeRect(_ i: Int) -> NSRect {
        let r = tabRect(i)
        return NSRect(x: r.maxX - 4 - 22, y: r.midY - 11, width: 22, height: 22)
    }

    private func tab(at p: NSPoint) -> Int? {
        tabs.indices.first { tabRect($0).contains(p) }
    }

    /// Roomy tabs all show their ×; narrow ones only the active and the hovered one.
    private func showsClose(_ i: Int, hoveredTab: Int?) -> Bool {
        tabWidth >= 110 || i == active || i == hoveredTab
    }

    override func draw(_ dirtyRect: NSRect) {
        let p = Palette.of(self)
        let hoveredTab = hoverPoint.flatMap(tab(at:))
        for (i, t) in tabs.enumerated() {
            let r = tabRect(i)
            let isActive = i == active
            let path = NSBezierPath(roundedTopRect: r, radius: Metrics.radius)
            if dropTab == i {
                // a drop target: the selection fill with an accent outline
                Draw.roundedRect(r, radius: Metrics.radius, fill: p.selection)
                p.accent.setStroke()
                NSBezierPath(roundedRect: r.insetBy(dx: 0.5, dy: 0.5), xRadius: Metrics.radius, yRadius: Metrics.radius).stroke()
            } else if isActive && !AppSettings.isGlass {
                // off glass the active tab is filled, as on Windows (no lens)
                p.tabActive.setFill()
                path.fill()
                if !p.dark {
                    p.border.setStroke()
                    NSBezierPath(roundedTopRect: r.insetBy(dx: 0.5, dy: 0.5), radius: Metrics.radius).stroke()
                }
            } else if isActive && !p.dark {
                // over light glass, give the active tab a bright frosted veil over the lens so its
                // dark label and accent icon stay legible against dark wallpapers and stand apart
                // from inactive tabs
                p.tabActive.withAlphaComponent(0.65).setFill()
                path.fill()
                p.border.setStroke()
                NSBezierPath(roundedTopRect: r.insetBy(dx: 0.5, dy: 0.5), radius: Metrics.radius).stroke()
            } else if !isActive && hoveredTab == i {
                p.hover.setFill()
                path.fill()
            }
            // pl_3, icon 14, gap_2, title, gap_2, close 22, pr_1
            var x = r.minX + 12
            Draw.icon(t.icon, in: NSRect(x: x, y: r.minY, width: 14, height: r.height), size: 14, color: isActive ? p.accent : p.textSecondary)
            x += 14 + 8
            let close = showsClose(i, hoveredTab: hoveredTab)
            let textEnd = close ? closeRect(i).minX - 8 : r.maxX - 4
            let room = NSRect(x: x, y: r.minY, width: max(textEnd - x, 0), height: r.height)
            let color = isActive ? p.text : p.textSecondary
            let font = isActive ? Metrics.textSmMedium : Metrics.textSm
            if AppSettings.tabPath, let path = t.path {
                Draw.text(path, in: room, font: isActive ? Metrics.textXsSemibold : Metrics.textXs, color: color, truncation: .byTruncatingTail)
            } else if let hint = t.hint {
                // the name first (never more than 70 % of the room), the telling folder after it, lighter
                let nameW = min(Draw.textWidth(t.title, font: font), room.width * 0.7)
                Draw.text(t.title, in: NSRect(x: room.minX, y: room.minY, width: nameW, height: room.height), font: font, color: color)
                let hx = room.minX + nameW + 4
                Draw.text("@" + hint, in: NSRect(x: hx, y: room.minY, width: max(room.maxX - hx, 0), height: room.height), font: Metrics.textSm, color: p.textTertiary)
            } else {
                Draw.text(t.title, in: room, font: font, color: color)
            }
            if close {
                let c = closeRect(i)
                if let hp = hoverPoint, c.contains(hp) {
                    Draw.roundedRect(c, radius: Metrics.radiusSmall, fill: p.pressed)
                }
                Draw.icon("x", in: c, size: 12, color: isActive ? p.text : p.textSecondary)
            }
        }
    }

    override func mouseDown(with event: NSEvent) {
        let pt = convert(event.locationInWindow, from: nil)
        guard let i = tab(at: pt) else {
            if event.clickCount == 2 {
                window?.performZoom(nil)
            } else {
                window?.performDrag(with: event)
            }
            return
        }
        if showsClose(i, hoveredTab: i), closeRect(i).contains(pt) {
            pressedClose = i
            return
        }
        onSelect?(i)
        dragging = (i, pt, false)
    }

    /// Dragging a tab sideways moves it among the others; far enough up or down, it leaves the
    /// strip and becomes a window of its own when let go (workspace.rs tab drag).
    override func mouseDragged(with event: NSEvent) {
        guard var d = dragging else { return }
        let pt = convert(event.locationInWindow, from: nil)
        d.out = tabs.count > 1 && abs(pt.y - bounds.midY) > bounds.height + 20
        if !d.out, abs(pt.x - d.start.x) > 4 {
            let target = min(max(Int((pt.x - 4) / (tabWidth + 4)), 0), tabs.count - 1)
            if target != d.index {
                onMove?(d.index, target)
                d.index = target
            }
        }
        dragging = d
    }

    override func mouseUp(with event: NSEvent) {
        let pt = convert(event.locationInWindow, from: nil)
        if let i = pressedClose, closeRect(i).contains(pt) { onClose?(i) }
        pressedClose = nil
        if let d = dragging, d.out, let w = window {
            onTearOff?(d.index, w.convertPoint(toScreen: event.locationInWindow))
        }
        dragging = nil
    }

    // MARK: files dropped on a tab

    override func viewDidMoveToWindow() {
        super.viewDidMoveToWindow()
        registerForDraggedTypes([.fileURL] + ArchiveDrag.draggedTypes)
    }

    private func dropOperation(_ info: NSDraggingInfo) -> NSDragOperation {
        let pt = convert(info.draggingLocation, from: nil)
        dropTab = tab(at: pt).flatMap { dropFolder?($0) != nil ? $0 : nil }
        guard let i = dropTab, let dest = dropFolder?(i) else { return [] }
        if ArchiveDrag.carries(info) { return .copy }
        let urls = FileListController.droppedURLs(info)
        guard !urls.isEmpty, !urls.allSatisfy({ $0.deletingLastPathComponent().standardizedFileURL == dest.standardizedFileURL }) else { return [] }
        return FileListController.dropKind(info, urls, into: dest) == .copy ? .copy : .move
    }

    override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation { dropOperation(sender) }
    override func draggingUpdated(_ sender: NSDraggingInfo) -> NSDragOperation { dropOperation(sender) }
    override func draggingExited(_ sender: NSDraggingInfo?) { dropTab = nil }

    override func performDragOperation(_ sender: NSDraggingInfo) -> Bool {
        defer { dropTab = nil }
        guard let i = dropTab, dropOperation(sender) != [] else { return false }
        return onDropFiles?(i, sender) ?? false
    }

    override func menu(for event: NSEvent) -> NSMenu? {
        guard let i = tab(at: convert(event.locationInWindow, from: nil)) else { return nil }
        return menuFor?(i)
    }

    /// Middle click closes a tab, as on Windows.
    override func otherMouseUp(with event: NSEvent) {
        guard event.buttonNumber == 2, let i = tab(at: convert(event.locationInWindow, from: nil)) else { return }
        onClose?(i)
    }
}

extension NSBezierPath {
    /// A rectangle with only its top corners rounded (flipped coordinates: top is minY).
    convenience init(roundedTopRect r: NSRect, radius: CGFloat) {
        self.init()
        move(to: NSPoint(x: r.minX, y: r.maxY))
        line(to: NSPoint(x: r.minX, y: r.minY + radius))
        appendArc(withCenter: NSPoint(x: r.minX + radius, y: r.minY + radius), radius: radius, startAngle: 180, endAngle: 270)
        line(to: NSPoint(x: r.maxX - radius, y: r.minY))
        appendArc(withCenter: NSPoint(x: r.maxX - radius, y: r.minY + radius), radius: radius, startAngle: 270, endAngle: 360)
        line(to: NSPoint(x: r.maxX, y: r.maxY))
        close()
    }
}

/// The 40 px bar at the top: (window buttons) · tabs · "+".
/// Empty space drags the window and a double click zooms it, like a title bar.
@MainActor
final class TitleBarView: FlippedView {
    let strip = TabStripView()
    let plus = IconButton(icon: "plus", size: NSSize(width: 28, height: 28), iconSize: 14, tooltip: String(localized: "New tab"))
    /// The active tab's glass: as wide as the tab and 8 px taller downwards, cut by the strip's
    /// box, so its top corners are round and its bottom is flat (glass.rs `LensShape::Tab`).
    private let tabClip = FlippedView()
    private let tabLens = LensView(cornerRadius: Metrics.radius)
    /// The round glass behind "+" (`LensShape::Circle`).
    private let plusLens = LensView(cornerRadius: 14)
    /// A saved session's name, beside the "+".
    var sessionName: String? { didSet { sessionLabel.stringValue = sessionName ?? ""; sessionLabel.isHidden = sessionName == nil } }
    private let sessionLabel = NSTextField(labelWithString: "")

    override init(frame: NSRect) {
        super.init(frame: frame)
        tabClip.wantsLayer = true
        tabClip.layer?.masksToBounds = true
        tabClip.addSubview(tabLens)
        // lenses first: they lie under the tabs' text and the "+" icon
        [tabClip, plusLens, strip, plus].forEach(addSubview)
        sessionLabel.font = Metrics.textXs
        sessionLabel.textColor = .tertiaryLabelColor
        sessionLabel.lineBreakMode = .byTruncatingTail
        sessionLabel.isHidden = true
        addSubview(sessionLabel)
    }

    @available(*, unavailable)
    required init?(coder: NSCoder) { fatalError() }

    override var mouseDownCanMoveWindow: Bool { true }

    override func mouseDown(with event: NSEvent) {
        if event.clickCount == 2 { window?.performZoom(nil) } else { window?.performDrag(with: event) }
    }

    override func layout() {
        super.layout()
        // The tabs start right after the window buttons (the sidebar toggle is in the nav row).
        // (in the window's own points: the window buttons do not grow with the interface scale)
        let stripX = Metrics.trafficLightsWidth / max(convert(NSSize(width: 1, height: 0), to: nil).width, 0.1)
        let room = max(bounds.width - stripX - 4 - 28 - 12, 120)
        strip.frame = NSRect(x: stripX, y: 0, width: room, height: bounds.height)
        // the "+" follows the last tab: ml_1 and 2 px off the bottom
        let plusX = stripX + min(strip.usedWidth, room)
        plus.frame = NSRect(x: plusX, y: bounds.height - 2 - 28, width: 28, height: 28)
        plusLens.frame = plus.frame
        plusLens.isHidden = !AppSettings.isGlass
        if !sessionLabel.isHidden {
            sessionLabel.textColor = Palette.of(self).textSecondary
            let w = min(sessionLabel.intrinsicContentSize.width, max(bounds.width - plus.frame.maxX - 16, 0))
            sessionLabel.frame = NSRect(x: plus.frame.maxX + 8, y: plus.frame.midY - 8, width: w, height: 16)
        }

        tabClip.frame = strip.frame
        var lens = strip.activeTabRect
        lens.size.height += Metrics.radius
        tabLens.move(to: lens, animated: !inLiveResize)
    }

    override func viewDidChangeEffectiveAppearance() {
        super.viewDidChangeEffectiveAppearance()
        sessionLabel.textColor = Palette.of(self).textSecondary
        strip.needsDisplay = true
    }
}