Commit 974c897ecb

974c897ecb48b138c67b0c0e8115887978db2b89

parent: d88176172f

Verified · cmc

cmc <hello@cleberg.net> · 2026-11-26 05:11 UTC

iOS key bar: slots widen with the symbols at larger text sizes

Ref #137

Layout: unified · split

Changed files (4)
Sources/OrgApp/KeyBarLayout.swift +9 · view
@@ -57,6 +57,15 @@ public enum KeyBarLayout {
5757 return [previousField, nextField].filter { !out.contains($0) } + out
5858 }
5959
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
6069 /// The buttons that fit in `available` slots, in order, and the rest, which go in a More
6170 /// button that takes a slot. A button takes `slots(id)` slots; past the first that doesn't
6271 /// fit, none show, so the order stays.
Sources/OrgstarMobile/KeyBar.swift +11 −5 · view
@@ -2,6 +2,7 @@
22import OrgApp
33import OrgCore
44import SwiftUI
5import UIKit
56
67/// Org's keys for a touch keyboard: each button does what its key does at the caret, as the
78/// Emacs keymap binds it (M-→ demotes a heading, indents an item, moves a table column), or
@@ -11,6 +12,7 @@ struct KeyBar: View {
1112 let editor: MobileEditor
1213 let palette: () -> Void
1314 @Environment(\.horizontalSizeClass) private var sizeClass
15 @Environment(\.dynamicTypeSize) private var typeSize
1416 @AppStorage(KeyBarLayout.Width.iphone.defaultsKey) private var iphone: String?
1517 @AppStorage(KeyBarLayout.Width.ipad.defaultsKey) private var ipad: String?
1618 @State private var width: CGFloat?
@@ -102,16 +104,15 @@ struct KeyBar: View {
102104 static let largestSize = DynamicTypeSize.accessibility2
103105 static let target: CGFloat = 44
104106 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.
106108 nonisolated static let widest: CGFloat = 64
107 static let slot: CGFloat = 32
108109 @ScaledMetric(relativeTo: .body) private var size = KeyBar.target
109110
110111 var body: some View {
111112 let layout = sizeClass == .regular ? KeyBarLayout.Width.ipad : .iphone
112113 var ids = ((layout == .ipad ? ipad : iphone).map(KeyBarLayout.ids) ?? layout.defaultIDs).filter { Self.item($0) != nil }
113114 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
115116 let (shown, more) = KeyBarLayout.fit(ids, available: available) { Self.item($0)?.slots ?? 1 }
116117 return SlotRow {
117118 ForEach(shown, id: \.self) { id in
@@ -192,10 +193,15 @@ struct KeyBar: View {
192193 }
193194 }
194195
195 /// The buttons grow taller with the text size, but their symbols stop growing here, so
196 /// that they fit the button's share of the width.
196 /// The buttons grow taller with the text size, but their symbols stop growing here.
197197 static let largestSymbolSize = DynamicTypeSize.xxxLarge
198198
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
199205 @ViewBuilder
200206 private func label(_ item: Item) -> some View {
201207 Group {
Tests/OrgAppTests/KeyBarLayoutTests.swift +16 · view
@@ -88,6 +88,22 @@ struct KeyBarLayoutTests {
8888 #expect(KeyBarLayout.fit([], available: 0, slots: slots) == ([], []))
8989 }
9090
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
91107 @Test func snippetFieldsTakePromoteAndDemote() {
92108 let next = KeyBarLayout.nextField, previous = KeyBarLayout.previousField
93109 #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
160160
161161A 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.
162162
163When 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.
163When 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.
164164
165165While 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.
166166
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