hex/ios/HEXShared/FullKeyboard/AccentPicker.swift
Ryan Vogel 4a2abedcab iOS keyboard: hold lists re-derived from the system keyboard by observation
What: every language's long-press lists (KeyboardLanguage.letterHolds, new
symbolHolds, and AccentCatalog's 123/#+= list) now match the system
keyboard's observed long-press options in the iOS 27.2 simulator: each
letter, 123 and #+= key held for ~3 s with a real-time HID touch and the
callout read from the accessibility tree, for English, Norwegian, Spanish,
German, Italian, French (France, Canada, Switzerland), Portuguese, Russian,
Ukrainian and Serbian (Latin).

Why: the Russian, Serbian and French lists said they were copied from
Apple's TextInput_ru/_sr/_fr bundles, and the other lists were from memory.
Hex may learn from Apple's keyboard only by black-box observation.

Replaced behavior:
- accentOverrides (overrides on a shared letter table) is replaced by
  letterHolds, a complete list per language: a letter the system keyboard
  holds nothing on now holds nothing (Spanish z, German z, Serbian e), where
  it used to fall back to the shared table.
- Lists gain the observed extra letters (English ŵ ř ț ğ ħ ķ ...,
  Norwegian's Sámi letters, Russian's combining-macron forms, which were
  left out before) and follow the system's order; Spanish n drops ñ (own key).
- 123/#+= holds: $ adds ₩ ₽, & holds §, = holds ≠ ≈, % holds ‰, 0 holds °;
  per language: № on # (Russian, Ukrainian), º ª on digits (Spanish,
  Italian, Portuguese), · on . and French quote order (French), „ quotes
  (German), kr on $ (Norwegian), ₴ on $ (Ukrainian).
- Capital ı shows İ (observed), not a second I.

Order: base first, then the others as the system callout shows them read
from the base's side, so Hex's callout (which reverses right-hand keys)
shows them in the system's left-to-right order. Deliberate differences:
Hex keeps the base at the thumb end and one row where the system wraps
long lists or puts the base mid-list.

Defaults where a hold was missed under simulator load: & = & § for German,
Norwegian, Swiss French and Portuguese (as all 8 other layouts observed);
Italian ~ < • treated as holding nothing (as everywhere else).

Docs: README and comments no longer say the data came from Apple files;
the unverified "iPad's Croatian layouts" clause is gone.

Verified: xcodebuild test on an iOS 27.2 iPhone 17 Pro simulator at
7770fb90 + this change: 425 tests, 0 failures, 1 skipped. Tests updated in
KeyboardLayoutTests (lists and on-screen order) and FullKeyboardEngineTests
(the haptics test holds Serbian c, since its e holds nothing).
2026-11-20 23:08:55 -05:00

