hex/ios/HEXShared/FullKeyboard/KeyboardStripView.swift
Ryan Vogel ea4651e112 iOS: emoji panel for the full keyboard (Experiments › Emoji)
What changed
- Experiments › Full keyboard › Emoji (experiments.emoji, off by default; shown with the
  full keyboard): an emoji key (face.smiling) right of 123, after the globe, in plain
  text fields (not URL, email or ASCII-only ones). The bottom row takes the compact
  sizes it uses beside the globe. "Clear emoji history" empties EmojiUsageStore.
- EmojiPanelView replaces the strip and the keys: a row of up to ten suggestions for
  the text before the cursor and your history (one emoji per family, your skin tone),
  a search button; a grid that scrolls sideways through Frequently used (your history,
  then everyday emoji, 16 at least) and the eight categories in four rows of 42 pt
  (three of 38 in landscape), each section's name kept at the left edge while it
  scrolls; a bar with ABC, the categories and a repeating delete, drawn like the
  letter keys' 123 and delete. Holding an emoji with skin tones opens the accent
  picker with its five tones; the pick is typed and remembered for that emoji. Key
  pop-ups and haptics follow their switches.
- Search: the letter keys type into a search field in the strip (EmojiRoutedTarget),
  results above it in a row the keyboard grows 44 pt for, your suggestions while it's
  empty; return, the field's close button or the emoji key go back to the panel.
  Typing logs and Learn how I type see the emoji typed into the field, never the query.
- The strip, when it would be empty after a space, offers the finished word's strong
  emoji ("pizza " -> 🍕, "birthday " -> 🎂 🎉 🥳), a lone one in the centre slot; never
  over word suggestions, Undo dictation, a status or the clipboard chips.
- Grid cells drop their text (and bitmap) when they scroll off and prefetching is off:
  scrolling every section had kept 353 cell bitmaps, 35 MB.
- FullKeyboardView+Emoji.swift holds the wiring; the engine's target is now the routed
  target, which forwards everything to the field unless a search is open.
- Scenes: full-keyboard-emoji, -emoji-search (--emoji-query), -emoji-tones,
  -emoji-food, -emoji-strip (each -dark and -dark-field), and -emoji-probe, which prints
  the panel's footprint and open latency.

Measured in the keyboard extension (Release, iPhone 17 simulator, iOS 27.2, a field
focused with the real HEXKeyboard.appex and a temporary, uncommitted probe; the
simulator counts no GPU memory and reports no headroom), footprint:
  keyboard with the emoji key, nothing loaded   19.0 MB
  after "pizza " (the strip loads the index)    19.0 MB
  panel open                                     28.7 MB
  picked three suggestions, closed               22.7 MB
  every section scrolled                         41.6 MB (peak 48.4; 71.1 before the cell fix)
  closed after that                              36.5 MB
The 17.5 MB left after browsing everything is about 351 emoji glyph images the system's
text rendering caches process-wide (18.7 MB in the heap, 351 CGImages); drawing with a
font instance the panel owns or at 21 pt left it the same, and no public API frees it.
Whether iOS purges it under memory pressure is unverified on an iPhone.
Opening: 22-26 ms from the emoji key to the panel laid out the first time, 12-14 ms
after (median of 10), about 21 ms until committed.

Verified: xcodebuild test (245 tests, 1 skipped, 0 failures); every scene above in
light, dark and dark field looked at on the simulator.
2026-11-20 10:49:41 -05:00

336 lines
14 KiB
Swift

