mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
TestFlight build 56 (2ef08aa4): after moving the cursor into the dictated text, Undo dictation did nothing. The chip stayed up for its 12 s after any cursor move, but undoDictation only deleted when the dictation was still right before the cursor, so a tap after a move just hid it. origin/dev (96b4f365) had the same code. Now the keyboard keeps the text either side of the dictation from when it went in (DictationUndo) and finds it again in the host's context around the cursor. When it is shown whole and exactly once, and the text either side still matches, Undo moves the cursor to its end (UTF-16 offsets that stay inside what the host showed), deletes it and puts the cursor back in the text it was moved to. Text views that show only the cursor's sentence leave out the space before it; that space goes too when the sentence starts what they show. When it can't be found (a selection, another sentence, the same words twice, text changed around it) the chip hides as the host reports the change instead of sitting there doing nothing. The unmoved case keeps its old check, now also refused while text is selected (deleteBackward would have taken the selection first) and when what followed it moved. KeyboardTextTarget gains selectedText (nil by default) for this. Verified: iPhone 16 Pro simulator, iOS 27.0, a UITextView host, a real dictation insert through Hex's hosted-keyboard-tour fixture ("Hi Sam." + " See you at ten."), real touches: build 56 left the text unchanged after moving into the dictation or before it; this change restores "Hi Sam." with the cursor at 7 after moving into it, unmoved undo still works, and moving to another sentence (before it, or into later text) hides the chip with nothing deleted. HEXMobile tests on that simulator: 661 run, the new case passes; the 11 cleanup-tagger failures were the unfetched model and pass (78/78) after scripts/fetch-cleanup-model.sh.
134 lines
5.3 KiB
Swift
134 lines
5.3 KiB
Swift
import UIKit
|
||
|
||
/// The emoji you use, kept in the App Group on this iPhone: a count that fades by half every
|
||
/// ten days (so both how often and how lately count) and the skin tone you last picked for
|
||
/// each emoji. No words, nothing about what you write. Settings › Keyboard › Clear emoji history
|
||
/// empties it.
|
||
@MainActor
|
||
final class EmojiUsageStore {
|
||
static let halfLife: TimeInterval = 10 * 86_400
|
||
/// The most emoji remembered; the faintest go first.
|
||
static let capacity = 120
|
||
|
||
private enum Key {
|
||
static let usage = "emoji.usage"
|
||
static let tones = "emoji.tones"
|
||
}
|
||
|
||
private let defaults: UserDefaults?
|
||
private let now: () -> Date
|
||
/// Base emoji to [score at `last`, last use as seconds since 1970].
|
||
private var usage: [String: [Double]]
|
||
/// Base emoji to the skin tone picked for it: 1 (light) to 5 (dark).
|
||
private var tones: [String: Int]
|
||
|
||
init(defaults: UserDefaults? = UserDefaults(suiteName: KeyboardBridge.appGroup), now: @escaping () -> Date = Date.init) {
|
||
self.defaults = defaults
|
||
self.now = now
|
||
usage = defaults?.dictionary(forKey: Key.usage) as? [String: [Double]] ?? [:]
|
||
tones = defaults?.dictionary(forKey: Key.tones) as? [String: Int] ?? [:]
|
||
}
|
||
|
||
/// You picked `base` (the emoji without a skin tone).
|
||
func record(_ base: String) {
|
||
let time = now().timeIntervalSince1970
|
||
usage[base] = [score(of: base, at: time) + 1, time]
|
||
if usage.count > Self.capacity {
|
||
let faintest = usage.keys.min { score(of: $0, at: time) < score(of: $1, at: time) }
|
||
if let faintest { usage[faintest] = nil }
|
||
}
|
||
defaults?.set(usage, forKey: Key.usage)
|
||
}
|
||
|
||
private func score(of base: String, at time: TimeInterval) -> Double {
|
||
guard let stored = usage[base], stored.count == 2 else { return 0 }
|
||
return stored[0] * pow(0.5, max(0, time - stored[1]) / Self.halfLife)
|
||
}
|
||
|
||
/// Your emoji, most used first, with scores scaled so the first is 1.
|
||
func ranked(limit: Int = 40) -> [(emoji: String, score: Float)] {
|
||
let time = now().timeIntervalSince1970
|
||
let scored = usage.keys
|
||
.map { ($0, score(of: $0, at: time)) }
|
||
.sorted { $0.1 != $1.1 ? $0.1 > $1.1 : $0.0 < $1.0 }
|
||
.prefix(limit)
|
||
guard let top = scored.first?.1, top > 0 else { return [] }
|
||
return scored.map { ($0.0, Float($0.1 / top)) }
|
||
}
|
||
|
||
/// 0 for the default yellow, else 1 (light) to 5 (dark).
|
||
func tone(for base: String) -> Int {
|
||
tones[base] ?? 0
|
||
}
|
||
|
||
func setTone(_ tone: Int, for base: String) {
|
||
tones[base] = tone == 0 ? nil : tone
|
||
defaults?.set(tones, forKey: Key.tones)
|
||
}
|
||
|
||
var isEmpty: Bool { usage.isEmpty && tones.isEmpty }
|
||
|
||
func clear() {
|
||
usage = [:]
|
||
tones = [:]
|
||
defaults?.removeObject(forKey: Key.usage)
|
||
defaults?.removeObject(forKey: Key.tones)
|
||
}
|
||
}
|
||
|
||
/// The keyboard's text target while the emoji panel is up: the field, or the emoji search's
|
||
/// query while you search, so the letter keys type into the search instead of the field.
|
||
@MainActor
|
||
final class EmojiRoutedTarget: KeyboardTextTarget {
|
||
let document: KeyboardTextTarget
|
||
/// Set while searching; the keys type here.
|
||
var query: String?
|
||
/// Return while searching.
|
||
var onSearchReturn: (() -> Void)?
|
||
|
||
init(document: KeyboardTextTarget) {
|
||
self.document = document
|
||
}
|
||
|
||
var isSearching: Bool { query != nil }
|
||
|
||
var documentContextBeforeInput: String? { query ?? document.documentContextBeforeInput }
|
||
var documentContextAfterInput: String? { isSearching ? nil : document.documentContextAfterInput }
|
||
var hasText: Bool { query.map { !$0.isEmpty } ?? document.hasText }
|
||
var autocapitalizationType: UITextAutocapitalizationType? { isSearching ? UITextAutocapitalizationType.none : document.autocapitalizationType }
|
||
var autocorrectionType: UITextAutocorrectionType? { isSearching ? .no : document.autocorrectionType }
|
||
var keyboardType: UIKeyboardType? { isSearching ? .default : document.keyboardType }
|
||
var returnKeyType: UIReturnKeyType? { isSearching ? .done : document.returnKeyType }
|
||
var enablesReturnKeyAutomatically: Bool? { isSearching ? false : document.enablesReturnKeyAutomatically }
|
||
var keyboardAppearance: UIKeyboardAppearance? { document.keyboardAppearance }
|
||
var textContentType: UITextContentType? { isSearching ? nil : document.textContentType }
|
||
var smartQuotesType: UITextSmartQuotesType? { isSearching ? .no : document.smartQuotesType }
|
||
var smartDashesType: UITextSmartDashesType? { isSearching ? .no : document.smartDashesType }
|
||
var selectedText: String? { isSearching ? nil : document.selectedText }
|
||
|
||
func insertText(_ text: String) {
|
||
guard let current = query else {
|
||
document.insertText(text)
|
||
return
|
||
}
|
||
if text.contains("\n") {
|
||
onSearchReturn?()
|
||
} else {
|
||
query = current + text
|
||
}
|
||
}
|
||
|
||
func deleteBackward() {
|
||
guard var current = query else {
|
||
document.deleteBackward()
|
||
return
|
||
}
|
||
if !current.isEmpty { current.removeLast() }
|
||
query = current
|
||
}
|
||
|
||
func adjustTextPosition(byCharacterOffset offset: Int) {
|
||
guard !isSearching else { return }
|
||
document.adjustTextPosition(byCharacterOffset: offset)
|
||
}
|
||
}
|