179 lines
6.9 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 UIKit
/// The options a held key offers after 400 ms: the language's letters (`KeyboardLanguage.letterHolds`),
/// else the 123 and #+= pages' list, the system English keyboard's observed long-press options
/// (iOS 27.2 simulator) unless the language's differ (`KeyboardLanguage.symbolHolds`).
nonisolated enum AccentCatalog {
private static let symbols: [String: [String]] = [
"0": ["0", "°"],
"-": ["-", "–", "—", "•"],
"/": ["/", "\\"],
"$": ["$", "₩", "£", "¢", "€", "¥", "₽"],
"\"": ["\"", "”", "“", "„", "»", "«"],
".": [".", "…"],
"?": ["?", "¿"],
"!": ["!", "¡"],
"'": ["'", "’", "‘", "`"],
"&": ["&", "§"],
"%": ["%", "‰"],
"=": ["=", "≠", "≈"],
]
/// The options for a held key, base first; nil when it holds nothing.
static func options(for base: String, uppercase: Bool, language: KeyboardLanguage = .english) -> [String]? {
let key = base.lowercased()
guard let list = language.letterHolds[key] ?? language.symbolHolds[key] ?? symbols[key],
list.count > 1 else { return nil }
guard uppercase else { return list }
return list.map { option in
// ẞ, Unicode's capital ß; İ for the dotless ı, as the system keyboard shows it in capitals.
switch option {
case "ß": "ẞ"
case "ı": "İ"
default: option.uppercased()
}
}
}
}
/// Pure inward-clamped geometry for the long-press accent callout so every option fits inside
/// the extension's clipped bounds across 320–440 pt widths.
nonisolated struct AccentMenu: Equatable, Sendable {
static let cellHeight: CGFloat = 44
static let padding: CGFloat = 4
static let verticalGap: CGFloat = 6
let keyFrame: CGRect
let bubbleFrame: CGRect
/// Ordered left-to-right on screen.
let options: [String]
let cellFrames: [CGRect]
var selectedIndex: Int
var selectedOption: String { options[selectedIndex] }
static func build(
for keyFrame: CGRect,
rawOptions: [String],
in layoutWidth: CGFloat,
horizontalInset: CGFloat = 0
) -> AccentMenu {
let minX = max(2, horizontalInset + 4)
let maxX = max(minX + 60, layoutWidth - horizontalInset - 4)
let availableWidth = maxX - minX
// Right-half keys reverse the list so the base letter sits under the thumb on the right
// and alternates fan inward toward the center of the keyboard.
let opensLeft = keyFrame.midX > layoutWidth * 0.52
let ordered = opensLeft ? Array(rawOptions.reversed()) : rawOptions
let defaultIndex = opensLeft ? max(0, ordered.count - 1) : 0
let count = CGFloat(max(1, ordered.count))
let idealCellW = min(38, max(28, keyFrame.width))
let maxCellW = (availableWidth - 2 * padding) / count
let cellW = min(idealCellW, maxCellW)
let bubbleW = count * cellW + 2 * padding
let bubbleH = cellHeight + 2 * padding
// Anchor the default cell over the pressed key, then clamp inside [minX, maxX - bubbleW].
let defaultCellCenterOffset = padding + (CGFloat(defaultIndex) + 0.5) * cellW
var bubbleX = keyFrame.midX - defaultCellCenterOffset
bubbleX = max(minX, min(maxX - bubbleW, bubbleX))
let bubbleY = max(1, keyFrame.minY - verticalGap - bubbleH)
let bubbleFrame = CGRect(x: bubbleX, y: bubbleY, width: bubbleW, height: bubbleH)
let cells = (0..<ordered.count).map { idx in
CGRect(
x: bubbleX + padding + CGFloat(idx) * cellW,
y: bubbleY + padding,
width: cellW,
height: cellHeight
)
}
return AccentMenu(
keyFrame: keyFrame,
bubbleFrame: bubbleFrame,
options: ordered,
cellFrames: cells,
selectedIndex: defaultIndex
)
}
/// Maps any horizontal finger location to the nearest cell index.
func index(for x: CGFloat) -> Int {
guard !cellFrames.isEmpty else { return 0 }
if x <= cellFrames[0].minX { return 0 }
if x >= cellFrames[cellFrames.count - 1].maxX { return cellFrames.count - 1 }
for (idx, frame) in cellFrames.enumerated() where x <= frame.maxX {
return idx
}
return cellFrames.count - 1
}
}
/// Floating callout view that renders an active `AccentMenu` over the key plane.
@MainActor
final class AccentCalloutView: UIView {
var palette = FullKeyboardPalette(isDark: false) { didSet { if palette != oldValue { setNeedsDisplay() } } }
private(set) var menu: AccentMenu?
override init(frame: CGRect) {
super.init(frame: frame)
isOpaque = false
backgroundColor = .clear
isUserInteractionEnabled = false
contentMode = .redraw
}
@available(*, unavailable)
required init?(coder: NSCoder) { fatalError() }
func show(_ menu: AccentMenu?) {
guard menu != self.menu else { return }
self.menu = menu
isHidden = menu == nil
setNeedsDisplay()
}
override func draw(_ rect: CGRect) {
guard let menu, let context = UIGraphicsGetCurrentContext() else { return }
let pill = UIBezierPath(roundedRect: menu.bubbleFrame, cornerRadius: 12)
context.saveGState()
context.setShadow(offset: CGSize(width: 0, height: 3), blur: 10, color: palette.popupShadow.cgColor)
context.addPath(pill.cgPath)
context.setFillColor(palette.popupFill.cgColor)
context.fillPath()
context.restoreGState()
let scale = max(1, traitCollection.displayScale)
context.saveGState()
context.addPath(pill.cgPath)
context.setLineWidth(1 / scale)
context.setStrokeColor(palette.popupBorder.cgColor)
context.strokePath()
context.restoreGState()
for (idx, option) in menu.options.enumerated() {
let cell = menu.cellFrames[idx]
let isSelected = idx == menu.selectedIndex
if isSelected {
context.saveGState()
let highlight = UIBezierPath(roundedRect: cell.insetBy(dx: 1.5, dy: 2), cornerRadius: 8)
context.addPath(highlight.cgPath)
context.setFillColor(palette.actionFill.cgColor)
context.fillPath()
context.restoreGState()
}
let attributes: [NSAttributedString.Key: Any] = [
.font: UIFont.systemFont(ofSize: 22, weight: isSelected ? .medium : .regular),
.foregroundColor: isSelected ? UIColor.white : palette.textBase,
]
let size = (option as NSString).size(withAttributes: attributes)
(option as NSString).draw(
at: CGPoint(x: cell.midX - size.width / 2, y: cell.midY - size.height / 2),
withAttributes: attributes
)
}
}
}