mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-27 07:58:01 +01:00
Four regressions from the Settings rework (#2450), nothing renamed or reordered:
1. Home and its setup sheet read the setup state once a second again while
they show and Hex is active (.task(id:) on the navigation stack, stopping
on any Settings page or in the background), as well as on foreground and
the keyboard's Full Access report. #2450 dropped the 1 Hz loop, so a
change made with Hex still in front never showed: the keyboard's Allow
Paste flag (Hex's own field) and a keyboard added from Settings beside Hex
in Split View. Timed on an iPhone 17 Pro simulator (iOS 27.2), Hex in
front: at 0304eb1c neither showed in 8 s; now 1.2 s and 1.4 s (147: 1.1 s
and 1.0 s); the setup sheet 1.8 s; the Keyboard page, by design, not.
2. Live words, Into field: where Type as I speak can't type this dictation
(Translate what I say on, or a field LiveFieldTyping never types in:
secure, password, one-time code or card number fields, a selection, a
two-thumb Dictate's range, an app that reports no text), the words now
show over the keys instead of nowhere, as build 147's two switches did.
LiveDictationSettings.liveWords maps the two stored keys to what one
dictation does; nothing stored changes. LiveFieldTyping.types(in:) is the
field check its init already made; KeyboardLiveTyping.takesTyping adds
the two-thumb case, read through LiveDictationText.fieldTakesTyping.
3. Home's "New: Hex's own speech model" card opens Settings › Dictation at
Speech model in keyboard (SettingsPageScaffold.startsAt, a
ScrollViewReader): the row's title lands at y 571 instead of 812 of
874 pt. Opening Dictation from Home's list still starts at the top.
4. ios/docs/settings-and-home.md, dictation.md, verification.md and
ios/README.md describe Settings as it is: Live words and its three
choices, the three Keyboard cards, the four Experiments groups (21
switches, 20 off a foldable), iOS Settings ↗, HEXMobile/Settings/.
Verified: testIntoFieldShowsTheWordsOverTheKeysWhereItCantType in
LiveDictationTests (DictationLearningTests.swift); the timed Home runs and
landing positions above on the simulator; full suite and code-shape --check
in the PR.
173 lines
7.3 KiB
Swift
173 lines
7.3 KiB
Swift
import Combine
|
||
import Foundation
|
||
|
||
/// The words heard so far while a dictation records, shown over the keys while it does:
|
||
/// `settled` words the recogniser won't change again, then the `tentative` tail it still may.
|
||
nonisolated struct LiveTranscript: Equatable, Sendable {
|
||
var settled = ""
|
||
var tentative = ""
|
||
|
||
init(settled: String = "", tentative: String = "") {
|
||
self.settled = settled
|
||
self.tentative = tentative
|
||
}
|
||
|
||
/// Both parts as one line, one space between them unless either already has one.
|
||
var text: String {
|
||
settled + tentativeJoined
|
||
}
|
||
|
||
/// The tentative words as they follow the settled ones.
|
||
var tentativeJoined: String {
|
||
guard let last = settled.last, let first = tentative.first,
|
||
!last.isWhitespace, !first.isWhitespace,
|
||
!LiveTranscript.joinsWithoutSpace(last, first) else { return tentative }
|
||
return " " + tentative
|
||
}
|
||
|
||
var isEmpty: Bool {
|
||
settled.allSatisfy(\.isWhitespace) && tentative.allSatisfy(\.isWhitespace)
|
||
}
|
||
|
||
/// Chinese and Japanese run on without spaces; a mark never takes one before it.
|
||
static func joinsWithoutSpace(_ last: Character, _ first: Character) -> Bool {
|
||
if first.isPunctuation { return true }
|
||
guard let a = last.unicodeScalars.first?.value, let b = first.unicodeScalars.first?.value else { return false }
|
||
func written(_ v: UInt32) -> Bool {
|
||
(0x3041...0x30FF).contains(v) || (0x3400...0x4DBF).contains(v)
|
||
|| (0x4E00...0x9FFF).contains(v) || (0xF900...0xFAFF).contains(v) || (0x3000...0x303F).contains(v)
|
||
}
|
||
return written(a) || written(b)
|
||
}
|
||
}
|
||
|
||
/// A recogniser's results as one transcript: a final result settles its words and the latest
|
||
/// volatile one is the tentative tail (Apple's `SpeechTranscriber` replaces its volatile
|
||
/// result each time and never revisits a final one).
|
||
nonisolated struct LiveTranscriptAccumulator {
|
||
private var finals: [String] = []
|
||
private(set) var transcript = LiveTranscript()
|
||
|
||
@discardableResult
|
||
mutating func apply(_ text: String, isFinal: Bool) -> LiveTranscript {
|
||
if isFinal {
|
||
finals.append(text)
|
||
transcript = LiveTranscript(settled: Self.joined(finals), tentative: "")
|
||
} else {
|
||
transcript.tentative = text.trimmingCharacters(in: .whitespacesAndNewlines)
|
||
}
|
||
return transcript
|
||
}
|
||
|
||
/// Final results back to back, as Apple writes them, trimmed at both ends.
|
||
static func joined(_ segments: [String]) -> String {
|
||
var text = ""
|
||
for segment in segments {
|
||
let piece = segment.trimmingCharacters(in: .whitespacesAndNewlines)
|
||
guard !piece.isEmpty else { continue }
|
||
text = LiveTranscript(settled: text, tentative: piece).text
|
||
}
|
||
return text
|
||
}
|
||
}
|
||
|
||
/// Where live words come from while the keyboard records: Apple's speech recognition fed as
|
||
/// the audio arrives today, the keyboard's speech model later. `append` takes the capture's
|
||
/// 16-bit mono chunks; `stop` frees it at once and reports nothing more.
|
||
@MainActor
|
||
protocol LiveTranscriptSource: AnyObject {
|
||
var onUpdate: ((LiveTranscript) -> Void)? { get set }
|
||
/// The source gave up (no model, no memory, an error): the preview goes and the dictation
|
||
/// goes on as without it.
|
||
var onFailure: ((String) -> Void)? { get set }
|
||
func append(_ pcm: Data)
|
||
func stop()
|
||
}
|
||
|
||
/// What the keyboard shows while it records: nil while no live source runs (the keys stay as
|
||
/// they are), else the words so far. The preview and Type as I speak read it; changing it
|
||
/// never refreshes the rest of the keyboard.
|
||
@MainActor
|
||
final class LiveDictationText: ObservableObject {
|
||
@Published private(set) var transcript: LiveTranscript?
|
||
/// Settings › Dictation's two switches as this dictation started.
|
||
private(set) var showsPreview = true
|
||
private(set) var typesIntoField = false
|
||
/// Counts dictations, so what follows the words can tell a new one from the last.
|
||
private(set) var dictation = 0
|
||
/// Whether Type as I speak could type in the field the keyboard is in now (`KeyboardLiveTyping`).
|
||
var fieldTakesTyping: () -> Bool = { true }
|
||
|
||
/// A source started: the preview shows, empty until the first words.
|
||
func begin(showsPreview: Bool, typesIntoField: Bool) {
|
||
self.showsPreview = showsPreview
|
||
self.typesIntoField = typesIntoField
|
||
dictation += 1
|
||
transcript = LiveTranscript()
|
||
}
|
||
|
||
func update(_ next: LiveTranscript) {
|
||
guard transcript != nil, next != transcript else { return }
|
||
transcript = next
|
||
}
|
||
|
||
func end() {
|
||
if transcript != nil { transcript = nil }
|
||
}
|
||
}
|
||
|
||
/// Settings › Dictation's two switches for what happens while you speak, in the App Group so
|
||
/// the keyboard reads them: Show what I say (the words over the keys, on by default) and
|
||
/// Type as I speak (the words typed into the field, off by default).
|
||
nonisolated struct LiveDictationSettings {
|
||
static let previewKey = "settings.liveDictationPreview"
|
||
static let typingKey = "settings.liveDictationTyping"
|
||
static let leftoverKey = "liveDictation.leftover"
|
||
|
||
private let defaults: UserDefaults?
|
||
|
||
init(defaults: UserDefaults? = UserDefaults(suiteName: KeyboardBridge.appGroup)) {
|
||
self.defaults = defaults
|
||
}
|
||
|
||
var showsPreview: Bool {
|
||
get { defaults?.object(forKey: Self.previewKey) as? Bool ?? true }
|
||
nonmutating set { defaults?.set(newValue, forKey: Self.previewKey) }
|
||
}
|
||
|
||
var typesIntoField: Bool {
|
||
get { defaults?.object(forKey: Self.typingKey) as? Bool ?? false }
|
||
nonmutating set { defaults?.set(newValue, forKey: Self.typingKey) }
|
||
}
|
||
|
||
/// Either switch needs live words while recording.
|
||
var wantsLiveWords: Bool { showsPreview || typesIntoField }
|
||
|
||
/// What one dictation does with its live words: typed into the field where Type as I speak
|
||
/// is on and can type this dictation, and shown over the keys where Show what I say is on,
|
||
/// or where Type as I speak is on but can't (Translate what I say on, or a field
|
||
/// `LiveFieldTyping` never types in), so the words still show, as they did while the two
|
||
/// were separate switches (TestFlight build 147).
|
||
func liveWords(translating: Bool, fieldTakesTyping: Bool) -> (showsPreview: Bool, typesIntoField: Bool) {
|
||
let types = typesIntoField && !translating && fieldTakesTyping
|
||
return (showsPreview || (typesIntoField && !types), types)
|
||
}
|
||
|
||
/// The words Type as I speak typed in a field whose keyboard hid before the dictation went
|
||
/// in (an app gone to the background takes no edits), and when: the held text replaces them
|
||
/// where the field still ends with them. A new dictation clears them.
|
||
var leftover: (text: String, at: TimeInterval)? {
|
||
get {
|
||
guard let stored = defaults?.dictionary(forKey: Self.leftoverKey),
|
||
let text = stored["text"] as? String, let at = stored["at"] as? Double else { return nil }
|
||
return (text, at)
|
||
}
|
||
nonmutating set {
|
||
if let newValue {
|
||
defaults?.set(["text": newValue.text, "at": newValue.at], forKey: Self.leftoverKey)
|
||
} else {
|
||
defaults?.removeObject(forKey: Self.leftoverKey)
|
||
}
|
||
}
|
||
}
|
||
}
|