Sources/OrgstarMobile/AppMenus.swift
157 lines · 7376 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 Mac's submenus,
103/// with the keys that run them in the current keymap.
104private struct OrgMenuItems: View {
105 let editor: MobileEditor?
106
107 /// The Mac's Org submenus (`Sources/Orgstar/OrgstarApp.swift`) and the command id prefixes
108 /// they hold. A command goes in the first submenu with a prefix it starts with.
109 static let groups: [(title: LocalizedStringKey, prefixes: [String])] = [
110 ("Visibility", ["org.cycle", "org.fold.", "org.narrow.", "org.widen", "org.sparse-tree", "org.toggle-inline-images", "org.latex-preview"]),
111 ("Headings", ["org.heading.", "org.subtree.", "org.element.", "org.goto", "org.sort", "org.archive.", "app.refile", "app.archive"]),
112 ("TODO and Tags", ["org.todo.", "org.priority.", "org.tags.", "org.effort."]),
113 ("Properties and Columns", ["org.property.", "org.columns", "org.drawer."]),
114 ("Dates and Timers", ["org.schedule", "org.deadline", "org.timestamp.", "org.timer."]),
115 ("Lists", ["org.item.", "org.checkbox."]),
116 ("Tables", ["org.table", "app.table-", "app.plot"]),
117 ("Links and Footnotes", ["org.link.", "org.id.", "org.footnote."]),
118 ("Blocks", ["org.babel.", "org.structure-template", "org.edit-special", "org.dblock.", "org.clock.report", "org.denote."]),
119 ("Text", ["org.emphasize", "org.fill-"]),
120 ("Attachments", ["app.attach."]),
121 ("Notes", ["app.dailies.", "app.node."]),
122 ("Encryption", ["app.crypt."]),
123 ]
124
125 private static func group(of id: String) -> Int? {
126 groups.firstIndex { $0.prefixes.contains { id.hasPrefix($0) } }
127 }
128
129 /// The commands the iOS editor knows, less the ones the App Store build hides, once each.
130 private static let shown: [(id: String, title: String)] = {
131 var seen = Set<String>()
132 return (CommandPalette.viewCommands + OrgCore.Commands.everything.map { ($0.id, $0.title) })
133 .filter { ($0.id.hasPrefix("org.") || group(of: $0.id) != nil) && !AppStoreCommands.hidden.contains($0.id) && seen.insert($0.id).inserted }
134 }()
135
136 var body: some View {
137 // Commands that fit no subject, such as Act at Point, come first.
138 ForEach(Self.shown.filter { Self.group(of: $0.id) == nil }, id: \.id) { item($0) }
139 Divider()
140 ForEach(Self.groups.indices, id: \.self) { index in
141 Menu(Self.groups[index].title) {
142 ForEach(Self.shown.filter { Self.group(of: $0.id) == index }, id: \.id) { item($0) }
143 }
144 }
145 }
146
147 private func item(_ command: (id: String, title: String)) -> some View {
148 Button {
149 editor?.run(command.id)
150 } label: {
151 Text(CommandTitle.localized(command.title))
152 if let keys = editor?.keymap.keyLabels(for: command.id).first { Text(keys) }
153 }
154 .disabled(editor == nil)
155 }
156}
157#endif