hex/ios/HEXShared/FullKeyboard/FullKeyboardEngine.swift
Ryan Vogel 9ca17141d6
iOS: the full keyboard matches the system keyboard one to one (#2178)
* iOS keyboard: key haptic fires on touch-down before the key's work, as a crisp tick

Adam (build 56) said the haptics feel weird. In KeyboardKeyPlaneView.began the
haptic and click came after engine.touchBegan, which for a fast typist first
commits the rolled-over key (autocorrect, suggestions) or deletes, so the tick
could trail the finger by tens of ms. The plane now asks the engine which key
kind the finger lands on (new FullKeyboardEngine.keyKind(at:), a pure hit test)
and fires onKeyDown before any typing; delete's repeat still ticks per step.

The tick itself is now a rigid impact at 0.6 (FullKeyboardView.keyTickStyle /
keyTickIntensity, shared with the dictation bar) instead of light at 0.65:
Apple's key haptic is a light, crisp tick and rigid is the sharpest impact
style. The Hex key's hold and menu keep the fuller light thud. The haptic now
fires before UIDevice.playInputClick rather than after it.

KeyboardViewController reads Settings > Keyboard > Haptics on each bridge sync
(appear and poll) instead of building FullKeyboardSettings on every key press.

Verified: new FullKeyboardEngineTests.testKeyFeedbackFiresOnceOnTouchDownBeforeTheKeyTypes
(rollover ticks before either letter types; space, delete, shift, return and
123 tick once each on the way down; lifting never ticks) and the existing
testEveryLayoutAsksForTheSameKeyHaptics; full HEXMobileTests 383 run, 0 failed
(iPhone 17 sim, iOS 27.2). The simulator has no Taptic Engine: style and
intensity need a real iPhone.

* iOS keyboard: Apple's 43 pt keys on a 54 pt row pitch, so rows sit where fingers expect

Black-box measurement beside Apple's keyboard in the same Messages field
(iPhone 17 simulator, iOS 27.2): Apple's keycaps are 43.0 pt tall on a 54 pt
pitch (rows at 598/652/706/760 pt on screen); Hex drew iOS 26's 45 pt on
56 pt, rows at 581/637/693/749. Hex's hit borders therefore sat 12 to 21 pt
above Apple's on screen, so taps aimed by Apple-trained thumbs landed low on
Hex's keys and crossed into the row below ("the hit box is super weird").

iPhone portrait is now 43 pt keys, 11 pt gaps, 5 pt under row 4 (262 pt, was
270): rows sit 9 pt above Apple's everywhere and borders within 6 to 9 pt.
Row 4 can't go lower: a third-party keyboard's view ends 4 pt above Apple's
row 4 bottom, and dropping the 5 pt margin cost low space-bar taps (held-out
benchmark EN 5.94% -> 6.19% WER). The Duo's cover screen keeps its measured
45 pt on 56 pt. Hit frames still tile the key area (gaps split at their
middles, edge keys to the screen edges), which matched Apple's base borders.

Verified: held-out tap benchmark (2,000 sentences each, Spatial on) EN 824
edits before and after (5.9409%); ES 1,145 -> 1,146 (one sentence better, one
worse, both from the fixed 5 pt next-letter reach). KeyboardLayoutTests updated
for 262 pt and the 54 pt pitch; HEXMobileTests 383 run, 0 failed.

Rebased on #2146: Adaptive touch's contexts now key on the key height and row
pitch too (AdaptiveTouchGeometry), so offsets learned on Hex's 45 pt keys on a
56 pt pitch, in keycap units, aren't read on these rows. A key plane with the
same rows keeps its profile, as before.

* iOS keyboard: the key plane matches Apple's, key for key, on every iPhone

Measured from screenshots beside Apple's keyboard in the same Messages field
(iOS 27.2 simulators: iPhone 17 Pro, 17e, Air, 17 Pro Max, SE 3rd gen; light and
dark; letters, shifted, numbers and symbols pages), black box only.

Geometry (KeyboardLayout):
- Key height and row pitch follow the screen (KeyboardPhoneSize): 43 pt on 54 pt
  on 844-874 pt iPhones, 45 on 56 on the Air and Pro Max, 42 on 54 on the SE.
  Hex drew 45 on 56 everywhere, rows 11-17 pt above Apple's. Rows now sit on
  Apple's lines. On 390/402 pt iPhones Apple's row 4 ends 4 pt below where a
  third-party keyboard's view ends (touches below y 799 never reach Hex), so
  Hex's row 4 keeps Apple's top edge and is 39 pt tall (bottomRowTrim); its
  labels are centred as on Apple's 43 pt row.
- The SE's keys start 6.5 pt in, as Apple's (Hex used 4.5).
- Shift, delete, #+= and 123 are 1.227 pitches less 3 pt (fits all five
  iPhones within a pixel; a fixed 1.34 letters was 0.5 pt narrow on the 17 Pro).
- The portrait bottom row stands on row 3's columns: 123 (and emoji, or the
  globe) end at z's right edge, space runs x to n, return m to the edge. With no
  emoji key 123 fills that block, which is what Apple's own keyboard does with
  the Emoji keyboard removed; on the SE Apple's row matches with dictation off.
- Apple's emoji key takes taps only 6 pt inside its sides and 7.5 pt below its
  top, the margins typing nothing (mapped by tapping); Hex's emoji key does the
  same (Erick hit it by accident).
- Keycaps are pixel-aligned and the corner radius is 7.9 pt (same corner fit as
  Apple's, 8.1 by the measuring tool; Hex's 8.5 measured 8.7).
Labels (KeyboardKeyPlaneView), fitted against labels rendered offscreen at known
sizes: lowercase 24.5 pt (was 22.5), capitals 21 pt weight 0.12, digits and
symbols 21.5 pt weight 0.08 with case-sensitive forms, a few symbols at their
own sizes, 123/ABC 17.75 pt, #+= 12.25 pt, shift/delete/return/globe symbols
19 pt; the quote keys show " and ' curly as Apple's (they still type straight).

Result: every keycap edge on every iPhone, page and appearance within 0.00 pt
of Apple's (except row 4's bottom on 390/402 pt and the emoji-less 123);
label boxes within 1 pt (median 0.34 pt), ink within 2% (median), except ^, <
and > on the symbols page (different shapes).

Held-out tap benchmark (2,000 sentences each, taps aimed at Hex's own keys):
EN 5.94% -> 6.41% WER, ES 8.61% -> 9.32%: low space-bar taps now fall below the
view's bottom edge (19.5 pt below the space bar's centre instead of 27.5).
With taps aimed where Apple's keys are (a user used to Apple's keyboard):
EN 21.34% -> 6.97%, ES 23.45% -> 10.04%.

KeyboardLayoutTests updated to the measured numbers (heights 253/260/272,
54/56 pt pitch, shift and delete edges, SE inset, bottom-row columns, emoji
target, tiling with the emoji margins); HEXMobileTests 385 run, 0 failed.

Rebased on #2142: the emoji panel's bar sits from keysBottom, so on these
iPhones it now reaches the panel's bottom edge as row 4 does.
testTheBarsKeysTakeTheWholeBandUnderTheGrid skips its probes below the panel,
where nothing can be hit.

Rebased on #2172: Dvorak's letters size from this shift width
(dvorakLetterWidth reads modifierWidth), so its row 3 of shift and nine letters
still fills edge to edge. Its rows take these heights and pitch like every
layout's.

* iOS keyboard: Settings > Keyboard > Strength for key haptics, Medium by default

Testers on X said Hex's key haptic is too weak beside Apple's keyboard (Tareq:
"add an option to adjust the haptic feedback strength", Erick: "not nearly as
nice as apple keyboard"). The tick was a light impact at 0.65 (0d4f78e5 made it
rigid 0.6).

- KeyHapticStrength (light, medium, strong) in the App Group, read on each
  bridge sync (FullKeyboardSettings.hapticStrength, default medium).
- Light is a rigid impact at 0.5, Medium (meant to feel like Apple's crisp
  tick) rigid at 0.8, Strong a medium impact at 1.0. The full keyboard and the
  dictation bar use the same mapping (FullKeyboardView.keyTickStyle/Intensity).
- Settings > Keyboard shows Strength (Light | Medium | Strong) under Haptics
  while Haptics is on, with zh-Hans strings (强度, 轻, 中, 强).

Verified: HEXMobileTests 385 run, 0 failed; the Settings page renders the row
in English and zh-Hans (simulator scene settings-keyboard-end). The simulator
has no Taptic Engine: which intensity matches Apple's tick needs a real iPhone.

* iOS keyboard: the Duo folded matches Apple's keys, and a tester fixture follows the new rows

The Duo folded (iPhone Duo simulator, iOS 27.2 iPhone19-4 runtime, cover screen,
Hex beside Apple's keyboard in Messages; Ethan on X: "a tiny bit off"):
- Apple's row 4 ends 7 pt above the keyboard's bottom; Hex's ended 5 pt above, so
  every Hex row sat 2 pt low. keysBottom is 7 folded and upright (the strip stays
  the top bar's 32 + 8 pt).
- Apple's keys start 6.67 pt from the screen's side, a third of a point outside
  the 452 pt view a third-party keyboard gets; Hex's started 8.0 pt in (a 1 pt
  minimum inset). The inset may now be 0, so Hex's keys run to the view's edges.
- Apple's keycaps round about 9.8 pt there (Hex drew 7.9).
- Row 4 stands on the letter columns, as Apple's does folded: 123, globe and
  emoji one key wide on q, w and e, space from r, return from l's column. Apple
  puts dictation on m's column; Hex has none, so its space bar runs on (a Hex key
  right of space takes that slot).
Result: rows on the same y as Apple's, every edge within 0.67 pt (the view's
edge), corners 10.2 vs 10.1 by the same measuring tool. Not measured unfolded
(no unfold tool on the Studio); the unfolded metrics are unchanged.

testTheTestersSentenceComesOutRightWithSpatialAutocorrect (from origin/dev) placed
its taps on the old 45 pt rows on a 56 pt pitch, down to y 242 where row 4 now
ends at 236, so low space taps fell off the keys. Each tap now sits at the same
spot on today's row; the expected sentence is unchanged.
KeyboardLayoutTests: the cover's Apple edge is 6.67 pt, and the bounds checks
allow 0.001 pt of rounding now that keys can touch the view's edge.

Verified: HEXMobileTests all passing (iPhone 17 sim, iOS 27.2); Duo cover
screenshots measured as above.

