hex/ios/HEXShared/FullKeyboard/FullKeyboardPalette.swift
Ryan Vogel 52b9e475a6
iOS: the full keyboard's top bar matches the system keyboard (#2135)
* iOS: the full keyboard's top bar matches Apple's predictive bar

Kit's App Store screenshot (dark mode) showed the suggestions sitting low
with a large empty band between the keyboard panel's top edge and the
words. Measured black-box against Apple's English keyboard in the same
Messages field (iOS 27.2 simulator, iPhone 17 Pro, SE 3rd gen, 17 Pro
Max): iOS 26 draws a band of its own above every third-party keyboard
(16.3 pt on the 17 Pro, 12.5 pt on the SE, 13.3 pt on the Pro Max) that
no extension can draw into (checked with a throwaway build drawing at
negative y: the hosted window is exactly the keyboard's height). Hex
then added Apple's whole 44 pt bar and 8 pt gap below it, so its keycaps
started 68 pt below the panel edge where Apple's start 48-52 pt, and its
words sat 38 pt down where Apple's sit 20-24 pt.

What changed:
- The phone strip is the 27 pt of Apple's bar below iOS's band, then the
  same 8 pt gap: row 1 starts 35 pt down (Apple's keycaps are 35.0-35.5 pt
  below the extension's top on all three iPhones), so the panel edge to
  row 1 now measures 51.3 pt against Apple's 51.7 pt on the 17 Pro. The
  keyboard is 253 pt (172 pt landscape) instead of 270 (189); keys do not
  move on screen, only the panel's top edge comes down 17 pt. The Duo
  keeps its 44 pt bar (KeyboardMetrics.insetStripHeight).
- Slots are thirds of the keyboard as Apple's are (dividers end at 134
  and 268 pt on a 402 pt iPhone), with the Hex key's room taken from the
  slot beside it (KeyboardLayout.stripSlotFrames), instead of thirds of
  the strip left of the Hex key. Typing logs record the real slot frames.
- Dividers are Apple's 1 x 24 pt (were 1 px x 22 pt), ending on the
  boundary, in Apple's measured ink.
- Words are Apple's muted ink (dark 0.58 white, light 0.67 black) instead
  of full white/black; the literal word no longer has its own alpha.
- A pending autocorrection is Apple's full-slot capsule with white words
  in regular weight (was a 10 pt-radius pill inset 4 pt, medium weight).
  The capsule is 26 pt tall, not Apple's 38 pt: Apple's top 11 pt falls
  in iOS's band.
- The Hex key, recording bar (Dot Scope well, X and check) and Undo /
  Paste chips use the strip's pill height (26 pt, 4 pt-inset 36 pt on the
  Duo); the Hex mark keeps its 19 pt. The Full Access hint's two lines
  fit 27 pt (13 / 11.5 pt).
- #2100's rule is unchanged: a fast tap on the strip's lower half (below
  its text line, now 13.5 pt down) types the row-1 key below; its tests
  pass as written.

Kept on purpose: words are centred in the 27 pt Hex can draw, about
5.5 pt lower than Apple's relative to the panel edge, so the pill has
room around them; Apple lets long words run past their slot at 17 pt,
Hex still shrinks them to 15 pt and truncates, so nothing runs under
the Hex key. Which suggestions appear (#2125) is untouched.

Verified: xcodebuild test on the iPhone 17 Pro Max simulator, 383 tests,
0 failures, 1 skipped, with updated KeyboardLayoutTests (heights, strip
metrics, row 1 at 35 pt) and a new case for Apple's thirds with the Hex
key on either side. Side-by-side screenshots of Apple's keyboard and
Hex in Messages (empty, typing, literal in quotes, pending correction,
long word, next word) on the 17 Pro and SE in light and dark, and the
recording bar, Full Access hint and Hex key on the left before and after.

* iOS: the top bar's light-mode autocorrection pill matches Apple's

The first pass guessed an opaque white capsule with black words for a
pending autocorrection in light mode. Apple's, measured in Messages on the
iPhone 17 Pro (iOS 27.2 simulator), is a faint white tint, (235,237,240)
on the (226,227,232) platter, under near-black (44,46,49) words; dark mode
was already measured and is unchanged.

Verified: rebuilt and compared the pending-correction state against
Apple's keyboard in the same field in light mode; xcodebuild test.

* iOS: the top bar offers three words in an empty field and between words

Ryan's screenshot of the current build in an empty field (dark mode, shift
on) showed an empty bar with two dividers and nothing in the slots, where
Apple's keyboard shows three sentence starters (I | The | I'm). After a
word, Hex often showed one prediction in the middle with empty slots
beside it, where Apple's fills all three.

