mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
Testers asked for the Norwegian layout and, with it, Spanish and German. The layout follows the dictation language unless set. What changed: - KeyboardLanguage holds each layout's letter rows, long-press accents and spelling dictionary: Norsk (a-ring ending row 1, o-slash and ae row 2), Espanol (n-tilde ending row 2), Deutsch (QWERTZ, u/o/a-umlaut, sharp s on a held S), as Apple lays them out. Adding a language is a new case with those three filled in. - KeyboardLayout takes the language. Rows of 11 keys narrow only the character keys; shift, delete and the bottom row keep their English (10-key) sizes, and row 3's letters sit centred between shift and delete. letterWidth stays the 10-key width; characterWidth is new. - Experiments > Full keyboard > Keyboard layout: Automatic, English, Norsk, Espanol, Deutsch (settings.keyboardLayout). Automatic follows the dictation language (no layout yet for Hebrew and others: English), else the first iPhone keyboard or preferred language with a layout. A pick in the keyboard's language switch changes the layout at once. - Autocorrect uses the layout's UITextChecker dictionary (nb, es, de); without one it offers nothing but your text replacements instead of correcting into English. The English contractions and typo list (i -> I) apply only to English; neighbouring-key costs follow the layout. LanguageSettings.effectiveDictationLanguage now prefers what Automatic picks over the last language dictation ran in. - Typing logs record the layout's language; the snapshot stage takes --keyboard-layout, follows --dictation-language and --accent-key. - TypingLogTests pins English, since FullKeyboardView now reads the App Group's dictation language. Verified: xcodebuild test on the iPhone 18 Pro simulator (iOS 27.2): 206 tests, 0 failures, 1 skipped (the Apple Speech clip test), with layout tests for every language at 320-440 pt and landscape (rows, 11-key widths, hit-frame tiling, balloons, accent menus) and autocorrect tests; Release generic-iOS build with CODE_SIGNING_ALLOWED=NO succeeds; light and dark screenshots of each layout, its accents, shift, the settings row and the dictation bar's re-shot trigger checked by eye. The simulator has dictionaries for nb, es and de. Accent lists are from Apple's layouts as remembered, not checked on a device; the 123 and #+= pages are English's in every language.
179 lines
7 KiB
Swift
179 lines
7 KiB
Swift
import UIKit
|
||
|
||
/// Alternate diacritics and punctuation variants shown when holding a key for 400 ms.
|
||
nonisolated enum AccentCatalog {
|
||
private static let table: [String: [String]] = [
|
||
"e": ["e", "è", "é", "ê", "ë", "ē", "ė", "ę"],
|
||
"a": ["a", "à", "á", "â", "ä", "æ", "ã", "å", "ā"],
|
||
"i": ["i", "ì", "í", "î", "ï", "ī", "į"],
|
||
"o": ["o", "ô", "ö", "ò", "ó", "œ", "ø", "ō", "õ"],
|
||
"u": ["u", "û", "ü", "ù", "ú", "ū"],
|
||
"y": ["y", "ÿ"],
|
||
"n": ["n", "ñ", "ń"],
|
||
"c": ["c", "ç", "ć", "č"],
|
||
"s": ["s", "ß", "ś", "š"],
|
||
"z": ["z", "ž", "ź", "ż"],
|
||
"l": ["l", "ł"],
|
||
"-": ["-", "–", "—", "•"],
|
||
"\"": ["\"", "“", "”", "„", "«", "»"],
|
||
"'": ["'", "‘", "’", "`"],
|
||
".": [".", "…"],
|
||
"?": ["?", "¿"],
|
||
"!": ["!", "¡"],
|
||
"$": ["$", "€", "£", "¥", "¢"],
|
||
"/": ["/", "\\"],
|
||
]
|
||
|
||
/// The accents for a held key; `language` may replace a key's list (`KeyboardLanguage.accentOverrides`).
|
||
static func options(for base: String, uppercase: Bool, language: KeyboardLanguage = .english) -> [String]? {
|
||
let key = base.lowercased()
|
||
guard let list = language.accentOverrides[key] ?? table[key] else { return nil }
|
||
guard uppercase else { return list }
|
||
return list.map { option in
|
||
// German sharp s capitalizes to SS or ẞ; standard `.uppercased()` matches system expectations.
|
||
option == "ß" ? "ẞ" : 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
|
||
)
|
||
}
|
||
}
|
||
}
|