* iOS keyboard: turned, the keys match Apple's on every iPhone

Measured turned beside Apple's keyboard in the same Messages field (iOS 27.2
simulators: iPhone 17e, 17 Pro, Air, 17 Pro Max, SE), black box:
- Apple's keys are 32 pt on a 40 pt pitch with 2 pt under row 4 on every iPhone
  (4 pt on the SE), corners 7.9 pt. Hex drew iOS 26's 27.5 pt on 35.5 pt with
  3 pt under, every row 2 to 17 pt below Apple's. The strip stays the top bar's
  42 + 8 pt; the keyboard is 204 pt (was 187).
- Row 4 stands on row 1's columns: 123 on q, emoji (or the globe) on w, space
  from e to i, return from o to p (KeyboardMetrics.bottomRowReturnColumn, which
  the Duo folded now shares with its own 8.5).
- Shift, delete, #+= and 123 on row 3 are 1.33 letters wide turned (the portrait
  rule left them 2.3 pt narrow).
- The SE sets its keys 72 pt in from each side, 47 pt wide; Hex's ran 4 pt from
  the edges, 60.5 pt wide.
Result: every keycap edge on the five iPhones within 0.5 pt of Apple's turned
(0.00 on all but the SE's delete, 0.5). Apple's SE reaches emoji through the globe
and shows no emoji key turned; Hex keeps its emoji key after the globe, so the
SE's space bar starts one key later. Not measured: the Duo turned (its cover
screen doesn't rotate in the simulator); it follows the iPhone.

Verified: KeyboardLayoutTests (heights 204 and 198 turned, new
testLandscapeMatchesApplesKeys); turned screenshots on all five iPhones.

* iOS keyboard: turned labels and held keys look like Apple's

Black box, beside Apple's keyboard in the same field (iOS 27.2 simulators):

Turned labels (17e, 17 Pro, SE; fitted against labels rendered offscreen at
known sizes and weights): letters, digits and symbols are about 0.875 of their
portrait sizes (lowercase 21.5 pt, capitals and symbols 18.75 pt a little
heavier), with per-symbol sizes where Apple's differ (. , : _ # * • and others);
123 15.5 pt, ABC 17.75 pt, #+= 12.25 pt; shift, delete and return symbols 18 to
18.5 pt (Hex drew 15.5 to 16), and the emoji key's face semibold, as Apple's.
Hex drew 18 and 19 pt labels, about 14% lighter than Apple's. Now each label's
box is within 1 to 1.25 pt of Apple's and its ink within 1% (median) on the 17e
and SE turned.

Held keys: Apple paints a held key one of three greys. A letter under its pop-up
222 (105 dark), which Hex already drew for every key; shift, delete, globe, emoji
and return 197, 197, 201 (131 dark); the space bar 242, 242, 247 (56 dark). 123
and ABC switch the page as they go down and aren't shaded. FullKeyboardPalette
gains keyHeldFill, functionHeldFill and spaceHeldFill; every held key now
measures within 1 grey level of Apple's in light and dark (17e).

Checked, nothing to change: keycaps have no shadow, edge highlight or inner
stroke on Apple's keys (letter or function, light or dark); the fill meets the
panel in one anti-aliased pixel, as Hex's does, and a 32 x 32 px corner differs
by at most 6/255. The keys draw over #2139's opaque backdrop.

Verified: HEXMobileTests passing; screenshots above on the simulators.

* Draw iOS 27's floating key pop-up and let it linger after lift

What: the key pop-up is now iOS 27's bubble: a rounded rectangle (radius 12)
23.33 pt wider than the key and 65 pt tall, floating 3.5 pt above it, with no
border or stem. The letter is 36.5 pt in either case; digits and symbols use the
key's own style scaled 1.5x. The dark bubble shades from 63 to 85 grey, and the
pressed key stays drawn grey under it. Over row 1 the bubble shrinks to the room
there is, down to 50 pt, because an extension can't draw above its view, which
Apple's can. After lift the pop-up and its grey key stay 45 ms, then go in one
frame.

Removed: iOS 26's balloon (a 46 pt bubble on an 8 pt stem that covered the key,
1 px border, 33 pt light label). Also removed: the pop-up disappearing before its
letter reached the field, and edge bubbles kept 2 pt in from the sides (now
pushed in only as far as the screen needs, as Apple's are).

Why: black-box comparison with the system keyboard on the iPhone 17e and 17 Pro
simulators (iOS 27.2), measured from screenshots and 60 fps recordings.

Verified: popups-17e-apple-vs-hex.png comparison, light and dark. On 60 fps
recordings of 120 ms taps, Hex's pop-up now leaves about 47 ms after the letter
lands (Apple about 50). Full HEXMobileTests pass.

* Match Apple's long-press accent callout: arrangement, layout, timing

What: the accent callout now looks and behaves like the system keyboard's
(iOS 27.2 simulators, every English letter, digit and symbol key held and
screenshotted):
- Arrangement: options sit where Apple puts them, the key's own selected over
  the key (e: ë é [e] è ê ě ẽ ē ė ę; $: ₽ ¥ € [$] ¢ £ ₩; " « » „ “ ” ["]).
  "a" gets two rows (æ ã å ā ă ą over a à á â ä ǎ). The characters stay
  #2140's observed lists (KeyboardLanguage.letterHolds, symbolHolds, the
  123/#+= list). AccentCatalog.arrangement only places them, and only while its
  table lists exactly the same options, so a list change can't drop or invent
  one. Languages without a checked arrangement open from the key toward the
  middle, as before.
- Geometry: cells are the held key's width + 4 pt (36.3 on a 390 pt iPhone's
  letters, 51.7 on the wide . , ? ! ' keys, 67 in landscape). Rows are 47 pt
  with 10.5/10.35 pt padding and 20.8 pt corners, 3 pt above the key, kept
  6.67 pt from the sides. Sliding picks by row, then by nearest cell.
- Look: the selection is a grey rounded square (227/227/230, dark 87 grey,
  6.3 pt corners) behind 21.5 pt regular options, on the pop-up's bubble with no
  border. The dark callout lightens toward its bottom.
- Letting go more than 57.5 pt below the key's middle closes the callout with
  nothing typed (Apple: 55 pt types, 60 cancels).
- Hold delay 0.36 s: the callout replaces the pop-up about 380 ms in (Apple
  382, Hex was 420).
The emoji panel's skin-tone picker passes its own cell width and keeps its
layout and drawing unchanged (AccentMenu.buildFixed, drawFixed).

Removed: the blue selected cell with white medium text, the 1 px border, the
44 pt cells and 4 pt padding, and opening every list sideways from the key with
the base at the thumb end.

Why: press-and-hold visuals should match the system keyboard one to one.

Verified: 17e simulator screenshots of Apple vs Hex holds (e with slide and
drag-off cancel, a, n, u, k, $, 0, ", ', .), light and dark, and landscape (i).
The drag-off typed nothing on both. Updated testLongPressAccentPicker... for
Apple's arrangement. Full HEXMobileTests pass apart from #2142's emoji bar band
test, fixed with the key-plane geometry it depends on.

Rebased on #2164 and #2172: the arrangement is looked up for the layout's
language and letters, as the options are (the bilingual English & Spanish
keyboard holds English's; Spanish on plain QWERTY, whose n also holds ñ, opens
its own list). testEnglishOnQWERTZTypesOnItsOwnKeys checks the selected option
is the key's own instead of the first, since Apple's y reads ÿ ŷ ý [y].

* Match Apple's key-plane micro-animations: shift press, key release, trackpad fade

What, each measured against the system keyboard from 60 fps recordings and
mid-press screenshots on the iPhone 17 Pro simulator (iOS 27.2):
- Shift: a press that turns shift on now shows nothing until lift: no shade,
  outline arrow, lowercase letters (ShiftStateMachine.pressShowsOff,
  FullKeyboardEngine.keycapLabel). Hex used to shade the key and show capitals on
  touch-down. Shift still engages on touch-down, so a chord types a capital, and
  the keycaps show capitals once a letter has been typed during the hold. Presses
  that turn shift or caps lock off still shade at once, as Apple's do.
- Delete, space, return, globe and emoji stay shaded 60 ms after a tap lifts.
  Apple shows them 265-284 ms for a 200 ms tap; Hex showed 200-210 and now
  265-277. A delete held long enough to repeat, or used for a word swipe, lets
  go at once, as Apple's does.
- Trackpad: the key labels fade out and back in, ease-out over 0.24 s (visible
  change 204/198 ms against Apple's 202/200, first step 10 ms after the change,
  Apple 13). The space bar unshades as it becomes the trackpad. Hex used to blank
  the labels in one frame and keep the space bar shaded. Reduce Motion, or no
  window, switches at once. A CADisplayLink drives the fade through a weak ticker,
  only while it runs.
- Trackpad hold delay 0.4 s -> 0.35 s: it now starts 363 ms after touch-down,
  as Apple's does (Hex was 417).

Unchanged, because Apple's are instant too: pop-up appear/disappear, page
switches (123/ABC/#+=), accent callout open, select and close, and caps lock.

Verified: recordings and screenshots of Apple vs Hex for shift (held from off,
from on and from caps lock), delete/space/return taps (50, 200 and 600 ms),
the trackpad, page switches and accent open. Full HEXMobileTests pass. Tap
latency is untouched (the linger and fade run after lift or after the trackpad
starts).

* Place portrait letter labels where Apple's sit, not centred by advance

What: letter keycaps (letters page, portrait) now draw each letter at its key's
middle plus Apple's measured offset for where that middle falls between whole
points: sixths of a point on 3x screens, quarters on 2x (KeyboardKeyPlaneView
drawLetter, letterNudge3x/letterNudge2x; lowercase and capitals separately).
Capitals move up a pixel (yOffset -1 -> -1.33).

Why: a TestFlight tester (build 56, iOS 27.2.1) said the letters look slightly
off-centre. Measured at native resolution against the system keyboard (iOS 27.2
simulators, ink centroid of each of the 26 letters relative to its keycap's
centre, lowercase and capitals, light and dark), Apple doesn't centre letters by
their advance as plain text does. Its offset depends on the key's position: on
the 17 Pro, keys centred on a whole point have letters 0.2-0.6 pt left of plain
centring, and keys centred on a half point have them 0.3-0.8 pt right. The same
fraction gives the same offset on the Pro Max. Hex's plain centring was therefore
0.27-0.32 pt off Apple's per letter (median; up to 0.82) on 3x screens, and
0.35/0.58 (up to 1.16) on the SE. Capitals also sat 0.27 pt (0.8 px) low on every
letter.

Removed: plain advance centring for portrait letters. Landscape letters, digits
and symbols keep it.

Verified (Apple minus Hex, ink centroids, median|max, pt):
- 17 Pro: lowercase 0.31|0.82 -> 0.09|0.49; capitals 0.27|0.63 -> 0.14|0.42
- Pro Max: lowercase 0.29|0.77 -> 0.10|0.44; capitals 0.28|0.75 -> 0.13|0.53
- Air (held out of the fit): lowercase 0.29|0.77 -> 0.10|0.44; capitals
  0.28|0.75 -> 0.13|0.53
- SE (2x): lowercase 0.58|1.16 -> 0.15|0.44; capitals 0.35|0.70 -> 0.12|0.37
- Vertical, capitals on 3x: 0.27 -> 0.06.
Lowercase stays 0.13 pt low and SE capitals 0.28 low: text lands on whole
pixels, so a whole pixel would overshoot. Light and dark measure the same. Full
HEXMobileTests pass.

* Draw Apple's grinning emoji-key face instead of SF Symbols' face.smiling

What: the emoji key draws the face Apple's shows (KeyboardKeyPlaneView
drawEmojiFace): a 19 pt face (18 in landscape) with an open mouth and a band of
teeth, centred 0.67 pt above the key's middle. Light mode draws a 1.58 pt ring;
dark mode draws a solid face with the eyes and mouth cut through to the keycap
(even-odd fill). The teeth are always the light colour.

Why: the key-plane overlays showed Hex's emoji key drawing a closed smile
(face.smiling), and in landscape at 16 pt instead of Apple's 18. Apple's grinning
face isn't a public SF Symbol (face.grinning and the like don't exist), so it is
drawn from primitives sized from measurements of 3x simulator screenshots on the
17 Pro, Pro Max, Air, 17e and SE (iOS 27.2): ring, eyes 2 x 2.67 pt at
(+/-2.78, -2.13), and a mouth 12.33 pt wide, its top 0.5-1.5 pt and its bottom
6.5 pt below the centre. No Apple artwork is copied.

