mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
TestFlight build 56 (iPhone 16, iOS 27.2.1): in ChatGPT and Capy the strip's
Paste and Code from Messages chips stayed up the whole time, in place of
suggestions, even with nothing to paste.
Cause: the strip treated every .oneTimeCode field as a code field for as
long as it had focus, and a code field's chips always replaced suggestions.
Chat apps can mark their message box .oneTimeCode (inferred from a test
host: ChatGPT and Capy can't run on a simulator). Such a box reports the
same content type, keyboard type and return key as Safari's
autocomplete=one-time-code input; only autocorrection differs (a native box
leaves it at default, WebKit always sets it). Separately, Paste replaced
next-word suggestions after every word for three minutes after a copy.
Change (KeyboardClipboardTracker.offer, now given the field's keyboard type,
autocorrection and whether it is empty):
- Nothing is offered unless the field is empty, as Apple's keyboard offers a
recent copy only in an empty field (iOS 27.2 black box). Removed: Paste in
place of next-word suggestions in a field that has text.
- Code from Messages needs a .oneTimeCode field with a number-style keyboard,
or with autocorrection set either way (Safari's fields), plus Apple's
keyboard as before. A .oneTimeCode box with letters and default
autocorrection gets suggestions and, for a fresh copy, Paste.
- Trade-off: a native .oneTimeCode text field with letters and default
autocorrection no longer gets the chip (Paste for a fresh copy still shows).
Verified on an iPhone 16 simulator, iOS 27.2, with real touches. Before (build
56 and dev f4a45df8), in a .oneTimeCode UITextView, the chips stayed while
empty, typing and after words, with or without a copy. Autocorrect worked
(teh -> the), but its suggestions were hidden. After: a fresh copy gives Paste
only while empty, a copy older than three minutes or an empty clipboard gives
starters, typing gives suggestions. A number-pad code field shows Paste + Code
from Messages and the chip leaves after a digit. Safari
autocomplete=one-time-code input, inputmode=numeric input and textarea keep the
chip, it leaves on typing, and a plain input gets starters. With only Hex +
Emoji there is no chip, and Paste only for a fresh copy. Apple's keyboard in
the same chat box: normal predictions and autocorrect, and "Paste from <app>"
for a recent copy only while empty. xcodebuild test: 675 tests, 1 skipped, 0
failures, with a new KeyboardClipboardTests case.
165 lines
8 KiB
Swift
165 lines
8 KiB
Swift
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).
|
||
case code(canPaste: Bool)
|
||
/// 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 skips Emoji when Apple's keyboard is on, in
|
||
/// either order (simulator, black box).
|
||
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")
|
||
}
|
||
}
|
||
}
|
||
|
||
/// 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. `hasAppleKeyboard` is asked only
|
||
/// where a code is expected: without Apple's keyboard the next keyboard is Emoji or another
|
||
/// third-party one (or there is none), which gets no code, so Code from Messages would do
|
||
/// nothing (TestFlight build 56).
|
||
func offer(
|
||
textContentType: UITextContentType?,
|
||
keyboardType: UIKeyboardType?,
|
||
autocorrectionType: UITextAutocorrectionType?,
|
||
fieldIsEmpty: Bool,
|
||
hasFullAccess: Bool,
|
||
hasAppleKeyboard: () -> Bool = { KeyboardInputModes.hasAppleKeyboard() },
|
||
pasteboard: UIPasteboard = .general,
|
||
now: Date = Date()
|
||
) -> KeyboardClipboardOffer? {
|
||
// Without Full Access a keyboard can't use the clipboard at all.
|
||
guard hasFullAccess, fieldIsEmpty else { return nil }
|
||
let fresh = hasFreshCopy(pasteboard: pasteboard, now: now)
|
||
if Self.expectsCode(textContentType: textContentType, keyboardType: keyboardType, autocorrectionType: autocorrectionType),
|
||
hasAppleKeyboard() {
|
||
return .code(canPaste: fresh)
|
||
}
|
||
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 }
|
||
switch keyboardType {
|
||
case .numberPad, .asciiCapableNumberPad, .decimalPad, .phonePad, .numbersAndPunctuation: return true
|
||
default: return autocorrectionType == .yes || autocorrectionType == .no
|
||
}
|
||
}
|
||
|
||
/// 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
|
||
}
|
||
}
|
||
|