Sources/OrgstarMobile/AppMenus.swift

19641366e5de9103a7d88bf80436ab2aebc03cf5
orgstar/Sources/OrgstarMobile/AppMenus.swift

139 lines · 6151 bytes

  1#if os(iOS)
  2import OrgApp
  3import OrgCore
  4import SwiftUI
  5
  6/// What the menu bar does in the focused window.
  7struct WindowActions {
  8    var show: (AppTab) -> Void
  9    var capture: () -> Void
 10    var addFolder: () -> Void
 11    var board: () -> Void
 12    var flashcards: () -> Void
 13    var clock: (String) -> Void
 14}
 15
 16/// The editor of the focused pane in the focused window.
 17struct EditorActions {
 18    let editor: MobileEditor
 19}
 20
 21extension FocusedValues {
 22    @Entry var windowActions: WindowActions?
 23    @Entry var editorActions: EditorActions?
 24}
 25
 26/// The iPad menu bar, after the Mac's File, Edit, View and Org menus, with their keys, and Go
 27/// in place of the Mac's windows.
 28struct AppMenus: SwiftUI.Commands {
 29    let model: MobileModel
 30    @FocusedValue(\.windowActions) private var window
 31    @FocusedValue(\.editorActions) private var editor
 32
 33    var body: some SwiftUI.Commands {
 34        CommandGroup(after: .newItem) {
 35            Button("Capture…") { window?.capture() }
 36                .keyboardShortcut("n", modifiers: [.command, .shift])
 37                .disabled(window == nil)
 38            Button("Add Folder…") { window?.addFolder() }
 39                .keyboardShortcut("o", modifiers: [.command, .shift])
 40                .disabled(window == nil)
 41        }
 42        CommandGroup(replacing: .saveItem) {
 43            Button("Save") { editor?.editor.run("app.save") }
 44                .keyboardShortcut("s")
 45                .disabled(editor == nil)
 46            Button("Save All") { for path in model.session.dirtyPaths { _ = model.session.save(path) } }
 47                .keyboardShortcut("s", modifiers: [.command, .option])
 48                .disabled(model.session.dirtyPaths.isEmpty)
 49        }
 50        CommandGroup(after: .textEditing) {
 51            Button("Search Notes") { window?.show(.search) }
 52                .keyboardShortcut("f", modifiers: [.command, .shift])
 53                .disabled(window == nil)
 54        }
 55        CommandGroup(after: .sidebar) {
 56            editorItem("Split Pane Below", "app.split-below", "2", [.command, .control])
 57            editorItem("Split Pane Right", "app.split-right", "3", [.command, .control])
 58            editorItem("Close Pane", "app.pane-close", "0", [.command, .control])
 59            editorItem("Close Other Panes", "app.pane-only", "1", [.command, .control])
 60            editorItem("Next Pane", "app.pane-next", "`", [.command, .option])
 61            Divider()
 62            Toggle("Show Markup", isOn: Bindable(model).showMarkup)
 63                .keyboardShortcut("m", modifiers: [.command, .shift])
 64            Toggle("Truncate Long Lines", isOn: Binding(get: { editor?.editor.truncatesLines ?? false }, set: { _ in editor?.editor.run("editor.toggle-truncate-lines") }))
 65                .disabled(editor == nil)
 66            Toggle("Auto Fill", isOn: Binding(get: { editor?.editor.autoFills ?? false }, set: { _ in editor?.editor.run("editor.toggle-auto-fill") }))
 67                .disabled(editor == nil)
 68        }
 69        CommandMenu("Org") {
 70            Menu("Clock") {
 71                ForEach(Self.clockCommands, id: \.id) { command in
 72                    Button(CommandTitle.localized(command.title)) { window?.clock(command.id) }
 73                }
 74            }
 75            .disabled(window == nil)
 76            Divider()
 77            OrgMenuItems(editor: editor?.editor)
 78        }
 79        CommandMenu("Go") {
 80            Button("Agenda") { window?.show(.agenda) }
 81                .keyboardShortcut("a", modifiers: [.command, .shift])
 82            Button("Board") { window?.board() }
 83                .keyboardShortcut("b", modifiers: [.command, .shift])
 84            Button("Review Flashcards") { window?.flashcards() }
 85            Button("Folders") { window?.show(.folders) }
 86            Button("Settings") { window?.show(.settings) }
 87                .keyboardShortcut(",")
 88            Divider()
 89            Button(CommandTitle.localized("Go to Clocked Entry")) { window?.clock("app.clock.goto") }
 90                .disabled(model.clock.running == nil)
 91        }
 92    }
 93
 94    /// Clock commands that run without an editor, as the clock accessory runs them.
 95    static let clockCommands = CommandPalette.viewCommands.filter { $0.id.hasPrefix("app.clock.") } + [("app.clock.report", "Clock Report")]
 96
 97    private func editorItem(_ title: LocalizedStringKey, _ id: String, _ key: KeyEquivalent, _ modifiers: EventModifiers) -> some View {
 98        Button(title) { editor?.editor.run(id) }
 99            .keyboardShortcut(key, modifiers: modifiers)
100            .disabled(editor == nil)
101    }
102}
103
104/// Org: the org commands and the app's commands for entries and notes, in the Org menu's
105/// submenus (`CommandGroups.orgMenu`), with the keys that run them in the current keymap.
106private struct OrgMenuItems: View {
107    let editor: MobileEditor?
108
109    private static func group(of id: String) -> Int? { CommandGroups.group(of: id) }
110
111    /// The commands the iOS editor knows, less the ones the App Store build hides, once each.
112    private static let shown: [(id: String, title: String)] = {
113        var seen = Set<String>()
114        return (CommandPalette.viewCommands + OrgCore.Commands.everything.map { ($0.id, $0.title) })
115            .filter { ($0.id.hasPrefix("org.") || group(of: $0.id) != nil) && !AppStoreCommands.hidden.contains($0.id) && seen.insert($0.id).inserted }
116    }()
117
118    var body: some View {
119        // Commands that fit no subject, such as Act at Point, come first.
120        ForEach(Self.shown.filter { Self.group(of: $0.id) == nil }, id: \.id) { item($0) }
121        Divider()
122        ForEach(CommandGroups.orgMenu.indices, id: \.self) { index in
123            Menu(CommandGroups.orgMenu[index].title) {
124                ForEach(Self.shown.filter { Self.group(of: $0.id) == index }, id: \.id) { item($0) }
125            }
126        }
127    }
128
129    private func item(_ command: (id: String, title: String)) -> some View {
130        Button {
131            editor?.run(command.id)
132        } label: {
133            Text(CommandTitle.localized(command.title))
134            if let keys = editor?.keymap.keyLabels(for: command.id).first { Text(keys) }
135        }
136        .disabled(editor == nil)
137    }
138}
139#endif
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form