hex/ios/HEXShared/FullKeyboard/KeyboardClipboard.swift
Ryan Vogel df65c24db2 iOS keyboard: Code from Messages opens the keyboard list where the next keyboard could be Emoji
Follow-up to the previous commit, at the coordinator's request: hiding the chip in text-style
code fields whenever Emoji or another third-party keyboard is on took it away from almost
everyone (Emoji is on by default) with no pointer to where codes come from.

Change:
- Where advanceToNextInputMode could land elsewhere (Emoji or another third-party keyboard on,
  field not a number pad), the chip stays and a tap opens iOS's own keyboard list, where the
  person picks Apple's keyboard. Replaced: the previous commit's hiding of the chip there.
  KeyboardInputModes.route gives .next or .list (nil without Apple's keyboard);
  KeyboardClipboardOffer.code gains opensList (default false).
- The chip's touches go to handleInputModeList(from:with:) as the globe's do, through the
  globe's own path (FullKeyboardView.inputModeListTouch, split out of globeTouch unchanged): the
  lift of a touch that stayed on the chip is held back, so UIKit's 0.2 s hold opens the list
  (as the globe does on the Duo's split posture, #2340). A hold shows it under the finger, and
  a slide onto a keyboard picks it on lift. The chip's .touchUpInside does nothing in that mode.
- Number pads and lists with only Apple's keyboards besides Hex keep the direct switch.
- Accessibility hint in list mode: "Shows your keyboards: pick Apple's, where iOS offers codes
  from Messages and Mail" (zh-Hans added). The direct mode keeps its hint.
- showsPasteChip matches .code(canPaste: false, _) so the list mode doesn't show Paste.

Verified (iPhone 15 Pro simulator, iOS 27.2, real touches; Apple's keyboard told apart through
its keys in the accessibility tree):
- List mode, a pick of English (US) from the list: 55 of 56 picks landed on Apple's keyboard
  across English + Emoji + Hex, Hex + Emoji + English, Hex + another third-party + English and
  English + Hex + another third-party; Safari one-time-code and a .oneTimeCode field with
  autocorrection off; upright and turned; beside Paste; Full Access on and off; tap, hold and
  slide-and-lift. The one miss ended on Hex with the list gone; once, the direct switch did
  the same (shown on Apple's keyboard, then Hex again half a second later), so it isn't
  specific to the list.
- Direct mode unchanged: number pads in every list and English + Hex reach Apple's keyboard
  with no list.
- On the head rebased onto #2386 (Paste alone takes the whole bar): 5 more list picks and 2
  number-pad switches reach Apple's keyboard, and Paste beside the chip still pastes.
- xcodebuild test on that head: 858 tests, 3 skipped, 0 failures, including
  testCodeFromMessagesOpensTheListWhereTheSwitchCouldMissApplesKeyboard. code-shape.py passes.
- Not verified: VoiceOver with the list mode, a real phone, and whether Apple's keyboard then
  shows the code (a simulator gets no texts).
2026-11-22 11:58:27 -05:00

204 lines
10 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 UIKit
/// What the strip offers from the clipboard. Deciding to offer reads only whether
/// something is there (`hasStrings`), never its contents; the clipboard is read when the
/// Paste chip is tapped.
///
/// iOS asks "Allow Paste" for every read while Settings › Apps › Hex › Paste from Other Apps
/// is Ask, its default. The keyboard's reads count as Hex's own: on the simulator, pasted
/// looks up the keyboard's identity as `anomaly.hex.mobile`, a keyboard read alone makes the
/// row appear under Hex, and with Allow the chip pastes without asking. `UIPasteControl`
/// (which never asks) draws blank in a keyboard, and `detectPatterns` can't give the text,
/// so the strip points to that setting after a paste that asked or was refused.
enum KeyboardClipboardOffer: Equatable {
/// Something was copied recently and hasn't been pasted from the keyboard yet, and the
/// field is empty.
case paste
/// An empty field that expects a code (`KeyboardClipboardTracker.expectsCode`) with Apple's
/// keyboard among the user's keyboards: paste a copied code, or switch to it so iOS can
/// offer a code from Messages or Mail (only Apple's keyboard receives those; a third-party
/// keyboard gets nothing, not even the system's suggestion above it). `opensList`: the chip
/// opens iOS's keyboard list, where a switch to the next keyboard could land elsewhere
/// (`KeyboardInputModes.route`).
case code(canPaste: Bool, opensList: Bool = false)
/// A finished dictation the keyboard didn't insert by itself
/// (`KeyboardViewModel.offeredDictation`); it never touches the clipboard.
case dictation
}
/// The user's keyboards, from `UITextInputMode.activeInputModes`, which a keyboard extension
/// sees in Settings › General › Keyboard › Keyboards order. On iOS 27.2 a third-party keyboard is
/// a `UIKeyboardExtensionInputMode` and Apple's are `UIKeyboardInputMode`; Emoji's language is
/// `emoji`. `advanceToNextInputMode` from Hex on a number pad lands on Apple's keyboard whenever
/// one is on: Emoji and other third-party keyboards don't take number pads. On any other field it
/// lands on Emoji or another third-party keyboard about half the time when one is in the list, in
/// any order (simulator, black box, TestFlight build 126); iOS's keyboard list
/// (`handleInputModeList`) lands where the person picks.
enum KeyboardInputModes {
struct Mode: Equatable {
var primaryLanguage: String?
var className: String
}
@MainActor
static func hasAppleKeyboard(_ modes: [UITextInputMode] = UITextInputMode.activeInputModes) -> Bool {
hasAppleKeyboard(modes.map { Mode(primaryLanguage: $0.primaryLanguage, className: NSStringFromClass(type(of: $0))) })
}
/// Apple's own keyboard for a language: not Emoji or dictation, and not an extension.
static func hasAppleKeyboard(_ modes: [Mode]) -> Bool {
modes.contains { mode in
!["emoji", "dictation"].contains(mode.primaryLanguage) && !mode.className.contains("Extension")
}
}
/// How Code from Messages reaches Apple's keyboard.
enum Route: Equatable {
/// A switch to the next keyboard always lands on Apple's.
case next
/// It could land on Emoji or another third-party keyboard: iOS's keyboard list instead,
/// where the person picks Apple's.
case list
}
@MainActor
static func route(numberPad: Bool, _ modes: [UITextInputMode] = UITextInputMode.activeInputModes) -> Route? {
route(modes.map { Mode(primaryLanguage: $0.primaryLanguage, className: NSStringFromClass(type(of: $0))) }, numberPad: numberPad)
}
/// Nil without Apple's keyboard. The next keyboard is always Apple's on a number pad, and
/// elsewhere only when Emoji and other third-party keyboards are off.
static func route(_ modes: [Mode], numberPad: Bool) -> Route? {
guard hasAppleKeyboard(modes) else { return nil }
if numberPad { return .next }
let onlyApple = !modes.contains { $0.primaryLanguage == "emoji" } && modes.filter { $0.className.contains("Extension") }.count <= 1
return onlyApple ? .next : .list
}
}
/// Remembers which copy the keyboard first noticed and which one it already pasted, in
/// the App Group, so the offer survives the keyboard process restarting between apps.
@MainActor
struct KeyboardClipboardTracker {
static let freshness: TimeInterval = 3 * 60
/// A read that took this long waited for someone to answer Allow Paste.
static let askedReadDuration: TimeInterval = 0.3
/// How many pastes that asked point to the setting; one refused always does.
static let allowPasteHintLimit = 3
/// The strip's pointer to the setting that stops Allow Paste.
static var allowPasteHint: String {
String(localized: "Settings › Apps › Hex › Paste from Other Apps › Allow")
}
static var allowPasteHintAnnouncement: String {
String(localized: "To paste without being asked, go to Settings, Apps, Hex, Paste from Other Apps, and choose Allow.")
}
private enum Key {
static let seenChange = "keyboard.clipboard.seenChange"
static let seenAt = "keyboard.clipboard.seenAt"
static let usedChange = "keyboard.clipboard.usedChange"
static let allowPasteHints = "keyboard.clipboard.allowPasteHints"
}
private let defaults: UserDefaults?
init(defaults: UserDefaults? = UserDefaults(suiteName: KeyboardBridge.appGroup)) {
self.defaults = defaults
}
/// Only in an empty field, as Apple's keyboard offers a recent copy (iOS 27.2): once
/// something is typed the strip goes back to suggestions. `appleKeyboardRoute` is asked only
/// where a code is expected, given whether the field is a number pad: Code from Messages shows
/// only with Apple's keyboard on (Emoji or another third-party keyboard gets no code,
/// TestFlight build 56), and opens the keyboard list where the next keyboard could be one of
/// those (TestFlight build 126). It needs no Full Access, which only Paste does.
/// `canSwitchKeyboard` is false where Apple's keyboard is
/// split (the Duo unfolded and upright, or not known yet): iOS 27.2's switch to the next
/// keyboard can end the app being typed in there (`FullKeyboardView.passesGlobeTouch`), so
/// only Paste is offered and the globe's list leads to Apple's keyboard.
func offer(
textContentType: UITextContentType?,
keyboardType: UIKeyboardType?,
autocorrectionType: UITextAutocorrectionType?,
fieldIsEmpty: Bool,
hasFullAccess: Bool,
canSwitchKeyboard: Bool = true,
appleKeyboardRoute: (_ numberPad: Bool) -> KeyboardInputModes.Route? = { KeyboardInputModes.route(numberPad: $0) },
pasteboard: UIPasteboard = .general,
now: Date = Date()
) -> KeyboardClipboardOffer? {
guard fieldIsEmpty else { return nil }
// Without Full Access a keyboard can't use the clipboard at all.
let fresh = hasFullAccess && hasFreshCopy(pasteboard: pasteboard, now: now)
if canSwitchKeyboard,
Self.expectsCode(textContentType: textContentType, keyboardType: keyboardType, autocorrectionType: autocorrectionType),
let route = appleKeyboardRoute(Self.isNumberPad(keyboardType)) {
return .code(canPaste: fresh, opensList: route == .list)
}
return fresh ? .paste : nil
}
/// A `.oneTimeCode` field with a number pad, or with autocorrection set either way: WebKit
/// always sets it, so Safari's `autocomplete="one-time-code"` fields count (iOS 27.2).
/// Some chat apps mark their message box `.oneTimeCode` too, with letters and autocorrection
/// left at default: that box gets suggestions, as it does with Apple's keyboard (TestFlight
/// build 56, ChatGPT and Capy).
static func expectsCode(
textContentType: UITextContentType?,
keyboardType: UIKeyboardType?,
autocorrectionType: UITextAutocorrectionType?
) -> Bool {
guard textContentType == .oneTimeCode else { return false }
if isNumberPad(keyboardType) || keyboardType == .numbersAndPunctuation { return true }
return autocorrectionType == .yes || autocorrectionType == .no
}
/// The pads where the switch to the next keyboard passes over Emoji and other third-party
/// keyboards (iOS 27.2 simulator).
static func isNumberPad(_ keyboardType: UIKeyboardType?) -> Bool {
[.numberPad, .asciiCapableNumberPad, .decimalPad, .phonePad].contains(keyboardType)
}
/// What the Paste chip inserts, in one read: text, else a link. Reading `string` and then
/// `url` would ask Allow Paste twice for a link.
static func read(_ pasteboard: UIPasteboard = .general) -> String? {
if pasteboard.hasStrings { return pasteboard.string }
if pasteboard.hasURLs { return pasteboard.url?.absoluteString }
return nil
}
/// After the chip's read: whether to point to Settings › Apps › Hex › Paste from Other
/// Apps. A refused read (the clipboard has text but none came back: Don't Allow, or the
/// setting is Deny) always does, since the chip did nothing; one that waited
/// `askedReadDuration` for Allow Paste does the first `allowPasteHintLimit` times, then
/// iOS's prompt is left alone.
func shouldPointToAllowPaste(readDuration: TimeInterval, refused: Bool) -> Bool {
if refused { return true }
guard readDuration >= Self.askedReadDuration else { return false }
let shown = defaults?.integer(forKey: Key.allowPasteHints) ?? 0
guard shown < Self.allowPasteHintLimit else { return false }
defaults?.set(shown + 1, forKey: Key.allowPasteHints)
return true
}
func markPasted(pasteboard: UIPasteboard = .general) {
defaults?.set(pasteboard.changeCount, forKey: Key.usedChange)
}
private func hasFreshCopy(pasteboard: UIPasteboard, now: Date) -> Bool {
guard pasteboard.hasStrings || pasteboard.hasURLs else { return false }
let change = pasteboard.changeCount
guard let defaults else { return true }
guard defaults.integer(forKey: Key.usedChange) != change else { return false }
if defaults.integer(forKey: Key.seenChange) != change {
defaults.set(change, forKey: Key.seenChange)
defaults.set(now.timeIntervalSince1970, forKey: Key.seenAt)
return true
}
return now.timeIntervalSince1970 - defaults.double(forKey: Key.seenAt) < Self.freshness
}
}