hex/ios/HEXShared/FullKeyboard/FullKeyboardEngine+Keys.swift
Ryan Vogel 6d16ec351a iOS keyboard: optional number row on iPhone (Settings › Keyboard › Number row)
What: a new switch, off by default (settings.keyboardNumberRow, App Group), that puts
1 to 0 in a row of their own above row 1 on the iPhone full keyboard, in every language,
portrait and landscape, on the Duo (folded and split) too. The most repeated request
Hex didn't answer: at least eight people in two days asked for a dedicated number row.

Why it looks this way: Apple's iPhone keyboard has no number row, so it follows SwiftKey
on iPhone (black box: its row is 0.85 of a letter row's pitch with the same gap; Gboard on
iPhone has none, only digits held on row 1). KeyboardMetrics.withNumberRow adds a band of
0.85 x the letter pitch above row 1 (34.9 pt keycaps, +45.9 pt on a 17 Pro, +47.6 Pro Max,
+34 turned); the keyboard grows upward and every row below keeps its place, since iOS keeps
a keyboard on the screen's bottom. Digits sit on the 123 page's columns, take the strip gap
as row 1 did, and hold what the 123 page's digits hold. The 123 page hides the row (its row 1
is the same digits) but keeps the band so nothing jumps, and its row 1 reaches up through it;
#+= keeps the row. fitted(toHeight:) scales the number row with the others. iPad: none yet
(KeyboardMetrics.for ignores numberRow with a pad placement).

Typing logic (for the shared Rust core): digit keys are KeyKind.character on row
KeyboardLayout.numberRowIndex (-2). FullKeyboardEngine.key(at:) (next-letter touch help)
now skips number-row keys both as the touched key and as neighbours. Nothing else changed:
TypingDecoder.Keys and AdaptiveTouchGeometry already take letters only, SwipeTyping only
starts swipes on letters, and AutocorrectEngine.currentToken already refuses tokens
touching digits.

Off: KeyboardLayout(numberRow: false) == KeyboardLayout(), existing layout tests untouched.

