Sources/OrgApp/KeyBarLayout.swift
95 lines · 4578 bytes
11 symbols in this file
1import Foundation
2import OrgCore
3
4/// The iOS key bar's buttons, `[key-bar]` in config.toml: `iphone` and `ipad`, each a list of
5/// command ids in order. The Mac has no key bar and only reads the table, so the file is the
6/// same on both.
7public enum KeyBarLayout {
8 public static let table = "key-bar"
9
10 /// Which list a bar uses: `iphone` where the editor is narrow (iPhone, and iPad in Slide
11 /// Over or a narrow split), `ipad` where it is wide.
12 public enum Width: String, CaseIterable, Sendable {
13 case iphone, ipad
14
15 /// Where the list is kept, its ids one per line; absent means the default.
16 public var defaultsKey: String { self == .iphone ? "keyBarIPhone" : "keyBarIPad" }
17
18 public var defaultIDs: [String] {
19 ["commands", "fold", "promote", "demote", "move-up", "move-down", "new-heading", "todo", "act"]
20 + (self == .ipad ? ["schedule", "deadline", "tags"] : ["plan"]) + ["hide-keyboard"]
21 }
22 }
23
24 public static let nextField = "org.snippet.next-field"
25 public static let previousField = "org.snippet.previous-field"
26
27 /// The list `defaults` holds for `width`, or nil for the default. Repeated ids count once.
28 public static func stored(_ width: Width, in defaults: UserDefaults) -> [String]? {
29 defaults.string(forKey: width.defaultsKey).map(ids)
30 }
31
32 /// The ids of a stored list, each once.
33 public static func ids(_ stored: String) -> [String] {
34 var seen = Set<String>()
35 return stored.split(separator: "\n").map(String.init).filter { seen.insert($0).inserted }
36 }
37
38 public static func store(_ ids: [String]?, _ width: Width, in defaults: UserDefaults) {
39 guard let ids else { return defaults.removeObject(forKey: width.defaultsKey) }
40 let joined = ids.joined(separator: "\n")
41 if defaults.string(forKey: width.defaultsKey) != joined { defaults.set(joined, forKey: width.defaultsKey) }
42 }
43
44 /// The problems of a list whose ids `known` doesn't know, which the bar skips.
45 public static func problems(_ ids: [String], _ width: Width, known: (String) -> Bool) -> [String] {
46 ids.filter { !known($0) }.map { "config.toml: \(table).\(width.rawValue) has no command \($0)" }
47 }
48
49 /// While a snippet's fields are filled in: Previous Field and Next Field in place of
50 /// Promote and Demote, which act on the heading or item rather than the snippet, or first
51 /// when the bar lacks them. A bar that has them already is unchanged.
52 public static func withSnippetFields(_ ids: [String]) -> [String] {
53 var out = ids
54 for (field, replaced) in [(previousField, "promote"), (nextField, "demote")] where !out.contains(field) {
55 if let index = out.firstIndex(of: replaced) { out[index] = field }
56 }
57 return [previousField, nextField].filter { !out.contains($0) } + out
58 }
59
60 /// A slot's width at the default text size: room for a symbol and the gaps beside it.
61 public static let slotWidth: CGFloat = 32
62
63 /// The slots in a bar `width` wide when its symbols are drawn `scale` times as large as at
64 /// the default text size: slots widen with the symbols, so fewer fit.
65 public static func availableSlots(width: CGFloat, scale: CGFloat) -> Int {
66 max(Int(width / (slotWidth * max(scale, 1))), 0)
67 }
68
69 /// The buttons that fit in `available` slots, in order, and the rest, which go in a More
70 /// button that takes a slot. A button takes `slots(id)` slots; past the first that doesn't
71 /// fit, none show, so the order stays.
72 public static func fit(_ ids: [String], available: Int, slots: (String) -> Int) -> (shown: [String], more: [String]) {
73 guard ids.reduce(0, { $0 + slots($1) }) > available else { return (ids, []) }
74 var used = 1
75 var shown: [String] = []
76 for id in ids {
77 used += slots(id)
78 guard used <= available else { break }
79 shown.append(id)
80 }
81 return (shown, Array(ids.dropFirst(shown.count)))
82 }
83
84 /// The text on a button for a command without a symbol: its title without the `…` that
85 /// says it asks something.
86 public static func face(title: String) -> String {
87 title.hasSuffix("…") ? String(title.dropLast()) : title
88 }
89
90 /// A button's width in slots: one for a symbol or a word of up to four letters, as TODO,
91 /// two for longer text, which wraps onto two lines.
92 public static func slots(symbol: Bool, face: String) -> Int {
93 symbol || face.count <= 4 ? 1 : 2
94 }
95}