hex/ios/HEXShared/FullKeyboard/FullKeyboardSettings.swift
Ryan Vogel 91ddb83369 iOS: Auto-capitalization switch for the full keyboard
TestFlight testers (builds 54/55) asked to turn off the capital at the start
of their typing ("you can lowercase everything on dictation but not regular
keyboard"). Settings › Keyboard gains Auto-capitalization, above Autocorrect,
on by default, as Apple's switch of the same name
(FullKeyboardSettings.autoCapitalization, App Group key
settings.keyboardAutoCapitalization; "Capitalizes the start of sentences",
zh-Hans 自动大写 / 句首字母自动大写).

Off, Hex adds no capital you didn't type, matching Apple's keyboard with
Auto-Capitalization off on the iOS 27.2 simulator ("hello world. i am mary.
im here. monday" -> "hello world. i am mary. i'm here. monday"; "^hi i speak
englsh on mondya with ^mary" -> "Hi i speak english on monday with Mary"):
- Shift never turns itself on, whatever the field's autocapitalizationType
  (sentences, words, allCharacters): not at a field's or sentence's start,
  after "." "!" "?", or after the double-space period (which still types
  ". "). FullKeyboardEngine.autoCapitalizes maps the mode to .none.
- Autocorrect (AutocorrectEngine.addsCapitals, passed to bilingual
  autocorrect's partner): English fixes, Spatial readings and UITextChecker
  guesses keep the case you typed ("i" stays, "im" -> "i'm", "mondya" ->
  "monday"); a fix that would only add a capital ("i" -> "I") isn't made.
  #2091's lowercase-name capitalisation ("mary" -> "Mary") is skipped.
- Swipe typing (#2106): no sentence-start capital from the space it adds,
  and no name capital; readings come out lowercase.
Kept: capitals you typed (shift, caps lock, "Teh" -> "The", "Im" -> "I'm"),
and your own spellings: text replacements, Contacts names and other UILexicon
terms, words you kept, fixes Hex learned (isOwnSpelling). The strip still
offers capitalised words to pick. On, nothing changes.
Dictation is separate: Settings › Dictation › Lowercase (#2103) is untouched.

Verified on an iPhone 17 simulator (iOS 27.2), Mac Studio:
- xcodebuild test: 365 tests, 0 failures, 1 skipped (needs
  HEX_SPEECH_CLIPS), including the new cases in FullKeyboardEngineTests
  (shift never turns on in any mode, shift and caps lock work, switching
  back on capitalises; a swiped word after "Great." stays lowercase) and
  AutocorrectEngineTests (fixes add no capitals; Spatial keeps "mary" and
  "monday" lowercase and a Contacts name's capital).
- The Settings switch, tapped, stored false in the App Group.
- Live in Messages with Hex's keyboard, off: an empty message showed
  lowercase keys; "hello world. i think im here. mary is here! ok" typed as
  "hello world. i think i'm here. mary is here! ok", and shift then "ok"
  gave "Ok". On: "Hello world. I think I'm here. Mary is here! Ok", as before.
- Settings › Keyboard rendered in English and Simplified Chinese.
2026-11-20 18:53:02 -05:00

126 lines
6 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 `--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"
}
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) }
}
/// Experiment: an emoji key whose panel suggests emoji for what you just typed, from
/// what the words mean and what you use. Off by default.
var emoji: Bool {
get { defaults?.bool(forKey: Key.emoji) ?? false }
nonmutating set { defaults?.set(newValue, forKey: Key.emoji) }
}
/// Experiment: slide through a word's letters to type it, on the English and Spanish
/// layouts (`SwipeTyping`). Off by default.
var swipeTyping: Bool {
get { defaults?.bool(forKey: Key.swipeTyping) ?? false }
nonmutating set { defaults?.set(newValue, forKey: Key.swipeTyping) }
}
/// Simulator scenes pass `--experiment-full-keyboard` or `--keyboard full|dictation-bar`
/// to pick the keyboard, `--experiment-emoji` to turn the emoji experiment on, and
/// `--experiment-swipe` for Swipe typing.
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
}
}
}