Removed: the face.smiling symbol on the emoji key.

Verified: zoomed Apple vs Hex crops on the 17 Pro in light, dark and landscape
match by eye; within the glyph, 5.2% (light) and 2.0% (dark) of pixels differ by
more than 64/255, mostly a one-pixel horizontal phase. Full HEXMobileTests pass.

* Lay out Spanish holds as Apple's; keep the key's own until the finger moves

What:
- Spanish callouts use the system keyboard's arrangement, read off every letter,
  digit and symbol held on the iOS 27.2 Spanish keyboard
  (AccentCatalog.spanishOrder). Examples: º ō œ ø õ ô ò ó [o] ö with ö to the
  right of o; ū û ù ü ú [u]; l·l [l]; ń [n]; 9ª [9] 9º; [0] 0ª ° 0º. Symbols as
  English's. The characters stay #2140's Spanish letterHolds and symbolHolds,
  which match these on screen.
- The accent callout keeps the key's own character selected until the finger
  moves into another cell, and lifting types the current selection
  (FullKeyboardEngine.accentOpenCell, accentIndex). The debug fixtures select a
  callout option directly (debugSelectAccent), as a slide to it does.

Removed: lifting choosing the cell under the finger even if the finger never
moved. At the screen's edge the callout shifts, so 0 typed "0º" on a plain hold;
Apple's types "0". Also removed: the fixtures' synthetic move, which landed in
the cell the finger opened in when a long callout shifted (Italian e), so it
selected nothing.

Why: the Spanish system keyboard comparison (17e simulators, both set to
Spanish) showed the arrangement differing and the 0 hold typing the wrong
character.

Verified: Apple vs Hex screenshots of o s d l n z c 1 9 0 held on the Spanish
keyboard all match. A plain hold of 0 now types "0", and slide-then-drag-off
cancels. Lowercase holds confirmed đ (Spanish) vs ď ð (English). Full
HEXMobileTests pass apart from #2142's emoji bar band test, fixed with the
key-plane geometry it depends on.

* Replace the letter-position table with per-scale constants: it didn't generalize

What: portrait letters are again centred by advance, plus a constant per case
and screen scale: lowercase +0.17 pt and capitals 0 on 3x screens, +0.55 and
+0.23 on the 2x SE (KeyboardKeyPlaneView drawLetter). The capitals' one-pixel
lift from cef53885 stays.

Removed: cef53885's letterNudge3x/letterNudge2x tables, which offset each letter
by where its key's middle falls between whole points. They were fitted on the
17 Pro and Pro Max (the Air shares the Max's key phases, so it wasn't an
independent check). On the 17e, held out, they made letters worse: lowercase
0.23 -> 0.42 pt (median, Apple minus Hex), capitals 0.22 -> 0.27, worst cases up
to 1.0. The 17e's half-point keys need the opposite offset from the 17 Pro's.
No model tried (rounding to pixels or points from the screen, the keyboard or
the row; ink-weighted centring; optical sizes) predicted Apple's per-key
scatter across devices; the best managed 0.31 pt RMS against 0.385 for plain
centring.

Why: only the per-case constant holds on every device of a scale. Apple's
lowercase sits 0.13-0.22 pt right of plain centring on all four 3x iPhones
measured, its capitals within 0.13 either way, and the SE's by 0.55 and 0.23.

Verified (ink centroids, all 26 letters, light and dark; median|max, pt):
- Lowercase x: 17 Pro 0.31|0.82 -> 0.25|0.82; Air and Pro Max unchanged
  0.29|0.77 (the half-pixel nudge rounds away there); 17e 0.26|0.81; SE
  0.58|1.16 -> 0.27|0.69.
- Capitals x: unchanged on 3x (0.25-0.28); SE 0.35|0.70 -> 0.33|0.64.
- Capitals y on 3x: 0.27 -> 0.06.
- Full HEXMobileTests pass.
2026-11-21 07:20:46 -05:00

