Sources/OrgstarMobile/KeyBarSettings.swift

d88176172f98786bd9a86c2f6ee051fd19fe426d
orgstar/Sources/OrgstarMobile/KeyBarSettings.swift

102 lines · 4334 bytes

  1#if os(iOS)
  2import OrgApp
  3import OrgCore
  4import SwiftUI
  5
  6/// Settings ▸ Key Bar: the key bar's buttons, added from the Commands list, removed and
  7/// reordered, for narrow and wide editors. Changes are written to `[key-bar]` in config.toml.
  8struct KeyBarSettings: View {
  9    let model: MobileModel
 10    @State private var width: KeyBarLayout.Width = UIDevice.current.userInterfaceIdiom == .pad ? .ipad : .iphone
 11    @State private var adding = false
 12
 13    private var stored: [String]? {
 14        let _ = model.settingsVersion
 15        return KeyBarLayout.stored(width, in: .standard)
 16    }
 17
 18    var body: some View {
 19        let ids = stored ?? width.defaultIDs
 20        List {
 21            Section {
 22                Picker("Layout", selection: $width) {
 23                    Text(verbatim: "iPhone").tag(KeyBarLayout.Width.iphone)
 24                    Text(verbatim: "iPad").tag(KeyBarLayout.Width.ipad)
 25                }
 26                .pickerStyle(.segmented)
 27            } footer: {
 28                Text("The iPhone buttons show where the editor is narrow, the iPad buttons where it is wide.")
 29            }
 30            Section {
 31                ForEach(ids, id: \.self) { row($0) }
 32                    .onMove { from, to in
 33                        var list = ids
 34                        list.move(fromOffsets: from, toOffset: to)
 35                        model.setKeyBar(list, width)
 36                    }
 37                    .onDelete { offsets in
 38                        var list = ids
 39                        list.remove(atOffsets: offsets)
 40                        model.setKeyBar(list, width)
 41                    }
 42            } header: {
 43                Text("Buttons")
 44            } footer: {
 45                Text("Buttons that don't fit go under More. While a snippet's fields are filled in, Previous Snippet Field and Next Snippet Field take the places of Promote and Demote.")
 46            }
 47            Section {
 48                Button("Add Button…") { adding = true }
 49                Button("Reset to Default") { model.setKeyBar(nil, width) }
 50                    .disabled(stored == nil)
 51            }
 52        }
 53        .environment(\.editMode, .constant(.active))
 54        .navigationTitle("Key Bar")
 55        .sheet(isPresented: $adding) {
 56            CommandList(rows: candidates(besides: ids), title: String(localized: "Add Button"), recordsUse: false) { id in
 57                model.setKeyBar(ids + [id], width)
 58            }
 59        }
 60    }
 61
 62    private func row(_ id: String) -> some View {
 63        let item = KeyBar.item(id)
 64        return HStack(spacing: 12) {
 65            Group {
 66                if let symbol = item?.symbol {
 67                    Image(systemName: symbol).font(.body)
 68                } else {
 69                    Text(item?.text ?? "?").font(.caption2.weight(.semibold)).lineLimit(2).minimumScaleFactor(0.5)
 70                        .multilineTextAlignment(.center)
 71                }
 72            }
 73            .foregroundStyle(.tint)
 74            // As on the bar, the tile stops growing where its symbols do.
 75            .dynamicTypeSize(...KeyBar.largestSymbolSize)
 76            .frame(width: 56, height: 40)
 77            .background(.fill.tertiary, in: .rect(cornerRadius: 8))
 78            .accessibilityHidden(true)
 79            VStack(alignment: .leading, spacing: 2) {
 80                Text(item?.label ?? id)
 81                if item == nil {
 82                    Text("Not a command here; the key bar skips it.").font(.caption).foregroundStyle(.red)
 83                }
 84            }
 85        }
 86    }
 87
 88    /// The bar's own buttons, then the editor's commands, named macros and snippets, without
 89    /// those already on the bar or a command one of the bar's own buttons runs.
 90    private func candidates(besides ids: [String]) -> [CommandRow] {
 91        let own = KeyBar.items.values.compactMap { item -> String? in
 92            if case .command(let id) = item.action { return id }
 93            return nil
 94        }
 95        let commands = KeyBar.items.keys.map { $0 } + CommandPalette.commands(macros: NamedMacros.shared).map(\.id)
 96            + SnippetLibrary.shared.snippets.filter { !$0.file }.map(\.commandID)
 97        return commands.filter { !ids.contains($0) && !own.contains($0) }.compactMap { id in
 98            KeyBar.item(id).map { CommandRow(id: id, title: $0.label) }
 99        }
100    }
101}
102#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