hex/ios/HEXShared/FullKeyboard/FullKeyboardView+Emoji.swift
Ryan Vogel 1b44779efb
iOS: text replacements without autocorrect, End with a period, and emoji and symbols on the Dictation bar (#2147)
* iOS: text replacements apply with Autocorrect off

A TestFlight tester (build 56) kept Settings > Keyboard > Autocorrect on only so
iOS text replacements (UILexicon shortcuts like "omw") would work: the engine's
isEnabled gate returned no fix on space or a closing mark, and the strip never
offered the replacement.

Apple's keyboard with Auto-Correction off (black box, iOS 27.2 simulator, Settings
> General > Keyboard) still shows "On my way!" as the highlighted centre
suggestion for "omw", puts it in on space and on ".", and leaves "teh" alone. Hex
now matches: with Autocorrect off, only text replacements show (auto-replacing) in
the strip and apply on space or punctuation; every other fix stays off. Fields with
autocorrectionType .no still get none, and the existing per-word suppression (a
tapped literal, an undone fix) still blocks them. One backspace puts the shortcut
back, as before.

Verified: new AutocorrectEngineTests case (strip, space, backspace, ".", a .no
field); AutocorrectEngineTests pass; live in Messages on an iOS 27.2 sim with
Hex's Autocorrect off: "omw" offers and becomes "On my way! ", backspace -> "Omw".

* iOS: Settings > Dictation > End with a period

A TestFlight tester (build 56) asked to dictate without punctuation at the end of
the sentence while keeping the marks inside it.

Until now each style decided: Polished ends every line with "." (or "。" after
Chinese or Japanese), Casual drops the final "."/"。" of a short message (one line,
up to three sentences) but keeps the one Apple Speech wrote on longer ones, Notes
drops it from every line, and Verbatim keeps the model's text.

The new switch, on by default, keeps exactly that. Off, no line in Polished,
Casual or Notes ends with a full stop (. 。 . 。, also before a closing quote or
bracket), whether Apple Speech or the style put it there; "?" "!" "?" "!" stay,
as do ellipses, abbreviations ("etc.") and initialisms ("U.S."). Lists (Format
lists) follow it per line. Verbatim still keeps the model's exact words. It lives
in the App Group (settings.dictationEndWithPeriod) and is read at the start of each
dictation in the app and in in-keyboard dictation, like Format lists. zh-Hans
strings included.

Verified: new TranscriptCleanupTests cases (English, Chinese 。/?/!, Japanese
「」, Casual's long messages, Notes, Verbatim, list lines, pipeline and the
setting's default); full HEXMobileTests suite passes; Settings > Dictation
checked in Chinese on an iOS 27.2 sim.

* iOS: punctuation keys and an emoji key on the dictation bar

A TestFlight tester (build 56) asked for an emoji button and symbol input on the
dictation bar keyboard style (Settings > Keyboard > Dictation bar), whose bottom row
had only the globe (on Touch ID iPhones), space, delete and return.

The bottom row now also holds an emoji key and the commonest punctuation
, . ? ! ' " @ #, as many as leave the space bar 60 pt (ViewThatFits: five on a
402 pt iPhone, six on a Pro Max, three on the SE beside its globe). The bar keeps
its 140 pt height, its top bar and its 44 pt well with Dictate/Hex untouched; to
make room the delete and return keys narrow (52 -> 44, 56 -> 48 pt) and the row's
spacing goes from 6 to 5 pt.

The emoji key opens the full keyboard's EmojiPanelView in place of the bar, at the
full keyboard's height (the bar can't hold its grid); its ABC key goes back to the
bar at 140 pt. It records use like the full keyboard (Frequently used is shared),
hides the search button (its query is typed on letter keys the bar doesn't have,
new EmojiPanelView.showsSearch), and closes when the keyboard hides, on a memory
warning, when dictation starts, or when the keyboard style changes. The panel's
fill logic moved from FullKeyboardView into EmojiPanelView.fill(before:) so both
share it. The key follows Settings > Keyboard > Emoji (whose detail no longer says
it needs the full keyboard while the bar is chosen) and shows in fields that take
emoji on iPhone, as the full keyboard's.

Verified: Release build on an iOS 27.2 sim (iPhone 17 Pro) in Messages, light and
dark: five punctuation keys typed ",.?!' ", the emoji panel opened at full height,
an emoji went in and delete worked, ABC returned to the bar; Settings > Keyboard in
Chinese; full HEXMobileTests suite passes.
2026-11-21 06:24:16 +00:00

