hex/ios/HEXMobile/TranslateDictationRow.swift
Ryan Vogel 7510bc8ad3 iOS settings prototype 1: short home screen with separate pages
Ryan found the home screen cluttered: a large Keyboard setup checklist sat at
the top even when setup was nearly done, real features were filed under
Experiments, and every setting shared one long scroll. This prototype keeps
every setting and its behaviour, and changes only layout and copy.

Home now fits one screen: the Hex header, one status line ("Hex is ready", or
"Finish setup · 1 step ›"), the Dictate bar, four rows (Dictation, Keyboard,
Privacy, Experiments), then Keyboard settings ↗ and Run setup again.

- Replaced: KeyboardSetupCard with SetupStatusRow plus SetupSheet, a sheet
  sized to its content. It has the same four checks, the Full Access check
  field and Open Settings. Full Access that hasn't been confirmed reads "Not
  confirmed yet" with a dotted circle, not as a failure. The keyboard's Open
  Hex setup link opens the sheet.
- Removed: ExperimentsCard, split into pages (SettingsPages.swift):
  Dictation has style, language, translate, clean up, Apple Intelligence
  clean-up, custom vocabulary, keep mic warm and iPhone microphone. Keyboard
  has full keyboard, then layout, autocorrect and key pop-ups only while it
  is on, haptics, and the test field that was on home. Privacy has
  "Everything stays on this iPhone" and Learn how I type. Experiments has
  Emoji, with Clear emoji history while it's on, plus Record typing logs and
  its viewer.
- Emoji used to show only while the full keyboard was on. It now shows
  either way and reads "Needs the full keyboard" while it's off, because it
  no longer sits under that switch.
- Haptics and Learn how I type stay visible with the full keyboard off,
  because the dictation bar uses them too (KeyboardViewController).
- Copy is cut to one line per row. "Insert in" is now a Menu in the app's
  muted style instead of the system picker, with the same choices and writes.
- Removed the practice field's autofocus for hosted-keyboard scenes. It was
  dead code, because HEXMobileApp sends those scenes to
  HostedKeyboardConversationStage.
- New --snap-state scenes: settings-home, setup-sheet, and
  settings-<page>[-on][-end]. These write the page's switches (Emoji
  included) to the App Group, so each render is deterministic.

Verified: the build passes on an iPhone 18 Pro simulator (iOS 27.2) on the
Mac Studio, rebased onto origin/dev with the emoji panel.
2026-11-20 11:03:35 -05:00

136 lines
5.8 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
@preconcurrency import Translation
/// Settings › Dictation › Translate what I say: the toggle, the target language, the switch the
/// keyboard's menus also have (paste translated or as spoken), and the one-time download
/// of Apple's on-device language pack.
struct TranslateDictationRow: View {
@State private var enabled = DictationTranslationSettings().isEnabled
@State private var target = DictationTranslationSettings().targetLanguage
@State private var pastesTranslation = DictationTranslationSettings().pastesTranslation
@State private var languages: [Locale.Language] = []
@State private var status: LanguageAvailability.Status?
@State private var download: TranslationSession.Configuration?
/// What you dictate in: Settings › Dictation language, or the language it last resolved.
private var source: Locale.Language {
LanguageSettings().effectiveDictationLanguage.map { Locale(identifier: $0).language }
?? Locale.current.language
}
/// "Paste in English" for what you dictate in, or nil when that is the target already.
private var pasteChoice: TranslationPasteChoice? {
TranslationPasteChoice(
target: target,
source: LanguageSettings().effectiveDictationLanguage ?? Locale.current.identifier
)
}
var body: some View {
VStack(alignment: .leading, spacing: 8) {
Toggle(isOn: $enabled) {
VStack(alignment: .leading, spacing: 2) {
Text("Translate what I say")
.font(.system(size: 15, weight: .regular))
.foregroundStyle(HexTheme.textBase)
Text("Speak one language, insert another")
.font(.system(size: 12))
.foregroundStyle(HexTheme.textMuted)
}
}
.tint(HexTheme.success)
.onChange(of: enabled) { _, value in
DictationTranslationSettings().isEnabled = value
// Turning it on means translating, whatever the switch said last time.
if value {
pastesTranslation = true
}
}
if enabled {
HStack {
Text("Insert in")
.font(.system(size: 13))
.foregroundStyle(HexTheme.textMuted)
Spacer()
Menu {
ForEach(languages, id: \.minimalIdentifier) { language in
Button {
target = language.minimalIdentifier
} label: {
if target == language.minimalIdentifier {
Label(name(of: language), systemImage: "checkmark")
} else {
Text(name(of: language))
}
}
}
} label: {
SettingsMenuValue(value: name(of: Locale.Language(identifier: target)))
}
.accessibilityLabel("Insert in")
}
.onChange(of: target) { _, value in
DictationTranslationSettings().targetLanguage = value
Task { await refreshStatus() }
}
if let pasteChoice {
Toggle(isOn: $pastesTranslation) {
Text(pasteChoice.translatedTitle)
.font(.system(size: 13))
.foregroundStyle(HexTheme.textMuted)
}
.tint(HexTheme.success)
.onChange(of: pastesTranslation) { _, value in
DictationTranslationSettings().pastesTranslation = value
}
}
switch status {
case .installed:
Text("Ready. Works offline.")
.font(.system(size: 12))
.foregroundStyle(HexTheme.textMuted)
case .supported:
Button("Download \(name(of: Locale.Language(identifier: target)))") {
download = TranslationSession.Configuration(
source: source,
target: Locale.Language(identifier: target)
)
}
.font(.system(size: 13, weight: .medium))
case .unsupported:
Text("Apple can't translate between these languages.")
.font(.system(size: 12))
.foregroundStyle(HexTheme.textMuted)
default:
EmptyView()
}
}
}
.translationTask(download) { session in
try? await session.prepareTranslation()
await refreshStatus()
}
.onAppear {
// The keyboard's menus change it too.
pastesTranslation = DictationTranslationSettings().pastesTranslation
}
.task {
let supported = await LanguageAvailability().supportedLanguages
languages = supported
.filter { $0.languageCode != source.languageCode }
.sorted { name(of: $0) < name(of: $1) }
await refreshStatus()
}
}
private func refreshStatus() async {
status = await LanguageAvailability().status(from: source, to: Locale.Language(identifier: target))
}
private func name(of language: Locale.Language) -> String {
Locale.current.localizedString(forIdentifier: language.minimalIdentifier) ?? language.minimalIdentifier
}
}