hex/ios/HEXShared/FullKeyboard/TypingJournal.swift
Ryan Vogel 0e7a1030a9
iOS: hold the bar to dictate, Learn how I type on, a plain centred Paste, and typing logs off (#2210)
* iOS: hold an empty part of the top bar to dictate

A tester found the Hex key (44 pt, in the strip) too small to hit quickly.
A still hold of 0.4 s (Apple's long press) anywhere on the bar with no word
under it now dictates exactly as holding the Hex key does: hold to talk,
release to finish. It reuses the Hex key's own path: a HexKeyGesture fed by
FullKeyboardView's touches, then the key's press (in-keyboard dictation arms)
and hexKeyHeld / hexKeyReleased, so without Full Access it shows the Full
Access hint, and a hold that can only open Hex opens Hex, as the key does.

The empty space is every touch nothing else takes in the bar: empty slots
(their buttons take no touches), the strip around Paste, Undo or a status,
the gaps beside the Hex key, and a bar with no suggestions. A word's slot
keeps its tap and its hold (Add to vocabulary, #2205). A tap on empty space
does nothing, and a slide past 10 pt cancels the hold. No hold while the bar
shows the Full Access hint or a notice, while a dictation records or
transcribes, or with the emoji panel or style menu up. Typing logs name the
source (bar_hold / bar_release).

Verified in the iOS 27.2 simulator (iPhone 17 Pro, a UIKit host app, Hex
keyboard with Full Access): holding an empty slot showed the recording panel
and releasing finished it; holding a suggestion ("The") and tapping empty
space did nothing; holds in the 2 pt gap left of the Hex key and past its
right edge took the Hex key's path (opened Hex, as the key does once
in-keyboard dictation is unavailable). On origin/dev the same hold did
nothing.

* iOS: Learn how I type is on by default

Settings › Privacy › Learn how I type now reads on when the switch was never
set (no key in the App Group: true). The toggle saves an explicit false when
turned off (UserDefaults.set on change), so "off" and "never set" are
distinct and nobody who turned it off is switched back on; no migration is
needed. Without an App Group (no Full Access) it stays off, as there is
nowhere to learn.

Every reader goes through TypingJournalSettings.isEnabled, so both sides
follow it: typing (TypingJournal.sharedIfEnabled in the full keyboard) and
dictation (the keyboard's DictationEditInference tracking in
learnsFromDictation, its learned fixes for in-keyboard dictation, and the
app's DictationPipeline fixes and raw-transcript sharing in
DictationController).

The switch's line now says what it does: "Learns on this iPhone from your
taps and the words you fix with Hex, never from what you write elsewhere"
(zh-Hans added; the old line is removed from the catalog).

Verified: TypingJournalLearningTests.testLearnHowITypeIsOnUntilTurnedOff
(an unset switch learns, one turned off stays off and stores nothing) in the
full xcodebuild test run.

* iOS: the bar's Paste is plain text, centred on the keyboard

The clipboard offer drew Paste as a chip (a filled background with a 15 pt
corner radius, 12.5 pt medium type) and centred it in the strip's bounds,
which leave out the Hex key, so it sat visibly left of the middle. Ryan
wanted no chip: plain text in the bar like a suggestion word.

- Paste (and Paste dictation) is now SF 17 pt regular in the bar words' ink
  (palette.stripText), with no background. The clipboard glyph stays, sized
  to the text's font (small scale), since it sits on the line beside the word.
- Paste and the Next keyboard chip (or the pair in a code field) centre on
  the keyboard's middle, where the middle slot's word centres
  (KeyboardStripView.barMidX = stripBarFrame.midX in the strip's
  coordinates), clamped inside the strip.
- Their line stays the bar's word line (textCenterY,
  KeyboardMetrics.stripTextCenter from #2207); the chip and line geometry
  of #2207 is unchanged.

Verified in the iOS 27.2 simulator (iPhone 17 Pro, a copied string through
simctl pbcopy): before, the chip spanned x 126-219 pt of a 402 pt keyboard
(centre 172); after, glyph and word span 169-232 (centre 201, the keyboard's
middle), on the same line as "The" in the middle slot.

* iOS: turn Record typing logs off once for everyone

The bar's "● Logging" mark shows while Settings › Privacy › Advanced ›
Record typing logs is on, and Ryan wants it gone. A one-time migration,
TypingLogSettings.turnOffOnce, switches the setting off for anyone who has it
on and records migrations.typingLogsOff.v1 in the App Group so it never runs
again: turning it back on afterwards keeps it on. It runs where either
process starts, the app's launch (before the simulator scenes' launch
argument) and the keyboard's viewDidLoad (before any typing log session), so
a keyboard updated before Hex is opened drops the mark too. The logs already
recorded stay, and so does the switch in Settings.

Nothing in a build turns it on: the key is set to true only by the switch's
consent dialog, the simulator-only --experiment-typing-logs launch argument,
and the Settings snapshot fixtures (settings-*-on scenes); no scheme,
ci_scripts or Xcode Cloud step passes either, so there was no build or
TestFlight default to remove.

Verified: TypingLogTests.testTypingLogsTurnOffOnceAndKeepWhatTheyRecorded
in the full xcodebuild test run, and in the iOS 27.2 simulator: with
experiments.typingLogs set to true and the flag removed from the keyboard's
defaults, showing the keyboard left it false and the flag true.
2026-11-21 12:11:58 -05:00

387 lines
No EOL
16 KiB
Swift

import CoreGraphics
import Foundation
import UIKit
/// Learn how I type (on by default): a private, on-device record of how you type and dictate,
/// kept in the App Group so the app can show it and the keyboard can personalize itself.
///
/// It is deliberately not a keystroke log or a copy of what you write. Taps are folded into
/// per-key averages (where your finger lands relative to each key), so no sequence of letters
/// is ever stored, and corrections are word-level pairs: a word you erased and retyped,
/// autocorrect fixes, fixes you undid, suggestions you picked, and words you fixed in a
/// dictation, with at most a word of context on each side. Nothing leaves the iPhone.
struct TypingJournalSettings {
private enum Key {
static let enabled = "experiments.typingJournal"
static let learnsPerLanguage = "experiments.learnPerLanguage"
static let disabledDictationFixes = "typingJournal.disabledDictationFixes"
static let erasedAt = "typingJournal.erasedAt"
}
private let defaults: UserDefaults?
init(defaults: UserDefaults? = UserDefaults(suiteName: KeyboardBridge.appGroup)) {
self.defaults = defaults
}
/// Learn how I type: on until you turn it off. Only a switch you turned off is saved as
/// off, so an iPhone that never touched it learns.
var isEnabled: Bool {
get { defaults.map { $0.object(forKey: Key.enabled) as? Bool ?? true } ?? false }
nonmutating set { defaults?.set(newValue, forKey: Key.enabled) }
}
/// Experiment (Learn each language separately): learned words and fixes stay with the
/// language you typed or dictated them in. Off by default, like every experiment: one
/// shared list and one shared learning, exactly as before.
var learnsPerLanguage: Bool {
get { defaults?.bool(forKey: Key.learnsPerLanguage) ?? false }
nonmutating set { defaults?.set(newValue, forKey: Key.learnsPerLanguage) }
}
/// Learned dictation fixes you switched off in Typing insights (their ids).
var disabledDictationFixes: Set<String> {
get { Set(defaults?.stringArray(forKey: Key.disabledDictationFixes) ?? []) }
nonmutating set { defaults?.set(newValue.sorted(), forKey: Key.disabledDictationFixes) }
}
/// The words autocorrect learned to leave alone (`LearnedWordsStore`), the shared list
/// and every language's own.
func eraseLearnedWords() {
guard let defaults else { return }
defaults.removeObject(forKey: LearnedWordsStore.defaultsKey)
for key in defaults.dictionaryRepresentation().keys where LearnedWordsStore.isScopedKey(key) {
defaults.removeObject(forKey: key)
}
}
/// When Delete what Hex learned last ran, so another process drops what it still holds.
var erasedAt: TimeInterval {
get { defaults?.double(forKey: Key.erasedAt) ?? 0 }
nonmutating set { defaults?.set(newValue, forKey: Key.erasedAt) }
}
}
nonisolated struct TypingJournalKeyStats: Codable, Equatable, Sendable {
var count = 0
/// Sums of the tap's offset from the keycap centre, in keycap widths and heights.
var dx = 0.0
var dy = 0.0
var meanOffset: CGVector { count == 0 ? .zero : CGVector(dx: dx / Double(count), dy: dy / Double(count)) }
}
nonisolated struct TypingJournalCorrection: Codable, Equatable, Sendable {
enum Kind: String, Codable, Sendable {
/// You erased a word and typed another.
case retyped
/// Autocorrect replaced it on space.
case autocorrected
/// You undid autocorrect with one backspace.
case undone
/// You tapped a suggestion.
case picked
/// You tapped what you typed instead of the autocorrection space would apply.
case keptTyped
}
var kind: Kind
var typed: String
var final: String
var at: TimeInterval
/// The keyboard language it was typed in (`KeyboardLanguage.rawValue`); nil for a
/// journal written before corrections carried one. It marks the language either way
/// (`Learn each language separately`).
var language: String? = nil
}
nonisolated struct TypingJournalSnapshot: Codable, Equatable, Sendable {
static let correctionLimit = 2000
static let dictationFixLimit = 1000
static let dictationOutcomeLimit = 500
var keys: [String: TypingJournalKeyStats] = [:]
var corrections: [TypingJournalCorrection] = []
/// Word-level (heard → meant) pairs from dictations, with counts.
var dictationFixes: [TypingJournalDictationFix] = []
/// What happened to each dictation (kept, edited, …), without its text.
var dictationOutcomes: [TypingJournalDictationOutcome] = []
var tapCount: Int { keys.values.reduce(0) { $0 + $1.count } }
var isEmpty: Bool {
tapCount == 0 && corrections.isEmpty && dictationFixes.isEmpty && dictationOutcomes.isEmpty
}
}
extension TypingJournalSnapshot {
private enum CodingKeys: String, CodingKey {
case keys, corrections, dictationFixes, dictationOutcomes
}
/// Journals written before dictation learning have only keys and corrections.
nonisolated init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
keys = try container.decodeIfPresent([String: TypingJournalKeyStats].self, forKey: .keys) ?? [:]
corrections = try container.decodeIfPresent([TypingJournalCorrection].self, forKey: .corrections) ?? []
dictationFixes = try container.decodeIfPresent([TypingJournalDictationFix].self, forKey: .dictationFixes) ?? []
dictationOutcomes = try container.decodeIfPresent([TypingJournalDictationOutcome].self, forKey: .dictationOutcomes) ?? []
}
}
@MainActor
final class TypingJournal {
static let fileName = "typing-journal.json"
private static var shared: TypingJournal?
/// The one journal this process writes while Learn how I type is on, shared by the
/// keys and dictation so they never overwrite each other; nil while it is off.
static func sharedIfEnabled(settings: TypingJournalSettings = TypingJournalSettings()) -> TypingJournal? {
guard settings.isEnabled else {
shared?.save()
shared = nil
return nil
}
if let shared { return shared }
let journal = TypingJournal(settings: settings)
shared = journal
return journal
}
private let url: URL?
private let settings: TypingJournalSettings
private var snapshot: TypingJournalSnapshot
/// Taps not yet written, added to whatever is on disk when saving.
private var pendingKeys: [String: TypingJournalKeyStats] = [:]
private var unsavedTaps = 0
private var erasedWord: String?
private var loadedAt: TimeInterval
private var fileDate: Date?
private var learning: (revision: Int, value: TypingLearning)?
/// `typingLearning(for:)`, one language at a time, for Learn each language separately.
private var languageLearning: [String: (revision: Int, value: TypingLearning)] = [:]
/// Bumped whenever corrections or dictation fixes change.
private(set) var revision = 0
init(
directory: URL? = FileManager.default.containerURL(
forSecurityApplicationGroupIdentifier: KeyboardBridge.appGroup
),
settings: TypingJournalSettings = TypingJournalSettings()
) {
url = directory?.appendingPathComponent(Self.fileName)
self.settings = settings
snapshot = Self.load(from: url)
loadedAt = Date().timeIntervalSince1970
fileDate = url.flatMap(Self.modificationDate)
}
static func load(from url: URL?) -> TypingJournalSnapshot {
guard let url, let data = try? Data(contentsOf: url),
let snapshot = try? JSONDecoder().decode(TypingJournalSnapshot.self, from: data)
else { return TypingJournalSnapshot() }
return snapshot
}
/// Erases everything Learn how I type gathered: the file, the switched-off fixes, the words
/// autocorrect learned to keep, and (through `erasedAt`) whatever another process still
/// holds in memory.
static func delete(
in directory: URL? = FileManager.default.containerURL(
forSecurityApplicationGroupIdentifier: KeyboardBridge.appGroup
),
settings: TypingJournalSettings = TypingJournalSettings()
) {
settings.erasedAt = Date().timeIntervalSince1970
settings.disabledDictationFixes = []
settings.eraseLearnedWords()
if let url = directory?.appendingPathComponent(fileName) {
try? FileManager.default.removeItem(at: url)
}
shared?.refreshFromDisk()
}
/// The learned dictation fixes on disk, for the app's dictation; empty while off.
static func storedDictationCorrections(
settings: TypingJournalSettings = TypingJournalSettings(),
directory: URL? = FileManager.default.containerURL(
forSecurityApplicationGroupIdentifier: KeyboardBridge.appGroup
),
language: String? = nil
) -> [DictationCorrection] {
guard settings.isEnabled else { return [] }
return load(from: directory?.appendingPathComponent(fileName)).dictationCorrections(
disabled: settings.disabledDictationFixes,
language: settings.learnsPerLanguage ? LanguageLearning.dictationCode(language) : nil
)
}
/// Fields a journal may learn from: plain text that allows autocorrect, never
/// addresses, codes, or anything credential-shaped.
static func allows(field: KeyboardFieldKind, target: KeyboardTextTarget) -> Bool {
guard field == .text, target.autocorrectionType != .no else { return false }
let blocked: Set<UITextContentType> = [
.password, .newPassword, .oneTimeCode, .username, .emailAddress, .URL,
.creditCardNumber, .telephoneNumber,
]
if let type = target.textContentType, blocked.contains(type) { return false }
return true
}
var current: TypingJournalSnapshot {
var merged = snapshot
Self.add(pendingKeys, to: &merged)
return merged
}
/// Typo fixes and rejected autocorrections for the keys, derived once per change.
var typingLearning: TypingLearning {
refreshFromDisk()
if let learning, learning.revision == revision { return learning.value }
let value = TypingLearning.derive(from: snapshot.corrections)
learning = (revision, value)
return value
}
/// Typo fixes and rejected autocorrections for `language`'s keys: with Learn each
/// language separately on, only the corrections typed in that language and the ones never
/// tagged; off, all of them.
func typingLearning(for language: KeyboardLanguage?) -> TypingLearning {
guard settings.learnsPerLanguage, let language else { return typingLearning }
refreshFromDisk()
let code = language.rawValue
if let cached = languageLearning[code], cached.revision == revision { return cached.value }
let value = TypingLearning.derive(from: snapshot.corrections, language: code)
languageLearning[code] = (revision, value)
return value
}
/// The learned dictation fixes, minus the ones switched off in Typing insights.
func dictationCorrections(language: String? = nil) -> [DictationCorrection] {
refreshFromDisk()
return snapshot.dictationCorrections(
disabled: settings.disabledDictationFixes,
language: settings.learnsPerLanguage ? LanguageLearning.dictationCode(language) : nil
)
}
func recordTap(on key: KeyboardKey, at point: CGPoint) {
guard let character = key.character?.lowercased(), character.count == 1,
key.frame.width > 0, key.frame.height > 0 else { return }
dropPendingIfErased()
var stats = pendingKeys[character] ?? TypingJournalKeyStats()
stats.count += 1
stats.dx += Double((point.x - key.frame.midX) / key.frame.width)
stats.dy += Double((point.y - key.frame.midY) / key.frame.height)
pendingKeys[character] = stats
unsavedTaps += 1
if unsavedTaps >= 40 { save() }
}
/// Called before a backspace removes part of the word being typed.
func willErase(word: String?) {
guard erasedWord == nil, let word, !word.isEmpty else { return }
erasedWord = word
}
/// Called when a word is finished; a retyped word close to the erased one is a fix.
func didFinish(word: String?, language: String? = nil) {
defer { erasedWord = nil }
guard let erased = erasedWord, let word, !word.isEmpty,
erased.lowercased() != word.lowercased(),
Self.distance(erased.lowercased(), word.lowercased()) <= max(2, word.count / 2)
else { return }
record(.retyped, typed: erased, final: word, language: language)
}
func record(_ kind: TypingJournalCorrection.Kind, typed: String, final: String, language: String? = nil) {
guard !typed.isEmpty, !final.isEmpty, typed != final else { return }
if kind != .retyped { erasedWord = nil }
refreshFromDisk()
snapshot.corrections.append(
TypingJournalCorrection(
kind: kind, typed: typed, final: final, at: Date().timeIntervalSince1970, language: language
)
)
if snapshot.corrections.count > TypingJournalSnapshot.correctionLimit {
snapshot.corrections.removeFirst(snapshot.corrections.count - TypingJournalSnapshot.correctionLimit)
}
revision += 1
save()
}
/// Adds what happened to one dictation: its outcome, and its word pairs with counts,
/// kept under the language it was dictated in (`LanguageLearning.dictationCode`).
func recordDictation(_ result: DictationEditResult, language: String? = nil) {
refreshFromDisk()
let now = Date().timeIntervalSince1970
snapshot.dictationOutcomes.append(TypingJournalDictationOutcome(
outcome: result.outcome,
words: result.wordCount,
changedWords: result.changedWordCount,
at: now
))
if snapshot.dictationOutcomes.count > TypingJournalSnapshot.dictationOutcomeLimit {
snapshot.dictationOutcomes.removeFirst(
snapshot.dictationOutcomes.count - TypingJournalSnapshot.dictationOutcomeLimit
)
}
snapshot.dictationFixes = TypingJournalDictationFix.merging(
result, into: snapshot.dictationFixes, at: now, language: LanguageLearning.dictationCode(language)
)
revision += 1
save()
}
func save() {
refreshFromDisk()
Self.add(pendingKeys, to: &snapshot)
pendingKeys = [:]
unsavedTaps = 0
guard let url, let data = try? JSONEncoder().encode(snapshot) else { return }
try? data.write(to: url, options: .atomic)
fileDate = Self.modificationDate(url)
}
/// Picks up what another process (the keyboard, or the app's keyboard preview) wrote,
/// and forgets everything when Delete what Hex learned ran since this journal loaded.
private func refreshFromDisk() {
dropPendingIfErased()
guard let url else { return }
let date = Self.modificationDate(url)
guard date != fileDate else { return }
snapshot = Self.load(from: url)
fileDate = date
revision += 1
}
private func dropPendingIfErased() {
let erasedAt = settings.erasedAt
guard erasedAt > loadedAt else { return }
snapshot = TypingJournalSnapshot()
pendingKeys = [:]
unsavedTaps = 0
erasedWord = nil
loadedAt = erasedAt
fileDate = nil
revision += 1
}
private static func add(_ pending: [String: TypingJournalKeyStats], to snapshot: inout TypingJournalSnapshot) {
for (key, stats) in pending {
var merged = snapshot.keys[key] ?? TypingJournalKeyStats()
merged.count += stats.count
merged.dx += stats.dx
merged.dy += stats.dy
snapshot.keys[key] = merged
}
}
private static func modificationDate(_ url: URL) -> Date? {
(try? FileManager.default.attributesOfItem(atPath: url.path))?[.modificationDate] as? Date
}
static func distance(_ a: String, _ b: String) -> Int {
DictationText.distance(a, b)
}
}