Verified: new cases in KeyboardLayoutTests/FullKeyboardEngineTests/FullKeyboardSettingsTests
pass; on the Studio's iOS 27.2 sims (17 Pro, 17 Pro Max, 13 mini, SE, Duo cover) Hex's rows
read 598/652/706/760 pt on a 17 Pro with the row off and on (Apple's overlay lines), the
view is granted 293 pt in a text view, a Safari form and Spotlight, HID taps typed
"Flat 12b at 9pm, call 555 0199 the 3rd" (teh corrected, digits untouched), swipes across
row 1 ("power", "quiet") typed, holds on e and 0 open their trays over the row, and the
trackpad blanks the row with the others.
2026-11-22 01:28:51 -05:00

122 lines
6.3 KiB
Swift

import QuartzCore
import UIKit
/// What the engine hands Spatial autocorrect and Record typing logs when the keys or the
/// field change: the letter keys to read taps against, and the keys' and shift's state.
@MainActor
extension FullKeyboardEngine {
/// The letter keys Spatial autocorrect scores taps against: this layout's, moved by
/// `tapOffsets`; with Adaptive touch on, by its personal centres instead (none while it
/// has too little evidence), with the others beside them as the baseline no correction
/// may go past (`AutocorrectEngine.decoderBaselineKeys`). Off the letters page the last
/// letters page's stay: a closing mark from the 123 page still reads the word's taps
/// ("ofthe." -> "of the.").
func syncDecoderKeys() {
guard let keys = TypingDecoder.Keys(layout: layout, offsets: tapOffsets) else { return }
guard let adaptive = adaptiveTouch?.offsets(for: layout),
let personal = TypingDecoder.Keys(layout: layout, offsets: adaptive) else {
autocorrect.decoderKeys = keys
autocorrect.decoderBaselineKeys = nil
return
}
autocorrect.decoderKeys = personal
autocorrect.decoderBaselineKeys = personal == keys ? nil : keys
}
/// Hands the layout and shift to typing logs, which writes a line when either changed.
func logState(_ cause: String) {
typingLog?.keyboardState(layout: layout, shift: shift, cause: cause)
}
/// Whether a finger going down at `point`, in the strip above the keys, belongs to the
/// keys: in the strip's lower half while you type (a key is down, or one went down or came
/// up within `stripTouchGuard`) it is a row-1 key aimed high, not a pick. Nobody reads a
/// new suggestion and taps it that fast; Record typing logs showed such picks were the e
/// below the left slot, which kept "th" and typed a space where "the" was meant.
func takesStripTouch(at point: CGPoint) -> Bool {
let strip = layout.stripFrame
guard point.y < layout.touchAreaFrame.minY, point.y >= strip.midY,
point.x >= strip.minX, point.x < strip.maxX else { return false }
if !touches.isEmpty { return true }
guard let last = lastKeyTouchAt else { return false }
let elapsed = clock() - last
return elapsed >= 0 && elapsed < Self.stripTouchGuard
}
/// `point`, or for one above the keys (a strip touch `takesStripTouch(at:)` gave them)
/// the same spot on row 1's top edge.
func pointOnKeys(_ point: CGPoint) -> CGPoint {
let top = layout.touchAreaFrame.minY
return point.y < top ? CGPoint(x: point.x, y: top) : point
}
/// The kind of key a finger going down at `point` lands on, or nil off the keys: what
/// `touchBegan(id:at:)` will press, up to the next-letter odds choosing a neighbouring
/// letter. Cheap and free of side effects, so the key haptic can fire before any typing.
func keyKind(at point: CGPoint) -> KeyKind? {
layout.hitTest(pointOnKeys(point))?.kind
}
/// The kinds of every key a finger holds down now, its slide's key too.
var pressedKinds: Set<KeyKind> {
Set(touches.values.flatMap { touch in [touch.key.kind] + (touch.slideKey.map { [$0.kind] } ?? []) })
}
/// A key's label as the keys read it: upper case on the letters page with shift on.
func displayLabel(for key: KeyboardKey) -> String {
guard let character = key.character else { return key.label }
return isUppercase && layerState.layer == .letters ? character.uppercased() : character
}
/// Whether the letter keys read as capitals: as `isUppercase`, except while a press that
/// turns shift on is still down (`ShiftStateMachine.pressShowsOff`).
var keycapsUppercase: Bool { isUppercase && !shift.pressShowsOff }
/// A key's label as its keycap shows it (`keycapsUppercase`); pop-ups use `displayLabel`.
func keycapLabel(for key: KeyboardKey) -> String {
guard let character = key.character else { return key.label }
return keycapsUppercase ? character.uppercased() : character
}
/// The key a touch going down at `point` presses: the one under it, or a neighbouring
/// letter within `letterReach` points that is enough likelier to come next in the word.
/// The number row's digits are never letters: a digit's tap stays a digit, and no digit
/// claims a row-1 letter's.
func key(at point: CGPoint) -> KeyboardKey? {
let point = pointOnKeys(point)
guard let key = layout.hitTest(point) else { return nil }
guard key.character != nil, !key.isNumberRow, let model = letterOdds, layerState.layer == .letters,
field == .text, autocorrect.isEnabled, target.autocorrectionType != .no,
shift.state != .locked, model.language == language.rawValue else { return key }
let reach = letterReach
let near = layout.keys.filter {
$0.character != nil && !$0.isNumberRow && $0.kind != key.kind && Self.distance(from: point, to: $0.hitFrame) < reach
}
guard !near.isEmpty, let continuations = continuationMass(model) else { return key }
// An accented letter without a key of its own is typed from its base letter's (é from e).
var mass = [Double](repeating: 0, count: continuations.count)
let keyed = Set(layout.keys.compactMap { key in
key.character.flatMap { $0.count == 1 ? model.codes[Character($0)] : nil }
})
for (code, m) in continuations.enumerated() where m > 0 {
mass[keyed.contains(UInt8(code)) ? code : Int(model.baseCodes[code])] += m
}
let total = mass.reduce(0, +)
func odds(_ k: KeyboardKey) -> Double? {
guard let c = k.character, c.count == 1, let code = model.codes[Character(c)] else { return nil }
return log(mass[Int(code)] + total * 1e-4)
}
guard let own = odds(key) else { return key }
var best = key
var bestScore = own
for other in near {
guard let theirs = odds(other) else { continue }
let score = theirs - letterOddsPerPoint * Double(Self.distance(from: point, to: other.hitFrame))
if score > bestScore {
best = other
bestScore = score
}
}
return best
}
}