Commit 974c897ecb
Verified · cmc
Changed files (4)
Sources/OrgApp/KeyBarLayout.swift +9 · view
| @@ -57,6 +57,15 @@ public enum KeyBarLayout { | |||
| 57 | return [previousField, nextField].filter { !out.contains($0) } + out | 57 | return [previousField, nextField].filter { !out.contains($0) } + out |
| 58 | } | 58 | } |
| 59 | 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 | |||
| 60 | /// The buttons that fit in `available` slots, in order, and the rest, which go in a More | 69 | /// The buttons that fit in `available` slots, in order, and the rest, which go in a More |
| 61 | /// button that takes a slot. A button takes `slots(id)` slots; past the first that doesn't | 70 | /// button that takes a slot. A button takes `slots(id)` slots; past the first that doesn't |
| 62 | /// fit, none show, so the order stays. | 71 | /// fit, none show, so the order stays. |
Sources/OrgstarMobile/KeyBar.swift +11 −5 · view
| @@ -2,6 +2,7 @@ | |||
| 2 | import OrgApp | 2 | import OrgApp |
| 3 | import OrgCore | 3 | import OrgCore |
| 4 | import SwiftUI | 4 | import SwiftUI |
| 5 | import UIKit | ||
| 5 | 6 | ||
| 6 | /// Org's keys for a touch keyboard: each button does what its key does at the caret, as the | 7 | /// Org's keys for a touch keyboard: each button does what its key does at the caret, as the |
| 7 | /// Emacs keymap binds it (M-→ demotes a heading, indents an item, moves a table column), or | 8 | /// Emacs keymap binds it (M-→ demotes a heading, indents an item, moves a table column), or |
| @@ -11,6 +12,7 @@ struct KeyBar: View { | |||
| 11 | let editor: MobileEditor | 12 | let editor: MobileEditor |
| 12 | let palette: () -> Void | 13 | let palette: () -> Void |
| 13 | @Environment(\.horizontalSizeClass) private var sizeClass | 14 | @Environment(\.horizontalSizeClass) private var sizeClass |
| 15 | @Environment(\.dynamicTypeSize) private var typeSize | ||
| 14 | @AppStorage(KeyBarLayout.Width.iphone.defaultsKey) private var iphone: String? | 16 | @AppStorage(KeyBarLayout.Width.iphone.defaultsKey) private var iphone: String? |
| 15 | @AppStorage(KeyBarLayout.Width.ipad.defaultsKey) private var ipad: String? | 17 | @AppStorage(KeyBarLayout.Width.ipad.defaultsKey) private var ipad: String? |
| 16 | @State private var width: CGFloat? | 18 | @State private var width: CGFloat? |
| @@ -102,16 +104,15 @@ struct KeyBar: View { | |||
| 102 | static let largestSize = DynamicTypeSize.accessibility2 | 104 | static let largestSize = DynamicTypeSize.accessibility2 |
| 103 | static let target: CGFloat = 44 | 105 | static let target: CGFloat = 44 |
| 104 | static let padding: CGFloat = 4 | 106 | static let padding: CGFloat = 4 |
| 105 | /// The widest a one-slot button gets where the bar has room, and the narrowest. | 107 | /// The widest a one-slot button gets where the bar has room. |
| 106 | nonisolated static let widest: CGFloat = 64 | 108 | nonisolated static let widest: CGFloat = 64 |
| 107 | static let slot: CGFloat = 32 | ||
| 108 | @ScaledMetric(relativeTo: .body) private var size = KeyBar.target | 109 | @ScaledMetric(relativeTo: .body) private var size = KeyBar.target |
| 109 | 110 | ||
| 110 | var body: some View { | 111 | var body: some View { |
| 111 | let layout = sizeClass == .regular ? KeyBarLayout.Width.ipad : .iphone | 112 | let layout = sizeClass == .regular ? KeyBarLayout.Width.ipad : .iphone |
| 112 | var ids = ((layout == .ipad ? ipad : iphone).map(KeyBarLayout.ids) ?? layout.defaultIDs).filter { Self.item($0) != nil } | 113 | var ids = ((layout == .ipad ? ipad : iphone).map(KeyBarLayout.ids) ?? layout.defaultIDs).filter { Self.item($0) != nil } |
| 113 | if editor.fields.active { ids = KeyBarLayout.withSnippetFields(ids) } | 114 | if editor.fields.active { ids = KeyBarLayout.withSnippetFields(ids) } |
| 114 | let available = width.map { Int(($0 - 12) / Self.slot) } ?? .max | 115 | let available = width.map { KeyBarLayout.availableSlots(width: $0 - 12, scale: symbolScale) } ?? .max |
| 115 | let (shown, more) = KeyBarLayout.fit(ids, available: available) { Self.item($0)?.slots ?? 1 } | 116 | let (shown, more) = KeyBarLayout.fit(ids, available: available) { Self.item($0)?.slots ?? 1 } |
| 116 | return SlotRow { | 117 | return SlotRow { |
| 117 | ForEach(shown, id: \.self) { id in | 118 | ForEach(shown, id: \.self) { id in |
| @@ -192,10 +193,15 @@ struct KeyBar: View { | |||
| 192 | } | 193 | } |
| 193 | } | 194 | } |
| 194 | 195 | ||
| 195 | /// The buttons grow taller with the text size, but their symbols stop growing here, so | 196 | /// The buttons grow taller with the text size, but their symbols stop growing here. |
| 196 | /// that they fit the button's share of the width. | ||
| 197 | static let largestSymbolSize = DynamicTypeSize.xxxLarge | 197 | static let largestSymbolSize = DynamicTypeSize.xxxLarge |
| 198 | 198 | ||
| 199 | /// How much larger than at the default size the symbols are drawn, which the slots follow. | ||
| 200 | private var symbolScale: CGFloat { | ||
| 201 | let traits = UITraitCollection(preferredContentSizeCategory: UIContentSizeCategory(min(typeSize, Self.largestSymbolSize))) | ||
| 202 | return UIFontMetrics(forTextStyle: .body).scaledValue(for: 1, compatibleWith: traits) | ||
| 203 | } | ||
| 204 | |||
| 199 | @ViewBuilder | 205 | @ViewBuilder |
| 200 | private func label(_ item: Item) -> some View { | 206 | private func label(_ item: Item) -> some View { |
| 201 | Group { | 207 | Group { |
Tests/OrgAppTests/KeyBarLayoutTests.swift +16 · view
| @@ -88,6 +88,22 @@ struct KeyBarLayoutTests { | |||
| 88 | #expect(KeyBarLayout.fit([], available: 0, slots: slots) == ([], [])) | 88 | #expect(KeyBarLayout.fit([], available: 0, slots: slots) == ([], [])) |
| 89 | } | 89 | } |
| 90 | 90 | ||
| 91 | @Test func slotsWidenWithTheSymbols() { | ||
| 92 | // iPhone 17: 402 points less the bar's margins. | ||
| 93 | let width: CGFloat = 390 | ||
| 94 | let ids = KeyBarLayout.Width.iphone.defaultIDs | ||
| 95 | #expect(KeyBarLayout.availableSlots(width: width, scale: 1) == 12) | ||
| 96 | #expect(KeyBarLayout.availableSlots(width: width, scale: 0.8) == 12) | ||
| 97 | #expect(KeyBarLayout.fit(ids, available: KeyBarLayout.availableSlots(width: width, scale: 1)) { _ in 1 } == (ids, [])) | ||
| 98 | // At the largest symbol size (23 against 17 points of body text), slots are a third wider. | ||
| 99 | let large = KeyBarLayout.availableSlots(width: width, scale: 23.0 / 17) | ||
| 100 | #expect(large == 9) | ||
| 101 | let (shown, more) = KeyBarLayout.fit(ids, available: large) { _ in 1 } | ||
| 102 | #expect(shown == Array(ids.prefix(8)) && more == Array(ids.dropFirst(8))) | ||
| 103 | #expect(CGFloat(large) * KeyBarLayout.slotWidth * 23 / 17 <= width) | ||
| 104 | #expect(KeyBarLayout.availableSlots(width: 0, scale: 2) == 0) | ||
| 105 | } | ||
| 106 | |||
| 91 | @Test func snippetFieldsTakePromoteAndDemote() { | 107 | @Test func snippetFieldsTakePromoteAndDemote() { |
| 92 | let next = KeyBarLayout.nextField, previous = KeyBarLayout.previousField | 108 | let next = KeyBarLayout.nextField, previous = KeyBarLayout.previousField |
| 93 | #expect(KeyBarLayout.withSnippetFields(KeyBarLayout.Width.iphone.defaultIDs).prefix(4) | 109 | #expect(KeyBarLayout.withSnippetFields(KeyBarLayout.Width.iphone.defaultIDs).prefix(4) |
docs/manual/guide/14-ios.org +1 −1 · view
| @@ -160,7 +160,7 @@ The lists are =[key-bar]= in the configuration folder's =config.toml= (see [[fil | |||
| 160 | 160 | ||
| 161 | A button shows its command's symbol where it has one, such as the arrows for Undo and Redo or a clock for Clock In; otherwise its title, on up to two lines, in a button twice as wide. A snippet shows its trigger and a macro its name. VoiceOver reads the command's title. | 161 | A button shows its command's symbol where it has one, such as the arrows for Undo and Redo or a clock for Clock In; otherwise its title, on up to two lines, in a button twice as wide. A snippet shows its trigger and a macro its name. VoiceOver reads the command's title. |
| 162 | 162 | ||
| 163 | When the buttons don't fit the width, the bar shows as many as fit, in order, and a More button (…) at the end holds the rest as a menu. The bar never scrolls. Turning the device on its side makes room for more. | 163 | When the buttons don't fit the width, the bar shows as many as fit, in order, and a More button (…) at the end holds the rest as a menu. The bar never scrolls. Buttons widen with their symbols up to the largest text size before the accessibility sizes, so at larger text sizes fewer fit; on iPhone the default bar then puts its last buttons under More. Turning the device on its side makes room for more. |
| 164 | 164 | ||
| 165 | While a snippet's fields are filled in, Previous Snippet Field and Next Snippet Field take the places of Promote and Demote, so the fields work without a hardware keyboard; Promote and Demote come back when the snippet ends. A bar without Promote or Demote shows the field buttons first. | 165 | While a snippet's fields are filled in, Previous Snippet Field and Next Snippet Field take the places of Promote and Demote, so the fields work without a hardware keyboard; Promote and Demote come back when the snippet ends. A bar without Promote or Demote shows the field buttons first. |
| 166 | 166 | ||