import UIKit
/// The 3-slot top bar above the keys, matching Apple's iOS 26 suggestion strip:
/// two 1 pt vertical hairlines at 1/3 and 2/3 width when idle or suggesting, replaced by
/// the centered `Undo dictation` chip or a status message when needed.
@MainActor
final class KeyboardStripView: UIView {
var palette = FullKeyboardPalette(isDark: false) { didSet { if palette != oldValue { applyPalette() } } }
/// Hosted extensions sit inside iOS 26's +17 pt Liquid Glass crown above `bounds.minY`,
/// so nudge the strip's visual centre slightly upward within `bounds` to align with Apple's bar.
var inSystemPlatter = true {
didSet {
guard inSystemPlatter != oldValue else { return }
for button in slotButtons { button.inSystemPlatter = inSystemPlatter }
setNeedsLayout()
setNeedsDisplay()
}
}
var onSelectSuggestion: ((KeyboardSuggestion) -> Void)?
var onUndoDictation: (() -> Void)?
var onPaste: (() -> Void)?
var onNextKeyboard: (() -> Void)?
private let statusLabel = UILabel()
private let undoChip = UIButton(type: .custom)
private let slotButtons: [SlotButton] = [SlotButton(), SlotButton(), SlotButton()]
private var currentSuggestions: [KeyboardSuggestion] = []
private var showsUndo = false
private var statusText: String?
private var clipboard: KeyboardClipboardOffer?
/// A plain chip: `UIPasteControl` renders as an empty blue box inside a keyboard
/// extension (TestFlight build 4), so this reads the clipboard on tap instead.
private let pasteControl = UIButton(type: .custom)
private let nextKeyboardChip = UIButton(type: .custom)
override init(frame: CGRect) {
super.init(frame: frame)
isOpaque = false
backgroundColor = .clear
contentMode = .redraw
statusLabel.font = .systemFont(ofSize: 13.5)
statusLabel.textAlignment = .center
statusLabel.adjustsFontSizeToFitWidth = true
statusLabel.minimumScaleFactor = 0.8
statusLabel.isHidden = true
addSubview(statusLabel)
var chip = UIButton.Configuration.plain()
chip.image = UIImage(
systemName: "arrow.uturn.backward",
withConfiguration: UIImage.SymbolConfiguration(pointSize: 12.5, weight: .medium)
)
chip.imagePadding = 6
chip.title = "Undo dictation"
chip.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 13, bottom: 0, trailing: 13)
chip.background.cornerRadius = 15
undoChip.configuration = chip
undoChip.isHidden = true
undoChip.addTarget(self, action: #selector(handleUndo), for: .touchUpInside)
var paste = UIButton.Configuration.plain()
paste.image = UIImage(
systemName: "doc.on.clipboard",
withConfiguration: UIImage.SymbolConfiguration(pointSize: 12.5, weight: .medium)
)
paste.imagePadding = 6
paste.title = "Paste"
paste.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 13, bottom: 0, trailing: 13)
paste.background.cornerRadius = 15
pasteControl.configuration = paste
pasteControl.isHidden = true
pasteControl.addTarget(self, action: #selector(handlePaste), for: .touchUpInside)
addSubview(pasteControl)
addSubview(undoChip)
var next = UIButton.Configuration.plain()
next.image = UIImage(
systemName: "keyboard",
withConfiguration: UIImage.SymbolConfiguration(pointSize: 12.5, weight: .medium)
)
next.imagePadding = 6
next.title = "Code from Messages"
next.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 13, bottom: 0, trailing: 13)
next.background.cornerRadius = 15
nextKeyboardChip.configuration = next
nextKeyboardChip.accessibilityHint = "Switches to the next keyboard, where iOS offers codes from Messages and Mail"
nextKeyboardChip.isHidden = true
nextKeyboardChip.addTarget(self, action: #selector(handleNextKeyboard), for: .touchUpInside)
addSubview(nextKeyboardChip)
for (index, button) in slotButtons.enumerated() {
button.tag = index
button.addTarget(self, action: #selector(handleSlotTap(_:)), for: .touchUpInside)
button.isHidden = true
addSubview(button)
}
applyPalette()
}
@available(*, unavailable)
required init?(coder: NSCoder) { fatalError() }
func update(
suggestions: [KeyboardSuggestion],
showsUndo: Bool,
statusText: String?,
clipboard: KeyboardClipboardOffer? = nil
) {
let changed = suggestions != currentSuggestions || showsUndo != self.showsUndo
|| statusText != self.statusText || clipboard != self.clipboard
guard changed else { return }
currentSuggestions = suggestions
self.showsUndo = showsUndo
self.statusText = statusText
self.clipboard = clipboard
undoChip.isHidden = !showsUndo
let showsClipboard = showsClipboard
pasteControl.isHidden = !showsClipboard || clipboard == .code(canPaste: false)
nextKeyboardChip.isHidden = !showsClipboard || clipboard == .paste
if showsUndo || showsClipboard {
statusLabel.isHidden = true
slotButtons.forEach { $0.isHidden = true }
} else if let statusText, suggestions.isEmpty {
statusLabel.text = statusText
statusLabel.isHidden = false
slotButtons.forEach { $0.isHidden = true }
} else {
statusLabel.isHidden = true
let slots = Self.slots(for: suggestions)
for (idx, button) in slotButtons.enumerated() {
if let item = slots.firstIndex(of: idx) {
button.configure(with: suggestions[item], palette: palette)
button.isHidden = false
} else {
button.isHidden = true
}
}
}
setNeedsLayout()
setNeedsDisplay()
}
/// Which slot shows each suggestion: in order, except a lone emoji, which takes the centre.
static func slots(for suggestions: [KeyboardSuggestion]) -> [Int] {
if suggestions.count == 1, suggestions[0].kind == .emoji { return [1] }
return Array(suggestions.indices.prefix(3))
}
/// Clipboard chips replace the empty strip; suggestions and status win.
private var showsClipboard: Bool {
guard let clipboard, !showsUndo, statusText == nil else { return false }
if case .code = clipboard { return true }
return currentSuggestions.isEmpty
}
@objc private func handlePaste() {
onPaste?()
}
@objc private func handleNextKeyboard() {
onNextKeyboard?()
}
private func applyPalette() {
statusLabel.textColor = palette.textMuted
var paste = pasteControl.configuration
paste?.baseForegroundColor = palette.textBase
paste?.background.backgroundColor = palette.chipFill
pasteControl.configuration = paste
var next = nextKeyboardChip.configuration
next?.baseForegroundColor = palette.textMuted
next?.background.backgroundColor = palette.chipFill
nextKeyboardChip.configuration = next
var chip = undoChip.configuration
chip?.baseForegroundColor = palette.textMuted
chip?.background.backgroundColor = palette.chipFill
undoChip.configuration = chip
for (item, slot) in Self.slots(for: currentSuggestions).enumerated() {
slotButtons[slot].configure(with: currentSuggestions[item], palette: palette)
}
setNeedsDisplay()
}
private var contentCenterY: CGFloat {
bounds.height * 0.5
}
override func layoutSubviews() {
super.layoutSubviews()
let midY = contentCenterY
statusLabel.frame = CGRect(x: 16, y: max(0, midY - 12), width: max(0, bounds.width - 32), height: 24)
let chipH = min(34, max(24, bounds.height - 10))
let chipSize = undoChip.sizeThatFits(CGSize(width: bounds.width, height: chipH))
undoChip.frame = CGRect(
x: (bounds.width - chipSize.width) / 2,
y: max(0, midY - chipH / 2),
width: chipSize.width,
height: chipH
)
let pasteW = pasteControl.sizeThatFits(CGSize(width: bounds.width, height: chipH)).width
let nextSize = nextKeyboardChip.sizeThatFits(CGSize(width: bounds.width, height: chipH))
let pair = !pasteControl.isHidden && !nextKeyboardChip.isHidden
let gap: CGFloat = 8
let total = pair ? pasteW + gap + nextSize.width : (pasteControl.isHidden ? nextSize.width : pasteW)
let startX = (bounds.width - total) / 2
let chipY = max(0, midY - chipH / 2)
pasteControl.frame = CGRect(x: startX, y: chipY, width: pasteW, height: chipH)
nextKeyboardChip.frame = CGRect(
x: pair ? startX + pasteW + gap : startX,
y: chipY,
width: nextSize.width,
height: chipH
)
let slotW = bounds.width / 3
for (idx, button) in slotButtons.enumerated() {
button.inSystemPlatter = inSystemPlatter
button.frame = CGRect(x: CGFloat(idx) * slotW, y: 0, width: slotW, height: bounds.height)
}
}
override func draw(_ rect: CGRect) {
guard !showsUndo, !showsClipboard, statusLabel.isHidden, bounds.width > 60 else { return }
// Hide the divider adjacent to a highlighted primary pill, matching Apple's strip.
let primaryHighlighted = currentSuggestions.count > 1 && currentSuggestions[1].willAutoReplace
guard !primaryHighlighted else { return }
let scale = max(1, traitCollection.displayScale)
let hairline = 1 / scale
let lineH: CGFloat = min(22, bounds.height * 0.66)
let y = max(1, contentCenterY - lineH / 2)
let slotW = bounds.width / 3
palette.separator.setFill()
UIRectFill(CGRect(x: (slotW * 1).rounded() - hairline / 2, y: y, width: hairline, height: lineH))
UIRectFill(CGRect(x: (slotW * 2).rounded() - hairline / 2, y: y, width: hairline, height: lineH))
}
@objc private func handleUndo() {
onUndoDictation?()
}
@objc private func handleSlotTap(_ sender: SlotButton) {
guard let item = Self.slots(for: currentSuggestions).firstIndex(of: sender.tag) else { return }
onSelectSuggestion?(currentSuggestions[item])
}
private final class SlotButton: UIControl {
var inSystemPlatter = true { didSet { if inSystemPlatter != oldValue { setNeedsDisplay() } } }
private var suggestion: KeyboardSuggestion?
private var palette = FullKeyboardPalette(isDark: false)
override init(frame: CGRect) {
super.init(frame: frame)
isOpaque = false
backgroundColor = .clear
contentMode = .redraw
isAccessibilityElement = true
accessibilityTraits = .button
addTarget(self, action: #selector(redraw), for: [.touchDown, .touchDragEnter, .touchDragExit, .touchCancel, .touchUpInside, .touchUpOutside])
}
@available(*, unavailable)
required init?(coder: NSCoder) { fatalError() }
override var isHighlighted: Bool { didSet { if isHighlighted != oldValue { setNeedsDisplay() } } }
func configure(with suggestion: KeyboardSuggestion, palette: FullKeyboardPalette) {
self.suggestion = suggestion
self.palette = palette
accessibilityLabel = suggestion.displayText
setNeedsDisplay()
}
@objc private func redraw() { setNeedsDisplay() }
override func draw(_ rect: CGRect) {
guard let suggestion else { return }
let midY = bounds.height * 0.5
// Like Apple's strip, the highlight fills its slot, 4 pt in from each edge.
let pillH = max(22, bounds.height - 8)
let pillInset = CGRect(
x: 4,
y: max(0.5, midY - pillH / 2),
width: max(0, bounds.width - 8),
height: pillH
)
if isHighlighted {
palette.keyPressedFill.setFill()
UIBezierPath(roundedRect: pillInset, cornerRadius: 10).fill()
} else if suggestion.willAutoReplace {
palette.selectFill.setFill()
UIBezierPath(roundedRect: pillInset, cornerRadius: 10).fill()
}
let isLiteral = suggestion.kind == .keepLiteral
let font = UIFont.systemFont(
ofSize: suggestion.kind == .emoji ? 24 : 17,
weight: suggestion.willAutoReplace ? .medium : .regular
)
let ink = isLiteral && !suggestion.willAutoReplace ? palette.textBase.withAlphaComponent(0.88) : palette.textBase
let attributes: [NSAttributedString.Key: Any] = [
.font: font,
.foregroundColor: ink,
]
let maxTextW = max(20, pillInset.width - 12)
var drawString = suggestion.displayText
var size = (drawString as NSString).size(withAttributes: attributes)
if size.width > maxTextW {
let smallAttrs: [NSAttributedString.Key: Any] = [
.font: UIFont.systemFont(ofSize: 15, weight: suggestion.willAutoReplace ? .medium : .regular),
.foregroundColor: ink,
]
size = (drawString as NSString).size(withAttributes: smallAttrs)
let drawRect = CGRect(
x: bounds.midX - min(maxTextW, size.width) / 2,
y: midY - size.height / 2,
width: min(maxTextW, size.width),
height: size.height
)
let style = NSMutableParagraphStyle()
style.lineBreakMode = .byTruncatingTail
style.alignment = .center
var truncatedAttrs = smallAttrs
truncatedAttrs[.paragraphStyle] = style
(drawString as NSString).draw(in: drawRect, withAttributes: truncatedAttrs)
return
}
drawString = suggestion.displayText
(drawString as NSString).draw(
at: CGPoint(x: bounds.midX - size.width / 2, y: midY - size.height / 2),
withAttributes: attributes
)
}
}
}