SPB Git

Repository Public License

MenuBarController.swift 3.6 KB 81 lines swift

1// ORIX Convert — App/Sources/Features/WatchFolders/MenuBarController.swift2// Description: The optional menu bar item of Watch Folders (off by default): state of the watcher, pause / resume,3//              recent activity, reopening the window, quitting. It exists only while the app runs; nothing is4//              registered at login.5// Author: Simon-Pierre Boucher — contact@spboucher.ai6import AppKit7import ConvertCore89final class MenuBarController: NSObject, NSMenuDelegate {10    private var item: NSStatusItem?11    private unowned let model: AppModel1213    init(model: AppModel) { self.model = model }1415    var isInstalled: Bool { item != nil }1617    /// Shows or removes the item according to the setting.18    func update(enabled: Bool) {19        if enabled, item == nil {20            let item = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength)21            item.button?.image = NSImage(systemSymbolName: "folder.badge.gearshape", accessibilityDescription: L("ORIX Convert — watched folders"))22            item.button?.toolTip = L("ORIX Convert — watched folders")23            let menu = NSMenu()24            menu.delegate = self25            item.menu = menu26            self.item = item27        } else if !enabled, let item {28            NSStatusBar.system.removeStatusItem(item)29            self.item = nil30        }31    }3233    func menuNeedsUpdate(_ menu: NSMenu) {34        menu.removeAllItems()35        let watch = model.watch36        let state: String37        if watch.rules.isEmpty { state = L("No watch rule") }38        else if watch.isPaused { state = L("Watching is paused") }39        else { state = watch.activeRuleCount == 1 ? L("Watching 1 folder") : L("Watching %lld folders", watch.activeRuleCount) }40        let header = NSMenuItem(title: state, action: nil, keyEquivalent: "")41        header.isEnabled = false42        menu.addItem(header)43        if !watch.rules.isEmpty {44            let toggle = NSMenuItem(title: watch.isPaused ? L("Resume All") : L("Pause All"), action: #selector(togglePause), keyEquivalent: "")45            toggle.target = self46            menu.addItem(toggle)47        }48        menu.addItem(.separator())49        let recent = watch.activity.prefix(6)50        let title = NSMenuItem(title: recent.isEmpty ? L("No recent activity") : L("Recent activity"), action: nil, keyEquivalent: "")51        title.isEnabled = false52        menu.addItem(title)53        for entry in recent {54            let line = NSMenuItem(title: (entry.path as NSString).lastPathComponent + " — " + entry.kind.title, action: #selector(reveal(_:)), keyEquivalent: "")55            line.target = self56            line.image = NSImage(systemSymbolName: entry.kind.symbol, accessibilityDescription: entry.kind.title)57            line.representedObject = entry.outputs.first ?? entry.path58            line.toolTip = entry.localizedDetail59            menu.addItem(line)60        }61        menu.addItem(.separator())62        let open = NSMenuItem(title: L("Open ORIX Convert"), action: #selector(openWindow), keyEquivalent: "")63        open.target = self64        menu.addItem(open)65        let quit = NSMenuItem(title: L("Quit ORIX Convert"), action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q")66        menu.addItem(quit)67    }6869    @objc private func togglePause() { model.watch.toggleAll() }7071    @objc private func reveal(_ sender: NSMenuItem) {72        guard let path = sender.representedObject as? String else { return }73        FileActions.reveal([URL(fileURLWithPath: path)])74    }7576    @objc private func openWindow() {77        model.section = .watchFolders78        model.showMainWindow()79    }80}81
Files main
Menu

Theme