mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
Follow-up to #2340 (10d64004). Hex's two other advanceToNextInputMode() callers reach the
same iOS 27.2 host crash on the iPhone Duo unfolded and upright (Apple's keyboard split):
UIKit's next-keyboard switch runs -[UIKeyboardLayoutStar activationIndicatorView], which
reads one key past the end of a split keyboard that isn't laid out yet. The automatic
hand-off on number-pad fields would crash the app being typed in without the person
touching anything.
- Number pads (KeyboardViewController.handOffNumericPadIfNeeded): hand off only when
KeyboardFieldRouting.handsOffNumberPad says so, i.e. iOS asks for a globe and Apple's
keyboard isn't split. On the split posture Hex stays on its own 123 page, as it already
does wherever iOS draws its own globe. The Duo's posture needs the keyboard's window
(FullKeyboardView.currentPosture is nil until then), so when it isn't known yet the
hand-off waits and viewWillLayoutSubviews asks again; on every other device the posture
is known at once and the hand-off runs exactly where it did.
- Code from Messages chip: not offered on the split posture (KeyboardClipboardTracker.offer
canSwitchKeyboard false, also while the Duo's posture isn't known); a copied code still
gets Paste, and the globe's list (#2340) leads to Apple's keyboard. Its tap handler also
refuses to advance there. Chosen over opening the list from the chip's own touch: the
chip's whole point is one tap, a list that appears after a 0.2 s hold is what the globe
already does on this posture, and hiding it keeps the change small. The strip refreshes
when the posture changes so the chip follows a fold or unfold.
Everywhere else (every iPhone, the Duo folded or turned, every iPad) behaviour is unchanged.
Verified (Mac Studio, iOS 27.2 simulators):
- xcodebuild test: 760 tests, 0 failures, including the new
ShiftStateMachineTests.testNumberPadsStayOnHexWhereApplesKeyboardSplits and
KeyboardClipboardTests.testNoCodeFromMessagesWhereApplesKeyboardSplits.
- iPhone SE, split posture forced by a diagnostic build: number pad stays on Hex's 123
page 20/20, no Code from Messages chip and a tap where it was stays on Hex 10/10.
Unforced: the chip shows and its tap switches 10/10, number pad hands off 9/10.
- Number-pad hand-off unchanged elsewhere: 20 launches each in parallel, iPhone SE
base 12/20 vs this 18/20, Duo cover base 16/20 vs this 18/20 (the simulator drops some
hand-offs on both builds, a pre-existing flake). Host never died.
- code-shape.py --check passes (FullKeyboardView.swift 1,459 lines, no new files).
- Not verified on a real Duo or an unfolded Duo simulator (it can't be unfolded).
210 lines
7.5 KiB
Swift
210 lines
7.5 KiB
Swift
import Foundation
|
||
import UIKit
|
||
|
||
nonisolated enum ShiftState: Equatable, Sendable {
|
||
case off
|
||
/// One-shot: the next character is uppercase, then shift turns off.
|
||
case on
|
||
/// Caps lock.
|
||
case locked
|
||
}
|
||
|
||
/// Shift as a pure state machine. Shift changes on touch-down so a left-thumb chord
|
||
/// (shift down, letter down) types a capital; a quick double tap locks.
|
||
nonisolated struct ShiftStateMachine: Equatable, Sendable {
|
||
static let doubleTapWindow: TimeInterval = 0.3
|
||
|
||
private(set) var state: ShiftState = .off
|
||
private(set) var isHeld = false
|
||
private var typedWhileHeld = false
|
||
private var lastTapDown: TimeInterval?
|
||
/// This press turned shift on (from off).
|
||
private var pressTurnedOn = false
|
||
|
||
init(state: ShiftState = .off) {
|
||
self.state = state
|
||
}
|
||
|
||
var isUppercase: Bool { state != .off || isHeld }
|
||
|
||
/// Whether the keys still look as they did before this press. Apple's keyboard shows a press
|
||
/// that turns shift on only when it lifts (iOS 27.2: held from off, the key isn't shaded and
|
||
/// the letters stay lowercase), though a letter typed meanwhile is a capital; a press that
|
||
/// turns shift or caps lock off shades the key at once.
|
||
var pressShowsOff: Bool { isHeld && pressTurnedOn && !typedWhileHeld }
|
||
|
||
mutating func shiftDown(now: TimeInterval) {
|
||
isHeld = true
|
||
typedWhileHeld = false
|
||
pressTurnedOn = false
|
||
if state == .locked {
|
||
state = .off
|
||
lastTapDown = nil
|
||
} else if let last = lastTapDown, now - last < Self.doubleTapWindow {
|
||
state = .locked
|
||
lastTapDown = nil
|
||
} else {
|
||
pressTurnedOn = state == .off
|
||
state = state == .off ? .on : .off
|
||
lastTapDown = now
|
||
}
|
||
}
|
||
|
||
mutating func shiftUp() {
|
||
isHeld = false
|
||
pressTurnedOn = false
|
||
if typedWhileHeld {
|
||
// A chord, not a tap: it neither stays on nor counts toward a double tap.
|
||
typedWhileHeld = false
|
||
lastTapDown = nil
|
||
if state != .locked {
|
||
state = .off
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Call after a character key commits.
|
||
mutating func didType() {
|
||
if isHeld {
|
||
typedWhileHeld = true
|
||
} else if state == .on {
|
||
state = .off
|
||
}
|
||
lastTapDown = nil
|
||
}
|
||
|
||
/// Auto-capitalization after an edit or a field change. Leaves caps lock and a held shift alone.
|
||
mutating func refreshAutoCapitalization(_ shouldCapitalize: Bool) {
|
||
guard state != .locked, !isHeld else { return }
|
||
state = shouldCapitalize ? .on : .off
|
||
}
|
||
|
||
mutating func reset() {
|
||
self = ShiftStateMachine()
|
||
}
|
||
}
|
||
|
||
nonisolated enum AutoCapitalization {
|
||
enum Mode: Equatable, Sendable {
|
||
case none
|
||
case words
|
||
case sentences
|
||
case allCharacters
|
||
|
||
init(_ type: UITextAutocapitalizationType?) {
|
||
switch type {
|
||
case .some(UITextAutocapitalizationType.none): self = Mode.none
|
||
case .some(.words): self = .words
|
||
case .some(.allCharacters): self = .allCharacters
|
||
default: self = .sentences
|
||
}
|
||
}
|
||
}
|
||
|
||
private static let sentenceEnders: Set<Character> = [".", "!", "?"]
|
||
private static let closers: Set<Character> = ["\"", "'", ")", "]", "\u{201D}", "\u{2019}"]
|
||
|
||
/// Whether the next letter should be a capital. `before` is the text before the cursor,
|
||
/// which web views sometimes report as `nil` even when the field has text: only the
|
||
/// start of an empty field capitalizes then. In Greek, ; is the question mark and ends a
|
||
/// sentence too, as on the system Greek keyboard ("Τι κάνεις; Καλά").
|
||
static func shouldCapitalize(mode: Mode, before: String?, hasText: Bool, language: KeyboardLanguage = .english) -> Bool {
|
||
switch mode {
|
||
case .none:
|
||
return false
|
||
case .allCharacters:
|
||
return true
|
||
case .words, .sentences:
|
||
break
|
||
}
|
||
guard let before else { return !hasText }
|
||
guard let last = before.last else { return true }
|
||
if last.isNewline { return true }
|
||
guard last.isWhitespace else { return false }
|
||
if mode == .words { return true }
|
||
|
||
var trimmed = Substring(before)
|
||
while let c = trimmed.last, c.isWhitespace, !c.isNewline {
|
||
trimmed = trimmed.dropLast()
|
||
}
|
||
guard let lastWritten = trimmed.last else { return true }
|
||
if lastWritten.isNewline { return true }
|
||
while let c = trimmed.last, closers.contains(c) {
|
||
trimmed = trimmed.dropLast()
|
||
}
|
||
guard let ender = trimmed.last else { return false }
|
||
return sentenceEnders.contains(ender) || (language == .greek && (ender == ";" || ender == "\u{037E}"))
|
||
}
|
||
}
|
||
|
||
/// Two spaces in a row after a word become ". ".
|
||
nonisolated enum DoubleSpacePeriod {
|
||
static let window: TimeInterval = 0.35
|
||
|
||
/// `lastSpaceAt` is when this keyboard typed the space now before the cursor (nil if
|
||
/// anything else happened since). Requires a letter or digit right before that space.
|
||
static func shouldReplace(before: String?, lastSpaceAt: TimeInterval?, now: TimeInterval) -> Bool {
|
||
guard let before, let lastSpaceAt, now - lastSpaceAt <= window, now >= lastSpaceAt else {
|
||
return false
|
||
}
|
||
guard before.last == " " else { return false }
|
||
guard let previous = before.dropLast().last else { return false }
|
||
return previous.isLetter || previous.isNumber
|
||
}
|
||
}
|
||
|
||
/// Which key page shows. 123 and #+= are transient: space or return goes back to letters.
|
||
nonisolated struct KeyboardLayerState: Equatable, Sendable {
|
||
private(set) var layer: KeyboardLayer
|
||
|
||
init(layer: KeyboardLayer = .letters) {
|
||
self.layer = layer
|
||
}
|
||
|
||
mutating func switchTo(_ next: KeyboardLayer) {
|
||
layer = next
|
||
}
|
||
|
||
mutating func didInsert(_ text: String) {
|
||
guard layer != .letters else { return }
|
||
if text == " " || text == "\n" || text == "'" {
|
||
layer = .letters
|
||
}
|
||
}
|
||
|
||
mutating func reset(to initial: KeyboardLayer = .letters) {
|
||
layer = initial
|
||
}
|
||
}
|
||
|
||
nonisolated enum KeyboardFieldRoute: Equatable, Sendable {
|
||
/// Hex's keys, with this field kind and first page.
|
||
case keys(KeyboardFieldKind, initialLayer: KeyboardLayer)
|
||
/// Number pads go to the next keyboard; Hex shows 123 in case there is none.
|
||
case handOff
|
||
}
|
||
|
||
nonisolated enum KeyboardFieldRouting {
|
||
static func route(for keyboardType: UIKeyboardType?) -> KeyboardFieldRoute {
|
||
switch keyboardType {
|
||
case .URL?:
|
||
return .keys(.url, initialLayer: .letters)
|
||
case .emailAddress?:
|
||
return .keys(.email, initialLayer: .letters)
|
||
case .numberPad?, .decimalPad?, .asciiCapableNumberPad?, .phonePad?, .namePhonePad?:
|
||
return .handOff
|
||
case .numbersAndPunctuation?:
|
||
return .keys(.text, initialLayer: .numbers)
|
||
default:
|
||
return .keys(.text, initialLayer: .letters)
|
||
}
|
||
}
|
||
|
||
/// Whether a number pad (`.handOff`) goes to the next keyboard: where iOS asks for a globe,
|
||
/// except where Apple's keyboard is split (the Duo unfolded and upright). There iOS 27.2's
|
||
/// switch to the next keyboard can end the app being typed in (TestFlight build 121,
|
||
/// `FullKeyboardView.passesGlobeTouch`), so Hex stays on its 123 page.
|
||
static func handsOffNumberPad(needsInputModeSwitchKey: Bool, posture: KeyboardPosture) -> Bool {
|
||
needsInputModeSwitchKey && !posture.splitsKeyboard
|
||
}
|
||
}
|