hex/ios/HEXMobile/SettingsComponents.swift
Ryan Vogel b8b161d499
iOS keyboard: Emoji key and Emoji suggestions as two switches (#2740)
* iOS keyboard: Emoji key and Emoji suggestions are two switches

Settings › Keyboard › Emoji was one switch for the emoji key and the
strip's emoji, so the only way to lose the key was to lose the
suggestions too. Three testers asked to keep the suggestions without the
key (X, tagged by Ryan, and a second person under it, 2026-11-24;
TestFlight build 148 feedback AOZxo_bQHPVi, 2026-11-23).

- Emoji key keeps the old key (experiments.emoji: unset or true on) and
  covers the key on the full keyboard and the dictation bar, with its
  panel and search. Emoji suggestions (settings.keyboardEmojiSuggestions)
  covers the strip's emoji for the typed and the finished word, and Emoji
  shortcodes' while that experiment is on.
- Migration: unset, Emoji suggestions reads Emoji key's value, so the one
  switch's on or off carries over to both in the app and the keyboard
  whichever reads first, with nothing written; the first change to Emoji
  key writes the suggestions' value it had, so turning the key off later
  leaves them on.
- With the key off there is no way into Hex's emoji panel or search
  (Apple's emoji keyboard stays a globe press away); suggestions don't
  need it.
- Settings: "Emoji key" ("Opens all emoji from the keyboard"; with the
  full keyboard off, "An emoji key on the dictation bar" as before) and,
  with the full keyboard on, "Emoji suggestions" ("Suggests an emoji for
  the word you type, like 😭 for cry"). Clear emoji history shows while
  either is on. "cry" rather than the tester's "sob": the index gives
  sob → 😭 a weight of 0.6, under the strip's 0.85, so "sob" offers
  nothing today. English and Simplified Chinese, the catalog's only
  languages.
- Removed: the "Emoji" switch title and "Emoji key and suggestions"
  detail, and FullKeyboardSettings.emoji (now emojiKey and
  emojiSuggestions). The typing log's settings line keeps "emoji" for the
  key and adds "emojiSuggestions".

Verified: build-for-testing (generic iOS Simulator) on basebook; new
cases in FullKeyboardSettingsTests (KeyboardLayoutTests.swift) for the
split, the migration and the key-off layout with the strip's 😭 for
"cry". Full suite on the Mac Studio not run yet.

* iOS Settings: shorter Emoji suggestions detail

"Suggests an emoji for the word you type, like 😭 for cry" wrapped to
two lines on an iPhone 17 Pro, leaving "😭 for cry" on its own line. It
is now "Suggests 😭 as you type cry" (zh-Hans "输入 cry 时推荐 😭"), one line
in both languages; "cry" still offers 😭 today (index weight 1.2), "sob"
does not (0.6, under the strip's 0.85).

Removed: the longer detail and its zh-Hans translation.

Verified: Debug build on basebook's one iOS 27.2 simulator (iPhone 17
Pro), Settings › Keyboard in English and Chinese, and the four
combinations of the two switches typing "Cry" in Hex's Test keyboard
field: the emoji key shows only with Emoji key on, 😭 only with Emoji
suggestions on.

* iOS emoji: "sob" offers 😭

The tester who asked for Emoji suggestions without the key types "sob"
for 😭, and the strip offered nothing for it: CLDR has "sob" only as one
of 😭's keywords, which the index weighs 0.6 (0.51 on the Norwegian,
Spanish and German layouts), under the 0.85 a typed or finished word's
emoji needs. "sob 😭" is now one of Hex's chat words
(scripts/emoji-aliases-en.txt), so it weighs 1.2 on every layout, as
"cry" and "crying" do.

The four index files were regenerated with scripts/build-emoji-data.py at
its pinned sources (emoji-test.txt 18.0, checked against its SHA-256, and
CLDR release-49). A build without the new line reproduced all four
committed files byte for byte first; with it, each file changes in that
one line ("W sob 95:..." to 1.200) and nothing else. LICENSE-Unicode.txt
and the files' source headers are unchanged.

Verified: build-for-testing on basebook; a case beside the "cry" one in
testEmojiSuggestionsWithoutTheEmojiKey (key off, suggestions on, "we sob"
offers 😭). Full suite on the Mac Studio not run yet.
2026-11-24 17:28:25 -05:00

337 lines
12 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 SwiftUI
/// The home screen's four settings pages.
enum SettingsPage: String, Hashable, CaseIterable {
case dictation
case keyboard
case privacy
case experiments
var title: String {
switch self {
case .dictation: String(localized: "Dictation")
case .keyboard: String(localized: "Keyboard")
case .privacy: String(localized: "Privacy")
case .experiments: String(localized: "Experiments")
}
}
var icon: String {
switch self {
case .dictation: "waveform"
case .keyboard: "keyboard"
case .privacy: "lock"
case .experiments: "testtube.2"
}
}
}
/// A settings page: a short scrolling column of cards under an inline title.
struct SettingsPageScaffold<Content: View>: View {
let title: LocalizedStringKey
/// The `id` of a row to start at instead of the top.
var startsAt: String?
@ViewBuilder var content: Content
var body: some View {
ScrollViewReader { proxy in
ScrollView {
VStack(alignment: .leading, spacing: 12) {
content
}
.frame(maxWidth: 500)
.padding(.horizontal, 20)
.padding(.top, 8)
.padding(.bottom, 28)
.frame(maxWidth: .infinity)
}
.onAppear {
guard let startsAt else { return }
DispatchQueue.main.async { proxy.scrollTo(startsAt, anchor: .top) }
}
}
.scrollBounceBehavior(.basedOnSize)
.scrollDismissesKeyboard(.interactively)
.defaultScrollAnchor(SettingsSnapshotFixture.scrollsToEnd ? .bottom : .top)
.background(HexTheme.bgDeep.ignoresSafeArea())
.navigationTitle(title)
.navigationBarTitleDisplayMode(.inline)
.toolbarBackground(HexTheme.bgDeep, for: .navigationBar)
}
}
/// Rows on `bgBase` with the theme's corner radius.
struct SettingsCard<Content: View>: View {
@ViewBuilder var content: Content
var body: some View {
VStack(spacing: 0) {
content
}
.background(
HexTheme.bgBase,
in: RoundedRectangle(cornerRadius: HexTheme.cardRadius, style: .continuous)
)
}
}
/// A small label above a card, such as Privacy's "Advanced".
struct SettingsSectionHeader: View {
let title: LocalizedStringKey
var body: some View {
Text(title)
.font(.system(size: 13, weight: .medium))
.foregroundStyle(HexTheme.textMuted)
.padding(.leading, 16)
.padding(.top, 12)
}
}
struct SettingsDivider: View {
var leading: CGFloat = 16
var body: some View {
Rectangle()
.fill(HexTheme.borderWeak)
.frame(height: 0.5)
.padding(.leading, leading)
}
}
/// A row's title, and its one-line description when it has one.
struct SettingsRowLabel: View {
let title: String
var detail: String?
var body: some View {
VStack(alignment: .leading, spacing: 2) {
Text(title)
.font(.system(size: 15, weight: .regular))
.foregroundStyle(HexTheme.textBase)
if let detail {
Text(detail)
.font(.system(size: 12))
.foregroundStyle(HexTheme.textMuted)
}
}
}
}
/// A switch with its one-line description.
struct SettingsToggleRow: View {
let title: String
var detail: String?
@Binding var isOn: Bool
var body: some View {
Toggle(isOn: $isOn) {
SettingsRowLabel(title: title, detail: detail)
}
.tint(HexTheme.success)
.padding(.horizontal, 16)
.padding(.vertical, 10)
.frame(minHeight: 50)
}
}
/// A row that opens something: a page (chevron) or another app (arrow).
struct SettingsLinkLabel: View {
let title: String
var icon: String?
var trailingIcon = "chevron.right"
var body: some View {
HStack(spacing: 12) {
if let icon {
Image(systemName: icon)
.font(.system(size: 15, weight: .regular))
.foregroundStyle(HexTheme.textMuted)
.frame(width: 22)
}
Text(title)
.font(.system(size: 15, weight: .regular))
.foregroundStyle(HexTheme.textBase)
Spacer(minLength: 8)
Image(systemName: trailingIcon)
.font(.system(size: 13, weight: .medium))
.foregroundStyle(HexTheme.textMuted)
}
.padding(.horizontal, 16)
.frame(minHeight: 50)
.contentShape(Rectangle())
}
}
/// A menu's current value with the up-down chevron.
struct SettingsMenuValue: View {
let value: String
var body: some View {
HStack(spacing: 5) {
Text(value)
.font(.system(size: 15, weight: .regular))
.foregroundStyle(HexTheme.textMuted)
.lineLimit(1)
Image(systemName: "chevron.up.chevron.down")
.font(.system(size: 11, weight: .medium))
.foregroundStyle(HexTheme.textMuted)
}
.padding(.vertical, 6)
.contentShape(Rectangle())
}
}
/// A small segmented control in the settings rows' style.
struct SettingsSegments<Item: Hashable>: View {
let items: [Item]
let selected: Item
let label: KeyPath<Item, String>
let onSelect: (Item) -> Void
var body: some View {
HStack(spacing: 2) {
ForEach(items, id: \.self) { item in
let isSelected = item == selected
Button {
onSelect(item)
} label: {
Text(item[keyPath: label])
.font(.system(size: 13, weight: .medium))
.lineLimit(1)
.fixedSize(horizontal: true, vertical: false)
.foregroundStyle(isSelected ? HexTheme.textBase : HexTheme.textMuted)
.padding(.horizontal, 10)
.frame(height: 28)
.background(
isSelected ? Color.white.opacity(0.14) : Color.clear,
in: RoundedRectangle(cornerRadius: 7, style: .continuous)
)
.contentShape(RoundedRectangle(cornerRadius: 7, style: .continuous))
}
.buttonStyle(.plain)
}
}
.fixedSize(horizontal: true, vertical: false)
.padding(2)
.background(
Color.white.opacity(0.055),
in: RoundedRectangle(cornerRadius: 9, style: .continuous)
)
}
}
/// Simulator scenes for the home screen and its pages (`--snap-state <name>`):
///
/// - `settings-home`: home, everything ready. `setup`: home, keyboard added but Full Access
/// not confirmed yet. `setup-sheet`: the same, with the setup sheet open;
/// `setup-sheet-microphone`: the sheet before iOS has asked for the microphone.
/// A scene with `-noaccess` (`setup-sheet-noaccess`, `settings-keyboard-noaccess`) has the
/// keyboard report Full Access off.
/// - `settings-<page>` opens a page (`dictation`, `keyboard`, `privacy`, `experiments`) with
/// every switch at its default; `-on` turns the switches on, `-end` starts scrolled to the end.
/// `settings-keyboard-languages[-on]` opens Keyboard › Languages.
/// `settings-privacy-confirm` opens Record typing logs' consent dialog.
///
/// `KeyboardSetupMonitor.refresh` and `DictationController` supply the matching state.
enum SettingsSnapshotFixture {
static let scene: String? = {
let args = ProcessInfo.processInfo.arguments
guard let idx = args.firstIndex(of: "--snap-state"), idx + 1 < args.count else { return nil }
let value = args[idx + 1]
return value.hasPrefix("settings-") || value.hasPrefix("setup-sheet") ? value : nil
}()
static var initialPath: [SettingsPage] {
guard let scene else { return [] }
let name = scene.dropFirst("settings-".count).split(separator: "-").first.map(String.init)
return name.flatMap(SettingsPage.init(rawValue:)).map { [$0] } ?? []
}
static var showsSetupSheet: Bool {
scene?.hasPrefix("setup-sheet") == true
}
/// `settings-privacy-confirm`: Record typing logs' consent dialog, open.
static var confirmsTypingLogs: Bool {
scene?.hasSuffix("-confirm") == true
}
static var scrollsToEnd: Bool {
scene?.hasSuffix("-end") == true
}
/// `settings-experiments-on`: Adaptive touch partway through learning, as a keyboard with
/// Full Access reports it.
static var adaptiveTouchStatus: AdaptiveTouchStatus? {
guard let scene, scene.hasPrefix("settings-experiments") else { return nil }
return scene.contains("-on") ? .learning(taps: 240) : nil
}
/// `settings-keyboard-languages[-on]`: Keyboard › Languages, open (Automatic, or with
/// `-on` English on QWERTZ, Français and Русский with Switch dictation too).
static var opensKeyboardLanguages: Bool {
scene?.hasPrefix("settings-keyboard-languages") == true
}
/// Writes the page's switches to the App Group so every run renders the same.
@MainActor
static func applyDefaults() {
guard let scene, scene.hasPrefix("settings-") else { return }
let on = scene.contains("-on")
let keyboard = FullKeyboardSettings()
keyboard.isEnabled = on || opensKeyboardLanguages
keyboard.autoCapitalization = true
// `--hex-key left` renders the page with the Hex key on the left.
let args = ProcessInfo.processInfo.arguments
keyboard.hexKeyOnLeft = args.firstIndex(of: FullKeyboardSettings.hexKeyLaunchArgument)
.flatMap { $0 + 1 < args.count ? args[$0 + 1] : nil } == "left"
keyboard.numberRow = on
keyboard.autocorrect = true
keyboard.spatialAutocorrect = true
keyboard.germanSpatialAutocorrect = on
keyboard.koreanKeyboard = on
keyboard.dvorakPunctuation = on
keyboard.commaPeriodKeys = on
keyboard.quickAccents = on
keyboard.emojiShortcodes = on
keyboard.emojiLettersUnderEmojiKey = on
keyboard.keyboardThemesEnabled = false
keyboard.keyboardTheme = .automatic
keyboard.suggestSwearWords = on
keyboard.keyPopups = true
keyboard.haptics = true
keyboard.emojiKey = true
keyboard.emojiSuggestions = true
keyboard.swipeTyping = true
keyboard.swipeMoreLanguages = on
let emojiUsage = EmojiUsageStore()
emojiUsage.clear()
if on {
emojiUsage.record("🎉")
}
LanguageSettings().keyboardLanguages = on ? [.english, .french, .russian] : []
LanguageSettings().setLetterLayout(on ? .qwertz : .qwerty, for: .english)
LanguageSettings().setLetterLayout(.azerty, for: .french)
LanguageSettings().currentKeyboardLanguage = nil
LanguageSettings().dictationFollowsKeyboard = on
CleanupTaggerSettings().isEnabled = on
AppleIntelligenceCleanupSettings().isEnabled = false
CustomVocabularySettings().isEnabled = on
CustomVocabularySettings().terms = on ? ["OpenCode", "SwiftUI", "TestFlight"] : []
DictationTranslationSettings().isEnabled = on
LiveDictationSettings().showsPreview = true
LiveDictationSettings().typesIntoField = on
DictationTranslationSettings().targetLanguage = "ja"
TypingJournalSettings().isEnabled = on
TypingJournalSettings().learnsPerLanguage = on
TypingLogSettings().isEnabled = on
HinglishSettings().isEnabled = on
GreeklishSettings().isEnabled = on
TwoLanguageDictationSettings().isEnabled = on
VoiceIsolationSettings().isEnabled = on
AdaptiveTouchSettings().isEnabled = on
// Settings scenes skip preparing a model, so this only draws the switch.
MultilingualParakeetSettings().isEnabled = on
}
}