SPB Git

Repository Public

Commands.swift 4.0 KB 84 lines swift

1import MCCCore2import SwiftUI34/// Menu bar commands and keyboard shortcuts. The same list is documented in Help ▸ Keyboard Shortcuts.5struct AppCommands: Commands {6    let app: AppModel7    @Environment(\.openWindow) private var openWindow89    private var signedIn: Bool { app.session.phase == .signedIn && !app.lock.isLocked }1011    var body: some Commands {12        CommandGroup(replacing: .appSettings) {13            Button(tr("menu.settings")) { app.show(.settings) }14                .keyboardShortcut(",", modifiers: .command)15                .disabled(!signedIn)16        }17        CommandGroup(replacing: .newItem) {18            Button(tr("menu.new_window")) { openWindow(id: "main") }19                .keyboardShortcut("n", modifiers: .command)20            Button(tr("menu.quick_note")) { app.quickNoteRequested = true; app.show(.maintenance) }21                .keyboardShortcut("n", modifiers: [.command, .shift])22                .disabled(!signedIn || !app.session.canMutate || !app.session.can("maintenance.write"))23        }24        CommandMenu(tr("menu.go")) {25            ForEach(SidebarItem.allCases) { item in26                if let key = item.shortcut {27                    Button(item.title) { app.show(item) }.keyboardShortcut(key, modifiers: .command).disabled(!signedIn)28                } else if item == .audit {29                    Button(item.title) { app.show(item) }.keyboardShortcut("a", modifiers: [.command, .option]).disabled(!signedIn)30                }31            }32            Divider()33            Button(tr("menu.command_palette")) { app.show(app.selection); app.paletteVisible = true }34                .keyboardShortcut("k", modifiers: .command)35                .disabled(!signedIn)36        }37        CommandGroup(after: .toolbar) {38            Button(tr("menu.refresh")) { app.session.reloadAll() }39                .keyboardShortcut("r", modifiers: .command)40                .disabled(!signedIn)41            Button(app.operationsCentre ? tr("menu.operations_centre.exit") : tr("menu.operations_centre.enter")) {42                app.operationsCentre.toggle()43                if app.operationsCentre { app.show(.overview) }44            }45            .keyboardShortcut("o", modifiers: [.command, .shift])46            .disabled(!signedIn)47            Picker(tr("menu.period"), selection: Binding(get: { app.range }, set: { app.range = $0 })) {48                ForEach(API.TimeRange.allCases, id: \.self) { Text($0.label).tag($0) }49            }50            Divider()51        }52        CommandGroup(after: .appInfo) {53            Button(tr("menu.lock")) { app.lock.lockNow() }54                .keyboardShortcut("l", modifiers: [.command, .control])55                .disabled(!app.lock.isEnabled)56            Button(tr("menu.sign_out")) { Task { await app.signOut() } }57                .disabled(app.session.phase != .signedIn)58        }59        CommandGroup(replacing: .help) {60            Button(tr("menu.shortcuts")) { app.show(.settings, focus: nil); ShortcutsHelp.present() }61                .keyboardShortcut("/", modifiers: .command)62        }63    }64}6566/// The keyboard shortcuts, for the Help menu and the documentation.67enum ShortcutsHelp {68    static var entries: [(keys: String, title: String)] {69        [("⌘1 – ⌘9, ⌘0", tr("shortcuts.sections")), ("⌥⌘A", SidebarItem.audit.title), ("⌘,", tr("menu.settings")),70         ("⌘K", tr("menu.command_palette")), ("⌘F", tr("shortcuts.search")), ("⌘R", tr("menu.refresh")),71         ("⇧⌘O", tr("menu.operations_centre.enter")), ("⇧⌘N", tr("menu.quick_note")), ("⌘N", tr("menu.new_window")),72         ("⌘[ / ⌘T / ⌘]", tr("shortcuts.calendar")), ("⌃⌘L", tr("menu.lock")), ("⌘↩", tr("shortcuts.confirm")), ("⎋", tr("shortcuts.cancel")), ("⌘/", tr("menu.shortcuts"))]73    }7475    @MainActor76    static func present() {77        let alert = NSAlert()78        alert.messageText = tr("menu.shortcuts")79        alert.informativeText = entries.map { "\($0.keys)   \($0.title)" }.joined(separator: "\n")80        alert.addButton(withTitle: tr("action.close"))81        alert.runModal()82    }83}84
Files main
Menu

Theme