hex/ios/HEXShared/FullKeyboard/KeyboardBackdropView.swift
Ryan Vogel ae6eca79ed
iOS: opaque keyboard panel so stale system layers can't show through, and a nil-safe field ID (#2139)
* iOS: an opaque panel under the keyboard so iOS's leftovers can't show through

TestFlight build 56 (iPhone 17, iOS 27.0.1, English and Indonesian): after a
switch from Apple's keyboard, Apple's layout showed through Hex about a row
above Hex's own keys, and kept doing so switch after switch.

Cause: Hex drew nothing under its keys. The keyboard view was clear and its
dark keys are white at 16% alpha, like Apple's over the iOS 26+ glass panel,
so anything iOS leaves behind the keyboard shows through. The tester's
screenshot has two layouts:
- Apple's keyboard, with its emoji key, the "ID EN" badge on its space bar
  and its wide greyed search key. Rows are about 54 pt apart, starting at
  the panel's top, where Apple puts them in a search field (no predictive
  bar).
- Hex, with the Logging mark and the Hex key in its strip, "Indonesia" on
  its space bar, and rows about 56 pt apart.
The upper layer has keys only Apple's keyboard has, so it's a leftover
system layer, not a second Hex surface. The iOS 27.0 and 27.2 simulators
never kept it: in every frame of the switch videos one keyboard is drawn
whole.

Fix: KeyboardBackdropView, an opaque shape layer under the surface, in the
panel's colour over a plain background, measured from simulator
screenshots (black box): 34,34,35 dark, 226,227,232 light. Its top corners
follow the panel's 26 pt corners, which start about 17.5 pt above the
keyboard, 1 pt inside the glass's rim. That holds on the iPhone 17, 17 Pro,
17 Pro Max, Air, 17e and SE, iOS 27.0 and 27.2. The keyboard's height is now
set once, before the first layout, at the surface's own height, instead of a
140 pt constraint then changed. Later changes (the emoji search's row) are
never animated. A remount removes only Hex's own views (the surface, child
views, any stray FullKeyboardView), never UIKit's.

Replaced: the clear keyboard. Over vivid content right behind the panel's
top, the glass still tints the band iOS draws above the keyboard but no
longer Hex's area. Over plain backgrounds the two are pixel-identical.

Verified: xcodebuild test on an iPhone 17 (iOS 27.2) simulator, all tests
passing (1 skipped, as on dev), with new KeyboardLayoutTests cases (opaque
in both modes, every key covered on every width, never outside the panel's
corners or rim). In the iOS 27.0 simulator (Messages and Safari, light and
dark), the backdrop matches the system panel pixel for pixel, the corners
and rim match Apple's, and the globe/list switch sequence is clean. The
overlap itself needs an iPhone on iOS 27.0.1 to confirm.

* iOS: read the field's documentIdentifier without trapping on nil

UITextDocumentProxy.documentIdentifier is declared non-optional, but the
keyboard's proxy returns nil early on (in viewDidLoad, and at times later).
Reading that nil as Swift's UUID traps. Two agents hit it: the Debug
keyboard crashed in followTypingLogField() and in viewDidLoad. In Release
it's undefined behaviour.

Reproduced on dev 71b5a8ec in the iOS 27.0 simulator with Record typing
logs on: Hex up in Safari's address bar, then Messages and back to Safari,
five times. The keyboard crashed 4 times, 2 EXC_BREAKPOINT in
UUID._unconditionallyBridgeFromObjectiveC from beginTypingLogSession
(viewWillAppear), each followed by the extension host's abort. Earlier
reports trap the same way from followTypingLogField (textDidChange). The
same script on this commit crashed 0 times.

Both reads (typing-log session start and field follow) now go through
TypingLogTraits.documentIdentifier(of:), which reads it through key-value
coding after a responds(to:) check and gives nil instead of trapping. A
document UIKit couldn't name yet counts as the session's field
(TypingLogTraits.isOtherField(than:)). So a session that began before the
field was named isn't split in two when the name arrives: it keeps the
document instead. Another document or other traits still start a new
session, as before.

Verified: xcodebuild test on an iPhone 17 (iOS 27.2) simulator, all tests
passing (1 skipped, as on dev), with new TypingLogTests cases: a nil
document reads as nil with no trap, a real one reads as itself, and an
object without the property gives nil. An unnamed document matches the same
field, and other documents or traits don't.
2026-11-21 03:45:47 +00:00

100 lines
4.9 KiB
Swift