1554 lines
69 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import QuartzCore
import UIKit
/// The text field the keys type into. `UITextDocumentProxy` in the keyboard; a fake in tests.
@MainActor
protocol KeyboardTextTarget: AnyObject {
var documentContextBeforeInput: String? { get }
var hasText: Bool { get }
var autocapitalizationType: UITextAutocapitalizationType? { get }
var autocorrectionType: UITextAutocorrectionType? { get }
var keyboardType: UIKeyboardType? { get }
var returnKeyType: UIReturnKeyType? { get }
var enablesReturnKeyAutomatically: Bool? { get }
var keyboardAppearance: UIKeyboardAppearance? { get }
var textContentType: UITextContentType? { get }
var isSecureTextEntry: Bool? { get }
func insertText(_ text: String)
func deleteBackward()
func adjustTextPosition(byCharacterOffset offset: Int)
}
extension KeyboardTextTarget {
var autocorrectionType: UITextAutocorrectionType? { .default }
var enablesReturnKeyAutomatically: Bool? { false }
var textContentType: UITextContentType? { nil }
var isSecureTextEntry: Bool? { nil }
func adjustTextPosition(byCharacterOffset offset: Int) {}
}
@MainActor
final class DocumentProxyTextTarget: KeyboardTextTarget {
private let proxy: () -> UITextDocumentProxy?
init(_ proxy: @escaping () -> UITextDocumentProxy?) {
self.proxy = proxy
}
var documentContextBeforeInput: String? { proxy()?.documentContextBeforeInput }
var hasText: Bool { proxy()?.hasText ?? false }
var autocapitalizationType: UITextAutocapitalizationType? { proxy()?.autocapitalizationType }
var autocorrectionType: UITextAutocorrectionType? { proxy()?.autocorrectionType }
var keyboardType: UIKeyboardType? { proxy()?.keyboardType }
var returnKeyType: UIReturnKeyType? { proxy()?.returnKeyType }
var enablesReturnKeyAutomatically: Bool? { proxy()?.enablesReturnKeyAutomatically }
var keyboardAppearance: UIKeyboardAppearance? { proxy()?.keyboardAppearance }
var textContentType: UITextContentType? { proxy()?.textContentType ?? nil }
var isSecureTextEntry: Bool? { proxy()?.isSecureTextEntry }
func insertText(_ text: String) { proxy()?.insertText(text) }
func deleteBackward() { proxy()?.deleteBackward() }
func adjustTextPosition(byCharacterOffset offset: Int) { proxy()?.adjustTextPosition(byCharacterOffset: offset) }
}
/// Everything the keys do, apart from drawing: multitouch with rollover, shift, pages,
/// long-press accents, spacebar cursor trackpad, cautious autocorrect, double-space period,
/// auto-capitalization and accelerating delete. Touches are identified by any stable integer
/// so tests can drive it without `UITouch`.
@MainActor
final class FullKeyboardEngine {
struct ActiveTouch: Equatable {
var key: KeyboardKey
var startPoint: CGPoint
var point: CGPoint
/// A character that already went in because another key went down after it.
var committed: Bool
/// The page a 123/ABC press came from; releasing over a character returns there.
var returnLayer: KeyboardLayer?
/// A 123, #+= or shift press slid onto this character key: it shows the key's pop-up,
/// and letting go types it (`slideTarget`), as on Apple's keyboard.
var slideKey: KeyboardKey?
/// True once such a press slid onto a character key, so letting go back on the page
/// key is a slide taken back rather than a tap.
var hasSlid = false
/// True once a spacebar press crosses `trackpadActivationDistance`.
var isTrackpad = false
/// True once a spacebar press slides before its hold (`holdSpace`) while it switches
/// language: letting go far enough along switches instead of typing a space.
var isLanguageSwipe = false
var trackpadAnchorX: CGFloat = 0
/// Characters the trackpad has moved the cursor, for Record typing logs.
var trackpadOffset = 0
/// True when the likelier next letter claimed this touch from the key under it; a
/// small move then keeps it rather than sliding back (`letterReach`).
var claimed = false
/// Where the finger went down, before a strip touch moved onto row 1 (Adaptive touch).
var downPoint: CGPoint = .zero
}
/// Apple's callout replaces the key's pop-up about 382 ms after it appeared (iOS 27.2, 60 fps
/// recordings); Hex's took 420 at 0.4 s, a frame later to show.
static let accentHoldDelay: TimeInterval = 0.36
/// How far below the held key's middle a finger lets go to close the callout untyped.
static let accentCancelDrop: CGFloat = 57.5
static let accentSlop: CGFloat = 10
static let trackpadActivationDistance: CGFloat = 10
static let trackpadPointsPerCharacter: CGFloat = 7.5
/// Holding the space bar this long without sliding starts the trackpad, as Apple's does;
/// letting go then types no space. With two or more languages a slide moves the cursor
/// only after it; a slide before it is a language swipe.
/// Apple's space bar turns into the trackpad about 363 ms after touch-down (iOS 27.2, 60 fps).
static let trackpadHoldDelay: TimeInterval = 0.35
/// A finger on the strip's lower half this soon after a key went down or came up (or
/// while one is down) types the row-1 key below it (`takesStripTouch(at:)`).
static let stripTouchGuard: TimeInterval = 0.25
/// How far along the space bar a language swipe must go; a shorter one types a space.
static let languageSwipeDistance: CGFloat = 24
private(set) var shift = ShiftStateMachine()
private(set) var layerState = KeyboardLayerState()
private(set) var field: KeyboardFieldKind = .text
private(set) var touches: [Int: ActiveTouch] = [:]
/// Touch ids in the order they went down.
private var touchOrder: [Int] = []
private var lastSpaceAt: TimeInterval?
/// The text before the cursor just after a suggestion typed its word and a space. A space
/// typed next, while the text is still that, is swallowed, as on Apple's keyboard.
private var suggestionSpaceBefore: String?
/// When a key last went down or came up, for `takesStripTouch(at:)`.
private var lastKeyTouchAt: TimeInterval?
/// Where each letter of the word being typed was tapped (nil for one that came another
/// way, such as an accent), for Spatial autocorrect.
private var tokenTaps: [CGPoint?] = []
/// Spatial autocorrect: where you tend to tap each letter, from its centre (Learn how I type).
var tapOffsets: [Character: CGVector] = [:] {
didSet { if tapOffsets != oldValue { syncDecoderKeys() } }
}
/// The layout language's word list, which lets the likelier of two neighbouring letters
/// claim a few points of the other's touch target, as Apple's keyboard does ("th" then a
/// tap on the e/d border types e). Used only while autocorrect is on in a text field
/// that allows it; nil keeps every target fixed.
var letterOdds: TypingDecoderModel? {
didSet {
if letterOdds !== oldValue {
continuationCache = nil
wordStartMass = nil
autocorrect.contextModel = letterOdds
}
}
}
/// How far past its own target a likelier letter may reach, in points.
var letterReach: CGFloat = 5
/// Nats of odds (ln of how many times likelier) a letter needs for each point it claims.
var letterOddsPerPoint = 0.8
private var continuationCache: (prefix: [UInt8], mass: [Double]?)?
/// The first letters' frequencies, the costliest to add up (every word), kept apart.
private var wordStartMass: [Double]??
private(set) var activeAccentMenu: AccentMenu?
private var activeAccentTouchID: Int?
/// The callout cell under the finger when it opened. Apple's callout keeps the key's own
/// character selected until the finger moves into another cell (iOS 27.2: held and let go
/// at the screen's edge, where the callout shifts so another cell is under the finger, it
/// types the key's own); then the cell under the finger is the one.
private var accentOpenCell: Int?
private(set) var suggestions: [KeyboardSuggestion] = []
let target: KeyboardTextTarget
let autocorrect: AutocorrectEngine
/// Learn how I type (Experiments); nil while off.
var journal: TypingJournal? {
didSet { syncLearning() }
}
private var learning: TypingJournal? {
guard let journal, TypingJournal.allows(field: field, target: target) else { return nil }
return journal
}
/// Record typing logs (Experiments); nil while it is off, in fields it skips, and in the
/// app's fixtures. Every call through it is skipped (arguments too) while nil.
var typingLog: TypingLogger?
/// Adaptive touch (Experiments): personal key centres for Spatial autocorrect, in place of
/// `tapOffsets`, learned from taps with evidence of what you meant. Nil while off.
var adaptiveTouch: AdaptiveTouch? {
didSet {
guard adaptiveTouch !== oldValue else { return }
adaptiveTouch?.onChange = { [weak self] in self?.syncDecoderKeys() }
adaptiveTouch?.clock = { [weak self] in self?.clock() ?? CACurrentMediaTime() }
adaptiveTouch?.forget()
syncAdaptiveField()
syncDecoderKeys()
}
}
/// Adaptive touch where this field lets it learn (plain text that allows autocorrect).
private var adaptiveLearning: AdaptiveTouch?
private func syncAdaptiveField() {
adaptiveLearning = adaptiveTouch.flatMap { AdaptiveTouch.allows(field: field, target: target) ? $0 : nil }
}
var width: CGFloat = 375 { didSet { if width != oldValue { rebuildLayout() } } }
var orientation: KeyboardOrientation = .portrait { didSet { if orientation != oldValue { rebuildLayout() } } }
var showsGlobe = false { didSet { if showsGlobe != oldValue { rebuildLayout() } } }
/// Settings › Keyboard › Emoji, in fields that take emoji.
var showsEmoji = false { didSet { if showsEmoji != oldValue { rebuildLayout() } } }
var horizontalInset: CGFloat = 0 { didSet { if horizontalInset != oldValue { rebuildLayout() } } }
/// An iPhone, or the iPhone Duo folded or unfolded (`KeyboardPosture`).
var posture: KeyboardPosture = .phone { didSet { if posture != oldValue { rebuildLayout() } } }
/// Which iPhone key plane to draw (`KeyboardPhoneSize`), from the screen's size.
var phoneSize: KeyboardPhoneSize = .regular { didSet { if phoneSize != oldValue { rebuildLayout() } } }
/// How far iOS set the keyboard in from each side of the screen (the Duo).
var windowInset: CGFloat = 0 { didSet { if windowInset != oldValue { rebuildLayout() } } }
/// Settings › Keyboard › Hex key: the strip's trailing (Right) or leading (Left) end.
var hexPlacement: HexKeyPlacement = .stripTrailing {
didSet { if hexPlacement != oldValue { rebuildLayout(cause: "hex_placement") } }
}
/// The letters, accents and spelling dictionary (Settings › Keyboard › Languages).
var language: KeyboardLanguage = .english {
didSet {
guard language != oldValue else { return }
cancelAllTouches()
autocorrect.setLanguage(language)
rebuildLayout(cause: "language")
refreshSuggestions()
}
}
/// The letters and long presses on the keys when not `language`'s (`KeyboardLanguageList.layout`).
var layoutLanguage: KeyboardLanguage? {
didSet { if layoutLanguage != oldValue { cancelAllTouches(); rebuildLayout(cause: "language") } }
}
/// How the keys' language arranges its letters (Settings › Keyboard › Languages › Layout);
/// nil, or one it doesn't offer, is its own.
var letterLayout: KeyboardLetterLayout? {
didSet { if letterLayout != oldValue { cancelAllTouches(); rebuildLayout(cause: "letters") } }
}
/// Bilingual autocorrect's second language on these keys (`KeyboardLanguageList.partner`),
/// or nil.
var partnerLanguage: KeyboardLanguage? {
get { autocorrect.partner?.language }
set {
guard newValue != autocorrect.partner?.language else { return }
autocorrect.setPartner(newValue)
refreshSuggestions()
}
}
/// Settings › Keyboard › Languages has two or more: a quick swipe along the space bar
/// switches language (`onLanguageSwipe`) and the cursor slides once the space bar is held
/// (`trackpadHoldDelay`), as on SwiftKey. With one, any slide moves the cursor at once.
var switchesLanguage = false
private(set) var layout = KeyboardLayout(width: 375)
var clock: () -> TimeInterval = { CACurrentMediaTime() }
/// Called whenever something visible changed (page, shift, pressed keys, suggestions, accents).
var onChange: (() -> Void)?
/// Called after the keys typed or deleted anything.
var onEdit: (() -> Void)?
/// Called when an accent cell or spacebar trackpad step ticks, for selection haptics.
var onSelectionStep: (() -> Void)?
/// Called just before the return key types its line break, so a send can be noticed.
var onReturn: (() -> Void)?
/// Called when the emoji key is let go.
var onEmojiKey: (() -> Void)?
/// Called when a swipe along the space bar switches language: 1 for the next (a swipe
/// to the left), -1 for the one before.
var onLanguageSwipe: ((Int) -> Void)?
init(target: KeyboardTextTarget, autocorrect: AutocorrectEngine? = nil) {
self.target = target
self.autocorrect = autocorrect ?? AutocorrectEngine()
rebuildLayout()
}
var isUppercase: Bool { layerState.layer == .letters && shift.isUppercase }
var isTrackpadActive: Bool {
touches.values.contains(where: \.isTrackpad)
}
/// Settings › Key pop-ups. Off: pressed keys only darken, as with iOS's Character Preview off.
var showsKeyPopups = true
/// Settings › Keyboard › Auto-capitalization. Off, shift never turns itself on, whatever
/// the field's `autocapitalizationType` asks, and autocorrect and swipe typing add no
/// capitals (`AutocorrectEngine.addsCapitals`). Shift and caps lock still work.
var autoCapitalizes = true {
didSet {
guard autoCapitalizes != oldValue else { return }
autocorrect.addsCapitals = autoCapitalizes
refreshAutoCapitalization()
refreshSuggestions()
}
}
/// What the field asks for, or no automatic capitals with Auto-capitalization off.
var autoCapitalizationMode: AutoCapitalization.Mode {
autoCapitalizes ? AutoCapitalization.Mode(target.autocapitalizationType) : .none
}
/// The key whose balloon shows: the newest character still held, or the one a 123, #+= or
/// shift press slid onto (hidden while an accent menu is up).
var previewKey: KeyboardKey? {
guard activeAccentMenu == nil else { return nil }
for id in touchOrder.reversed() {
guard let touch = touches[id] else { continue }
if let slid = touch.slideKey { return slid }
if touch.key.character != nil, !touch.committed {
return touch.key
}
}
return nil
}
var pressedKinds: Set<KeyKind> {
Set(touches.values.flatMap { touch in [touch.key.kind] + (touch.slideKey.map { [$0.kind] } ?? []) })
}
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
}
// MARK: - Field
/// Call when the keyboard appears or the field changes.
func configureForField() {
switch KeyboardFieldRouting.route(for: target.keyboardType) {
case .keys(let kind, let initialLayer):
field = kind
layerState.reset(to: initialLayer)
case .handOff:
field = .text
layerState.reset(to: .numbers)
}
shift.reset()
lastSpaceAt = nil
suggestionSpaceBefore = nil
tokenTaps.removeAll()
swipedWord = nil
finishedWord = nil
autocorrect.clearPendingUndo()
adaptiveTouch?.beginSession()
syncAdaptiveField()
cancelAllTouches()
rebuildLayout(cause: "field")
refreshAutoCapitalization()
refreshSuggestions()
}
/// Call after the text or the cursor changed outside these keys.
func textDidChange() {
lastSpaceAt = nil
// Hosts that report the keys' own edits leave the text as the suggestion left it.
if target.documentContextBeforeInput != suggestionSpaceBefore {
suggestionSpaceBefore = nil
}
tokenTaps.removeAll()
swipedWord = nil
finishedWord = nil
autocorrect.clearPendingUndo()
syncAdaptiveField()
adaptiveLearning?.hostChanged(before: target.documentContextBeforeInput)
refreshAutoCapitalization()
refreshSuggestions()
}
func refreshAutoCapitalization() {
let before = shift
shift.refreshAutoCapitalization(
AutoCapitalization.shouldCapitalize(
mode: autoCapitalizationMode,
before: target.documentContextBeforeInput,
hasText: target.hasText
)
)
if shift != before {
logState("auto")
onChange?()
}
}
func refreshSuggestions() {
syncLearning()
let next: [KeyboardSuggestion]
if let swipeSuggestions {
next = swipeSuggestions
} else {
autocorrect.decoderTaps = currentTokenTaps
next = autocorrect.suggestions(
beforeInput: target.documentContextBeforeInput,
field: field,
autocorrectionType: target.autocorrectionType,
predictionModel: letterOdds,
capitalizesNext: AutoCapitalization.shouldCapitalize(
mode: autoCapitalizationMode,
before: target.documentContextBeforeInput,
hasText: target.hasText
)
)
}
if next != suggestions {
suggestions = next
typingLog?.suggestionsChanged(next)
onChange?()
}
}
func acceptSuggestion(_ suggestion: KeyboardSuggestion) {
// A queued tap on a prediction must not replace a word started in the meantime.
if suggestion.kind == .nextWord,
AutocorrectEngine.currentToken(in: target.documentContextBeforeInput) != nil
|| target.documentContextBeforeInput.map({ !($0.isEmpty || $0.last == " " || $0.last!.isNewline) }) == true
|| !suggestions.contains(suggestion) { return }
// The strip shows a swipe's readings: mid-swipe they're only shown, after it one
// takes the swiped word's place.
if isSwiping { return }
if suggestion.kind == .swiped {
replaceSwipedWord(with: suggestion.replacement)
return
}
// What the suggestion takes the place of: the word being typed, or a text
// replacement's shortcut, which may hold digits or punctuation ("ad2").
let before = target.documentContextBeforeInput
let replacing = autocorrect.textReplacement(before: before)?.typed ?? AutocorrectEngine.currentToken(in: before)
if let log = typingLog {
let token = replacing ?? ""
log.edit("suggestion", [
TypingLogField("slot", suggestions.firstIndex(of: suggestion).map { .int($0) } ?? .null),
TypingLogField("text", .string(suggestion.displayText)),
TypingLogField("deleted", .string(token)),
TypingLogField("inserted", .string(suggestion.replacement + " ")),
TypingLogField("candidates", TypingLogger.candidates(suggestions)),
])
}
if let journal = learning {
let token = replacing
if let token, !token.isEmpty, token != suggestion.replacement {
journal.record(.picked, typed: token, final: suggestion.replacement)
} else {
journal.didFinish(word: token)
// Tapping what you typed over the autocorrection space would apply turns it down.
if suggestion.kind == .keepLiteral, let token,
let autoFix = suggestions.first(where: \.willAutoReplace) {
journal.record(.keptTyped, typed: autoFix.replacement, final: token)
}
}
}
if let token = replacing {
adaptiveLearning?.picked(
token: token, replacement: suggestion.replacement,
overCorrection: suggestion.kind == .keepLiteral && suggestions.contains(where: \.willAutoReplace),
before: target.documentContextBeforeInput
)
for _ in 0..<token.count {
target.deleteBackward()
}
if suggestion.kind == .keepLiteral {
autocorrect.suppressAndKeep(token)
}
}
autocorrect.clearPendingUndo()
insert(suggestion.replacement + " ", source: nil)
suggestionSpaceBefore = target.documentContextBeforeInput
typingLog?.context("suggestion")
}
/// Types an emoji from the emoji panel: no spacing or autocorrect around it.
func insertEmoji(_ text: String, source: String) {
autocorrect.clearPendingUndo()
adaptiveTouch?.forget()
learning?.didFinish(word: AutocorrectEngine.currentToken(in: target.documentContextBeforeInput))
insert(text, source: source)
}
// MARK: - Touches
/// 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.
func key(at point: CGPoint) -> KeyboardKey? {
let point = pointOnKeys(point)
guard let key = layout.hitTest(point) else { return nil }
guard key.character != nil, 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.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
}
/// The word list's next-letter frequencies after the word being typed, or nil when they
/// can't help: inside something that isn't a word, or a word the list doesn't have.
private func continuationMass(_ model: TypingDecoderModel) -> [Double]? {
let before = target.documentContextBeforeInput ?? ""
let token = AutocorrectEngine.currentToken(in: before)
if token == nil, let last = before.last, last.isLetter || last.isNumber || last == "'" { return nil }
guard let prefix = model.encode((token ?? "").lowercased()) else { return nil }
if prefix.isEmpty {
if let cached = wordStartMass { return cached }
let mass = model.continuations(after: [])
wordStartMass = .some(mass)
return mass
}
if let cache = continuationCache, cache.prefix == prefix { return cache.mass }
let mass = model.continuations(after: prefix)
continuationCache = (prefix, mass)
return mass
}
/// 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.
private func pointOnKeys(_ point: CGPoint) -> CGPoint {
let top = layout.touchAreaFrame.minY
return point.y < top ? CGPoint(x: point.x, y: top) : point
}
private static func distance(from point: CGPoint, to rect: CGRect) -> CGFloat {
let dx = max(rect.minX - point.x, 0, point.x - rect.maxX)
let dy = max(rect.minY - point.y, 0, point.y - rect.maxY)
return (dx * dx + dy * dy).squareRoot()
}
/// 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
}
func touchBegan(id: Int, at point: CGPoint) {
let downPoint = point
let point = pointOnKeys(point)
guard let key = key(at: point) else { return }
lastKeyTouchAt = clock()
// Rollover: a new key going down commits any character or accent picker still held.
for other in touchOrder {
guard var touch = touches[other], touch.key.character != nil, !touch.committed else { continue }
if activeAccentTouchID == other, let menu = activeAccentMenu {
typingLog?.ui("accent_pick", [
TypingLogField("id", .int(other)),
TypingLogField("index", .int(menu.selectedIndex)),
TypingLogField("chosen", .string(menu.selectedOption)),
TypingLogField("rollover", .bool(true)),
])
commitAccentSelection(menu.selectedOption)
activeAccentMenu = nil
activeAccentTouchID = nil
} else {
commitCharacter(touch.key, at: touch.startPoint, down: touch.downPoint)
}
touch.committed = true
touches[other] = touch
}
var touch = ActiveTouch(
key: key,
startPoint: point,
point: point,
committed: false,
returnLayer: nil
)
touch.claimed = !key.hitFrame.contains(point)
touch.downPoint = downPoint
if key.character != nil {
learning?.recordTap(on: key, at: point)
}
switch key.kind {
case .shift:
shift.shiftDown(now: clock())
logState("shift_key")
case .delete:
deleteBackward(allowAutocorrectUndo: true)
case .layerSwitch(let next):
touch.returnLayer = layerState.layer
layerState.switchTo(next)
if next == .letters { shift.reset() }
rebuildLayout(cause: "layer_key")
if next == .letters { refreshAutoCapitalization() }
case .character, .space, .returnKey, .hex, .globe, .emoji:
break
}
touches[id] = touch
touchOrder.append(id)
onChange?()
}
@discardableResult
func openAccentMenuIfEligible(forTouch id: Int) -> Bool {
guard activeAccentMenu == nil,
let touch = touches[id],
!touch.committed,
let base = touch.key.character else { return false }
let dx = touch.point.x - touch.startPoint.x
let dy = touch.point.y - touch.startPoint.y
guard (dx * dx + dy * dy).squareRoot() <= Self.accentSlop else { return false }
guard let options = AccentCatalog.options(for: base, uppercase: isUppercase, language: layout.language, letters: layout.letters),
options.count > 1 else { return false }
if let arrangement = AccentCatalog.arrangement(for: base, uppercase: isUppercase, language: layout.language, letters: layout.letters) {
activeAccentMenu = AccentMenu.build(
for: touch.key.frame,
arrangement: arrangement,
in: layout.width,
horizontalInset: layout.horizontalInset
)
} else {
activeAccentMenu = AccentMenu.build(
for: touch.key.frame,
rawOptions: options,
in: layout.width,
horizontalInset: layout.horizontalInset
)
}
activeAccentTouchID = id
accentOpenCell = activeAccentMenu?.index(for: touch.point)
typingLog?.ui("accent_open", [
TypingLogField("id", .int(id)),
TypingLogField("key", .string(TypingLogger.keyID(touch.key.kind))),
TypingLogField("options", .strings(activeAccentMenu?.options ?? [])),
])
onChange?()
return true
}
/// Fixtures: select a callout option as sliding to it does.
func debugSelectAccent(_ index: Int) {
guard var menu = activeAccentMenu, menu.cellFrames.indices.contains(index), index != menu.selectedIndex else { return }
menu.selectedIndex = index
activeAccentMenu = menu
accentOpenCell = nil
onSelectionStep?()
onChange?()
}
/// The callout's selection for a finger at `point` (`accentOpenCell`).
private func accentIndex(in menu: AccentMenu, at point: CGPoint) -> Int {
let under = menu.index(for: point)
guard let open = accentOpenCell else { return under }
if under == open { return menu.selectedIndex }
accentOpenCell = nil
return under
}
func touchMoved(id: Int, to point: CGPoint) {
guard var touch = touches[id] else { return }
touch.point = point
// 1. Active long-press accent picker: horizontal drag selects the alternate character.
if activeAccentTouchID == id, var menu = activeAccentMenu {
let nextIndex = accentIndex(in: menu, at: point)
touches[id] = touch
if nextIndex != menu.selectedIndex {
menu.selectedIndex = nextIndex
activeAccentMenu = menu
onSelectionStep?()
onChange?()
}
return
}
// 2. Spacebar cursor trackpad: horizontal drag >= 10 pt slides the text caret. With two
// or more languages only once the space bar was held (`holdSpace`): a slide before
// that is a language swipe.
if touch.key.kind == .space {
if touch.isLanguageSwipe {
touches[id] = touch
return
}
if !touch.isTrackpad, abs(point.x - touch.startPoint.x) >= Self.trackpadActivationDistance {
if switchesLanguage {
touch.isLanguageSwipe = true
touches[id] = touch
return
}
startTrackpad(&touch, id: id, anchorX: point.x, hold: false)
return
}
if touch.isTrackpad {
let delta = point.x - touch.trackpadAnchorX
let rawSteps = Int((delta / Self.trackpadPointsPerCharacter).rounded(.towardZero))
if rawSteps != 0 {
touch.trackpadAnchorX += CGFloat(rawSteps) * Self.trackpadPointsPerCharacter
touch.trackpadOffset += rawSteps
touches[id] = touch
typingLog?.edit("cursor", [TypingLogField("offset", .int(rawSteps))])
tokenTaps.removeAll()
finishedWord = nil
adaptiveTouch?.forget()
suggestionSpaceBefore = nil
target.adjustTextPosition(byCharacterOffset: rawSteps)
onSelectionStep?()
refreshAutoCapitalization()
refreshSuggestions()
return
}
touches[id] = touch
return
}
}
// 3. Holding 123, #+= or shift and sliding: the character key under the finger shows its
// pop-up; over any other key, or off the keys, none does. Never a swipe: swipes start on
// letters only.
if Self.slidesToType(touch) {
let next = slideTarget(for: touch, at: point)
if next?.kind != touch.slideKey?.kind {
touch.slideKey = next
if next != nil { touch.hasSlid = true }
touches[id] = touch
onChange?()
return
}
touches[id] = touch
return
}
// 4. Sliding across character keys updates the active key and re-anchors the hold timer.
// A letter the next-letter odds claimed keeps the touch while it stays within reach.
if touch.key.character != nil, !touch.committed,
let under = layout.hitTest(point), under.character != nil, under.kind != touch.key.kind,
!(touch.claimed && Self.distance(from: point, to: touch.key.hitFrame) < letterReach) {
touch.claimed = false
touch.key = under
touch.startPoint = point
touches[id] = touch
onChange?()
return
}
touches[id] = touch
}
/// The space bar held `trackpadHoldDelay` without sliding: the trackpad starts where the
/// finger is, as Apple's does, so letting go types no space (a press held to move the
/// cursor used to type one beside the space already there).
func holdSpace(forTouch id: Int) {
guard var touch = touches[id], touch.key.kind == .space, !touch.isTrackpad, !touch.isLanguageSwipe else { return }
startTrackpad(&touch, id: id, anchorX: touch.point.x, hold: true)
}
private func startTrackpad(_ touch: inout ActiveTouch, id: Int, anchorX: CGFloat, hold: Bool) {
touch.isTrackpad = true
touch.trackpadAnchorX = anchorX
lastSpaceAt = nil
swipedWord = nil
autocorrect.clearPendingUndo()
touches[id] = touch
var fields = [TypingLogField("id", .int(id))]
if hold { fields.append(TypingLogField("hold", .bool(true))) }
typingLog?.ui("trackpad_start", fields)
onSelectionStep?()
onChange?()
}
func touchEnded(id: Int, at point: CGPoint) {
guard var touch = touches.removeValue(forKey: id) else { return }
touchOrder.removeAll { $0 == id }
lastKeyTouchAt = clock()
touch.point = point
if activeAccentTouchID == id, let menu = activeAccentMenu {
let index = accentIndex(in: menu, at: point)
accentOpenCell = nil
let chosen = menu.options[index]
activeAccentMenu = nil
activeAccentTouchID = nil
// Let go well below the held key, the callout closes with nothing typed, as Apple's
// does (iOS 27.2: 55 pt down types, 60 pt cancels; any distance up types).
if point.y - menu.keyFrame.midY > Self.accentCancelDrop {
typingLog?.ui("accent_cancel", [TypingLogField("id", .int(id))])
onChange?()
return
}
if !touch.committed {
typingLog?.ui("accent_pick", [
TypingLogField("id", .int(id)),
TypingLogField("index", .int(index)),
TypingLogField("chosen", .string(chosen)),
])
commitAccentSelection(chosen)
}
onChange?()
return
}
switch touch.key.kind {
case .character:
if !touch.committed {
commitCharacter(touch.key, at: touch.startPoint, down: touch.downPoint)
}
case .shift:
// Hold shift, slide to a letter, let go: a capital, as on Apple's keyboard.
if let under = slideTarget(for: touch, at: point) {
commitCharacter(under, at: point)
}
shift.shiftUp()
logState("shift_key")
case .space:
if touch.isTrackpad {
typingLog?.ui("trackpad_end", [
TypingLogField("id", .int(id)),
TypingLogField("offset", .int(touch.trackpadOffset)),
])
typingLog?.context("trackpad")
} else if touch.isLanguageSwipe, let step = Self.languageSwipeStep(from: touch.startPoint, to: point) {
typingLog?.ui("language_swipe", [
TypingLogField("id", .int(id)),
TypingLogField("step", .int(step)),
])
onLanguageSwipe?(step)
} else {
typeSpace(at: CGPoint(x: layout.unsplitX(touch.startPoint.x), y: touch.startPoint.y))
}
case .returnKey:
if target.enablesReturnKeyAutomatically == true && !target.hasText {
typingLog?.edit("return", [
TypingLogField("returnKeyType", .string(TypingLogTraits.name(target.returnKeyType ?? .default))),
TypingLogField("ignored", .bool(true)),
])
break
}
learning?.didFinish(word: AutocorrectEngine.currentToken(in: target.documentContextBeforeInput))
autocorrect.clearPendingUndo()
typingLog?.edit("return", [
TypingLogField("inserted", .string("\n")),
TypingLogField("returnKeyType", .string(TypingLogTraits.name(target.returnKeyType ?? .default))),
TypingLogField("send", .bool(target.returnKeyType == .send)),
])
onReturn?()
// A text replacement goes in on return too, as on Apple's keyboard; the new line
// goes in on its own, so a field's return (Send, Search) still sees just "\n".
_ = applyTextReplacement(ending: "")
insert("\n", source: nil)
typingLog?.context("return")
case .layerSwitch:
// Hold 123, slide to a key, let go: type it and go back. Back on the page key after
// a slide: nothing, and back too. Off the keys: nothing, and the page stays.
if let returnLayer = touch.returnLayer {
if let under = slideTarget(for: touch, at: point), let character = under.character {
if !correctWord(endingWith: character) {
autocorrect.clearPendingUndo()
insert(character, source: "slide")
}
layerState.switchTo(returnLayer)
rebuildLayout(cause: "layer_slide")
refreshAutoCapitalization()
} else if touch.hasSlid, layout.hitTest(point)?.kind == .layerSwitch(returnLayer) {
layerState.switchTo(returnLayer)
if returnLayer == .letters { shift.reset() }
rebuildLayout(cause: "layer_slide_back")
if returnLayer == .letters { refreshAutoCapitalization() }
}
}
case .emoji:
onEmojiKey?()
case .delete, .hex, .globe:
break
}
onChange?()
}
/// A press that types the character key it slides onto: 123, ABC and #+= (with a page to
/// go back to), and shift.
private static func slidesToType(_ touch: ActiveTouch) -> Bool {
touch.returnLayer != nil || touch.key.kind == .shift
}
/// The character key under `point` that such a press types on letting go, or nil over
/// any other key or off the keys.
private func slideTarget(for touch: ActiveTouch, at point: CGPoint) -> KeyboardKey? {
guard Self.slidesToType(touch), let under = layout.hitTest(point),
under.character != nil, under.kind != touch.key.kind else { return nil }
return under
}
/// A language swipe's direction, or nil when it was too short or more up than along.
private static func languageSwipeStep(from start: CGPoint, to end: CGPoint) -> Int? {
let dx = end.x - start.x
guard abs(dx) >= languageSwipeDistance, abs(dx) >= abs(end.y - start.y) else { return nil }
return dx < 0 ? 1 : -1
}
func touchCancelled(id: Int) {
if activeAccentTouchID == id {
activeAccentMenu = nil
activeAccentTouchID = nil
typingLog?.ui("accent_cancel", [TypingLogField("id", .int(id))])
}
guard touches.removeValue(forKey: id) != nil else { return }
touchOrder.removeAll { $0 == id }
onChange?()
}
func cancelAllTouches() {
let hadTouches = !touches.isEmpty || activeAccentMenu != nil || isSwiping
touches.removeAll()
touchOrder.removeAll()
isSwiping = false
swipePreview = nil
activeAccentMenu = nil
activeAccentTouchID = nil
if shift.isHeld {
shift.shiftUp()
logState("cancel")
}
if hadTouches { onChange?() }
}
// MARK: - Delete
/// The delete key repeats after 360 ms, accelerates at 850 ms, and removes whole words after 1.4 s.
static let repeatDelay: TimeInterval = 0.36
static let repeatInterval: TimeInterval = 0.075
static let fastRepeatAfter: TimeInterval = 0.85
static let wordDeleteAfter: TimeInterval = 1.4
func repeatDelete(heldFor: TimeInterval) {
autocorrect.clearPendingUndo()
if heldFor >= Self.wordDeleteAfter {
deleteWordBackward()
} else if heldFor >= Self.fastRepeatAfter {
deleteBackward(allowAutocorrectUndo: false, source: "repeat_fast")
deleteBackward(allowAutocorrectUndo: false, source: "repeat_fast")
} else {
deleteBackward(allowAutocorrectUndo: false, source: "repeat")
}
}
func deleteBackward(allowAutocorrectUndo: Bool = true, source: String = "key") {
// Right after a swipe, delete takes back its whole word (and the space it added).
let swiped = swipedWord
swipedWord = nil
if allowAutocorrectUndo, let swiped, deleteSwipedWord(swiped) { return }
if allowAutocorrectUndo,
let undo = autocorrect.consumePendingUndo(),
let before = target.documentContextBeforeInput,
before.hasSuffix(undo.insertedText) {
typingLog?.edit("autocorrect_undo", [
TypingLogField("deleted", .string(undo.insertedText)),
TypingLogField("inserted", .string(undo.original)),
])
for _ in 0..<undo.insertedText.count {
target.deleteBackward()
}
tokenTaps.removeAll()
finishedWord = nil
adaptiveLearning?.undone(original: undo.original)
target.insertText(undo.original)
for change in undo.changes {
autocorrect.suppressAndKeep(change.from)
learning?.record(.undone, typed: change.to, final: change.from)
}
lastSpaceAt = nil
didEdit()
return
}
autocorrect.clearPendingUndo()
learning?.willErase(word: AutocorrectEngine.currentToken(in: target.documentContextBeforeInput))
adaptiveLearning?.willDelete(before: target.documentContextBeforeInput)
if let log = typingLog {
let deleted = target.documentContextBeforeInput?.last.map { String($0) }
log.edit("delete", [
deleted.map { TypingLogField("deleted", .string($0)) } ?? TypingLogField("deletedCount", .int(1)),
TypingLogField("src", .string(source)),
])
}
if let last = target.documentContextBeforeInput?.last, last.isLetter || last == "'", !tokenTaps.isEmpty {
tokenTaps.removeLast()
} else {
tokenTaps.removeAll()
}
target.deleteBackward()
lastSpaceAt = nil
didEdit()
}
func deleteWordBackward() {
swipedWord = nil
autocorrect.clearPendingUndo()
adaptiveTouch?.forget()
guard let before = target.documentContextBeforeInput, !before.isEmpty else {
deleteBackward(allowAutocorrectUndo: false, source: "repeat_word")
return
}
learning?.willErase(word: AutocorrectEngine.currentToken(in: before))
var count = 0
var index = before.endIndex
while index > before.startIndex, before[before.index(before: index)].isWhitespace {
index = before.index(before: index)
count += 1
}
while index > before.startIndex, !before[before.index(before: index)].isWhitespace {
index = before.index(before: index)
count += 1
}
tokenTaps.removeAll()
typingLog?.edit("delete_word", [
TypingLogField("deleted", .string(String(before.suffix(max(1, count))))),
TypingLogField("src", .string("repeat_word")),
])
for _ in 0..<max(1, count) {
target.deleteBackward()
}
lastSpaceAt = nil
didEdit()
typingLog?.context("delete_word")
}
// MARK: - Typing
/// `down`: where a tap's finger went down, for Adaptive touch (nil for a slide from shift).
private func commitCharacter(_ key: KeyboardKey, at point: CGPoint, down: CGPoint? = nil) {
guard let character = key.character else { return }
spaceAfterSwipedWord(before: character)
let text = layerState.layer == .letters && shift.isUppercase ? character.uppercased() : character
if layerState.layer == .letters {
shift.didType()
}
if !correctWord(endingWith: text) {
autocorrect.clearPendingUndo()
insert(text, source: "key", at: point)
}
if let down, layerState.layer == .letters, character.first?.isLetter == true {
adaptiveLearning?.typed(character, down: down, slid: pointOnKeys(down) != point)
}
logState("typed")
}
/// Punctuation that closes a word, as Apple's keyboard treats it: , . ! ? ; : ) and ".
static let wordEnders: Set<Character> = [",", ".", "!", "?", ";", ":", ")", "\""]
/// Punctuation right after a word corrects it as space does ("teh," becomes "the,"), and
/// goes in after the fix. Backspace right after takes back both, as after a space ("teh").
/// False when there is nothing to fix: the caller types the mark as usual.
private func correctWord(endingWith text: String) -> Bool {
guard text.count == 1, let mark = text.first, Self.wordEnders.contains(mark), swipedWord == nil else { return false }
if applyTextReplacement(ending: text) { return true }
let before = target.documentContextBeforeInput
guard let last = before?.last, last.isLetter, let token = AutocorrectEngine.currentToken(in: before) else { return false }
// "approx." and "etc." are abbreviations, not typos.
if mark == ".", AutocorrectEngine.abbreviations.contains(token.lowercased()) { return false }
learning?.didFinish(word: token)
syncLearning()
autocorrect.decoderTaps = currentTokenTaps
guard finishWord(ending: text) else { return false }
finishedWord = nil
lastSpaceAt = nil
layerState.didInsert(text)
rebuildLayoutIfNeeded()
didEdit()
return true
}
/// Puts your text replacement in place of its shortcut as `ending` (a space or a closing
/// mark; nothing for return, whose new line follows) finishes it, as Apple's keyboard does
/// (black box, iOS 27.2), whatever autocorrect would do. There, delete right after takes
/// only `ending` back, never the replacement, and keeping the shortcut never turns it
/// off: so no undo is recorded and nothing is learned. False, typing nothing, when no
/// shortcut ends the text.
private func applyTextReplacement(ending: String) -> Bool {
let before = target.documentContextBeforeInput
guard field == .text, target.autocorrectionType != .no,
let replaced = autocorrect.textReplacement(before: before) else { return false }
adaptiveLearning?.finish(token: replaced.typed, corrected: replaced.replacement, before: before)
for _ in 0..<replaced.typed.count {
target.deleteBackward()
}
typingLog?.edit("autocorrect", [
TypingLogField("from", .string(replaced.typed)),
TypingLogField("to", .string(replaced.replacement)),
TypingLogField("deleted", .string(replaced.typed)),
TypingLogField("inserted", .string(replaced.replacement + ending)),
TypingLogField("textReplacement", .bool(true)),
])
target.insertText(replaced.replacement + ending)
autocorrect.clearPendingUndo()
tokenTaps.removeAll()
swipedWord = nil
finishedWord = nil
lastSpaceAt = nil
layerState.didInsert(ending)
rebuildLayoutIfNeeded()
didEdit()
return true
}
/// The word a space finished, while the next one is typed: that word's space or mark may
/// still revise it ("well see" becomes "we'll see"), as Apple's keyboard decides some
/// words at the next one.
private struct FinishedWord {
/// The word as it reads now.
var text: String
/// As you typed it: one you gave an apostrophe yourself is never revised.
var typed: String
/// The text before the cursor right after its space.
var before: String
/// Where its letters were tapped, and its space: a space near the letter row's edge
/// may have been a letter, and the next word joins it (`AutocorrectEngine.merge`).
var taps: [CGPoint]?
var space: CGPoint?
}
private var finishedWord: FinishedWord?
/// Corrects the word before the cursor as `ending` (a space or a closing mark) finishes
/// it, and revises the word before it now that this one follows (`AutocorrectEngine.
/// revision`); then types `ending`. One backspace right after puts back what was typed.
/// False, typing nothing, when neither changes.
private func finishWord(ending: String) -> Bool {
let before = target.documentContextBeforeInput ?? ""
guard let token = AutocorrectEngine.currentToken(in: before) else { return false }
if let finished = finishedWord, before == finished.before + token,
let taps = finished.taps, let space = finished.space, field == .text, target.autocorrectionType != .no,
let merged = autocorrect.merge(
first: finished.typed, firstTaps: taps, space: space, second: token,
before: String(finished.before.dropLast())
) {
// Two words machine-joined teach nothing.
adaptiveTouch?.forget()
let original = finished.text + " " + token
for _ in 0..<original.count {
target.deleteBackward()
}
tokenTaps.removeAll()
typingLog?.edit("autocorrect", [
TypingLogField("from", .string(original)),
TypingLogField("to", .string(merged)),
TypingLogField("deleted", .string(original)),
TypingLogField("inserted", .string(merged + ending)),
TypingLogField("merged", .bool(true)),
])
target.insertText(merged + ending)
learning?.record(.autocorrected, typed: token, final: merged)
autocorrect.recordAppliedCorrection(
original: original, insertedText: merged + ending, changes: [.init(from: token, to: merged)]
)
swipedWord = nil
return true
}
let fix = autocorrect.replacementOnSpace(beforeInput: before, field: field, autocorrectionType: target.autocorrectionType)
let word = fix?.replacement ?? token
var revised: (from: String, to: String)?
if let finished = finishedWord, before == finished.before + token, !finished.typed.contains("'"),
let to = autocorrect.revision(
of: finished.text, before: String(finished.before.dropLast()), next: word,
field: field, autocorrectionType: target.autocorrectionType
) {
revised = (finished.text, to)
}
guard fix != nil || revised != nil else { return false }
adaptiveLearning?.finish(token: token, corrected: fix?.replacement, previousRevised: revised != nil, before: before)
let original = (revised.map { $0.from + " " } ?? "") + token
let inserted = (revised.map { $0.to + " " } ?? "") + word + ending
for _ in 0..<original.count {
target.deleteBackward()
}
tokenTaps.removeAll()
var fields = [
TypingLogField("from", .string(token)),
TypingLogField("to", .string(word)),
TypingLogField("deleted", .string(original)),
TypingLogField("inserted", .string(inserted)),
]
if ending != " " { fields.append(TypingLogField("ending", .string(ending))) }
if let revised { fields.append(TypingLogField("revised", .strings([revised.from, revised.to]))) }
typingLog?.edit("autocorrect", fields)
target.insertText(inserted)
var changes: [AutocorrectEngine.AppliedCorrection.Change] = []
if let revised {
changes.append(.init(from: revised.from, to: revised.to))
learning?.record(.autocorrected, typed: revised.from, final: revised.to)
}
if fix != nil {
changes.append(.init(from: token, to: word))
learning?.record(.autocorrected, typed: token, final: word)
}
autocorrect.recordAppliedCorrection(original: original, insertedText: inserted, changes: changes)
swipedWord = nil
return true
}
/// The taps of the word being typed when every letter came from a key, else nil.
private var currentTokenTaps: [CGPoint]? {
let taps = tokenTaps.compactMap { $0 }
return taps.count == tokenTaps.count && !taps.isEmpty ? taps : nil
}
private func commitAccentSelection(_ chosen: String) {
spaceAfterSwipedWord(before: chosen)
if layerState.layer == .letters {
shift.didType()
}
autocorrect.clearPendingUndo()
insert(chosen, source: "accent")
logState("typed")
}
/// `point` is where the space bar was tapped, as on the unsplit keyboard.
private func typeSpace(at point: CGPoint? = nil) {
// Space never autocorrects a swiped word: it is a reading, not a typo.
let afterSwipe = swipedWord != nil
swipedWord = nil
let now = clock()
if let before = suggestionSpaceBefore {
suggestionSpaceBefore = nil
if before.last == " ", before == target.documentContextBeforeInput {
// The suggestion typed this space already. It still counts as one, so a quick
// second press types ". ".
typingLog?.edit("space_swallowed", [TypingLogField("after", .string("suggestion"))])
lastSpaceAt = now
tokenTaps.removeAll()
layerState.didInsert(" ")
rebuildLayoutIfNeeded()
onChange?()
return
}
}
if DoubleSpacePeriod.shouldReplace(before: target.documentContextBeforeInput, lastSpaceAt: lastSpaceAt, now: now) {
autocorrect.clearPendingUndo()
typingLog?.edit("double_space_period", [
TypingLogField("deleted", .string(" ")),
TypingLogField("inserted", .string(". ")),
])
target.deleteBackward()
target.insertText(". ")
tokenTaps.removeAll()
finishedWord = nil
lastSpaceAt = nil
layerState.didInsert(" ")
rebuildLayoutIfNeeded()
didEdit()
typingLog?.context("space")
return
}
let token = AutocorrectEngine.currentToken(in: target.documentContextBeforeInput)
let taps = currentTokenTaps
learning?.didFinish(word: token)
syncLearning()
autocorrect.decoderTaps = taps
if !afterSwipe, applyTextReplacement(ending: " ") {
lastSpaceAt = now
typingLog?.context("space")
return
}
if !afterSwipe, finishWord(ending: " ") {
rememberFinishedWord(typed: token, taps: taps, space: point)
lastSpaceAt = now
layerState.didInsert(" ")
rebuildLayoutIfNeeded()
didEdit()
typingLog?.context("space")
return
}
autocorrect.clearPendingUndo()
insert(" ", source: "key")
if !afterSwipe, let token { rememberFinishedWord(typed: token, taps: taps, space: point) }
lastSpaceAt = now
typingLog?.context("space")
}
/// The word right before the space just typed, for the next word's revision.
private func rememberFinishedWord(typed: String?, taps: [CGPoint]?, space: CGPoint?) {
guard let typed, let before = target.documentContextBeforeInput, before.last == " ",
let word = AutocorrectEngine.currentToken(in: String(before.dropLast())) else {
finishedWord = nil
return
}
// After a split or a merge the word reads other letters than you typed ("of the" for
// "ofthe"): it is remembered as it reads, without taps.
guard word.filter({ $0 != "'" }).count == typed.count else {
finishedWord = FinishedWord(text: word, typed: word, before: before, taps: nil, space: nil)
return
}
finishedWord = FinishedWord(text: word, typed: typed, before: before, taps: taps, space: space)
}
/// `source` names the edit in typing logs; nil when the caller writes its own line.
private func insert(_ text: String, source: String?, at point: CGPoint? = nil) {
swipedWord = nil
// Anything but the next word's letters ends the chance to revise the word before.
if !(text.count == 1 && (text.first?.isLetter == true || text == "'")) {
finishedWord = nil
}
if text.count == 1, text.first?.isLetter == true {
// Spatial autocorrect reads taps as on the unsplit keyboard (`KeyboardLayout.unsplitX`).
tokenTaps.append(point.map { CGPoint(x: layout.unsplitX($0.x), y: $0.y) })
} else {
tokenTaps.removeAll()
if let adaptive = adaptiveLearning {
let before = target.documentContextBeforeInput
adaptive.finish(token: AutocorrectEngine.currentToken(in: before), before: before)
}
}
if let source {
typingLog?.edit("insert", [
TypingLogField("inserted", .string(text)),
TypingLogField("src", .string(source)),
])
}
target.insertText(text)
lastSpaceAt = nil
layerState.didInsert(text)
rebuildLayoutIfNeeded()
didEdit()
}
/// Hands what the journal learned (typos, rejected corrections) to autocorrect;
/// nothing while Learn how I type is off.
private func syncLearning() {
autocorrect.applyLearning(journal?.typingLearning ?? TypingLearning())
}
private func didEdit() {
suggestionSpaceBefore = nil
refreshAutoCapitalization()
refreshSuggestions()
onEdit?()
}
// MARK: - Swipe typing (Settings › Keyboard › Swipe typing; SwipeTyping.swift)
private(set) var swipedWord: SwipedWord?
/// From a touch becoming a swipe until it ends.
private(set) var isSwiping = false
/// What the swipe under way reads as so far, for the strip.
private(set) var swipePreview: String?
/// A letter touch became a swipe: its key no longer types.
func beginSwipe(touchID id: Int) {
guard touches.removeValue(forKey: id) != nil else { return }
touchOrder.removeAll { $0 == id }
isSwiping = true
swipePreview = nil
typingLog?.ui("swipe_start", [TypingLogField("id", .int(id))])
refreshSuggestions()
onChange?()
}
/// The swipe under way reads as `words` so far (best first): the strip shows the first
/// that may be offered.
func showSwipePreview(_ words: [String]) {
guard isSwiping else { return }
let next = swipeReadings(words).first.map { swipeCased($0, capital: swipeCapital) }
guard next != swipePreview else { return }
swipePreview = next
refreshSuggestions()
}
/// A swipe ended. `words` are its readings, best first (none when it was cancelled): the
/// first that may be offered goes in, after a space unless the text before ends in one or
/// in opening punctuation, as Apple's keyboard types a swiped word, with a capital when
/// shift is on (all capitals with caps lock); the next three wait in the strip.
func endSwipe(typing words: [String], points: Int, decodeTime: TimeInterval?) {
guard isSwiping else { return }
isSwiping = false
swipePreview = nil
// The finger came up off a key: a quick tap on the strip's lower half next is a key.
lastKeyTouchAt = clock()
let readings = swipeReadings(words)
if let log = typingLog {
log.ui("swipe_end", [
TypingLogField("points", .int(points)),
TypingLogField("ms", decodeTime.map { .ms($0 * 1000) } ?? .null),
TypingLogField("readings", .strings(Array(readings.prefix(4)))),
])
}
guard let first = readings.first else {
refreshSuggestions()
onChange?()
return
}
let before = target.documentContextBeforeInput
let space = SwipeTyping.needsSpace(before: before)
let capital = swipeCapital
let word = swipeCased(first, capital: capital)
let inserted = (space ? " " : "") + word
if space { learning?.didFinish(word: AutocorrectEngine.currentToken(in: before)) }
autocorrect.clearPendingUndo()
tokenTaps.removeAll()
adaptiveTouch?.forget()
typingLog?.edit("insert", [
TypingLogField("inserted", .string(inserted)),
TypingLogField("src", .string("swipe")),
])
target.insertText(inserted)
finishedWord = nil
lastSpaceAt = nil
layerState.didInsert(inserted)
if layerState.layer == .letters { shift.didType() }
swipedWord = SwipedWord(
inserted: inserted,
word: word,
alternatives: swipeAlternatives(readings, capital: capital)
)
logState("typed")
didEdit()
typingLog?.context("swipe")
}
/// Puts another of the swipe's readings in its word's place; the word joins the others.
private func replaceSwipedWord(with text: String) {
guard var swiped = swipedWord, let before = target.documentContextBeforeInput, before.hasSuffix(swiped.word) else {
swipedWord = nil
refreshSuggestions()
return
}
typingLog?.edit("suggestion", [
TypingLogField("slot", suggestions.firstIndex { $0.replacement == text }.map { .int($0) } ?? .null),
TypingLogField("text", .string(text)),
TypingLogField("deleted", .string(swiped.word)),
TypingLogField("inserted", .string(text)),
TypingLogField("candidates", TypingLogger.candidates(suggestions)),
])
for _ in 0..<swiped.word.count {
target.deleteBackward()
}
target.insertText(text)
swiped.inserted = String(swiped.inserted.dropLast(swiped.word.count)) + text
swiped.alternatives = Array(([swiped.word] + swiped.alternatives.filter { $0 != text }).prefix(3))
swiped.word = text
swipedWord = swiped
lastSpaceAt = nil
didEdit()
typingLog?.context("suggestion")
}
/// Right after a swipe, a tapped letter, digit, opening mark or symbol goes in after a
/// space, as Apple's keyboard keeps one pending after a swiped word ("hello" then "w"
/// types "hello w", "(" types "hello ("); punctuation in `swipeJoiners` goes against the
/// word ("hello,"). Shift as you set it stays; with it off, the space may bring a capital
/// (a field that capitalises every word).
private func spaceAfterSwipedWord(before text: String) {
guard let swiped = swipedWord, let first = text.first, !Self.swipeJoiners.contains(first),
let before = target.documentContextBeforeInput, before.hasSuffix(swiped.word) else { return }
swipedWord = nil
learning?.didFinish(word: AutocorrectEngine.currentToken(in: before))
typingLog?.edit("insert", [
TypingLogField("inserted", .string(" ")),
TypingLogField("src", .string("swipe_space")),
])
target.insertText(" ")
tokenTaps.removeAll()
finishedWord = nil
lastSpaceAt = nil
if shift.state == .off { refreshAutoCapitalization() }
}
/// Takes back what a swipe typed when it is still right before the cursor.
private func deleteSwipedWord(_ swiped: SwipedWord) -> Bool {
guard let before = target.documentContextBeforeInput, before.hasSuffix(swiped.inserted) else { return false }
autocorrect.clearPendingUndo()
typingLog?.edit("delete", [
TypingLogField("deleted", .string(swiped.inserted)),
TypingLogField("src", .string("swipe")),
])
for _ in 0..<swiped.inserted.count {
target.deleteBackward()
}
tokenTaps.removeAll()
lastSpaceAt = nil
didEdit()
return true
}
// MARK: - Layout
private func rebuildLayoutIfNeeded() {
if layout.layer != layerState.layer {
rebuildLayout(cause: "layer_auto")
}
}
private func rebuildLayout(cause: String = "layout") {
layout = KeyboardLayout(
width: width,
orientation: orientation,
layer: layerState.layer,
field: field,
showsGlobe: showsGlobe,
showsEmoji: showsEmoji,
horizontalInset: horizontalInset,
language: layoutLanguage ?? language,
letters: letterLayout,
posture: posture,
windowInset: windowInset,
hexPlacement: hexPlacement,
phoneSize: phoneSize
)
autocorrect.setKeys(layout.language, letters: layout.letters)
syncDecoderKeys()
logState(cause)
}
/// 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.").
private 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)
}
}