hex/ios/HEXShared/FullKeyboard/FullKeyboardSettings.swift
Ryan Vogel 82f5df56c9
iOS: Emoji and Swipe typing are on by default, under Settings › Keyboard (#2129)
Ryan approved graduating both full-keyboard experiments. Swipe typing
(#2106) and the emoji key and panel (#2051) are on for new users and
move out of Experiments.

- Defaults: FullKeyboardSettings.emoji and .swipeTyping read
  experiments.emoji and experiments.swipeTyping as object-or-true (was
  bool(forKey:), unset meaning off), as spatialAutocorrect did in #2097.
  Only their Experiments switches ever wrote those keys, and only on a
  change, so unset means never touched: fresh installs and testers who
  never touched them get them on, a stored false stays off, a stored
  true stays on. The keys keep their names; no migration. Without the
  App Group (no Full Access on an iPhone) both read as on, like the
  keyboard's other on-by-default switches. --experiment-emoji and
  --experiment-swipe still turn them on.
- Settings › Keyboard: a card under the main one with Swipe typing and
  Emoji, plus Clear emoji history while Emoji is on. Both keep "Needs the
  full keyboard" while the Dictation bar is chosen (now from the page's
  own Full/Dictation bar choice). Settings › Experiments keeps Hinglish
  and Clean up with Apple Intelligence. No new strings; every string
  already had zh-Hans.
- Record typing logs' session line adds swipeTyping and emoji, beside
  spatialAutocorrect and cleanupTagger.
- settings-<page> scenes write both switches on, as for
  spatialAutocorrect. Doc comments and README point at Settings ›
  Keyboard.
- Test: KeyboardLayoutTests.testSwipeTypingAndEmojiAreOnByDefaultAnd-
  AStoredOffStaysOff (unset, no App Group, stored off, launch arguments).

Held back: Clean up what I say stays off by default. On the held-out
generated set its false deletes match the reference (0.5% of words; 3.9%
of clean and 2.7% of literal sentences lose a word; 1/43 hand-written
clean sentences), and "late" was never deleted (0/247). But it deletes
every hyphenated name tried: "Tell Hye-jin I said thanks." became "Tell
thanks.", and Jean-Luc, Anne-Marie, Mary-Kate, Ji-woo, Marie-Claire,
Smith-Jones and Zeta-Jones all went (P(DELETE) 0.996-1.0), with
"follow-up" and "re-entry". It also drops a literal "maybe" ("Call me
later, maybe after dinner").

Verified on iPhone 17 simulators (iOS 27.2) on the Mac Studio:
- xcodebuild test: 383 tests, 1 skipped (needs HEX_SPEECH_CLIPS), 0
  failures.
- Swipe on vs off, taps unchanged: the hex-vs-apple held-out sets (2,000
  English and 2,000 Spanish sentences) typed through a real
  KeyboardKeyPlaneView with a temporary, uncommitted harness gave
  identical output in every sentence and 0 swipes from 116,253 letter
  touches, for still taps, motion drawn from Record typing logs, every
  letter dragged 30-95% of a key's pitch, and overlapping (rollover) taps.
  English WER 5.94%, 79/9,203 correct words broken (still) both ways;
  Spanish 8.61%, 23/7,978. A control dragging 1.05-1.5 pitches made
  7,000/7,000 swipes with it on, 0 off. In the typing logs, 1 of about
  1,000 letter touches moved more than a pitch.
- Live keyboard in Messages: with no stored keys the emoji key shows
  and a drag from h to e swipes "He" (strip: Here, Her, He's); with both
  stored false (an upgrade that turned them off) there's no emoji key
  and the same drag slides and types "E".
- Keyboard memory (Release, Full Access, mock mic, probe transcript,
  vmmap physical footprint, resident/peak MB, 2 runs each; a temporary,
  uncommitted probe faked the transcript and headroom):
    typing, emoji and swipe off            34.0 / 46.1
    typing, both on                        35.0-39.9 / 47.0-51.8
    + in-keyboard dictation, Clean up off  35.0 / 46.6
    + dictation, Clean up on               37.6-43.6 / 46.5-51.9
    + emoji panel, a few picks             38.3-38.8 / 51.4-52.2
    + every emoji category browsed         58.8-59.3 / 68.2-68.3
    + all that, then dictation, Clean up   69.2 / 73.7
  Typing sets the peak, not the tagger (loaded after recording stops,
  about 2.6 MB). With a faked 60 MB headroom the 80 MB cleanup guard
  skipped the tagger and the transcript went in as heard (2/2). The
  simulator reports no headroom, so #2045's 32 MB start guard can't fire
  there. On a 4 GB iPhone (reported limits 60-77 MB), browsing every
  emoji category approaches the limit, and a dictation after it would
  open Hex instead. Unverified on a device.
2026-11-21 02:41:15 +00:00

155 lines
7.7 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 Foundation
/// The full keyboard's switches in the shared App Group.
///
/// Without Full Access an iPhone may not let the keyboard read the App Group (the simulator
/// does), so every switch's default must work there: the full keyboard, which types
/// without Full Access.
@MainActor
struct FullKeyboardSettings {
static let launchArgument = "--experiment-full-keyboard"
static let emojiLaunchArgument = "--experiment-emoji"
static let swipeLaunchArgument = "--experiment-swipe"
/// Simulator scenes pass `--hex-key left` (or `right`) for Settings › Keyboard › Hex key.
static let hexKeyLaunchArgument = "--hex-key"
/// Simulator scenes pass `--keyboard dictation-bar` (or `full`) to pick the keyboard.
static let keyboardLaunchArgument = "--keyboard"
private enum Key {
static let fullKeyboard = "experiments.fullKeyboard"
static let autocorrect = "settings.keyboardAutocorrect"
static let autoCapitalization = "settings.keyboardAutoCapitalization"
static let spatialAutocorrect = "experiments.spatialAutocorrect"
static let keyPopups = "settings.keyboardKeyPopups"
static let haptics = "settings.keyboardHaptics"
static let suggestSwearWords = "settings.keyboardSuggestSwearWords"
static let emoji = "experiments.emoji"
static let swipeTyping = "experiments.swipeTyping"
static let hexKeyOnLeft = "settings.keyboardHexKeyLeft"
}
private let defaults: UserDefaults?
init(defaults: UserDefaults? = UserDefaults(suiteName: KeyboardBridge.appGroup)) {
self.defaults = defaults
}
/// Settings › Keyboard: Full (true) or Dictation bar (false). The full keyboard (letters,
/// numbers, symbols, autocorrect and the Hex key) by default; the dictation bar (space,
/// delete, return and Dictate) only once it was chosen. Stored as
/// `experiments.fullKeyboard`, from when the full keyboard was an experiment: unset or
/// true is the full keyboard and false the dictation bar, so testers who never touched
/// the experiment get the full keyboard and those who turned it off keep the bar.
var isEnabled: Bool {
get { defaults?.object(forKey: Key.fullKeyboard) as? Bool ?? true }
nonmutating set { defaults?.set(newValue, forKey: Key.fullKeyboard) }
}
/// Settings › Keyboard › Auto-capitalization, as Apple's switch of the same name. On by
/// default: shift turns itself on where the field asks (the start of a field or sentence,
/// after "." "!" "?"). Off, Hex never adds a capital you didn't type: shift stays off
/// whatever the field's `autocapitalizationType`, and autocorrect and swipe typing keep
/// words lowercase ("i", "i'm", "monday") unless they're your own spelling. Dictation
/// keeps its own Lowercase setting.
var autoCapitalization: Bool {
get { defaults?.object(forKey: Key.autoCapitalization) as? Bool ?? true }
nonmutating set { defaults?.set(newValue, forKey: Key.autoCapitalization) }
}
/// Cautious autocorrect on space. On by default.
var autocorrect: Bool {
get { defaults?.object(forKey: Key.autocorrect) as? Bool ?? true }
nonmutating set { defaults?.set(newValue, forKey: Key.autocorrect) }
}
/// Settings › Keyboard › Spatial autocorrect: autocorrect reads your taps with a language
/// model (`TypingDecoder`) instead of `UITextChecker`'s one-edit guesses, in English and
/// Spanish. On by default. Stored as `experiments.spatialAutocorrect`, from when it was an
/// experiment (off by default) that only its switch ever wrote: unset or true is on and
/// false off, so testers who never touched it get it and those who turned it off keep it off.
var spatialAutocorrect: Bool {
get { defaults?.object(forKey: Key.spatialAutocorrect) as? Bool ?? true }
nonmutating set { defaults?.set(newValue, forKey: Key.spatialAutocorrect) }
}
/// Suggestions and autocorrect may offer swear words (`OffensiveWords`); slurs never.
/// Off by default, like Apple's keyboard.
var suggestSwearWords: Bool {
get { defaults?.bool(forKey: Key.suggestSwearWords) ?? false }
nonmutating set { defaults?.set(newValue, forKey: Key.suggestSwearWords) }
}
/// The enlarged letter that pops up over a pressed key. On by default.
var keyPopups: Bool {
get { defaults?.object(forKey: Key.keyPopups) as? Bool ?? true }
nonmutating set { defaults?.set(newValue, forKey: Key.keyPopups) }
}
/// A light tap under your finger for each key press. On by default.
var haptics: Bool {
get { defaults?.object(forKey: Key.haptics) as? Bool ?? true }
nonmutating set { defaults?.set(newValue, forKey: Key.haptics) }
}
/// Settings › Keyboard › Emoji: an emoji key whose panel suggests emoji for what you just
/// typed, from what the words mean and what you use. On by default. Stored as
/// `experiments.emoji`, from when it was an experiment (off by default) that only its
/// switch ever wrote: unset or true is on and false off, so testers who turned it off
/// keep it off.
var emoji: Bool {
get { defaults?.object(forKey: Key.emoji) as? Bool ?? true }
nonmutating set { defaults?.set(newValue, forKey: Key.emoji) }
}
/// Settings › Keyboard › Swipe typing: slide through a word's letters to type it, on the
/// English and Spanish layouts (`SwipeTyping`). On by default. Stored as
/// `experiments.swipeTyping`, from when it was an experiment (off by default): unset or
/// true is on and false off, so testers who turned it off keep it off.
var swipeTyping: Bool {
get { defaults?.object(forKey: Key.swipeTyping) as? Bool ?? true }
nonmutating set { defaults?.set(newValue, forKey: Key.swipeTyping) }
}
/// Settings › Keyboard › Hex key: Right (false, the default) or Left. Left puts the Hex key
/// at the strip's leading end, for people who type with their left thumb; everything
/// tied to it (the recording bar's ✕ and ✓, its menu, the Full Access hint) follows.
var hexKeyOnLeft: Bool {
get { defaults?.bool(forKey: Key.hexKeyOnLeft) ?? false }
nonmutating set { defaults?.set(newValue, forKey: Key.hexKeyOnLeft) }
}
/// Where the full keyboard's Hex key sits for `hexKeyOnLeft`.
var hexKeyPlacement: HexKeyPlacement {
hexKeyOnLeft ? .stripLeading : .stripTrailing
}
/// Simulator scenes pass `--experiment-full-keyboard` or `--keyboard full|dictation-bar`
/// to pick the keyboard, `--experiment-emoji` to turn Emoji on, `--experiment-swipe` for
/// Swipe typing (both on by default; the arguments override a stored off), and
/// `--hex-key left|right` for the Hex key's side.
func applyLaunchArguments(_ arguments: [String] = ProcessInfo.processInfo.arguments) {
if arguments.contains(Self.launchArgument) {
isEnabled = true
}
if let index = arguments.firstIndex(of: Self.keyboardLaunchArgument), index + 1 < arguments.count {
switch arguments[index + 1] {
case "full": isEnabled = true
case "dictation-bar": isEnabled = false
default: break
}
}
if arguments.contains(Self.emojiLaunchArgument) {
emoji = true
}
if arguments.contains(Self.swipeLaunchArgument) {
swipeTyping = true
}
if let index = arguments.firstIndex(of: Self.hexKeyLaunchArgument), index + 1 < arguments.count {
switch arguments[index + 1] {
case "left": hexKeyOnLeft = true
case "right": hexKeyOnLeft = false
default: break
}
}
}
}