mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
Testers asked to change the dictation language without opening Hex.
What changed:
- Dictation bar: a language trigger beside the style ("Norsk") opens a
tray of chips: Auto plus up to five languages.
- Full keyboard: the Hex key menu gets a Language row and page with the
same choices, and the space bar names the dictation language faintly
("English", "Norsk"), like Apple's.
- DictationLanguageSwitch picks the choices (the language set, recent
picks, then the iPhone's preferred languages, each matched to a locale
Apple Speech has; Nynorsk maps to Bokmal) and names each in its own
language, adding a region code only where two share a language.
- LanguageSettings keeps recent picks, what Automatic resolves to, and
Apple's language list (cached when Hex or the keyboard lists it); a
pick in the keyboard writes the same settings.dictationLanguage as
Hex's menu. In-keyboard dictation reads it per dictation; a warm Hex
session follows it in its 100 ms loop and prepares the language
between dictations; a keyboard handoff and the Action Button pick it
up before recording.
- Typing logs record the dictation language setting and a
dictation_language UI event.
Verified: xcodebuild test on the iPhone 18 Pro simulator (iOS 27.2):
195 tests, 0 failures, 1 skipped (the Apple Speech clip test, which
needs HEX_SPEECH_CLIPS), including new switch, recents, follow and
space-bar tests; Release generic-iOS build with CODE_SIGNING_ALLOWED=NO
succeeds; screenshots of the Hex menu's language page (light, dark),
the space bar, the dictation bar's tray and the settings row checked by
eye. The trigger's last change (chevron instead of a globe, which looked
like the next-keyboard key) was compiled, not re-shot. Not tried on a
physical iPhone.
119 lines
4.8 KiB
Swift
119 lines
4.8 KiB
Swift
import AVFAudio
|
||
import Foundation
|
||
import Speech
|
||
|
||
/// The languages Apple Speech transcribes on this iPhone, shared by the app and in-keyboard
|
||
/// dictation so both pick the same language and the same downloaded model.
|
||
@available(iOS 26.0, *)
|
||
nonisolated enum AppleSpeechLanguages {
|
||
/// The language dictation uses: Settings › Dictation language, else Automatic. Records
|
||
/// the result in the App Group for the keyboard layout and translation.
|
||
static func resolve(settings: LanguageSettings = LanguageSettings()) async -> SpeechLanguage? {
|
||
let explicit = settings.dictationLanguage
|
||
let picked = await pick(explicit: explicit)
|
||
if let picked, settings.resolvedDictationLanguage != picked.identifier {
|
||
settings.resolvedDictationLanguage = picked.identifier
|
||
}
|
||
if explicit == nil, let picked, settings.automaticDictationLanguage != picked.identifier {
|
||
settings.automaticDictationLanguage = picked.identifier
|
||
}
|
||
return picked
|
||
}
|
||
|
||
/// What Automatic picks on this iPhone, whatever the setting says. Records it for the
|
||
/// keyboard's language switch.
|
||
static func automatic(settings: LanguageSettings = LanguageSettings()) async -> SpeechLanguage? {
|
||
let picked = await pick(explicit: nil)
|
||
if let picked, settings.automaticDictationLanguage != picked.identifier {
|
||
settings.automaticDictationLanguage = picked.identifier
|
||
}
|
||
return picked
|
||
}
|
||
|
||
private static func pick(explicit: String?) async -> SpeechLanguage? {
|
||
await SpeechLanguagePicker.pick(
|
||
explicit: explicit,
|
||
candidates: SpeechLanguagePicker.automaticCandidates(
|
||
preferred: Locale.preferredLanguages,
|
||
current: Locale.current.identifier
|
||
),
|
||
speech: { locale in
|
||
guard SpeechTranscriber.isAvailable else { return nil }
|
||
return await SpeechTranscriber.supportedLocale(equivalentTo: locale)
|
||
},
|
||
dictation: { locale in
|
||
await DictationTranscriber.supportedLocale(equivalentTo: locale)
|
||
}
|
||
)
|
||
}
|
||
|
||
/// Every language either module transcribes, sorted by name. Records the list for the
|
||
/// keyboard's language switch.
|
||
static func options(
|
||
display: Locale = .current,
|
||
settings: LanguageSettings = LanguageSettings()
|
||
) async -> [SpeechLanguageOption] {
|
||
var identifiers: [String] = []
|
||
var seen = Set<String>()
|
||
let speech = SpeechTranscriber.isAvailable ? await SpeechTranscriber.supportedLocales : []
|
||
for locale in speech + (await DictationTranscriber.supportedLocales) {
|
||
let identifier = locale.identifier(.bcp47)
|
||
if seen.insert(identifier).inserted {
|
||
identifiers.append(identifier)
|
||
}
|
||
}
|
||
if !identifiers.isEmpty, settings.supportedDictationLanguages != identifiers {
|
||
settings.supportedDictationLanguages = identifiers
|
||
}
|
||
return identifiers
|
||
.map { SpeechLanguageOption(id: $0, name: SpeechLanguagePicker.displayName(for: $0, in: display)) }
|
||
.sorted { $0.name.localizedStandardCompare($1.name) == .orderedAscending }
|
||
}
|
||
}
|
||
|
||
/// One dictation's Apple Speech module: `SpeechTranscriber` or `DictationTranscriber`.
|
||
@available(iOS 26.0, *)
|
||
nonisolated enum AppleSpeechModule {
|
||
case speech(SpeechTranscriber)
|
||
case dictation(DictationTranscriber)
|
||
|
||
init(_ language: SpeechLanguage) {
|
||
switch language.module {
|
||
case .speech:
|
||
self = .speech(SpeechTranscriber(locale: language.locale, preset: .transcription))
|
||
case .dictation:
|
||
// Punctuated, without the short-form hint: dictations run up to a minute.
|
||
self = .dictation(DictationTranscriber(locale: language.locale, preset: .longDictation))
|
||
}
|
||
}
|
||
|
||
var module: any SpeechModule {
|
||
switch self {
|
||
case .speech(let transcriber): transcriber
|
||
case .dictation(let transcriber): transcriber
|
||
}
|
||
}
|
||
|
||
var name: String {
|
||
switch self {
|
||
case .speech: "SpeechTranscriber"
|
||
case .dictation: "DictationTranscriber"
|
||
}
|
||
}
|
||
|
||
/// The final results joined, once the analyzer has finished the file.
|
||
func finalText() async throws -> String {
|
||
var transcript = AttributedString()
|
||
switch self {
|
||
case .speech(let transcriber):
|
||
for try await result in transcriber.results where result.isFinal {
|
||
transcript += result.text
|
||
}
|
||
case .dictation(let transcriber):
|
||
for try await result in transcriber.results where result.isFinal {
|
||
transcript += result.text
|
||
}
|
||
}
|
||
return String(transcript.characters).trimmingCharacters(in: .whitespacesAndNewlines)
|
||
}
|
||
}
|