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.

10.7 KB
Raw Blame
import AppKit

/// The menu bar, built in code (no nib). Items with a `nil` target go down the responder
/// chain: the file list, then its window's controller, then the app delegate. Shortcuts are
/// the Mac's own where it has one, the Windows app's otherwise (F2, Backspace in the list).
@MainActor
enum MainMenu {
    /// `install`: also hand the Window and Help menus to AppKit (false: a copy to read).
    static func make(install: Bool = true) -> NSMenu {
        let bar = NSMenu()
        bar.addItem(submenu(appMenu()))
        bar.addItem(submenu(fileMenu()))
        bar.addItem(submenu(editMenu()))
        bar.addItem(submenu(viewMenu()))
        bar.addItem(submenu(goMenu()))
        let window = windowMenu()
        bar.addItem(submenu(window))
        let help = NSMenu(title: String(localized: "Help"))
        bar.addItem(submenu(help))
        if install {
            NSApp.windowsMenu = window
            NSApp.helpMenu = help
        }
        // Settings › Keyboard shortcuts
        Shortcuts.apply(bar)
        return bar
    }

    private static func submenu(_ menu: NSMenu) -> NSMenuItem {
        let item = NSMenuItem(title: menu.title, action: nil, keyEquivalent: "")
        item.submenu = menu
        return item
    }

    private static func item(
        _ title: String, _ action: Selector?, _ key: String = "", _ mods: NSEvent.ModifierFlags = .command
    ) -> NSMenuItem {
        let i = NSMenuItem(title: title, action: action, keyEquivalent: key)
        i.keyEquivalentModifierMask = mods
        return i
    }

    private static func key(_ code: Int) -> String { String(Character(UnicodeScalar(code)!)) }

    private typealias W = BrowserWindowController

