Sources/OrgstarMobile/AppMenus.swift

29b57626b6e9757f773ad01c0bf814d1a32df160
orgstar/Sources/OrgstarMobile/AppMenus.swift

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