hex/ios/HEXShared/InputModeListTouch.swift
Ryan Vogel a0d72ab4bc
iOS keyboard: the dictation bar's globe opens the keyboard list where Apple's keyboard is split, so the host doesn't crash (#2417)
On the iPhone Duo unfolded and upright, a tap on the dictation bar's globe still
ended the app being typed in (iOS 27.2: -[UIKeyboardLayoutStar
activationIndicatorView] index 43 beyond bounds [0 .. 42], the crash #2340 fixed
for the full keyboard). The bar's GlobeKeyRepresentable sent every touch straight
to handleInputModeList(from:with:), so a tap switched to the next keyboard blind.

The rule moves out of FullKeyboardView into ios/HEXShared/InputModeListTouch.swift
and both keyboards use it:
- InputModeListTouch.forward holds back the lift of a tap that stayed on the key
  where Apple's keyboard may be split, so UIKit's 0.2 s hold opens the list.
  Holds, slides and picks from the list pass through unchanged.
- "May be split" is KeyboardPosture.current(in: window): the Duo unfolded and
  upright, or a Duo posture not known yet (no window), as the strip already
  treated it. Every other iPhone and iPad is .phone and switches as before.
- The dictation bar's globe gets a coordinator that forwards through it.
- The full keyboard's globe reads the posture from its window at touch time
  instead of engine.posture (the same value once laid out); the Code from
  Messages chip keeps always holding back its tap (alwaysHoldsBackTap).
- Removed: FullKeyboardView.passesGlobeTouch and globeTouchLeftKey, replaced by
  InputModeListTouch.passes and its own state; the existing globe test now
  checks InputModeListTouch.passes.

Verified (Mac Studio, iPhone Duo simulator, iOS 27.2 runtime 24B91016s, real
touches injected through the inner screen's own digitizer, Apple's split
keyboard on):
- Before (origin/dev 63f05e3c), unfolded and upright, a fresh host that opened
  with the bar: one tap on the globe ended KBHost and Messages, each with a
  crash report in -[UIKeyboardLayoutStar activationIndicatorView].
- After: 20 fresh hosts (10 KBHost, 10 Messages), one tap each: the list
  opened 20/20, no host ended, no crash reports. Picking English (US) from the
  list switched to Apple's split keyboard, host alive.
- Folded and unfolded turned: a tap still switches straight to Apple's
  keyboard, host alive.
- xcodebuild test (iPhone 17 Pro, iOS 27.2): 740 tests, 0 failures, 3 skipped;
  the runner was killed once mid-run in DictationControllerTests (signal TERM,
  busy shared Studio) and that class passed 74/74 on a rerun.
- python3 scripts/code-shape.py --check passes; FullKeyboardView.swift is 1,436
  lines.
2026-11-22 14:13:11 -05:00

46 lines
2.6 KiB
Swift

import UIKit
/// Hands a key's own touches to UIKit's `handleInputModeList(from:with:)` as its real events, as
/// Apple documents: a tap switches to the next keyboard, a hold (0.2 s) or a slide up opens the
/// list, and lifting over the list picks that keyboard. The full keyboard's globe and Code from
/// Messages chip and the dictation bar's globe all go through here.
///
/// Where Apple's keyboard is split (the Duo unfolded and upright), a lift on the key that never
/// left it isn't passed on, so UIKit's hold opens the list instead of switching to the next
/// keyboard. iOS 27.2's switch to the next keyboard can end the app being typed in there
/// (TestFlight build 121, iPhone Duo: `-[UIKeyboardLayoutStar activationIndicatorView]` reads
/// one key past the end of a split keyboard not laid out yet, to show its language); a pick
/// from the list doesn't show that indicator.
@MainActor
final class InputModeListTouch {
/// This touch went off the key before lifting.
private var leftKey = false
/// `alwaysHoldsBackTap`: a tap opens the list everywhere (Code from Messages, where the next
/// keyboard could be Emoji). Otherwise only where Apple's keyboard may be split: the Duo
/// unfolded and upright, or its posture not known yet.
func forward(_ sender: UIView, with event: UIEvent, to controller: UIInputViewController, alwaysHoldsBackTap: Bool = false) {
if let touch = event.touches(for: sender)?.first {
let holdsBackTap = alwaysHoldsBackTap || KeyboardPosture.current(in: sender.window)?.splitsKeyboard ?? true
let onKey = sender.bounds.contains(touch.location(in: sender))
if touch.phase == .began { leftKey = false }
if touch.phase == .moved, !onKey { leftKey = true }
guard Self.passes(phase: touch.phase, onKey: onKey, leftKey: leftKey, appleKeyboardSplits: holdsBackTap) else { return }
}
controller.handleInputModeList(from: sender, with: event)
}
static func passes(phase: UITouch.Phase, onKey: Bool, leftKey: Bool, appleKeyboardSplits: Bool) -> Bool {
!(appleKeyboardSplits && phase == .ended && onKey && !leftKey)
}
}
extension KeyboardPosture {
/// The posture as `window` shows it now; nil on the Duo before the keyboard has a window,
/// where Hex takes Apple's keyboard to be split.
@MainActor static func current(in window: UIWindow?) -> KeyboardPosture? {
guard FullKeyboardView.isFoldable else { return .phone }
guard let screen = window?.windowScene?.screen ?? window?.screen else { return nil }
return current(screenSize: screen.bounds.size, isFoldable: true)
}
}