mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-25 01:30:06 +08:00
iOS: the dictation bar can start dictating by itself (Auto-start: never, on switch, always) (#2267)
This commit is contained in:
@@ -43,6 +43,18 @@ final class KeyboardViewController: UIInputViewController {
|
||||
/// How long the note that a dictation went in as spoken stays up.
|
||||
static let translationNoteDuration: TimeInterval = 8
|
||||
private var isShown = false
|
||||
/// This appearance hasn't yet checked Settings › Keyboard › Auto-start, which waits until
|
||||
/// its keys are up (and UIKit has named the field).
|
||||
private var autoStartPending = false
|
||||
private let switchGuess = KeyboardSwitchGuessStore()
|
||||
/// Notes when the field's app goes to the background, for `KeyboardSwitchGuess`; one per process.
|
||||
private static let hostLeaveObserver: NSObjectProtocol = NotificationCenter.default.addObserver(
|
||||
forName: .NSExtensionHostDidEnterBackground,
|
||||
object: nil,
|
||||
queue: .main
|
||||
) { _ in
|
||||
KeyboardSwitchGuessStore().hostLeft(at: Date().timeIntervalSince1970)
|
||||
}
|
||||
/// The dictation bar's key haptic; the full keyboard has its own generator.
|
||||
private lazy var barHaptics = UIImpactFeedbackGenerator(style: FullKeyboardView.keyTickStyle(hapticStrength))
|
||||
/// Settings › Keyboard › Strength as of the last bridge sync.
|
||||
@@ -90,6 +102,7 @@ final class KeyboardViewController: UIInputViewController {
|
||||
// Before any typing log starts: the keyboard may update before Hex is opened.
|
||||
TypingLogSettings().turnOffOnce()
|
||||
KeyboardLocalDictation.allowHapticsWhileRecording()
|
||||
_ = Self.hostLeaveObserver
|
||||
configureView()
|
||||
syncFromBridge(animated: false)
|
||||
}
|
||||
@@ -97,6 +110,7 @@ final class KeyboardViewController: UIInputViewController {
|
||||
override func viewWillAppear(_ animated: Bool) {
|
||||
super.viewWillAppear(animated)
|
||||
reportFullAccess(force: true)
|
||||
autoStartPending = true
|
||||
coldStartJobID = UUID().uuidString
|
||||
didHandOffCurrentField = false
|
||||
// The keyboard may come back for another field: settle on where the dictation was last seen.
|
||||
@@ -145,14 +159,49 @@ final class KeyboardViewController: UIInputViewController {
|
||||
self.listTranslationLanguagesIfNeeded()
|
||||
self.prepareLocalDictationIfNeeded()
|
||||
}
|
||||
launchWork = [lexicon, optional]
|
||||
// After the optional work's turn: opening the microphone is the heaviest thing the
|
||||
// keyboard does, and iOS gives up on a keyboard slow to draw.
|
||||
let autoStart = DispatchWorkItem { [weak self] in
|
||||
self?.autoStartDictationIfPending()
|
||||
}
|
||||
launchWork = [lexicon, optional, autoStart]
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + KeyboardLaunchWork.afterFirstFrame, execute: lexicon)
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + KeyboardLaunchWork.optionalWorkDelay, execute: optional)
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + KeyboardLaunchWork.autoStartDelay, execute: autoStart)
|
||||
}
|
||||
|
||||
/// Settings › Keyboard › Auto-start for this appearance: Always, or When switching if you
|
||||
/// seem to have switched back to Hex in this field (`KeyboardSwitchGuess`).
|
||||
private func wantsAutoStart() -> Bool {
|
||||
switch FullKeyboardSettings().dictationBarAutoStart {
|
||||
case .never: false
|
||||
case .always: true
|
||||
case .whenSwitching: switchGuess.guess.isSwitchBack(to: currentDocument)
|
||||
}
|
||||
}
|
||||
|
||||
/// Starts dictating as Dictate's tap would, only on the dictation bar, idle, and where it
|
||||
/// dictates without leaving the field: a warm Hex session or in-keyboard dictation, never
|
||||
/// by opening Hex.
|
||||
private func autoStartDictationIfPending() {
|
||||
guard autoStartPending, isShown else { return }
|
||||
autoStartPending = false
|
||||
guard wantsAutoStart(), fullKeyboard == nil, barEmojiPanel == nil, viewModel.hasFullAccess,
|
||||
viewModel.stagePhase == .idle,
|
||||
KeyboardFieldRouting.route(for: textDocumentProxy.keyboardType) != .handOff
|
||||
else { return }
|
||||
if viewModel.dictatesThroughHex {
|
||||
startWarmDictation()
|
||||
} else if let local = viewModel.onLocalDictateTap {
|
||||
local()
|
||||
}
|
||||
}
|
||||
|
||||
override func viewWillDisappear(_ animated: Bool) {
|
||||
super.viewWillDisappear(animated)
|
||||
bridge.recordKeyboardHidden(at: Date().timeIntervalSince1970)
|
||||
switchGuess.keyboardHid(in: currentDocument, at: Date().timeIntervalSince1970)
|
||||
autoStartPending = false
|
||||
if dictationEdits.isTracking { dictationEdits.finish(editContext) }
|
||||
TypingJournal.sharedIfEnabled()?.save()
|
||||
AdaptiveTouch.current?.save()
|
||||
|
||||
@@ -3652,6 +3652,76 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"Never" : {
|
||||
"localizations" : {
|
||||
"zh-Hans" : {
|
||||
"stringUnit" : {
|
||||
"state" : "translated",
|
||||
"value" : "从不"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"On switch" : {
|
||||
"localizations" : {
|
||||
"zh-Hans" : {
|
||||
"stringUnit" : {
|
||||
"state" : "translated",
|
||||
"value" : "切换时"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"Always" : {
|
||||
"localizations" : {
|
||||
"zh-Hans" : {
|
||||
"stringUnit" : {
|
||||
"state" : "translated",
|
||||
"value" : "始终"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"Auto-start" : {
|
||||
"localizations" : {
|
||||
"zh-Hans" : {
|
||||
"stringUnit" : {
|
||||
"state" : "translated",
|
||||
"value" : "自动开始"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"Tap Dictate to start" : {
|
||||
"localizations" : {
|
||||
"zh-Hans" : {
|
||||
"stringUnit" : {
|
||||
"state" : "translated",
|
||||
"value" : "轻点“听写”开始"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"When you switch back to Hex in a field" : {
|
||||
"localizations" : {
|
||||
"zh-Hans" : {
|
||||
"stringUnit" : {
|
||||
"state" : "translated",
|
||||
"value" : "在输入框中切换回 Hex 时"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"Each time the dictation bar opens" : {
|
||||
"localizations" : {
|
||||
"zh-Hans" : {
|
||||
"stringUnit" : {
|
||||
"state" : "translated",
|
||||
"value" : "每次打开听写栏时"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"version" : "1.0"
|
||||
|
||||
@@ -197,6 +197,25 @@ private enum KeyboardChoice: CaseIterable {
|
||||
}
|
||||
}
|
||||
|
||||
/// Settings › Keyboard › Auto-start, while the dictation bar is chosen.
|
||||
private extension DictationBarAutoStart {
|
||||
var label: String {
|
||||
switch self {
|
||||
case .never: String(localized: "Never")
|
||||
case .whenSwitching: String(localized: "On switch")
|
||||
case .always: String(localized: "Always")
|
||||
}
|
||||
}
|
||||
|
||||
var detail: String {
|
||||
switch self {
|
||||
case .never: String(localized: "Tap Dictate to start")
|
||||
case .whenSwitching: String(localized: "When you switch back to Hex in a field")
|
||||
case .always: String(localized: "Each time the dictation bar opens")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Settings › Keyboard › Hex key: which end of the strip the full keyboard's Hex key sits at.
|
||||
private extension KeyHapticStrength {
|
||||
var label: String {
|
||||
@@ -220,7 +239,8 @@ private enum HexKeySide: CaseIterable {
|
||||
}
|
||||
}
|
||||
|
||||
/// Settings › Keyboard: Full or Dictation bar, the full keyboard's rows (only while it's
|
||||
/// Settings › Keyboard: Full or Dictation bar (with Auto-start under the bar, On switch by
|
||||
/// default, and always on iPad, which shows the bar), the full keyboard's rows (only while it's
|
||||
/// chosen; Auto-capitalization, on by default as Apple's, sits above Autocorrect; Spatial autocorrect only while Autocorrect is on, and Suggest swear words off by
|
||||
/// default, as Apple's keyboard never offers them, then Vocabulary, Hex's list of your own
|
||||
/// words; Hex key, Right by default, under Languages),
|
||||
@@ -241,6 +261,7 @@ struct KeyboardSettingsPage: View {
|
||||
@State private var emoji = FullKeyboardSettings().emoji
|
||||
@State private var emojiHistoryEmpty = EmojiUsageStore().isEmpty
|
||||
@State private var hapticStrength = FullKeyboardSettings().hapticStrength
|
||||
@State private var autoStart = FullKeyboardSettings().dictationBarAutoStart
|
||||
@State private var practiceText = ""
|
||||
|
||||
/// The full keyboard is iPhone only: on an iPad the keyboard shows the dictation bar, whichever
|
||||
@@ -280,6 +301,24 @@ struct KeyboardSettingsPage: View {
|
||||
.padding(.vertical, 10)
|
||||
.frame(minHeight: 50)
|
||||
|
||||
if !fullKeyboard || Self.runsOnPad {
|
||||
SettingsDivider()
|
||||
|
||||
HStack(spacing: 12) {
|
||||
SettingsRowLabel(title: String(localized: "Auto-start"), detail: autoStart.detail)
|
||||
Spacer(minLength: 8)
|
||||
SettingsSegments(
|
||||
items: DictationBarAutoStart.allCases,
|
||||
selected: autoStart,
|
||||
label: \.label,
|
||||
onSelect: { autoStart = $0 }
|
||||
)
|
||||
}
|
||||
.padding(.horizontal, 16)
|
||||
.padding(.vertical, 10)
|
||||
.frame(minHeight: 50)
|
||||
}
|
||||
|
||||
if fullKeyboard {
|
||||
KeyboardLanguagesRow()
|
||||
|
||||
@@ -431,6 +470,9 @@ struct KeyboardSettingsPage: View {
|
||||
.onChange(of: hapticStrength) { _, strength in
|
||||
FullKeyboardSettings().hapticStrength = strength
|
||||
}
|
||||
.onChange(of: autoStart) { _, choice in
|
||||
FullKeyboardSettings().dictationBarAutoStart = choice
|
||||
}
|
||||
.onAppear {
|
||||
fullKeyboard = FullKeyboardSettings().isEnabled
|
||||
autoCapitalization = FullKeyboardSettings().autoCapitalization
|
||||
@@ -444,6 +486,7 @@ struct KeyboardSettingsPage: View {
|
||||
emoji = FullKeyboardSettings().emoji
|
||||
emojiHistoryEmpty = EmojiUsageStore().isEmpty
|
||||
hapticStrength = FullKeyboardSettings().hapticStrength
|
||||
autoStart = FullKeyboardSettings().dictationBarAutoStart
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
import XCTest
|
||||
@testable import HEX
|
||||
|
||||
@MainActor
|
||||
final class KeyboardSwitchGuessTests: XCTestCase {
|
||||
private let field = UUID()
|
||||
|
||||
func testSameFieldAfterHidingIsASwitchBack() {
|
||||
let guess = KeyboardSwitchGuess(hiddenDocument: field, hiddenAt: 100, hostLeftAt: nil)
|
||||
XCTAssertTrue(guess.isSwitchBack(to: field))
|
||||
}
|
||||
|
||||
func testAnotherFieldOrAnUnnamedOneIsNot() {
|
||||
let guess = KeyboardSwitchGuess(hiddenDocument: field, hiddenAt: 100, hostLeftAt: nil)
|
||||
XCTAssertFalse(guess.isSwitchBack(to: UUID()))
|
||||
XCTAssertFalse(guess.isSwitchBack(to: nil))
|
||||
XCTAssertFalse(KeyboardSwitchGuess().isSwitchBack(to: field))
|
||||
}
|
||||
|
||||
func testTheAppLeavingAroundOrAfterTheHideIsNot() {
|
||||
let leftAfter = KeyboardSwitchGuess(hiddenDocument: field, hiddenAt: 100, hostLeftAt: 130)
|
||||
let leftJustBefore = KeyboardSwitchGuess(hiddenDocument: field, hiddenAt: 100, hostLeftAt: 99)
|
||||
let leftLongBefore = KeyboardSwitchGuess(hiddenDocument: field, hiddenAt: 100, hostLeftAt: 50)
|
||||
XCTAssertFalse(leftAfter.isSwitchBack(to: field))
|
||||
XCTAssertFalse(leftJustBefore.isSwitchBack(to: field))
|
||||
XCTAssertTrue(leftLongBefore.isSwitchBack(to: field))
|
||||
}
|
||||
|
||||
func testStoreRoundTrips() throws {
|
||||
let suite = "KeyboardSwitchGuessTests.\(UUID().uuidString)"
|
||||
let defaults = try XCTUnwrap(UserDefaults(suiteName: suite))
|
||||
defer { defaults.removePersistentDomain(forName: suite) }
|
||||
let store = KeyboardSwitchGuessStore(defaults: defaults)
|
||||
store.keyboardHid(in: field, at: 100)
|
||||
XCTAssertTrue(store.guess.isSwitchBack(to: field))
|
||||
store.hostLeft(at: 101)
|
||||
XCTAssertFalse(store.guess.isSwitchBack(to: field))
|
||||
}
|
||||
|
||||
func testAutoStartDefaultsToWhenSwitching() throws {
|
||||
let suite = "KeyboardSwitchGuessTests.\(UUID().uuidString)"
|
||||
let defaults = try XCTUnwrap(UserDefaults(suiteName: suite))
|
||||
defer { defaults.removePersistentDomain(forName: suite) }
|
||||
let settings = FullKeyboardSettings(defaults: defaults)
|
||||
XCTAssertEqual(settings.dictationBarAutoStart, .whenSwitching)
|
||||
settings.dictationBarAutoStart = .always
|
||||
XCTAssertEqual(settings.dictationBarAutoStart, .always)
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,16 @@ nonisolated enum KeyHapticStrength: String, CaseIterable, Sendable {
|
||||
case strong
|
||||
}
|
||||
|
||||
/// Settings › Keyboard › Auto-start, while the dictation bar is chosen: whether the bar starts
|
||||
/// dictating by itself as it shows, so Dictate needs no tap.
|
||||
nonisolated enum DictationBarAutoStart: String, CaseIterable, Sendable {
|
||||
case never
|
||||
/// Only when you switch to Hex from another keyboard in the same field
|
||||
/// (`KeyboardSwitchGuess`), not when a field opens with Hex already chosen.
|
||||
case whenSwitching
|
||||
case always
|
||||
}
|
||||
|
||||
/// The full keyboard's switches in the shared App Group.
|
||||
///
|
||||
/// Without Full Access an iPhone may not let the keyboard read the App Group (the simulator
|
||||
@@ -38,6 +48,7 @@ struct FullKeyboardSettings {
|
||||
static let emojiLettersUnderEmojiKey = "experiments.emojiLettersUnderEmojiKey"
|
||||
static let swipeTyping = "experiments.swipeTyping"
|
||||
static let hexKeyOnLeft = "settings.keyboardHexKeyLeft"
|
||||
static let dictationBarAutoStart = "settings.dictationBarAutoStart"
|
||||
}
|
||||
|
||||
private let defaults: UserDefaults?
|
||||
@@ -167,6 +178,16 @@ struct FullKeyboardSettings {
|
||||
nonmutating set { defaults?.set(newValue, forKey: Key.hexKeyOnLeft) }
|
||||
}
|
||||
|
||||
/// Settings › Keyboard › Auto-start: when the dictation bar starts dictating by itself.
|
||||
/// When switching by default.
|
||||
var dictationBarAutoStart: DictationBarAutoStart {
|
||||
get {
|
||||
defaults?.string(forKey: Key.dictationBarAutoStart).flatMap(DictationBarAutoStart.init(rawValue:))
|
||||
?? .whenSwitching
|
||||
}
|
||||
nonmutating set { defaults?.set(newValue.rawValue, forKey: Key.dictationBarAutoStart) }
|
||||
}
|
||||
|
||||
/// Where the full keyboard's Hex key sits for `hexKeyOnLeft`.
|
||||
var hexKeyPlacement: HexKeyPlacement {
|
||||
hexKeyOnLeft ? .stripLeading : .stripTrailing
|
||||
|
||||
@@ -11,6 +11,8 @@ nonisolated enum KeyboardLaunchWork {
|
||||
static let afterFirstFrame: TimeInterval = 0.3
|
||||
/// The optional work, a turn of its own after the text checker's.
|
||||
static let optionalWorkDelay: TimeInterval = 0.6
|
||||
/// The dictation bar's Auto-start, a turn after the optional work.
|
||||
static let autoStartDelay: TimeInterval = 0.8
|
||||
/// Memory left below iOS's limit for the keyboard that the optional work needs: what the
|
||||
/// first in-keyboard dictation needs (`KeyboardLocalDictation.startHeadroomNeeded`), as
|
||||
/// the capture page is that dictation's start made early.
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
import Foundation
|
||||
|
||||
/// Whether the keyboard is showing because you switched to it from another keyboard, which iOS
|
||||
/// never says: a keyboard just appears, whether a field opened with it already chosen or you
|
||||
/// picked it with the globe key. A guess from when the keyboard last hid: it comes back in
|
||||
/// the same field (UIKit's `documentIdentifier`), and that field's app didn't go to the
|
||||
/// background in between (opening Hex, going home, another app). So switching away and back
|
||||
/// in one field counts; a field that opens with Hex's keyboard already chosen is another
|
||||
/// document, and so is the first switch to Hex in a field another keyboard opened, which this
|
||||
/// misses. Not yet proven on a device.
|
||||
nonisolated struct KeyboardSwitchGuess: Equatable {
|
||||
/// The field the keyboard was in when it last hid, and when.
|
||||
var hiddenDocument: UUID?
|
||||
var hiddenAt: TimeInterval?
|
||||
/// When the field's app last went to the background.
|
||||
var hostLeftAt: TimeInterval?
|
||||
|
||||
/// The background notice and the hide can come in either order as the app leaves.
|
||||
static let hostLeaveSlack: TimeInterval = 2
|
||||
|
||||
func isSwitchBack(to document: UUID?) -> Bool {
|
||||
guard let document, let hiddenDocument, let hiddenAt, document == hiddenDocument else { return false }
|
||||
if let hostLeftAt, hostLeftAt >= hiddenAt - Self.hostLeaveSlack { return false }
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
/// `KeyboardSwitchGuess` in the keyboard's own defaults, outliving its process.
|
||||
nonisolated struct KeyboardSwitchGuessStore {
|
||||
private enum Key {
|
||||
static let hiddenDocument = "keyboard.switch.hiddenDocument"
|
||||
static let hiddenAt = "keyboard.switch.hiddenAt"
|
||||
static let hostLeftAt = "keyboard.switch.hostLeftAt"
|
||||
}
|
||||
|
||||
private let defaults: UserDefaults
|
||||
|
||||
init(defaults: UserDefaults = .standard) {
|
||||
self.defaults = defaults
|
||||
}
|
||||
|
||||
var guess: KeyboardSwitchGuess {
|
||||
KeyboardSwitchGuess(
|
||||
hiddenDocument: defaults.string(forKey: Key.hiddenDocument).flatMap(UUID.init(uuidString:)),
|
||||
hiddenAt: defaults.object(forKey: Key.hiddenAt) as? TimeInterval,
|
||||
hostLeftAt: defaults.object(forKey: Key.hostLeftAt) as? TimeInterval
|
||||
)
|
||||
}
|
||||
|
||||
func keyboardHid(in document: UUID?, at time: TimeInterval) {
|
||||
defaults.set(document?.uuidString, forKey: Key.hiddenDocument)
|
||||
defaults.set(time, forKey: Key.hiddenAt)
|
||||
}
|
||||
|
||||
func hostLeft(at time: TimeInterval) {
|
||||
defaults.set(time, forKey: Key.hostLeftAt)
|
||||
}
|
||||
}
|
||||
+17
-1
@@ -259,6 +259,21 @@ never at a launch by itself.
|
||||
more than 10 pt first opens the style menu, dropping that capture; releasing over a row picks it, and releasing elsewhere leaves
|
||||
the menu open for taps (`HexKeyGesture`). VoiceOver activation dictates,
|
||||
and the **Dictation styles** action opens the menu.
|
||||
- **Auto-start:** Settings › Keyboard › Auto-start (`FullKeyboardSettings.dictationBarAutoStart`)
|
||||
lets the dictation bar start dictating by itself as it shows, as a tap on **Dictate**
|
||||
would, 0.8 s after it appears (after the first frame and the launch's optional work),
|
||||
only while idle, outside number pads and the emoji panel, and only where it dictates
|
||||
without leaving the field (a warm session or in-keyboard dictation, never by opening
|
||||
Hex). **Never**, **On switch** (the default) or **Always**. iOS doesn't tell a
|
||||
keyboard whether you switched to it or a field opened with it, so On switch guesses
|
||||
(`KeyboardSwitchGuess`): the keyboard comes back in the field it last hid in
|
||||
(`documentIdentifier`), and the field's app didn't go to the background
|
||||
(`NSExtensionHostDidEnterBackground`) from 2 s before that hide on. Switching to
|
||||
another keyboard and back in one field starts; a field that opens with Hex already
|
||||
chosen doesn't. Known gap: the first switch to Hex in a field another keyboard opened
|
||||
is another document, so it doesn't start. Unproven on a device: whether
|
||||
`documentIdentifier` stays the same across a keyboard switch and changes when a field
|
||||
is focused again, which the guess relies on.
|
||||
- **Push to talk:** Holding still for 0.35 s on the full keyboard's Hex key
|
||||
or the dictation bar's **Dictate** starts dictation at once with a light
|
||||
haptic (Settings › Keyboard › Haptics), and lifting the finger finishes it as
|
||||
@@ -651,7 +666,8 @@ never at a launch by itself.
|
||||
from when the full keyboard was an experiment: unset or true is Full, false
|
||||
the dictation bar, so testers who turned that experiment off keep the bar;
|
||||
on an iPad, where the keyboard always shows the dictation bar, the row says so:
|
||||
"On iPad, Hex shows the dictation bar"), then Languages, Auto-capitalization, Autocorrect (with Spatial autocorrect below it while it's
|
||||
"On iPad, Hex shows the dictation bar"), Auto-start (Never, On switch by default, or
|
||||
Always) while the dictation bar is chosen or on an iPad, then Languages, Auto-capitalization, Autocorrect (with Spatial autocorrect below it while it's
|
||||
on), Suggest swear words, Vocabulary (Hex's custom vocabulary list, how many words it
|
||||
holds) and Key pop-ups only while Full is chosen, Haptics (the dictation bar uses it too),
|
||||
then Swipe typing and Emoji (Clear emoji history while it's on), which say they need the
|
||||
|
||||
Reference in New Issue
Block a user