mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-25 01:30:06 +08:00
iOS keyboard: Paste alone takes a tap anywhere on the bar (#2386)
When Paste (or Paste dictation) is the only chip in the bar above the keys, only the small chip took the tap. Ryan asked for the whole width to work. What changed - KeyboardStripView: the Paste chip is now a StripChip, a UIButton whose touch can reach past its frame (touchArea). While Paste is the only chip, its area is the bar from the keyboard's top to the bar's bottom, across to the Hex key's cell (FullKeyboardView passes it as barTouchFrame, from the layout's stripBarFrame and the Hex key's hit frame, so it follows the bar's bounds, not a fixed height: 44 pt Apple bar on iOS 26+, 38 pt strip on iOS 18). The chip lights on touch-down anywhere there, follows the finger in and out, and pastes only if it lifts inside, so a finger slid off the bar cancels. The chip's look, size, place and VoiceOver element are unchanged. - The Hex key's cell and row 1's reach above the keys sit over the strip and keep their own touches. Beside Code from Messages, with Code alone, and with words, every chip and word keeps its own touch area, as before. - Behaviour given up: while Paste is the only chip, a still hold on an empty part of the bar no longer starts a bar-hold dictation there (the tap is Paste's); the Hex key's hold still does. Verified - Real HID touches on Studio simulators (iPhone 17 Pro) on this head's code, Paste only: iOS 27.2 upright 13 rows x 21 columns, 26.5 upright 10 x 21, 27.2 turned 9 x 26. Every point inside Hex's bar left of the Hex key's cell pasted (x up to 341 pt, cell divider at 343); every point in the Hex key's cell gave the Hex key's own response and never pasted; points 1 pt above row 1 (and the bar's bottom 6-8 pt, row 1's reach) typed the key; points above Hex's view (iOS's band) and outside the turned bar's safe area did nothing, as on origin/dev. - xcodebuild test on an iPhone 17 Pro iOS 27.2 simulator: 854 tests, 0 failures (3 skipped), including the new TranslationPasteSwitchTests testPasteTakesTheWholeBar (hit tests across the bar for Paste, Paste dictation, the two-chip case, Code alone and words). - python3 scripts/code-shape.py passes; no new files.
This commit is contained in:
@@ -386,6 +386,59 @@ final class TranslationPasteSwitchTests: XCTestCase {
|
||||
XCTAssertFalse(visibleLabels(in: strip).contains("Couldn't translate, pasted as spoken"))
|
||||
}
|
||||
|
||||
/// Paste alone takes a tap anywhere on the bar, top to bottom and up to the Hex key's cell,
|
||||
/// which keeps its own; beside Code from Messages, Code alone, and the words, every chip and
|
||||
/// word keeps its own touch area.
|
||||
@MainActor
|
||||
func testPasteTakesTheWholeBar() throws {
|
||||
let view = FullKeyboardView(model: KeyboardViewModel(), target: FixtureTextTarget(text: ""))
|
||||
view.frame = CGRect(x: 0, y: 0, width: 402, height: 262)
|
||||
view.layoutIfNeeded()
|
||||
let strip = view.stripView
|
||||
let hex = view.hexKey.frame
|
||||
XCTAssertTrue(view.hexKey.inStrip)
|
||||
func show(_ clipboard: KeyboardClipboardOffer?, _ words: [KeyboardSuggestion] = []) {
|
||||
strip.update(suggestions: words, showsUndo: false, statusText: nil, clipboard: clipboard)
|
||||
strip.layoutIfNeeded()
|
||||
}
|
||||
func chip(_ title: String) throws -> UIButton {
|
||||
try XCTUnwrap(visibleControls(in: strip).first { $0.configuration?.title == title }, title)
|
||||
}
|
||||
// Every point of the bar the keys' own reach above row 1 doesn't take.
|
||||
let barPoints = stride(from: 0.5, to: hex.minX, by: 6).flatMap { x in
|
||||
stride(from: 0.5, to: strip.frame.maxY, by: 4).map { CGPoint(x: x, y: $0) }
|
||||
}.filter { !view.plane.point(inside: view.convert($0, to: view.plane), with: nil) }
|
||||
XCTAssertGreaterThan(barPoints.count, 200)
|
||||
|
||||
show(.paste)
|
||||
let paste = try chip("Paste")
|
||||
for point in barPoints { XCTAssertTrue(view.hitTest(point, with: nil) === paste, "\(point)") }
|
||||
XCTAssertTrue(view.hitTest(CGPoint(x: hex.minX - 0.5, y: strip.frame.midY), with: nil) === paste)
|
||||
XCTAssertTrue(view.hitTest(CGPoint(x: hex.minX + 0.5, y: strip.frame.midY), with: nil) === view.hexKey)
|
||||
XCTAssertTrue(view.hitTest(CGPoint(x: hex.midX, y: strip.frame.midY), with: nil) === view.hexKey)
|
||||
XCTAssertEqual(visibleControls(in: strip).map(\.configuration?.title), ["Paste"], "one Paste for VoiceOver, at the chip")
|
||||
|
||||
show(.dictation)
|
||||
let dictation = try chip("Paste dictation")
|
||||
for point in barPoints { XCTAssertTrue(view.hitTest(point, with: nil) === dictation, "\(point)") }
|
||||
|
||||
show(.code(canPaste: true))
|
||||
let pasteCode = try chip("Paste"), code = try chip("Code from Messages")
|
||||
for point in barPoints {
|
||||
let hit = view.hitTest(point, with: nil)
|
||||
let inPaste = pasteCode.point(inside: view.convert(point, to: pasteCode), with: nil)
|
||||
let inCode = code.bounds.contains(view.convert(point, to: code))
|
||||
XCTAssertTrue(inPaste ? hit === pasteCode : inCode ? hit === code : !(hit is UIButton), "two chips keep their own: \(point)")
|
||||
}
|
||||
|
||||
show(.code(canPaste: false))
|
||||
let codeAlone = try chip("Code from Messages")
|
||||
XCTAssertFalse(view.hitTest(CGPoint(x: 2, y: strip.frame.midY), with: nil) === codeAlone, "Code alone keeps its chip's own area")
|
||||
|
||||
show(.paste, ["one", "two", "three"].map { KeyboardSuggestion(displayText: $0, replacement: $0, kind: .secondary) })
|
||||
for point in barPoints { XCTAssertFalse(view.hitTest(point, with: nil) is UIButton, "words, not Paste: \(point)") }
|
||||
}
|
||||
|
||||
/// Undo dictation still takes the dictation back after the cursor moves within it, before it
|
||||
/// or after it, and the cursor stays in the text you moved it to. Where it can't tell the
|
||||
/// dictation from other text it goes, and deletes nothing.
|
||||
|
||||
@@ -748,6 +748,14 @@ final class FullKeyboardView: UIView {
|
||||
stripView.dividerBesideHex = layout.metrics.appleBar && layout.key(.hex).map { $0.row < 0 } == true
|
||||
stripView.hexOnLeading = layout.hexOnLeading
|
||||
stripView.barMidX = layout.stripBarFrame.midX - layout.stripFrame.minX
|
||||
// The bar from the keyboard's top to the strip's bottom, up to the Hex key beside it.
|
||||
var bar = CGRect(x: layout.stripBarFrame.minX, y: 0, width: layout.stripBarFrame.width, height: layout.stripBarFrame.maxY)
|
||||
if let hex = layout.key(.hex), hex.row < 0 {
|
||||
bar = layout.hexOnLeading
|
||||
? CGRect(x: hex.hitFrame.maxX, y: bar.minY, width: bar.maxX - hex.hitFrame.maxX, height: bar.height)
|
||||
: CGRect(x: bar.minX, y: bar.minY, width: hex.hitFrame.minX - bar.minX, height: bar.height)
|
||||
}
|
||||
stripView.barTouchFrame = bar.offsetBy(dx: -layout.stripFrame.minX, dy: -layout.stripFrame.minY)
|
||||
|
||||
hexKey.cornerRadius = layout.metrics.cornerRadius
|
||||
if let hex = layout.key(.hex) {
|
||||
|
||||
@@ -99,11 +99,16 @@ final class KeyboardStripView: UIView {
|
||||
/// Where the bar's middle slot centres, from this view's leading edge: the keyboard's middle,
|
||||
/// not this view's, which leaves out the Hex key. Paste and Next keyboard centre on it.
|
||||
var barMidX: CGFloat? { didSet { if barMidX != oldValue { setNeedsLayout() } } }
|
||||
/// The bar around this view, in its coordinates: from the keyboard's top to the bar's bottom
|
||||
/// and across it up to the Hex key's cell. While Paste (or Paste dictation) is the only chip,
|
||||
/// a tap anywhere on it pastes; beside Code from Messages each chip keeps its own frame. The
|
||||
/// Hex key and row 1's reach, laid over it, keep theirs.
|
||||
var barTouchFrame: CGRect? { didSet { if barTouchFrame != oldValue { setNeedsLayout() } } }
|
||||
/// Plain text on the words' line, as a suggestion is: `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 pasteControl = StripChip()
|
||||
private let nextKeyboardChip = UIButton(type: .custom)
|
||||
|
||||
override init(frame: CGRect) {
|
||||
@@ -374,6 +379,7 @@ final class KeyboardStripView: UIView {
|
||||
width: nextSize.width,
|
||||
height: chipH
|
||||
)
|
||||
pasteControl.touchArea = pasteShown && !nextShown ? barTouchFrame ?? bounds : nil
|
||||
// The hint's two lines and chip centre on the words' line, as everything in the strip.
|
||||
let hintHalf = min(midY, bounds.height - midY)
|
||||
let hintY = midY - hintHalf
|
||||
@@ -389,6 +395,11 @@ final class KeyboardStripView: UIView {
|
||||
}
|
||||
}
|
||||
|
||||
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
|
||||
if pasteControl.touchArea != nil, let barTouchFrame, barTouchFrame.contains(point) { return true }
|
||||
return super.point(inside: point, with: event)
|
||||
}
|
||||
|
||||
private var currentSlotFrames: [CGRect] {
|
||||
if let slotFrames, slotFrames.count == 3 { return slotFrames }
|
||||
let slotW = bounds.width / 3
|
||||
@@ -438,6 +449,66 @@ final class KeyboardStripView: UIView {
|
||||
onHoldSuggestion?(currentSuggestions[item])
|
||||
}
|
||||
|
||||
/// A chip whose touch can reach past its frame (`touchArea`, in the strip's coordinates): it
|
||||
/// lights wherever there the finger lands, follows it in and out, and acts only if it lifts
|
||||
/// inside, so a finger slid off the bar cancels. Without one, a plain button.
|
||||
private final class StripChip: UIButton {
|
||||
var touchArea: CGRect?
|
||||
private var trackedTouch: UITouch?
|
||||
|
||||
private func inArea(_ touch: UITouch) -> Bool {
|
||||
guard let touchArea, let superview else { return false }
|
||||
return touchArea.contains(touch.location(in: superview))
|
||||
}
|
||||
|
||||
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
|
||||
guard let touchArea, let superview else { return super.point(inside: point, with: event) }
|
||||
return touchArea.contains(convert(point, to: superview))
|
||||
}
|
||||
|
||||
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
|
||||
guard touchArea != nil, trackedTouch == nil, let touch = touches.first else {
|
||||
super.touchesBegan(touches, with: event)
|
||||
return
|
||||
}
|
||||
trackedTouch = touch
|
||||
isHighlighted = true
|
||||
sendActions(for: .touchDown)
|
||||
}
|
||||
|
||||
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
|
||||
guard let touch = trackedTouch, touches.contains(touch) else {
|
||||
super.touchesMoved(touches, with: event)
|
||||
return
|
||||
}
|
||||
let inside = inArea(touch)
|
||||
guard inside != isHighlighted else { return }
|
||||
isHighlighted = inside
|
||||
sendActions(for: inside ? .touchDragEnter : .touchDragExit)
|
||||
}
|
||||
|
||||
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
|
||||
guard let touch = trackedTouch, touches.contains(touch) else {
|
||||
super.touchesEnded(touches, with: event)
|
||||
return
|
||||
}
|
||||
trackedTouch = nil
|
||||
let inside = inArea(touch)
|
||||
isHighlighted = false
|
||||
sendActions(for: inside ? .touchUpInside : .touchUpOutside)
|
||||
}
|
||||
|
||||
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
|
||||
guard let touch = trackedTouch, touches.contains(touch) else {
|
||||
super.touchesCancelled(touches, with: event)
|
||||
return
|
||||
}
|
||||
trackedTouch = nil
|
||||
isHighlighted = false
|
||||
sendActions(for: .touchCancel)
|
||||
}
|
||||
}
|
||||
|
||||
/// 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
|
||||
|
||||
Reference in New Issue
Block a user