    private static func appMenu() -> NSMenu {
        let m = NSMenu(title: "rustyfiles")
        m.addItem(item(String(localized: "About rustyfiles"), #selector(NSApplication.orderFrontStandardAboutPanel(_:))))
        m.addItem(item(String(localized: "Check for Updates…"), #selector(AppDelegate.checkForUpdates(_:))))
        m.addItem(.separator())
        m.addItem(item(String(localized: "Settings…"), #selector(W.openSettings(_:)), ","))
        m.addItem(.separator())
        m.addItem(item(String(localized: "Hide rustyfiles"), #selector(NSApplication.hide(_:)), "h"))
        m.addItem(item(String(localized: "Hide Others"), #selector(NSApplication.hideOtherApplications(_:)), "h", [.command, .option]))
        m.addItem(item(String(localized: "Show All"), #selector(NSApplication.unhideAllApplications(_:))))
        m.addItem(.separator())
        m.addItem(item(String(localized: "Quit rustyfiles"), #selector(NSApplication.terminate(_:)), "q"))
        return m
    }

    private static func fileMenu() -> NSMenu {
        let m = NSMenu(title: String(localized: "File"))
        m.addItem(item(String(localized: "New Window"), #selector(AppDelegate.newWindow(_:)), "n"))
        m.addItem(item(String(localized: "New Tab"), #selector(W.newTab(_:)), "t"))
        m.addItem(item(String(localized: "New Folder"), #selector(W.newFolder(_:)), "n", [.command, .shift]))
        m.addItem(.separator())
        m.addItem(item(String(localized: "Open"), #selector(W.openSelection(_:)), key(NSDownArrowFunctionKey)))
        m.addItem(item(String(localized: "Quick Look"), #selector(W.toggleQuickLook(_:)), "y"))
        m.addItem(.separator())
        m.addItem(item(String(localized: "Properties"), #selector(W.showProperties(_:)), "i"))
        m.addItem(.separator())
        m.addItem(item(String(localized: "Close Tab"), #selector(W.closeTab(_:)), "w"))
        m.addItem(item(String(localized: "Reopen Closed Tab"), #selector(W.reopenClosedTab(_:)), "t", [.command, .shift]))
        m.addItem(item(String(localized: "Close Window"), #selector(NSWindow.performClose(_:)), "w", [.command, .shift]))
        return m
    }

    private static func editMenu() -> NSMenu {
        let m = NSMenu(title: String(localized: "Edit"))
        m.addItem(item(String(localized: "Undo"), Selector(("undo:")), "z"))
        m.addItem(item(String(localized: "Redo"), Selector(("redo:")), "z", [.command, .shift]))
        m.addItem(.separator())
        m.addItem(item(String(localized: "Cut"), #selector(W.cut(_:)), "x"))
        m.addItem(item(String(localized: "Copy"), #selector(W.copy(_:) as (W) -> (Any?) -> Void), "c"))
        m.addItem(item(String(localized: "Paste"), #selector(W.paste(_:)), "v"))
        m.addItem(item(String(localized: "Select All"), #selector(NSResponder.selectAll(_:)), "a"))
        m.addItem(item(String(localized: "Duplicate"), #selector(W.duplicate(_:)), "d"))
        m.addItem(.separator())
        m.addItem(item(String(localized: "Rename"), #selector(W.rename(_:)), key(NSF2FunctionKey), []))
        m.addItem(item(String(localized: "Move to Trash"), #selector(W.moveToTrash(_:)), key(NSBackspaceCharacter), .command))
        m.addItem(item(String(localized: "Add to Shelf"), #selector(W.addToShelf(_:)), "s", [.command, .shift]))
        m.addItem(.separator())
        m.addItem(item(String(localized: "Filter"), #selector(W.focusSearch(_:)), "f"))
        m.addItem(item(String(localized: "Go to Path"), #selector(W.focusAddress(_:)), "l"))
        m.addItem(.separator())
        m.addItem(item(String(localized: "Command Palette…"), #selector(W.showCommandPalette(_:)), "k"))
        return m
    }

    private static func viewMenu() -> NSMenu {
        let m = NSMenu(title: String(localized: "View"))
        m.addItem(item(String(localized: "Details"), #selector(W.showDetails(_:)), "1"))
        m.addItem(item(String(localized: "Tiles"), #selector(W.showTiles(_:)), "2"))
        m.addItem(item(String(localized: "Cards"), #selector(W.showCards(_:)), "3"))
        m.addItem(item(String(localized: "List"), #selector(W.showListStyle(_:)), "4"))
        m.addItem(item(String(localized: "Columns"), #selector(W.showColumns(_:)), "5"))
        m.addItem(item(String(localized: "Zoom In"), #selector(W.zoomIn(_:)), "+"))
        m.addItem(item(String(localized: "Zoom Out"), #selector(W.zoomOut(_:)), "-"))
        m.addItem(item(String(localized: "Bigger Interface"), #selector(W.interfaceBigger(_:)), "+", [.command, .option]))
        m.addItem(item(String(localized: "Smaller Interface"), #selector(W.interfaceSmaller(_:)), "-", [.command, .option]))
        m.addItem(item(String(localized: "Actual Size"), #selector(W.interfaceActual(_:)), "0", [.command, .option]))
        // ⌘= as well, where + needs Shift
        let eq = item(String(localized: "Zoom In"), #selector(W.zoomIn(_:)), "=")
        eq.isHidden = true
        eq.allowsKeyEquivalentWhenHidden = true
        m.addItem(eq)
        m.addItem(.separator())
        m.addItem(item(String(localized: "Show Hidden Files"), #selector(W.toggleHidden(_:)), ".", [.command, .shift]))
        m.addItem(item(String(localized: "Refresh"), #selector(W.refresh(_:)), "r"))
        m.addItem(item(String(localized: "Collapse / Expand All Groups"), #selector(W.toggleAllGroups(_:)), "g", [.control, .shift]))
        m.addItem(.separator())
        m.addItem(item(String(localized: "Hide Sidebar"), #selector(W.toggleSidebar(_:)), "s", [.command, .control]))
        m.addItem(item(String(localized: "Show Preview"), #selector(W.togglePreviewPane(_:)), "p", [.command, .shift]))
        m.addItem(item(String(localized: "Show Shelf"), #selector(W.toggleShelf(_:)), "s", [.command, .option]))
        m.addItem(item(String(localized: "Show Bookmarks Bar"), #selector(W.toggleBookmarksBar(_:)), "b", [.command, .shift]))
        m.addItem(item(String(localized: "Bookmark This Folder"), #selector(W.bookmarkThisFolder(_:)), "b", [.command, .control]))
        m.addItem(item(String(localized: "Dual Pane"), #selector(W.toggleDualPane(_:)), "d", [.command, .option]))
        m.addItem(item(String(localized: "Switch Pane"), #selector(W.switchPane(_:)), key(NSF6FunctionKey), []))
        m.addItem(item(String(localized: "Copy to Other Pane"), #selector(W.copyToOtherPane(_:)), key(NSF5FunctionKey), .shift))
        m.addItem(item(String(localized: "Move to Other Pane"), #selector(W.moveToOtherPane(_:)), key(NSF6FunctionKey), .shift))
        return m
    }

    private static func goMenu() -> NSMenu {
        let m = NSMenu(title: String(localized: "Go"))
        m.addItem(item(String(localized: "Back"), #selector(W.goBack(_:)), "["))
        m.addItem(item(String(localized: "Forward"), #selector(W.goForward(_:)), "]"))
        m.addItem(item(String(localized: "Up"), #selector(W.goUp(_:)), key(NSUpArrowFunctionKey)))
        m.addItem(.separator())
        m.addItem(item(String(localized: "Home"), #selector(W.goHome(_:)), "h", [.command, .shift]))
        m.addItem(item(String(localized: "Home Folder"), #selector(W.goHomeFolder(_:)), "h", [.command, .option]))
        m.addItem(item(String(localized: "Desktop"), #selector(W.goDesktop(_:)), "d", [.command, .shift]))
        m.addItem(item(String(localized: "Documents"), #selector(W.goDocuments(_:)), "o", [.command, .shift]))
        m.addItem(item(String(localized: "Downloads"), #selector(W.goDownloads(_:)), "l", [.command, .option]))
        m.addItem(item(String(localized: "Applications"), #selector(W.goApplications(_:)), "a", [.command, .shift]))
        m.addItem(.separator())
        m.addItem(item(String(localized: "Recent Folders…"), #selector(W.showFolderHistory(_:)), "p"))
        m.addItem(item(String(localized: "Connect to Server…"), #selector(W.connectToServer(_:)), "k", [.command, .control]))
        m.addItem(item(String(localized: "Disk Usage"), #selector(W.showDiskUsage(_:)), "u", [.command, .option]))
        return m
    }

    private static func windowMenu() -> NSMenu {
        let m = NSMenu(title: String(localized: "Window"))
        m.addItem(item(String(localized: "Minimize"), #selector(NSWindow.performMiniaturize(_:)), "m"))
        m.addItem(item(String(localized: "Zoom"), #selector(NSWindow.performZoom(_:))))
        m.addItem(.separator())
        m.addItem(item(String(localized: "Next Tab"), #selector(W.selectNextTab(_:)), "\t", .control))
        m.addItem(item(String(localized: "Previous Tab"), #selector(W.selectPreviousTab(_:)), "\t", [.control, .shift]))
        m.addItem(item(String(localized: "Search Tabs…"), #selector(W.searchTabs(_:)), "\\", [.command, .shift]))
        // ⌃1…⌃8 go to that tab, ⌃9 to the last one (Windows' Ctrl+1…9; ⌘1…4 are the views here)
        let go = NSMenu(title: String(localized: "Go to Tab"))
        for n in 1...9 {
            let title = n == 9 ? String(localized: "Last Tab") : String(format: String(localized: "Tab %d"), n)
            let i = item(title, #selector(W.selectTabByNumber(_:)), "\(n)", .control)
            i.tag = n
            go.addItem(i)
        }
        let goItem = NSMenuItem(title: go.title, action: nil, keyEquivalent: "")
        goItem.submenu = go
        m.addItem(goItem)
        return m
    }
}