- An empty field, and a new sentence (after ". ", "! ", "? " or a new
  line), offers three common first words from a short hand-written list
  per language (the typing model has no start-of-sentence row): English
  I / The / I'm, Spanish Yo / El / La, and so on for every layout,
  Ukrainian included. They take a capital when shift would turn itself on
  (never with Auto-capitalization off; "I" and "I'm" keep theirs), pass
  never-suggest, and are tap-only like #2125's predictions.
- After a word and a space, the model's next words keep #2125's ranking
  for the slots they fill; when it has fewer than three, the language's
  commonest words fill the rest, so no slot stays empty.
- After a comma (or ; : ) ” ’ —) and a space, three words that start a
  clause (English I / but / and). After a handle, a path, a number or a
  second space the bar stays empty, as #2125 had it.
- With Emoji (on by default since #2129) a finished word's emoji no longer
  replaces the predictions: one emoji takes the centre with next words on
  either side ("pizza ": and | 🍕 | hut); two or three fill from the left.
- Picking a starter in an empty field or after a new line inserts it (the
  queued-tap guard now checks that no word was started, not that the text
  ends in a space).

Verified: xcodebuild test on the iPhone 17 Pro simulator (411 tests,
0 failures, 1 skipped), with a new FullKeyboardEngineTests case for the
starters, capitals, the fill and the comma, and #2125's boundary test
updated for ". " and a new line. Held-out tap benchmark (2,000 English
and 2,000 Spanish test sentences, Spatial autocorrect, freshly erased
simulators): finished text is unchanged within the benchmark's own noise
(two runs of the same build differ in 21 English and 32 Spanish sentences,
from the system spell checker); English words broken 528 and 529 before,
528 after, fixed 3,147/3,148 before, 3,147 after; Spanish broken 419/416
before, 417 after, fixed 3,744 before, 3,745 after. Side-by-side shots
against Apple's keyboard in Messages: empty field, after ". ", after
"hi ", after a comma, light and dark, iPhone 17 Pro, SE and 17 Pro Max.

* iOS: the top bar matches Apple's in landscape and on the Duo's cover screen

The 27 pt strip assumed the band iOS draws above a third-party keyboard,
which the iPhone has in portrait. Measured against Apple's keyboard
(iOS 27.2 simulator):

- Landscape (iPhone 17 Pro Max): iOS draws no band, and Apple's keycaps
  start 50 pt below the panel's edge where Hex's started 35 pt down. The
  landscape strip is now 42 pt plus the 8 pt gap (row 1 at 50 pt, keyboard
  187 pt, was 172 pt).
- The Duo folded and upright: the band is 8.7 pt and Apple's keycaps start
  48.7 pt below the edge where Hex's started 52.7 pt down (its 44 pt bar
  sat right on the keys). It is now 32 pt plus the 8 pt gap, so the panel
  edge to row 1 measures 48.7 pt for both. The unfolded postures and the
  cover in landscape keep the 44 pt bar (not measured against the band).
- Pills (an autocorrection, a pressed word, the Hex key, the recording
  bar) are the strip less half a point above and below, at most 36 pt.

Keys do not move on screen; only the panel's top edge does. iPhone
portrait keeps the 27 + 8 pt contract (51.3 pt panel edge to row 1 on
the 17 Pro against Apple's 51.7; 48.67 pt on the 17 Pro Max, the same as
Apple's).

Verified: KeyboardLayoutTests updated for the new heights and metrics;
xcodebuild test (411 tests, 0 failures, 1 skipped); screenshots of the
hosted keyboard next to Apple's in landscape and on the Duo's cover.

* iOS: top-bar dividers, Logging mark and micro-animations follow Apple's

Measured on Apple's keyboard in the iOS 27.2 simulator, recorded at
60 fps with simctl io recordVideo and timed frame by frame:

- A changed word swaps letter by letter: letters the old and new word
  start with stay, the old word's other letters fade from its end and the
  new word's from its start, each in 0.1 s, staggered over 0.2 s (Apple:
  0.14-0.23 s). A word leaving an emptied slot fades the same way. The
  pending-correction capsule and the dividers change at once, as Apple's
  do, and so does a pressed word's capsule on touch down and release.
  Reduce Motion swaps at once. Before, every change was an instant redraw.
