hex/ios/HEXShared/FullKeyboard/FullKeyboardEngine+Keys.swift
Ryan Vogel 9588830c7e
iOS keyboard: French AZERTY gets Apple's apostrophe key after N (#2354)
What: Français (France)'s AZERTY is now Apple's "AZERTY – French", the layout
Apple lists first for France and Belgium: row 3 is W X C V B N and the
apostrophe key, seven keys a letter wide centred between shift and delete,
which keep their English size. As on Apple's, the key shows ’ right after a
letter and types the apostrophe (curled as the field curls quotes, straight
where it doesn't), and holds ' ’ ‘ `; anywhere else it shows and types ´
(U+00B4, never a dead key) and holds ´ ˋ ˆ ¨. A held choice goes in as it is.

Typing logic touched (for the Rust core move):
- FullKeyboardEngine: typeLayoutRules gets a .french case (FrenchTyping in
  CzechTyping.swift) that turns the key into ' after a letter; keycapLabel and
  displayLabel show ’ there; openAccentMenuIfEligible holds the 123 page's '
  after a letter (heldBase); textDidChange redraws the keys when the French
  key is on screen, so a cursor move updates its label.
- Decoder, AdaptiveTouch, next-letter odds, swipe typing: untouched. They read
  letters only (isLetter), so the key never enters their letter sets, and
  French has no typing model anyway.
- AutocorrectEngine: a word with an apostrophe used to be left alone in every
  language. In French only, frenchElisionFix now corrects each side of an
  elision's apostrophe: the word after it as a word of its own (l'hpmme ->
  l'homme, d'acvord -> d'accord), and a head that is no elision to the
  commonest one a key away when the dictionary rejects the whole word.
  Words the dictionary knows whole (aujourd'hui), a lone l' and other
  languages are unchanged. ownReplacement loses private for it.
- keyCoordinates: French AZERTY's row 3 offset 2.0 -> 1.5 (W now sits
  between Z and E), so the neighbouring-key cost matches the keys drawn.
- The iPad keeps its own ' and : key: padRows drops the iPhone key.

Replaced: PR #2096 left this key out ("Apple's French-only ´ key"); two
TestFlight testers (builds 120 and 122) and a post on X asked for it back.
Apple's Canadian and Swiss French default to "QWERTY – French" and "QWERTZ –
French" (the key at the end of row 2); Hex keeps those plain for now.

Verified: Apple's keyboard on iOS 27.2 simulators (iPhone 17 Pro and SE,
upright and turned) measured as a black box, Settings' layout lists read
through accessibility. Hex's keycaps fall on Apple's to the pixel in all four
(SE turned: 0.5 pt on shift/delete edges). Real HID touches on both phones:
"l'homme qu'il a vu n'est pas d'ici, c'est sûr" types right with the number
row off and on; l'hpmme, c'eat, d'acvord, n'eat are corrected; jai -> j'ai and
l' unchanged; the 123 page identical to origin/dev; a drag across row 3 types
the key under the lift, as before. xcodebuild test on origin/dev 3d834b6a plus
this change (iPhone 17 Pro, iOS 27.2): 803 tests, 0 failures, 3 skipped.
python3 scripts/code-shape.py passes.
2026-11-22 05:09:39 -05:00

126 lines
6.6 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 }
if typesFrenchApostrophe(character) { return SmartPunctuation.apostrophe }
guard isUppercase, layerState.layer == .letters else { return character }
return layout.language == .korean ? HangulComposer.shifted(character) : character.uppercased()
}
/// 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 }
if typesFrenchApostrophe(character) { return SmartPunctuation.apostrophe }
guard keycapsUppercase else { return character }
return layout.language == .korean ? HangulComposer.shifted(character) : character.uppercased()
}
/// 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
}
}