mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
* iOS: delete after an autocorrection takes only its space, and the strip offers what you typed
X tester Brandon McConnell (and a TestFlight tester about text replacements, fixed in
uncorrected version." Apple's keyboard doesn't (black box, iOS 27.2 simulator, a
UITextView): after "becuase " became "Because ", delete took only the space and the text
view showed "Becuase" with an undo arrow; space then kept "Because ", a letter continued
the word, a second delete took a letter, a comma went like a space, tapping the bubble put
"Becuase" back without the space, and deleting back to the word later brought the bubble
back. Revisions ("well see " -> "We'll see ", bubble "Well see") and splits ("ofthe" ->
"Of the", bubble "Ofthe") work the same.
Removed: Hex's deliberate "one backspace undoes the correction" (FullKeyboardEngine.
deleteBackward's undo branch and AutocorrectEngine's pending undo, consumed by the next
delete). Replaced by:
- AutocorrectEngine.lastCorrection keeps the latest autocorrection with what was typed,
what went in and an anchor (the text before the cursor right after it); it is offered
while the text before the cursor reads that again (its last 64 characters for long
text), and forgotten on a new field, a new language or a newer correction. The many
clearPendingUndo calls after other input are gone: the anchor check covers them.
- KeyboardSuggestion.Kind.revert: the strip's left slot shows it in place of the word as
it reads, pill-highlighted with Apple's undo arrow (a keyboard can't draw a bubble in
the host's text). Tapping it puts back what was typed, suppresses the fix for the
session, and counts as an undone autocorrection for Learn how I type and Adaptive touch
(whose word taps the delete of the space now keeps for it). Typing logs: autocorrect_undo
with src strip, context why revert.
- Merges, splits and #2144's revisions go through the same record.
- deleteBackward's allowAutocorrectUndo is now takesSwipeBack: it only gates a swipe's
whole-word delete, which stays.
Verified on the Studio (iPhone 17, iOS 27.2 simulator): AutocorrectEngineTests,
FullKeyboardEngineTests and TypingLogTests pass (updated: the keep loop, the revision,
the edge-space join, punctuation and Adaptive touch tests now revert from the strip; new:
testDeleteAfterAnAutocorrectionTakesOnlyItsSpaceAsOnApplesKeyboard with the black-box
cases).
* iOS: smart quotes and dashes as the system keyboard types them, per language
X tester Brandon McConnell: "no smart quotes/etc." Hex typed straight quotes and two
hyphens everywhere. Apple's keyboard (black box, iOS 27.2 simulator, a UITextView with
each trait on and off, a text field, Messages, and 15 of its keyboards):
- ' and " open at the start, after a space or new line, a bracket or an opening quote,
and close elsewhere: 'hi' it's "yes" ("ok") 5'9" became ‘Hi’ it’s “yes” (“ok”) 5’9”.
- A second hyphen makes an em dash (a--b is a—b, a third stays a hyphen, delete takes
the dash whole); " - " stays.
- Autocorrect's apostrophes are ’ (Don’t, I’ll), and the strip shows I’m.
- smartQuotesType or smartDashesType .no turns each off; Messages' composer types
straight quotes and -- (both off there).
- Quotes per keyboard: „ “ ‚ ‘ in German and Swiss German (the apostrophe key types ‘
even inside a word), « » with no-break spaces inside in French, Canadian and Swiss
French, „ ” ‚ ’ in Polish, « » in Norwegian, Russian and Ukrainian, “ ” ‘ ’ in English,
Spanish, Italian, Portuguese and Romanian. Serbian (no simulator keyboard) gets the
English ones.
- smartInsertDeleteType changed nothing it types: after a picked suggestion, , . ! ? ;
: ) took the suggestion's space ("Hello,") with it on or off; " ( @ kept it, and so did
a space typed by hand ("Hello .").
Hex now does the same:
- SmartPunctuation.swift: SmartQuotes per KeyboardLanguage, curled() for typed keys and
for autocorrect, suggestion, swipe and revert text, the em dash in typeIntoField, and
the strip's apostrophes (displayed). Accent picks, emoji and text replacements go in as
they are. KeyboardTextTarget gains smartQuotesType and smartDashesType (the proxy's;
nil, off, for fixtures); .default counts as on, Smart Punctuation's default, which a
keyboard can't read.
- The keys still reason with straight quotes: FullKeyboardEngine reads textBefore, the
field's text with curly quotes made straight one for one (SmartPunctuation.plain), so
tokens, revisions, reverts and swipes keep matching what they typed.
- dropsSuggestionSpace: , . ! ? ; : ) right after a picked suggestion delete its space.
- AutocorrectEngine.midSentenceMarks gains ' (what ’ reads as now).
Verified on the Studio (iPhone 17, iOS 27.2 simulator): AutocorrectEngineTests,
FullKeyboardEngineTests, TypingLogTests and EmojiSuggesterTests pass, with new
testSmartQuotesAndDashesFollowTheFieldAsOnApplesKeyboard, testSmartQuotesFollowTheKeysLanguage
and testMarksGoAgainstAPickedSuggestion. Live in the simulator's HEXKeyboard (Release, App
Group) in a UITextView host: 'hi' it's "yes" well--known dont typed ‘hi’ it’s “yes”
well—known don’t, straight quotes with smartQuotesType .no, well--known with smartDashesType
.no, and the strip showed I’m.
* iOS: the space-bar trackpad moves up and down lines, as the system keyboard's does
X tester Brandon McConnell: "holding down on the space bar only allows scrubbing
horizontally, whereas Apple's keyboard treats it like a cursor on both x/y axes".
Apple's (black box, iOS 27.2 simulator, iPhone 17, a UITextView with 17 pt text and
20.3 pt lines, the caret read back from the host): after the hold, the caret moved a line
every 15 to 16 pt of finger travel up or down, the first after about 8 (crossings at 9,
24, 39, 54, 72 and 87 pt both ways), kept its x, and stayed on the first or last line
past them; in a wrapped paragraph it moved by a wrapped line (about 43 characters in a
360 pt line). Along a line it moved about a character per 6 pt and stopped at the line's
ends.
A keyboard can only move the cursor by characters (adjustTextPosition), so
TrackpadCursor works out the line before or after from the text the field shares around
the cursor: lines split at new lines and every lineLength characters (the keyboard's
width less 40 pt, at 8.2 pt a character, as the field's width and font can't be read),
at the same column, kept across short lines until a move along the line. Vertical steps
are trackpadPointsPerLine (15.5 pt), the first half as far; past the first or last line
the cursor stays and coming back moves it at once. Horizontal movement is unchanged (7.5 pt
a character, crossing line ends as before).
The host's context lags the moves: in the simulator a second line up read the text from
before the first, and after a move the host shared only the cursor's own line. So each
gesture keeps a copy of the text (TrackpadCursor.State) and adds the host's context once it
has caught up (it reads the same either side of the cursor). Lines the host never shares
stay out of reach. KeyboardTextTarget gains documentContextAfterInput.
Not done: Apple's tap with a second finger while holding space starts a selection. The
iOS 27.2 SDK's UITextDocumentProxy has documentContextBeforeInput/AfterInput, selectedText
(read-only), documentInputMode, documentIdentifier, adjustTextPosition(byCharacterOffset:),
setMarkedText(_:selectedRange:) and unmarkText, and UIInputViewController nothing more: no
public API makes or extends a selection, and marked text would type provisional text into
the field, so Hex doesn't fake it.
TrackpadCursor is surface-independent so the dictation bar's trackpad (#2180) can use it
too; it isn't wired there here.
Verified on the Studio (iPhone 17, iOS 27.2 simulator): FullKeyboardEngineTests (new
testTheSpaceBarTrackpadMovesUpAndDownLines), AutocorrectEngineTests and EmojiSuggesterTests
pass. Live in the simulator's HEXKeyboard (Release) in a UITextView with four lines: from
the end, holding space and sliding up moved the cursor 88 -> 64 -> 41 -> 20 (each line up,
same column), further up kept it on the first line, and sliding back down returned it 41 ->
64 -> 88; nothing was typed.
* iOS: key clicks: the keyboard's input view adopts UIInputViewAudioFeedback
TestFlight: "Cannot hear the iOS typing sound when using your keyboard. Need the clicky
sound for my dopamine pls."
Hex already called UIDevice.playInputClick() on every key's touch-down, but the
UIInputViewAudioFeedback conformance sat on FullKeyboardView, a subview; UIKit plays the
click only for the input view itself. In the iOS 27.2 simulator's keyboard log, typing
"abc" and a delete reached AudioServices' system sound 1104 0 times with the old view and
4 times (one per press) with the new one.
- KeyboardInputView (HEXKeyboard): a UIInputView adopting UIInputViewAudioFeedback, with
enableInputClicksWhenVisible true. KeyboardViewController.loadView keeps UIKit's own input
view's style, size and sizing flags but makes it this class; nothing else in the
controller's lifecycle changes.
- Removed: FullKeyboardView's UIInputViewAudioFeedback conformance, which did nothing.
keyFeedback plays the click first, through a playInputClick seam tests count.
- The dictation bar's key feedback clicks too.
- One click for every key (delete, shift, 123 and return included, and each delete repeat):
Apple's keyboard plays other sounds for delete and the modifier keys, but the click is the
only keyboard sound the API offers a keyboard.
- No setting: the click follows Settings › Sounds & Haptics › Keyboard Feedback › Sound
(in the simulator, keyboard-audio NO in com.apple.preferences.sounds made it play none).
Apple's custom keyboard guide lists playing keyboard clicks among the things that need
open access; the simulator played it with the keyboard's Full Access rows removed too, so
Hex calls it either way and iOS decides.
Verified on the Studio (iPhone 17, iOS 27.2 simulator): FullKeyboardEngineTests (new
testEveryKeyClicksOnTouchDown) and HexKeyGestureTests pass; the Release HEXKeyboard
launched, laid out and typed as before in a UITextView host, with the click counts above.
A real iPhone must confirm the sound itself, with Full Access on and off.
762 lines
35 KiB
Swift
762 lines
35 KiB
Swift
import UIKit
|
||
|
||
/// The 3-slot top bar above the keys, drawn as Apple's iOS 26 predictive bar is (measured in
|
||
/// the iOS 27.2 simulator): SF 17 pt regular in muted ink, slots a third of the keyboard wide,
|
||
/// 1 × 24 pt dividers ending on the thirds, and a pending autocorrection as a full-slot
|
||
/// capsule with the divider beside it hidden. iOS's band above the keyboard takes the top of
|
||
/// Apple's bar, so everything here is centred in the 27 pt below it. The centred
|
||
/// `Undo dictation` chip or a status message replaces the slots when needed.
|
||
@MainActor
|
||
final class KeyboardStripView: UIView {
|
||
var palette = FullKeyboardPalette(isDark: false) { didSet { if palette != oldValue { applyPalette() } } }
|
||
/// Hosted in iOS's platter (not a snapshot scene's own backdrop).
|
||
var inSystemPlatter = true {
|
||
didSet {
|
||
guard inSystemPlatter != oldValue else { return }
|
||
for button in slotButtons { button.inSystemPlatter = inSystemPlatter }
|
||
setNeedsLayout()
|
||
setNeedsDisplay()
|
||
}
|
||
}
|
||
/// Each slot's frame in this view, from `KeyboardLayout.stripSlotFrames`; thirds of the
|
||
/// view until the keyboard lays it out.
|
||
var slotFrames: [CGRect]? { didSet { if slotFrames != oldValue { setNeedsLayout(); setNeedsDisplay() } } }
|
||
/// How far the pills sit in from the strip's top and bottom (`KeyboardMetrics.stripInset`).
|
||
var pillInset: CGFloat = 0.5 {
|
||
didSet {
|
||
guard pillInset != oldValue else { return }
|
||
for button in slotButtons { button.pillInset = pillInset }
|
||
setNeedsLayout()
|
||
}
|
||
}
|
||
var onSelectSuggestion: ((KeyboardSuggestion) -> Void)?
|
||
var onUndoDictation: (() -> Void)?
|
||
var onPaste: (() -> Void)?
|
||
var onNextKeyboard: (() -> Void)?
|
||
/// The Full Access hint's Open Hex was tapped (it opens Hex's setup).
|
||
var onOpenHexFromHint: (() -> Void)?
|
||
/// The notice's chip was tapped (`KeyboardStripNotice`).
|
||
var onOpenNotice: (() -> 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?
|
||
private var showsFullAccessHint = false
|
||
/// Built the first time the Hex key is tapped without Full Access.
|
||
private var fullAccessHintView: FullAccessHintView?
|
||
private var notice: KeyboardStripNotice?
|
||
/// Built for the first notice, in the Full Access hint's shape.
|
||
private var noticeView: FullAccessHintView?
|
||
/// The Hex key sits left of the strip (Settings › Keyboard › Hex key): the Full Access
|
||
/// hint's Open Hex leads, beside the key, and the Logging mark moves to the far corner.
|
||
var hexOnLeading = false {
|
||
didSet {
|
||
guard hexOnLeading != oldValue else { return }
|
||
fullAccessHintView?.chipLeads = hexOnLeading
|
||
noticeView?.chipLeads = hexOnLeading
|
||
setNeedsLayout()
|
||
}
|
||
}
|
||
/// Record typing logs is recording: a small mark in the strip's corner away from the Hex key.
|
||
var showsLoggingMark = false {
|
||
didSet {
|
||
guard showsLoggingMark != oldValue else { return }
|
||
if showsLoggingMark, loggingMark == nil {
|
||
let mark = TypingLogMarkView()
|
||
mark.textColor = palette.textMuted
|
||
addSubview(mark)
|
||
loggingMark = mark
|
||
}
|
||
loggingMark?.isHidden = !showsLoggingMark
|
||
setNeedsLayout()
|
||
}
|
||
}
|
||
private var loggingMark: TypingLogMarkView?
|
||
/// 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, once, which
|
||
/// iOS asks about until Paste from Other Apps is Allow (`KeyboardClipboardTracker`).
|
||
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)
|
||
addSubview(button)
|
||
}
|
||
applyPalette()
|
||
}
|
||
|
||
@available(*, unavailable)
|
||
required init?(coder: NSCoder) { fatalError() }
|
||
|
||
/// `fullAccessHint`, while true, replaces everything else in the strip with what
|
||
/// dictation needs (`FullAccessHintView`); `notice` does the same after it.
|
||
func update(
|
||
suggestions: [KeyboardSuggestion],
|
||
showsUndo: Bool,
|
||
statusText: String?,
|
||
clipboard: KeyboardClipboardOffer? = nil,
|
||
fullAccessHint: Bool = false,
|
||
notice: KeyboardStripNotice? = nil
|
||
) {
|
||
let changed = suggestions != currentSuggestions || showsUndo != self.showsUndo
|
||
|| statusText != self.statusText || clipboard != self.clipboard
|
||
|| fullAccessHint != showsFullAccessHint || notice != self.notice
|
||
guard changed else { return }
|
||
currentSuggestions = suggestions
|
||
self.showsUndo = showsUndo
|
||
self.statusText = statusText
|
||
self.clipboard = clipboard
|
||
showsFullAccessHint = fullAccessHint
|
||
self.notice = notice
|
||
|
||
if fullAccessHint, fullAccessHintView == nil { _ = makeFullAccessHintView() }
|
||
if let hint = fullAccessHintView { show(hint, fullAccessHint) }
|
||
if let notice, noticeView == nil { _ = makeNoticeView() }
|
||
if let notice { noticeView?.show(notice) }
|
||
if let noticeView { show(noticeView, notice != nil && !fullAccessHint) }
|
||
let showsHint = fullAccessHint || notice != nil
|
||
show(undoChip, showsUndo && !showsHint)
|
||
let showsClipboard = showsClipboard
|
||
var paste = pasteControl.configuration
|
||
let pasteTitle = clipboard == .dictation ? "Paste dictation" : "Paste"
|
||
if paste?.title != pasteTitle {
|
||
paste?.title = pasteTitle
|
||
pasteControl.configuration = paste
|
||
}
|
||
show(pasteControl, showsPasteChip)
|
||
show(nextKeyboardChip, showsNextKeyboardChip)
|
||
// Words swap letter by letter (and leave the same way when a chip or status takes the
|
||
// strip); the strip being shown for the first time just draws them.
|
||
let animated = window != nil
|
||
if showsHint || showsUndo || showsClipboard {
|
||
statusLabel.isHidden = true
|
||
slotButtons.forEach { $0.configure(with: nil, palette: palette, animated: animated) }
|
||
} else if let statusText, suggestions.isEmpty {
|
||
statusLabel.text = statusText
|
||
statusLabel.isHidden = false
|
||
slotButtons.forEach { $0.configure(with: nil, palette: palette, animated: false) }
|
||
} else {
|
||
statusLabel.isHidden = true
|
||
let slots = Self.slots(for: suggestions)
|
||
for (idx, button) in slotButtons.enumerated() {
|
||
let item = slots.firstIndex(of: idx).map { suggestions[$0] }
|
||
button.configure(with: item, palette: palette, animated: animated)
|
||
}
|
||
}
|
||
setNeedsLayout()
|
||
setNeedsDisplay()
|
||
}
|
||
|
||
/// Shows or hides a chip, fading it over Apple's 0.2 s word swap (at once with Reduce Motion).
|
||
private func show(_ view: UIView, _ shown: Bool) {
|
||
let visible = !view.isHidden && view.alpha > 0
|
||
guard shown != visible || (shown && view.alpha < 1) else { return }
|
||
guard window != nil, !UIAccessibility.isReduceMotionEnabled else {
|
||
view.layer.removeAllAnimations()
|
||
view.alpha = 1
|
||
view.isHidden = !shown
|
||
return
|
||
}
|
||
if shown {
|
||
if view.isHidden { view.alpha = 0 }
|
||
view.isHidden = false
|
||
}
|
||
UIView.animate(withDuration: 0.2, delay: 0, options: [.beginFromCurrentState, .allowUserInteraction, .curveEaseOut]) {
|
||
view.alpha = shown ? 1 : 0
|
||
} completion: { _ in
|
||
guard view.alpha == 0 else { return }
|
||
view.isHidden = true
|
||
view.alpha = 1
|
||
}
|
||
}
|
||
|
||
/// Which slot shows each suggestion: in order, except a lone emoji, swiped word or reading
|
||
/// of one, which takes the centre. A revert keeps the left slot.
|
||
static func slots(for suggestions: [KeyboardSuggestion]) -> [Int] {
|
||
if suggestions.count == 1, ![.keepLiteral, .secondary, .revert].contains(suggestions[0].kind) { 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, !showsFullAccessHint, notice == nil else { return false }
|
||
if case .code = clipboard { return true }
|
||
return currentSuggestions.isEmpty
|
||
}
|
||
|
||
private var showsPasteChip: Bool {
|
||
showsClipboard && clipboard != .code(canPaste: false)
|
||
}
|
||
|
||
private var showsNextKeyboardChip: Bool {
|
||
guard showsClipboard, case .code = clipboard else { return false }
|
||
return true
|
||
}
|
||
|
||
@objc private func handlePaste() {
|
||
onPaste?()
|
||
}
|
||
|
||
@objc private func handleNextKeyboard() {
|
||
onNextKeyboard?()
|
||
}
|
||
|
||
private func makeFullAccessHintView() -> FullAccessHintView {
|
||
let hint = FullAccessHintView()
|
||
hint.chipLeads = hexOnLeading
|
||
hint.onOpenHex = { [weak self] in self?.onOpenHexFromHint?() }
|
||
hint.isHidden = true
|
||
addSubview(hint)
|
||
fullAccessHintView = hint
|
||
applyPalette()
|
||
setNeedsLayout()
|
||
return hint
|
||
}
|
||
|
||
private func makeNoticeView() -> FullAccessHintView {
|
||
let view = FullAccessHintView()
|
||
view.chipLeads = hexOnLeading
|
||
view.onOpenHex = { [weak self] in self?.onOpenNotice?() }
|
||
view.isHidden = true
|
||
addSubview(view)
|
||
noticeView = view
|
||
applyPalette()
|
||
setNeedsLayout()
|
||
return view
|
||
}
|
||
|
||
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
|
||
fullAccessHintView?.palette = palette
|
||
noticeView?.palette = palette
|
||
loggingMark?.textColor = palette.textMuted
|
||
for (item, slot) in Self.slots(for: currentSuggestions).enumerated() {
|
||
slotButtons[slot].configure(with: currentSuggestions[item], palette: palette, animated: false)
|
||
}
|
||
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 - 2 * pillInset))
|
||
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))
|
||
// Laid out for what is shown, not what is still fading out.
|
||
let pasteShown = showsPasteChip
|
||
let nextShown = showsNextKeyboardChip
|
||
let pair = pasteShown && nextShown
|
||
let gap: CGFloat = 8
|
||
let total = pair ? pasteW + gap + nextSize.width : (pasteShown ? pasteW : nextSize.width)
|
||
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
|
||
)
|
||
var hintFrame = bounds.insetBy(dx: 12, dy: 0)
|
||
var markRoom: CGFloat = 0
|
||
if let loggingMark, showsLoggingMark {
|
||
// On the text line at the outer slot's end, in from the keyboard's rounded corner;
|
||
// that slot centres its word in the rest, so a long word never runs under it.
|
||
let size = loggingMark.sizeThatFits(bounds.size)
|
||
let markX = hexOnLeading ? bounds.width - 12 - size.width : 12
|
||
loggingMark.frame = CGRect(x: markX, y: (midY - size.height / 2).rounded(), width: size.width, height: size.height)
|
||
markRoom = 12 + size.width + 4
|
||
bringSubviewToFront(loggingMark)
|
||
hintFrame = hexOnLeading
|
||
? CGRect(x: 12, y: 0, width: max(0, loggingMark.frame.minX - 8 - 12), height: bounds.height)
|
||
: CGRect(x: loggingMark.frame.maxX + 8, y: 0, width: max(0, bounds.width - loggingMark.frame.maxX - 20), height: bounds.height)
|
||
}
|
||
fullAccessHintView?.frame = hintFrame
|
||
fullAccessHintView?.chipHeight = chipH
|
||
noticeView?.frame = hintFrame
|
||
noticeView?.chipHeight = chipH
|
||
let slots = currentSlotFrames
|
||
for (idx, button) in slotButtons.enumerated() {
|
||
button.inSystemPlatter = inSystemPlatter
|
||
button.frame = slots[idx]
|
||
button.leadingRoom = !hexOnLeading && idx == 0 ? markRoom : 0
|
||
button.trailingRoom = hexOnLeading && idx == 2 ? markRoom : 0
|
||
}
|
||
}
|
||
|
||
private var currentSlotFrames: [CGRect] {
|
||
if let slotFrames, slotFrames.count == 3 { return slotFrames }
|
||
let slotW = bounds.width / 3
|
||
return (0..<3).map { CGRect(x: CGFloat($0) * slotW, y: 0, width: slotW, height: bounds.height) }
|
||
}
|
||
|
||
override func draw(_ rect: CGRect) {
|
||
guard !showsUndo, !showsClipboard, !showsFullAccessHint, notice == nil, statusLabel.isHidden, bounds.width > 60 else { return }
|
||
// Hide the divider adjacent to a highlighted primary pill, matching Apple's strip, and the
|
||
// one beside a revert's.
|
||
let primaryHighlighted = currentSuggestions.count > 1 && currentSuggestions[1].willAutoReplace
|
||
guard !primaryHighlighted else { return }
|
||
let reverting = currentSuggestions.first?.kind == .revert
|
||
|
||
// Apple's dividers: 1 pt wide, ending on the slot boundary, 24 pt tall on the text's centre.
|
||
let lineH: CGFloat = min(24, bounds.height - 2)
|
||
let y = contentCenterY - lineH / 2
|
||
palette.stripDivider.setFill()
|
||
// Only between two filled slots: nothing to show, no dividers (as Apple's bar in a
|
||
// field it offers nothing for).
|
||
let filled = Set(Self.slots(for: currentSuggestions))
|
||
for (index, slot) in currentSlotFrames.prefix(2).enumerated()
|
||
where slot.maxX < bounds.width - 1 && filled.contains(index) && filled.contains(index + 1) && !(reverting && index == 0) {
|
||
UIRectFill(CGRect(x: slot.maxX - 1, y: y, width: 1, 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])
|
||
}
|
||
|
||
/// One suggestion slot. A word that changes swaps letter by letter, as Apple's bar does
|
||
/// (iOS 27.2, recorded at 60 fps): the letters both words share stay, the old word's other
|
||
/// letters fade from its end and the new word's from its start, each in 0.1 s, staggered
|
||
/// over 0.2 s. The capsule and dividers change at once. Reduce Motion swaps at once.
|
||
private final class SlotButton: UIControl {
|
||
var inSystemPlatter = true { didSet { if inSystemPlatter != oldValue { setNeedsDisplay() } } }
|
||
var pillInset: CGFloat = 0.5 { didSet { if pillInset != oldValue { setNeedsDisplay() } } }
|
||
/// Room the Logging mark takes at the slot's outer end: the word centres in the rest.
|
||
var leadingRoom: CGFloat = 0 { didSet { if leadingRoom != oldValue { setNeedsDisplay() } } }
|
||
var trailingRoom: CGFloat = 0 { didSet { if trailingRoom != oldValue { setNeedsDisplay() } } }
|
||
private var suggestion: KeyboardSuggestion?
|
||
private var palette = FullKeyboardPalette(isDark: false)
|
||
/// The word leaving while its letters fade, and when the swap began.
|
||
private var outgoing: KeyboardSuggestion?
|
||
private var swapStart: CFTimeInterval = 0
|
||
private var displayLink: CADisplayLink?
|
||
|
||
static let letterFade: CFTimeInterval = 0.1
|
||
static let swapLength: CFTimeInterval = 0.2
|
||
static let maxStagger: CFTimeInterval = 0.025
|
||
|
||
override init(frame: CGRect) {
|
||
super.init(frame: frame)
|
||
isOpaque = false
|
||
backgroundColor = .clear
|
||
contentMode = .redraw
|
||
isAccessibilityElement = false
|
||
isUserInteractionEnabled = false
|
||
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() } } }
|
||
|
||
/// nil empties the slot (its word fades out when animated).
|
||
func configure(with suggestion: KeyboardSuggestion?, palette: FullKeyboardPalette, animated: Bool) {
|
||
let old = self.suggestion
|
||
self.suggestion = suggestion
|
||
self.palette = palette
|
||
isAccessibilityElement = suggestion != nil
|
||
isUserInteractionEnabled = suggestion != nil
|
||
accessibilityLabel = suggestion?.displayText
|
||
accessibilityHint = suggestion?.kind == .revert ? "Undoes the autocorrection" : nil
|
||
if old?.displayText != suggestion?.displayText {
|
||
if animated, window != nil, !UIAccessibility.isReduceMotionEnabled {
|
||
outgoing = old
|
||
swapStart = CACurrentMediaTime()
|
||
startTicking()
|
||
} else {
|
||
endSwap()
|
||
}
|
||
}
|
||
setNeedsDisplay()
|
||
}
|
||
|
||
override func didMoveToWindow() {
|
||
super.didMoveToWindow()
|
||
if window == nil { endSwap() }
|
||
}
|
||
|
||
private func startTicking() {
|
||
guard displayLink == nil else { return }
|
||
let link = CADisplayLink(target: self, selector: #selector(tick))
|
||
link.add(to: .main, forMode: .common)
|
||
displayLink = link
|
||
}
|
||
|
||
private func endSwap() {
|
||
outgoing = nil
|
||
displayLink?.invalidate()
|
||
displayLink = nil
|
||
}
|
||
|
||
@objc private func tick() {
|
||
if CACurrentMediaTime() - swapStart >= Self.swapLength { endSwap() }
|
||
setNeedsDisplay()
|
||
}
|
||
|
||
@objc private func redraw() { setNeedsDisplay() }
|
||
|
||
private struct Word {
|
||
let text: String
|
||
let attributes: [NSAttributedString.Key: Any]
|
||
let origin: CGPoint
|
||
/// Drawn in a box with a truncating tail (too long for the slot): never swapped by letter.
|
||
let truncatedRect: CGRect?
|
||
}
|
||
|
||
private func layout(_ suggestion: KeyboardSuggestion, ink: UIColor) -> Word {
|
||
let midY = bounds.height * 0.5
|
||
// Apple's words are all SF 17 pt regular, the one in the capsule included.
|
||
let font = UIFont.systemFont(ofSize: suggestion.kind == .emoji ? 22 : 17, weight: .regular)
|
||
let attributes: [NSAttributedString.Key: Any] = [.font: font, .foregroundColor: ink]
|
||
// The word centres in the slot, less any room the Logging mark takes; a revert's
|
||
// with its arrow after it.
|
||
let arrowRoom = suggestion.kind == .revert ? Self.revertArrowSize + Self.revertArrowGap : 0
|
||
let textMidX = (leadingRoom + bounds.width - trailingRoom - arrowRoom) / 2
|
||
let maxTextW = max(20, min(bounds.width - 12, bounds.width - leadingRoom - trailingRoom - 8) - arrowRoom)
|
||
let text = suggestion.displayText
|
||
let size = (text as NSString).size(withAttributes: attributes)
|
||
guard size.width > maxTextW else {
|
||
let origin = CGPoint(x: textMidX - size.width / 2, y: midY - size.height / 2)
|
||
return Word(text: text, attributes: attributes, origin: origin, truncatedRect: nil)
|
||
}
|
||
let style = NSMutableParagraphStyle()
|
||
style.lineBreakMode = .byTruncatingTail
|
||
style.alignment = .center
|
||
let small: [NSAttributedString.Key: Any] = [
|
||
.font: UIFont.systemFont(ofSize: 15, weight: .regular), .foregroundColor: ink, .paragraphStyle: style,
|
||
]
|
||
let smallSize = (text as NSString).size(withAttributes: small)
|
||
let width = min(maxTextW, smallSize.width)
|
||
let box = CGRect(x: textMidX - width / 2, y: midY - smallSize.height / 2, width: width, height: smallSize.height)
|
||
return Word(text: text, attributes: small, origin: box.origin, truncatedRect: box)
|
||
}
|
||
|
||
/// Draws the word with each letter's opacity from alpha (1 is the ink's own).
|
||
private func draw(_ word: Word, alpha: (Int) -> CGFloat) {
|
||
let ink = word.attributes[.foregroundColor] as? UIColor ?? .label
|
||
if let box = word.truncatedRect {
|
||
var attributes = word.attributes
|
||
attributes[.foregroundColor] = ink.withAlphaComponent(ink.cgColor.alpha * max(0, alpha(0)))
|
||
(word.text as NSString).draw(in: box, withAttributes: attributes)
|
||
return
|
||
}
|
||
let string = NSMutableAttributedString(string: word.text, attributes: word.attributes)
|
||
var offset = 0
|
||
for (index, character) in word.text.enumerated() {
|
||
let length = String(character).utf16.count
|
||
let a = alpha(index)
|
||
if a < 1 {
|
||
let faded = ink.withAlphaComponent(ink.cgColor.alpha * max(0, a))
|
||
string.addAttribute(.foregroundColor, value: faded, range: NSRange(location: offset, length: length))
|
||
}
|
||
offset += length
|
||
}
|
||
string.draw(at: word.origin)
|
||
}
|
||
|
||
private static func progress(_ t: CFTimeInterval, start: CFTimeInterval) -> CGFloat {
|
||
let x = min(1, max(0, (t - start) / letterFade))
|
||
return CGFloat(x * x * (3 - 2 * x))
|
||
}
|
||
|
||
static let revertArrowSize: CGFloat = 13
|
||
static let revertArrowGap: CGFloat = 5
|
||
|
||
/// Apple's revert bubble's undo arrow, after the word.
|
||
private func drawRevertArrow(after word: Word, ink: UIColor) {
|
||
let config = UIImage.SymbolConfiguration(pointSize: Self.revertArrowSize, weight: .medium)
|
||
guard let arrow = UIImage(systemName: "arrow.uturn.backward", withConfiguration: config)?
|
||
.withTintColor(ink, renderingMode: .alwaysOriginal) else { return }
|
||
let textWidth = word.truncatedRect?.width ?? (word.text as NSString).size(withAttributes: word.attributes).width
|
||
let x = word.origin.x + textWidth + Self.revertArrowGap
|
||
arrow.draw(at: CGPoint(x: x, y: bounds.height * 0.5 - arrow.size.height / 2))
|
||
}
|
||
|
||
override func draw(_ rect: CGRect) {
|
||
let midY = bounds.height * 0.5
|
||
let pilled = suggestion != nil && (isHighlighted || suggestion?.willAutoReplace == true || suggestion?.kind == .revert)
|
||
if pilled {
|
||
// Like Apple's, the highlight is a capsule as wide as its slot.
|
||
let pillH = max(22, bounds.height - 2 * pillInset)
|
||
let pill = CGRect(x: 0, y: max(0.5, midY - pillH / 2), width: bounds.width, height: pillH)
|
||
(isHighlighted ? palette.stripPillPressedFill : palette.stripPillFill).setFill()
|
||
UIBezierPath(roundedRect: pill, cornerRadius: pillH / 2).fill()
|
||
}
|
||
let ink = pilled ? palette.stripPillText : palette.stripText
|
||
let incoming = suggestion.map { layout($0, ink: ink) }
|
||
if let incoming, suggestion?.kind == .revert { drawRevertArrow(after: incoming, ink: ink) }
|
||
guard var leaving = outgoing.map({ layout($0, ink: ink) }) else {
|
||
if let incoming { draw(incoming) { _ in 1 } }
|
||
return
|
||
}
|
||
let t = CACurrentMediaTime() - swapStart
|
||
let oldLetters = Array(leaving.text)
|
||
let newLetters = Array(incoming?.text ?? "")
|
||
let byLetter = leaving.truncatedRect == nil && incoming?.truncatedRect == nil
|
||
// Letters both words start with stay put (drawn once, in the new word).
|
||
var shared = 0
|
||
if byLetter, incoming != nil {
|
||
while shared < min(oldLetters.count, newLetters.count), oldLetters[shared] == newLetters[shared] {
|
||
shared += 1
|
||
}
|
||
}
|
||
if shared > 0, let incoming {
|
||
// The old ending continues from the shared letters, so it never jumps aside.
|
||
leaving = Word(text: leaving.text, attributes: leaving.attributes, origin: incoming.origin, truncatedRect: nil)
|
||
}
|
||
let changing = max(oldLetters.count, newLetters.count) - shared
|
||
let stagger = changing > 1 ? min(Self.maxStagger, (Self.swapLength - Self.letterFade) / Double(changing - 1)) : 0
|
||
draw(leaving) { index in
|
||
guard index >= shared else { return 0 }
|
||
guard byLetter else { return 1 - Self.progress(t, start: 0) }
|
||
return 1 - Self.progress(t, start: Double(oldLetters.count - 1 - index) * stagger)
|
||
}
|
||
if let incoming {
|
||
draw(incoming) { index in
|
||
guard index >= shared else { return 1 }
|
||
guard byLetter else { return Self.progress(t, start: 0) }
|
||
return Self.progress(t, start: Double(index - shared) * stagger)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/// A two-line message with a chip that replaces the strip for a few seconds, in the Full Access
|
||
/// hint's shape: "Couldn't translate, pasted as spoken" over its fix, and Open Hex.
|
||
struct KeyboardStripNotice: Equatable {
|
||
let title: String
|
||
let detail: String
|
||
let button: String
|
||
}
|
||
|
||
/// What the Hex key shows without Full Access: dictation needs Allow Full Access, where to
|
||
/// turn it on, and an Open Hex chip, the only part that opens Hex, on the Hex key's side.
|
||
@MainActor
|
||
final class FullAccessHintView: UIView {
|
||
static let title = "Allow Full Access to dictate"
|
||
static let path = "Settings › Apps › Hex › Keyboards"
|
||
|
||
var palette = FullKeyboardPalette(isDark: false) { didSet { applyPalette() } }
|
||
var chipHeight: CGFloat = 30 { didSet { setNeedsLayout() } }
|
||
/// The Hex key is on the left: Open Hex sits at the leading end, the text after it.
|
||
var chipLeads = false { didSet { if chipLeads != oldValue { setNeedsLayout() } } }
|
||
var onOpenHex: (() -> Void)?
|
||
|
||
private let titleLabel = UILabel()
|
||
private let pathLabel = UILabel()
|
||
private let openButton = UIButton(type: .custom)
|
||
|
||
override init(frame: CGRect) {
|
||
super.init(frame: frame)
|
||
backgroundColor = .clear
|
||
titleLabel.text = Self.title
|
||
titleLabel.font = .systemFont(ofSize: 13, weight: .medium)
|
||
pathLabel.text = Self.path
|
||
pathLabel.font = .systemFont(ofSize: 11.5)
|
||
pathLabel.accessibilityLabel = "Settings, Apps, Hex, Keyboards"
|
||
for label in [titleLabel, pathLabel] {
|
||
label.adjustsFontSizeToFitWidth = true
|
||
label.minimumScaleFactor = 0.8
|
||
label.lineBreakMode = .byTruncatingTail
|
||
addSubview(label)
|
||
}
|
||
var open = UIButton.Configuration.plain()
|
||
open.title = "Open Hex"
|
||
open.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 13, bottom: 0, trailing: 13)
|
||
open.background.cornerRadius = 15
|
||
openButton.configuration = open
|
||
openButton.accessibilityHint = "Opens Hex's setup"
|
||
openButton.addTarget(self, action: #selector(handleOpen), for: .touchUpInside)
|
||
addSubview(openButton)
|
||
applyPalette()
|
||
}
|
||
|
||
@available(*, unavailable)
|
||
required init?(coder: NSCoder) { fatalError() }
|
||
|
||
@objc private func handleOpen() {
|
||
onOpenHex?()
|
||
}
|
||
|
||
/// Shows `notice` in place of the Full Access words.
|
||
func show(_ notice: KeyboardStripNotice) {
|
||
titleLabel.text = notice.title
|
||
pathLabel.text = notice.detail
|
||
pathLabel.accessibilityLabel = nil
|
||
var open = openButton.configuration
|
||
open?.title = notice.button
|
||
openButton.configuration = open
|
||
openButton.accessibilityHint = nil
|
||
setNeedsLayout()
|
||
}
|
||
|
||
private func applyPalette() {
|
||
titleLabel.textColor = palette.textBase
|
||
pathLabel.textColor = palette.textMuted
|
||
var open = openButton.configuration
|
||
open?.baseForegroundColor = palette.textBase
|
||
open?.background.backgroundColor = palette.chipFill
|
||
openButton.configuration = open
|
||
}
|
||
|
||
override func layoutSubviews() {
|
||
super.layoutSubviews()
|
||
let buttonWidth = openButton.sizeThatFits(CGSize(width: bounds.width, height: chipHeight)).width
|
||
openButton.frame = CGRect(
|
||
x: chipLeads ? 0 : bounds.width - buttonWidth,
|
||
y: (bounds.height - chipHeight) / 2,
|
||
width: buttonWidth,
|
||
height: chipHeight
|
||
)
|
||
let textX = chipLeads ? openButton.frame.maxX + 10 : 0
|
||
let textWidth = chipLeads ? max(0, bounds.width - textX) : max(0, openButton.frame.minX - 10)
|
||
// Two lines in the strip's 27 pt.
|
||
let lineHeight: CGFloat = min(16, (bounds.height + 1) / 2)
|
||
let top = (bounds.height - 2 * lineHeight) / 2
|
||
titleLabel.frame = CGRect(x: textX, y: top, width: textWidth, height: lineHeight)
|
||
pathLabel.frame = CGRect(x: textX, y: top + lineHeight, width: textWidth, height: lineHeight)
|
||
}
|
||
}
|
||
|
||
/// Record typing logs is recording this field: a dot and "Logging", flat and small, in the
|
||
/// strip's corner and in the recording bar, so logging is never hidden.
|
||
@MainActor
|
||
final class TypingLogMarkView: UIView {
|
||
static let text = "Logging"
|
||
static let accessibilityText = "Typing logs are recording"
|
||
|
||
/// The dot and the word share the strip's muted ink: an orange dot would read as iOS's
|
||
/// microphone indicator.
|
||
var textColor: UIColor = .secondaryLabel {
|
||
didSet {
|
||
label.textColor = textColor
|
||
dot.backgroundColor = textColor
|
||
}
|
||
}
|
||
|
||
private let dot = UIView()
|
||
private let label = UILabel()
|
||
private static let dotSize: CGFloat = 5
|
||
private static let gap: CGFloat = 4
|
||
|
||
override init(frame: CGRect) {
|
||
super.init(frame: frame)
|
||
isUserInteractionEnabled = false
|
||
isAccessibilityElement = true
|
||
accessibilityLabel = Self.accessibilityText
|
||
dot.backgroundColor = textColor
|
||
dot.layer.cornerRadius = Self.dotSize / 2
|
||
label.text = Self.text
|
||
label.font = .systemFont(ofSize: 10, weight: .medium)
|
||
label.textColor = textColor
|
||
addSubview(dot)
|
||
addSubview(label)
|
||
}
|
||
|
||
@available(*, unavailable)
|
||
required init?(coder: NSCoder) { fatalError() }
|
||
|
||
override func sizeThatFits(_ size: CGSize) -> CGSize {
|
||
let text = label.sizeThatFits(size)
|
||
return CGSize(width: Self.dotSize + Self.gap + ceil(text.width), height: max(Self.dotSize, ceil(text.height)))
|
||
}
|
||
|
||
override func layoutSubviews() {
|
||
super.layoutSubviews()
|
||
dot.frame = CGRect(x: 0, y: (bounds.height - Self.dotSize) / 2, width: Self.dotSize, height: Self.dotSize)
|
||
label.frame = CGRect(x: Self.dotSize + Self.gap, y: 0, width: max(0, bounds.width - Self.dotSize - Self.gap), height: bounds.height)
|
||
}
|
||
}
|