- A pressed word gets the same capsule as a pending correction (Apple's
  measured identical: (115,115,115) dark, (235,237,240) light), not a
  lighter one.
- The Undo dictation, Paste and Code from Messages chips and the Full
  Access hint fade in and out over the same 0.2 s instead of popping.
- Dividers are drawn only between two filled slots, so a bar with
  nothing to show (a URL or email field, or a lone "Kq") has none, as
  Apple's has none in secure fields. Apple does draw them beside empty
  slots in URL fields and for a lone literal; Hex doesn't, by request.
- The Logging mark (Record typing logs) sat at the strip's top corner,
  over slot 1's word since the strip became 27 pt ("Logging" overlapped
  "ritsrdo" in the Italian scene). It now sits on the text line at the
  outer end, and that slot centres its word in the room left.

Verified: xcodebuild test (411 tests, 0 failures, 1 skipped); recordings
of Apple's bar and Hex's on the iPhone SE simulator (typing "hello", a
pending correction, a held and released pick, the Paste chip), and
before/after snapshot scenes for the Logging mark on either side.
2026-11-21 03:26:34 +00:00

125 lines
6.1 KiB
Swift

import UIKit
/// The full keyboard's colours, light and dark, calibrated against Apple's iOS 26
/// Liquid Glass system keyboard. Dark follows the keyboard's trait collection, which iOS
/// sets from the field's `keyboardAppearance` and the host's current style, as it does
/// the system platter behind the keys. The document proxy's `keyboardAppearance` can't
/// decide it: the host resolves a default field to light or dark when it sends its traits
/// and doesn't send them again when only its style changes, so after switching apps it
/// could still say light in dark mode (TestFlight build 29).
struct FullKeyboardPalette: Equatable {
let isDark: Bool
static func isDark(traits: UITraitCollection) -> Bool {
traits.userInterfaceStyle == .dark
}
private static func rgb(_ hex: UInt32, alpha: CGFloat = 1) -> UIColor {
UIColor(
red: CGFloat((hex >> 16) & 0xFF) / 255,
green: CGFloat((hex >> 8) & 0xFF) / 255,
blue: CGFloat(hex & 0xFF) / 255,
alpha: alpha
)
}
/// Stand-in for the system Liquid Glass platter, used only in standalone snapshot scenes.
var backdrop: UIColor { isDark ? Self.rgb(0x17181A) : Self.rgb(0xDFE1E6) }
var textBase: UIColor { isDark ? .white : .black }
var textMuted: UIColor { isDark ? UIColor(white: 1, alpha: 0.56) : Self.rgb(0x525763) }
/// Unified iOS 26 Liquid Glass keycap fill across letters and modifiers:
/// `white @ 0.16` composites to `(61,61,63)` on dark `(23,24,26)` and `(94,94,96)` on dark-field `(64,64,66)`.
var keyFill: UIColor { isDark ? UIColor(white: 1, alpha: 0.16) : .white }
var keyPressedFill: UIColor { isDark ? UIColor(white: 1, alpha: 0.28) : Self.rgb(0xD8DCE4) }
/// Secondary fill for non-key chips and the recording panel's cancel pill.
var functionFill: UIColor { isDark ? UIColor(white: 1, alpha: 0.09) : UIColor(white: 0, alpha: 0.065) }
var keyShadow: UIColor { .clear }
/// Opaque so balloons and menus float cleanly over the strip and neighbouring keys.
var popupFill: UIColor { isDark ? Self.rgb(0x4E5056) : .white }
var popupBorder: UIColor { isDark ? UIColor(white: 1, alpha: 0.14) : UIColor(white: 0, alpha: 0.08) }
var popupShadow: UIColor { UIColor(white: 0, alpha: isDark ? 0.42 : 0.18) }
var chipFill: UIColor { isDark ? UIColor(white: 1, alpha: 0.11) : UIColor(white: 0, alpha: 0.065) }
var wellFill: UIColor { isDark ? UIColor(white: 1, alpha: 0.055) : UIColor(white: 0, alpha: 0.045) }
var separator: UIColor { isDark ? UIColor(white: 1, alpha: 0.12) : UIColor(white: 0, alpha: 0.10) }
var selectFill: UIColor { isDark ? UIColor(white: 1, alpha: 0.18) : UIColor(white: 0, alpha: 0.075) }
var selectText: UIColor { textBase }
/// Apple's predictive bar, measured on its platter (dark (34,34,35), light (226,227,232),
/// iOS 27.2): words (163,163,164) / (74,74,76), dividers (50,50,53) / (211,212,217), and the
/// pending autocorrection's and a pressed word's capsule (115,115,115) under white words /
/// (235,237,240) under (44,46,49).
var stripText: UIColor { isDark ? UIColor(white: 1, alpha: 0.58) : UIColor(white: 0, alpha: 0.67) }
var stripDivider: UIColor { isDark ? UIColor(white: 1, alpha: 0.075) : UIColor(white: 0, alpha: 0.066) }
var stripPillFill: UIColor { isDark ? UIColor(white: 1, alpha: 0.37) : UIColor(white: 1, alpha: 0.33) }
/// A pressed word gets the same capsule (measured the same on Apple's bar).
var stripPillPressedFill: UIColor { stripPillFill }
var stripPillText: UIColor { isDark ? .white : UIColor(white: 0, alpha: 0.81) }
/// Apple system keyboard action blue (`#007AFF`).
var actionFill: UIColor { Self.rgb(0x007AFF) }
var actionPressedFill: UIColor { Self.rgb(0x0062CC) }
var warmCore: UIColor { Self.rgb(0xFF9500) }
static let cornerRadius: CGFloat = 8.5
}
/// The Hex mark (two hexagon outlines and a filled core) as UIKit paths.
enum HexMarkPath {
private static let outer: [CGPoint] = [
CGPoint(x: 32, y: 4), CGPoint(x: 57, y: 18.5), CGPoint(x: 57, y: 45.5),
CGPoint(x: 32, y: 60), CGPoint(x: 7, y: 45.5), CGPoint(x: 7, y: 18.5),
]
private static let middle: [CGPoint] = [
CGPoint(x: 32, y: 15), CGPoint(x: 48, y: 24.25), CGPoint(x: 48, y: 39.75),
CGPoint(x: 32, y: 49), CGPoint(x: 16, y: 39.75), CGPoint(x: 16, y: 24.25),
]
private static let core: [CGPoint] = [
CGPoint(x: 32, y: 24), CGPoint(x: 39.5, y: 28.25), CGPoint(x: 39.5, y: 36.75),
CGPoint(x: 32, y: 41), CGPoint(x: 24.5, y: 36.75), CGPoint(x: 24.5, y: 28.25),
]
static func draw(in context: CGContext, rect: CGRect, color: UIColor, coreColor: UIColor?) {
let side = min(rect.width, rect.height)
let s = side / 64
let origin = CGPoint(x: rect.midX - side / 2, y: rect.midY - side / 2)
func path(_ points: [CGPoint]) -> CGPath {
let p = CGMutablePath()
p.addLines(between: points.map { CGPoint(x: origin.x + $0.x * s, y: origin.y + $0.y * s) })
p.closeSubpath()
return p
}
context.saveGState()
context.setLineWidth(max(1.15, 4.5 * s))
context.setLineCap(.round)
context.setLineJoin(.round)
context.setStrokeColor(color.cgColor)
context.addPath(path(outer))
context.addPath(path(middle))
context.strokePath()
context.setFillColor((coreColor ?? color).cgColor)
context.addPath(path(core))
context.fillPath()
context.restoreGState()
}
}
extension CGContext {
/// Draws a continuous-corner iOS 26 Liquid Glass keycap with no legacy under-shadow.
func fillKeycap(_ rect: CGRect, fill: UIColor, shadow: UIColor = .clear, radius: CGFloat = FullKeyboardPalette.cornerRadius) {
let path = UIBezierPath(roundedRect: rect, cornerRadius: radius).cgPath
saveGState()
if shadow != .clear {
setShadow(offset: CGSize(width: 0, height: 1), blur: 0.5, color: shadow.cgColor)
}
addPath(path)
setFillColor(fill.cgColor)
fillPath()
restoreGState()
}
}