249 lines
9.7 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import QuartzCore
import UIKit
/// Settings › Keyboard › Emoji in the full keyboard: the emoji key, its panel, the emoji search
/// (the letter keys type into it while the results sit above), and an emoji in the empty
/// strip for the word just finished ("pizza " offers 🍕).
///
/// The index (`EmojiLibrary`, a memory-mapped file and about 0.7 MB of offsets) loads on
/// the first emoji key press or finished word and stays while the keyboard runs, apart
/// from memory warnings; the panel, its grid and the search go when the panel closes.
extension FullKeyboardView {
var emojiEnabled: Bool {
emojiOverride ?? FullKeyboardSettings().emoji
}
/// Emoji in text fields that take any text: not web addresses, email addresses or
/// ASCII-only fields.
func refreshEmojiKey() {
let document = routedTarget.document
let takesEmoji: Bool = {
guard case .keys(.text, _) = KeyboardFieldRouting.route(for: document.keyboardType) else { return false }
return document.keyboardType != .asciiCapable
}()
let shows = emojiEnabled && takesEmoji
guard engine.showsEmoji != shows else { return }
engine.showsEmoji = shows
plane.setNeedsDisplay()
setNeedsLayout()
}
var isShowingEmoji: Bool { emojiPanel != nil }
func loadEmojiLibrary() -> EmojiLibrary? {
let language = engine.language.rawValue
if let library = emojiLibrary, library.language == language || !EmojiLibrary.languages.contains(language) {
return library
}
emojiLibrary = EmojiLibrary.load(language: language, bundle: Bundle(for: FullKeyboardView.self))
return emojiLibrary
}
// MARK: - Panel
func emojiKeyTapped() {
if routedTarget.isSearching {
endEmojiSearch()
} else {
openEmojiPanel()
}
}
func openEmojiPanel() {
guard emojiPanel == nil, !showsRecording else { return }
let started = CACurrentMediaTime()
guard let library = loadEmojiLibrary() else { return }
typingLog?.ui("emoji_open")
dismissStyleMenu()
plane.cancelAllTouches()
let panel = EmojiPanelView(library: library, usage: makeEmojiUsage())
panel.palette = palette
panel.showsKeyPopups = engine.showsKeyPopups
panel.onKeyDown = { [weak self] in self?.keyFeedback() }
panel.onSelectionStep = { [weak self] in self?.selectionFeedback() }
panel.onInsert = { [weak self] text, entry, source in self?.insertEmoji(text, entry: entry, source: source) }
panel.onDelete = { [weak self] in self?.engine.deleteBackward(source: "emoji") }
panel.onDeleteRepeat = { [weak self] held in self?.engine.repeatDelete(heldFor: held) }
panel.onLetters = { [weak self] in
self?.typingLog?.ui("emoji_close")
self?.closeEmojiPanel()
}
panel.onSearch = { [weak self] in self?.beginEmojiSearch() }
insertSubview(panel, belowSubview: callout)
emojiPanel = panel
for view in [plane, stripView, hexKey, globeButton] as [UIView] { view.isHidden = true }
callout.show(nil, label: "")
accentCallout.isHidden = true
fillEmojiPanel()
setNeedsLayout()
layoutIfNeeded()
lastEmojiPanelLatency = CACurrentMediaTime() - started
}
/// The row's suggestions for the text before the cursor and your use, and the grid's
/// Frequently used.
func fillEmojiPanel() {
emojiPanel?.fill(before: routedTarget.document.documentContextBeforeInput)
}
func closeEmojiPanel() {
guard let panel = emojiPanel else { return }
if routedTarget.isSearching { endEmojiSearch() }
panel.tearDown()
panel.removeFromSuperview()
emojiPanel = nil
for view in [plane, stripView, hexKey] as [UIView] {
view.isHidden = false
view.alpha = 1
}
engine.textDidChange()
refreshReturnKey()
refreshStrip()
setNeedsLayout()
}
func insertEmoji(_ text: String, entry: Int, source: String) {
if routedTarget.isSearching {
// The keys type into the search; the emoji goes into the field.
suspendedTypingLog?.edit("insert", [
TypingLogField("inserted", .string(text)),
TypingLogField("src", .string("emoji_\(source)")),
])
routedTarget.document.insertText(text)
onTextEdit?()
} else {
engine.insertEmoji(text, source: "emoji_\(source)")
}
if let library = emojiPanel?.library ?? emojiLibrary, entry < library.entries.count {
emojiPanel?.usage.record(library.entries[entry].emoji)
}
}
/// An emoji picked from the strip: counted like one from the panel.
func recordEmojiUse(_ emoji: String) {
guard let library = emojiLibrary, let entry = library.entryByEmoji[emoji] else { return }
makeEmojiUsage().record(library.entries[entry].emoji)
}
// MARK: - Search
func beginEmojiSearch() {
guard let panel = emojiPanel, !routedTarget.isSearching else { return }
typingLog?.ui("emoji_search")
suspendedTypingLog = engine.typingLog
engine.typingLog = nil
routedTarget.query = ""
panel.isHidden = true
let field = EmojiSearchField()
field.palette = palette
field.onClose = { [weak self] in self?.endEmojiSearch() }
addSubview(field)
emojiSearchField = field
let results = EmojiRowView()
results.renderer = panel.glyphs
results.palette = palette
results.fontSize = panel.rowFontSize
results.emptyText = "No emoji"
results.onPick = { [weak self] index in
guard let self, index < self.emojiResultEntries.count, let panel = self.emojiPanel else { return }
self.keyFeedback()
let entry = self.emojiResultEntries[index]
self.insertEmoji(panel.display(for: entry), entry: entry, source: "search")
}
addSubview(results)
emojiResults = results
plane.isHidden = false
emojiSearchLift = engine.layout.metrics.stripHeight
onPreferredHeightChange?()
engine.configureForField()
refreshReturnKey()
refreshEmojiSearch()
setNeedsLayout()
layoutIfNeeded()
}
/// Typing changed the query: the results follow, your suggestions while it's empty.
func refreshEmojiSearch() {
guard let panel = emojiPanel, let query = routedTarget.query else { return }
emojiSearchField?.query = query
let library = panel.library
let ranked = panel.usage.ranked(limit: 32)
var boost: [Int: Float] = [:]
for (emoji, score) in ranked {
if let entry = library.entryByEmoji[emoji] { boost[entry] = score }
}
emojiResultEntries = query.trimmingCharacters(in: .whitespaces).isEmpty
? panel.suggestions
: EmojiSuggester.search(query, library: library, usage: boost)
emojiResults?.setItems(emojiResultEntries.map { (panel.display(for: $0), library.name(of: $0)) })
}
func endEmojiSearch() {
guard routedTarget.isSearching else { return }
routedTarget.query = nil
plane.cancelAllTouches()
engine.typingLog = suspendedTypingLog
suspendedTypingLog = nil
emojiSearchField?.removeFromSuperview()
emojiSearchField = nil
emojiResults?.removeFromSuperview()
emojiResults = nil
emojiResultEntries = []
emojiSearchLift = 0
onPreferredHeightChange?()
engine.configureForField()
refreshReturnKey()
if let panel = emojiPanel {
plane.isHidden = true
panel.isHidden = false
fillEmojiPanel()
}
setNeedsLayout()
}
func layoutEmoji(content: CGRect, lift: CGFloat) {
let layout = engine.layout
let m = layout.metrics
if let panel = emojiPanel {
panel.frame = routedTarget.isSearching ? content : bounds
panel.metrics = m
panel.horizontalInset = layout.horizontalInset
}
let inset = layout.horizontalInset
emojiSearchField?.frame = CGRect(x: inset, y: m.stripTop + lift, width: bounds.width - 2 * inset, height: m.stripHeight)
emojiResults?.frame = CGRect(x: inset + m.side, y: 0, width: bounds.width - 2 * (inset + m.side), height: lift)
}
// MARK: - Strip
/// Up to three emoji for the word just finished, when the strip would be empty.
func emojiForFinishedWord() -> [KeyboardSuggestion] {
guard engine.showsEmoji, emojiPanel == nil, !routedTarget.isSearching,
engine.target.autocorrectionType != .no else { return [] }
let before = routedTarget.document.documentContextBeforeInput
guard let before, before.hasSuffix(" "), !before.hasSuffix(" ") else { return [] }
guard let library = loadEmojiLibrary() else { return [] }
let usage = makeEmojiUsage()
return EmojiSuggester.forFinishedWord(before, library: library).map { entry in
let item = library.entries[entry]
let tone = item.hasTones ? usage.tone(for: item.emoji) : 0
let emoji = tone > 0 ? item.tones[tone - 1] : item.emoji
return KeyboardSuggestion(displayText: emoji, replacement: emoji, kind: .emoji)
}
}
// MARK: - Fixtures
/// Fixtures: open the panel, optionally searching `query`.
func debugOpenEmoji(search query: String? = nil) {
openEmojiPanel()
guard let query else { return }
beginEmojiSearch()
for character in query {
routedTarget.insertText(String(character))
}
refreshEmojiSearch()
}
}