mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
What changed - The trackpad drags a point through the text at the text's own scale, in two dimensions, and puts the cursor at the nearest position (TrackpadCursor.State), as Apple's does. The text the field shares is laid out with the body font's own character widths, rows wrapped at the keyboard width less 40 pt, 20 pt lines. - Gain follows the finger's speed like Apple's: 1x slowly, 1.4x at 400 pt/s, 2x at 1,000, 2.6x at 2,000 (TrackpadCursor.gain), from a windowed speed of the touch's own timestamps, the same up, down or slanting. - Lines: one per line height (the first after half a line), x kept across short lines; it stops at a line's start and end and at the first/last line. Replaces the fixed 7.5 pt per character and 15.5 pt per line, and the move on past a line end into the next line. - Hosts share only the cursor's sentence/line (UITextView: "Lin" | "e two is a bit longer"), and drop a move that would leave the text (moves sent together add up). So at the edge of what's known the cursor waits for the host to show more, and past the first/last known line it steps just past it in one move, which the host makes only if text is there; no answer in 100 ms means it never moved. Before, moving down past the current line did nothing. - Offsets are UTF-16 (the proxy's unit): a 👍🏽 is one step, not four (three of them stalled inside the emoji). - Keys blank to keycaps 60% of the way to the backdrop (Apple: 242,242,247 on 226,227,232 light, 54 on 32 dark); Hex kept white keycaps. - No haptic on start or per character (Apple's keyboard asks for no feedback after the space bar's own); a second finger while the trackpad is on types nothing (it typed the blank key under it). Selection stays impossible: UITextDocumentProxy can't set one. - Trackpad code moved to FullKeyboardEngine+Trackpad.swift; FullKeyboardEngine.swift is 1,476 lines. Verified - Apple black box, iOS 27.2 simulator, same scripted HID gestures on Apple's, origin/dev and this build (UITextView logging selectedRange): 80 pt right at 150/400/1,000/2,000 pt/s gave 11/13/21/26 characters on Apple's, 10/10/10/10 before, 11/13/20/26 now; 140 pt down from line 1 reached line 5 on Apple's and now, nothing before; up 40 pt in 40 ms: 4 lines on both (before 3); 100 pt past a line end stops at it on both (before ran into the next line); column kept up 8 lines (index 7 on both). - Independent check (coordinator, iPhone 15 Pro / iOS 27.2 simulator, native UITextView, matched HID): build 56 up stays line 8 / down stays 5 / right moves 4; build 110 (19747178) up 8->6 / down stays 5 / right 4; this candidate up 8->6 / down 5->6 / right moves one, no text edits. - xcodebuild test: 624 tests, 0 failures, 1 skipped; after rebasing on dev3f374f42, FullKeyboardEngineTests' 118 pass.
122 lines
6.1 KiB
Swift
122 lines
6.1 KiB
Swift
import UIKit
|
|
|
|
/// The space bar's trackpad, as Apple's moves it (`TrackpadCursor`): held, or with one language
|
|
/// slid, it moves the cursor through the text in two dimensions, faster the faster the finger.
|
|
@MainActor
|
|
extension FullKeyboardEngine {
|
|
/// The space bar held `trackpadHoldDelay` without sliding: the trackpad starts where the
|
|
/// finger is, as Apple's does, so letting go types no space (a press held to move the
|
|
/// cursor used to type one beside the space already there).
|
|
func holdSpace(forTouch id: Int) {
|
|
guard var touch = touches[id], touch.key.kind == .space, !touch.isTrackpad, !touch.isLanguageSwipe else { return }
|
|
startTrackpad(&touch, id: id, hold: true, time: nil)
|
|
}
|
|
|
|
/// A space bar touch moved to `point`: true once it's the keys' to keep (a language swipe,
|
|
/// or the trackpad starting or moving the cursor), false while it may still be a tap.
|
|
func moveSpaceBar(_ touch: inout ActiveTouch, id: Int, to point: CGPoint, time: TimeInterval?) -> Bool {
|
|
if touch.isLanguageSwipe { return true }
|
|
if !touch.isTrackpad, abs(point.x - touch.startPoint.x) >= Self.trackpadActivationDistance {
|
|
if switchesLanguage {
|
|
touch.isLanguageSwipe = true
|
|
} else {
|
|
startTrackpad(&touch, id: id, hold: false, time: time)
|
|
}
|
|
return true
|
|
}
|
|
guard touch.isTrackpad else { return false }
|
|
let offset = trackpadStep(&touch, to: point, time: time ?? clock())
|
|
moveCursor(&touch, id: id, by: offset)
|
|
return true
|
|
}
|
|
|
|
private func startTrackpad(_ touch: inout ActiveTouch, id: Int, hold: Bool, time: TimeInterval?) {
|
|
touch.isTrackpad = true
|
|
touch.trackpadPath = [TrackpadCursor.Sample(time: time ?? clock(), point: touch.point)]
|
|
touch.trackpad = TrackpadCursor.State(
|
|
before: target.documentContextBeforeInput ?? "",
|
|
after: target.documentContextAfterInput ?? "",
|
|
wrapWidth: TrackpadCursor.wrapWidth(forWidth: width),
|
|
lineHeight: Self.trackpadFont().lineHeight,
|
|
width: Self.trackpadWidth
|
|
)
|
|
trackpadUpdated(touch, id: id, started: true)
|
|
var fields = [TypingLogField("id", .int(id))]
|
|
if hold { fields.append(TypingLogField("hold", .bool(true))) }
|
|
typingLog?.ui("trackpad_start", fields)
|
|
onChange?()
|
|
}
|
|
|
|
/// The finger moved to `point` at `time`: the UTF-16 offset to move the cursor by. The
|
|
/// finger's speed sets how far the point goes (`TrackpadCursor.gain(speed:)`); with no time
|
|
/// between moves (a test's clock) it goes as far as the finger.
|
|
private func trackpadStep(_ touch: inout ActiveTouch, to point: CGPoint, time: TimeInterval) -> Int {
|
|
let last = touch.trackpadPath.last ?? TrackpadCursor.Sample(time: time, point: point)
|
|
let sample = TrackpadCursor.Sample(time: time, point: point)
|
|
touch.trackpadPath = time > last.time ? touch.trackpadPath.suffix(7) + [sample] : [sample]
|
|
guard var state = touch.trackpad else { return 0 }
|
|
let gain = TrackpadCursor.gain(speed: TrackpadCursor.speed(along: touch.trackpadPath))
|
|
let dx = point.x - last.point.x, dy = point.y - last.point.y
|
|
let vertical = touch.trackpadPath.count > 1 ? TrackpadCursor.isVertical(touch.trackpadPath) : abs(dy) > abs(dx)
|
|
var offset = state.move(by: CGVector(dx: dx * gain, dy: dy * gain), at: time, vertical: vertical)
|
|
if state.nearsEdge, catchUp(&state) { offset += state.settle(at: time) }
|
|
touch.trackpad = state
|
|
return offset
|
|
}
|
|
|
|
/// Adds what the field shows beyond the trackpad's copy of the text, if it does.
|
|
private func catchUp(_ state: inout TrackpadCursor.State) -> Bool {
|
|
state.catchUp(before: target.documentContextBeforeInput ?? "", after: target.documentContextAfterInput ?? "", width: Self.trackpadWidth)
|
|
}
|
|
|
|
/// The field changed: while the trackpad is on that's mostly the host following its moves.
|
|
/// It may show more of the text, or that the cursor is at the text's start or a line's end,
|
|
/// so the cursor goes where the point is once more.
|
|
func trackpadFieldChanged() {
|
|
guard let (id, found) = touches.first(where: { $0.value.isTrackpad }), var state = found.trackpad else { return }
|
|
var touch = found
|
|
if state.nearsEdge { _ = catchUp(&state) }
|
|
let offset = state.settle(at: clock())
|
|
touch.trackpad = state
|
|
moveCursor(&touch, id: id, by: offset)
|
|
}
|
|
|
|
/// The trackpad finger lifts: the cursor goes where the point is, among the rows known.
|
|
func liftTrackpad(_ touch: inout ActiveTouch) {
|
|
guard var state = touch.trackpad else { return }
|
|
let offset = state.settle(at: clock(), lifting: true)
|
|
touch.trackpad = state
|
|
log(&touch, offset)
|
|
if offset != 0 { trackpadMovedCursor(by: offset) }
|
|
}
|
|
|
|
private func moveCursor(_ touch: inout ActiveTouch, id: Int, by offset: Int) {
|
|
log(&touch, offset)
|
|
trackpadUpdated(touch, id: id)
|
|
if offset != 0 { trackpadMovedCursor(by: offset) }
|
|
}
|
|
|
|
private func log(_ touch: inout ActiveTouch, _ offset: Int) {
|
|
guard offset != 0 else { return }
|
|
touch.trackpadOffset += offset
|
|
typingLog?.edit("cursor", [TypingLogField("offset", .int(offset))])
|
|
}
|
|
|
|
private static var trackpadWidths: (font: UIFont, widths: [Character: CGFloat]) = (.systemFont(ofSize: 17), [:])
|
|
|
|
/// Body text, as most fields show it (17 pt, larger with Dynamic Type): the font the
|
|
/// trackpad measures characters in (`trackpadWidth`).
|
|
static func trackpadFont() -> UIFont {
|
|
let font = UIFont.preferredFont(forTextStyle: .body)
|
|
if font != trackpadWidths.font { trackpadWidths = (font, [:]) }
|
|
return font
|
|
}
|
|
|
|
/// A character's width in `trackpadFont()`, measured once.
|
|
static func trackpadWidth(_ character: Character) -> CGFloat {
|
|
if let width = trackpadWidths.widths[character] { return width }
|
|
let width = character.isNewline ? 0 : (String(character) as NSString).size(withAttributes: [.font: trackpadWidths.font]).width
|
|
trackpadWidths.widths[character] = width
|
|
return width
|
|
}
|
|
}
|