import UIKit
/// The keyboard panel's colour, opaque, under everything the keyboard extension draws.
///
/// iOS 26 and later draw a glass panel behind a third-party keyboard, and Hex drew nothing of
/// its own under its translucent keys. Switching from Apple's keyboard, iOS 27.0.1 could leave
/// Apple's layout in that panel, pinned to its top, and it showed through Hex a row above
/// Hex's own keys, switch after switch (TestFlight build 56). The panel's colour, opaque,
/// hides whatever iOS leaves behind the keyboard.
///
/// The colours are Apple's panel over a plain background, measured from simulator
/// screenshots (iOS 27.0 and 27.2). Over vivid content the glass takes on some of its colour
/// (about a quarter) and this doesn't, so there the strip iOS draws above the keyboard and the
/// globe row below can be tinted while the keys' area isn't: that's the cost of hiding
/// everything under the keys.
@MainActor
final class KeyboardBackdropView: UIView {
/// Apple's keyboard panel over a plain background: dark over black or Safari's dark page,
/// light over white (over Safari's light grey page it's 224, 225, 232).
nonisolated static func fill(isDark: Bool) -> UIColor {
isDark
? UIColor(red: 34 / 255, green: 34 / 255, blue: 35 / 255, alpha: 1)
: UIColor(red: 226 / 255, green: 227 / 255, blue: 232 / 255, alpha: 1)
}
/// The panel's top corners are circular with a 26 pt radius, and iOS starts a third-party
/// keyboard about 17.5 pt below the panel's top edge, so the corners reach 8.5 pt into the
/// keyboard (1.4 pt in at its top). Measured on the iPhone 17, 17 Pro, 17 Pro Max, Air, 17e
/// and SE, iOS 27.0 and 27.2.
nonisolated static let panelCornerRadius: CGFloat = 26
/// A little under the measured 17.5 pt, so the outline errs inside the panel.
nonisolated static let panelTopAbove: CGFloat = 17
/// The glass's rim (an outline, and a highlight on one side) is about 1 pt wide: the fill
/// stops inside it so the panel's edge looks as it does around Apple's keys.
nonisolated static let rimInset: CGFloat = 1
override class var layerClass: AnyClass { CAShapeLayer.self }
private var shapeLayer: CAShapeLayer {
// swiftlint:disable:next force_cast
layer as! CAShapeLayer
}
override init(frame: CGRect) {
super.init(frame: frame)
isUserInteractionEnabled = false
isAccessibilityElement = false
backgroundColor = .clear
applyAppearance()
registerForTraitChanges([UITraitUserInterfaceStyle.self]) { (view: KeyboardBackdropView, _) in
view.applyAppearance()
}
}
@available(*, unavailable)
required init?(coder: NSCoder) { fatalError() }
override func layoutSubviews() {
super.layoutSubviews()
shapeLayer.path = Self.outline(in: bounds).cgPath
}
private func applyAppearance() {
// Light or dark with the keyboard's traits, like its keys.
shapeLayer.fillColor = Self.fill(isDark: FullKeyboardPalette.isDark(traits: traitCollection)).cgColor
}
/// Where the backdrop draws in a keyboard of `bounds`: the panel's outline, `rimInset`
/// inside it. Straight sides and bottom, and at the top the panel's rounded corners, whose
/// circles are centred `radius - topAbove` below the keyboard's top edge.
nonisolated static func outline(
in bounds: CGRect,
radius: CGFloat = KeyboardBackdropView.panelCornerRadius,
topAbove: CGFloat = KeyboardBackdropView.panelTopAbove,
inset: CGFloat = KeyboardBackdropView.rimInset
) -> UIBezierPath {
let rect = bounds.insetBy(dx: inset, dy: 0)
guard rect.width > 0, rect.height > 0 else { return UIBezierPath() }
let centerY = bounds.minY + radius - topAbove
let r = radius - inset
let rise = centerY - bounds.minY
// The panel's corners end above the keyboard (or it's too narrow for them): a rectangle.
guard rise > 0, rise < r, rect.width > 2 * radius else { return UIBezierPath(rect: rect) }
// The inner circle meets the keyboard's top edge `reach` in from the circle's centre.
let reach = sqrt(max(0, r * r - rise * rise))
let sweep = asin(min(1, rise / r))
let left = CGPoint(x: bounds.minX + radius, y: centerY)
let right = CGPoint(x: bounds.maxX - radius, y: centerY)
let path = UIBezierPath()
path.move(to: CGPoint(x: rect.minX, y: rect.maxY))
path.addLine(to: CGPoint(x: rect.minX, y: centerY))
path.addArc(withCenter: left, radius: r, startAngle: .pi, endAngle: .pi + sweep, clockwise: true)
path.addLine(to: CGPoint(x: right.x + reach, y: bounds.minY))
path.addArc(withCenter: right, radius: r, startAngle: -sweep, endAngle: 0, clockwise: true)
path.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY))
path.close()
return path
}
}