mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
Testers asked for desktop's lowercase option on mobile. Desktop's
Lowercase is a built-in transformation (src/personal_commands.rs,
str::to_lowercase) that runs last on the final text: everything goes
lowercase, custom vocabulary and replacements included ("OpenCode
shipped." becomes "opencode shipped", src/rune_ui/output.rs).
Mobile matches it: a Lowercase toggle under Settings › Dictation, right
below Style, off by default (DictationLowercaseSettings, App Group key
settings.dictationLowercase). DictationPipeline lowercases after the style
and the vocabulary's second spelling pass, so it applies to every style and
every path: the app, the keyboard's warm session and cold start, the
Action Button and Control Center (read when recording starts, like the
other dictation settings) and in-keyboard dictation (read when
transcription finishes, like the style). Custom vocabulary is lowercased
too, as on desktop. Casual with Lowercase on writes what Casual wrote
before the previous commit. Strings "Lowercase" and "Lowercases
everything" are desktop's, with its zh-Hans "小写" / "全部转为小写".
Verified: xcodebuild test on an iPhone 17 simulator (iOS 27.2), 309 tests,
0 failures, including testLowercaseRunsAfterTheStyleAndVocabulary,
testLowercaseAppliesOnlyWhenCaptured (Action Button path, mid-capture
toggle ignored) and the keyboard/app parity test with Lowercase on.
94 lines
4 KiB
Swift
94 lines
4 KiB
Swift
import Foundation
|
||
|
||
/// Settings › Dictation › Lowercase, in the shared App Group. Off by default.
|
||
nonisolated struct DictationLowercaseSettings {
|
||
private enum Key {
|
||
static let enabled = "settings.dictationLowercase"
|
||
}
|
||
|
||
private let defaults: UserDefaults?
|
||
|
||
init(defaults: UserDefaults? = UserDefaults(suiteName: KeyboardBridge.appGroup)) {
|
||
self.defaults = defaults
|
||
}
|
||
|
||
/// Everything dictation writes is lowercased after its style, custom vocabulary included,
|
||
/// as desktop's Lowercase step does (`str::to_lowercase` on the final text).
|
||
var isEnabled: Bool {
|
||
get { defaults?.bool(forKey: Key.enabled) ?? false }
|
||
nonmutating set { defaults?.set(newValue, forKey: Key.enabled) }
|
||
}
|
||
}
|
||
|
||
/// The steps between the speech model and the text Hex inserts, the same wherever Hex
|
||
/// dictates: `DictationController` (the app, the keyboard's warm session and cold start, the
|
||
/// Action Button, Control Center and Siri) and in-keyboard dictation
|
||
/// (`KeyboardLocalDictation`), so a style and Lowercase apply on every path.
|
||
///
|
||
/// In order: learned fixes, custom vocabulary, Clean up what I say
|
||
/// (`CleanupTagger.cleanIfEnabled`, which guards its own memory in the keyboard), `refine`
|
||
/// (Clean up with Apple Intelligence, only in the app), translation, the style
|
||
/// (`TranscriptFormatter`), the spellings again, then Lowercase. The style and Lowercase are
|
||
/// string rules with no model, so in-keyboard dictation needs no more memory to run them.
|
||
nonisolated struct DictationPipeline: Sendable {
|
||
var style: DictationStyle
|
||
/// The dictation language (BCP 47).
|
||
var language: String
|
||
/// Custom vocabulary's terms; empty while it is off.
|
||
var vocabulary: [String] = []
|
||
/// Learn how I type's dictation fixes; empty while it is off.
|
||
var corrections: [DictationCorrection] = []
|
||
var lowercase = false
|
||
|
||
/// Each step's text, for Record typing logs.
|
||
struct Stages: Equatable, Sendable {
|
||
/// After learned fixes.
|
||
var fixed: String
|
||
/// After custom vocabulary.
|
||
var spelled: String
|
||
/// After Clean up what I say.
|
||
var tagged: String
|
||
/// After `refine`.
|
||
var cleaned: String
|
||
var translated: String?
|
||
/// What Hex inserts.
|
||
var formatted: String
|
||
}
|
||
|
||
/// `translationSource` is the dictation language as Apple Speech names it; nil reads
|
||
/// `language`.
|
||
@MainActor
|
||
func run(
|
||
_ heard: String,
|
||
translationSource: Locale.Language? = nil,
|
||
refine: ((String) async -> String)? = nil
|
||
) async -> Stages {
|
||
let fixed = DictationCorrections.apply(corrections, to: heard)
|
||
let spelled = VocabularySpelling.apply(vocabulary, to: fixed)
|
||
let tagged = await CleanupTagger.cleanIfEnabled(spelled, style: style, language: language)
|
||
let cleaned = await refine?(tagged) ?? tagged
|
||
let translated = await DictationTranslator.translateIfEnabled(
|
||
cleaned,
|
||
source: translationSource ?? Locale(identifier: language).language
|
||
)
|
||
// A translation is formatted in the language it is now in.
|
||
let formatLanguage = translated == nil ? language : DictationTranslationSettings().targetLanguage
|
||
return Stages(
|
||
fixed: fixed,
|
||
spelled: spelled,
|
||
tagged: tagged,
|
||
cleaned: cleaned,
|
||
translated: translated,
|
||
formatted: format(translated ?? cleaned, language: formatLanguage)
|
||
)
|
||
}
|
||
|
||
/// `text` in the style, then the vocabulary's and learned fixes' spellings again (a style
|
||
/// can lowercase a term or capitalize a sentence start: "iPhone" -> "IPhone"), then
|
||
/// Lowercase.
|
||
func format(_ text: String, language: String?) -> String {
|
||
let styled = TranscriptFormatter.format(text, style: style, language: language)
|
||
let spelled = VocabularySpelling.apply(vocabulary + DictationCorrections.spellings(corrections), to: styled)
|
||
return lowercase ? spelled.lowercased() : spelled
|
||
}
|
||
}
|