hex/ios/HEXShared/HomeSwipeCue.swift
Ryan Vogel bb50edc034
iOS: a tap after a swiped word starts the next word, and the Metal HUD stays off the home page (#2159)
* iOS: a tap right after a swiped word starts the next word, as on Apple's keyboard

TestFlight (build 56, iPhone 15 Pro, iOS 27.0): "Manual typing right after swipe
typing a word should add a space after for the next word." Hex joined a tapped
letter onto the swiped word ("hello" + w-o-r-l-d typed "helloworld").

Apple's keyboard, watched black-box on the iOS 27.2 simulator in Messages, keeps a
space pending after a swiped word (the field reads "Hello", no trailing space):
- a letter, digit, opening bracket, straight quote or symbol (@ # $ & % * = _ ...)
  goes in after a space: "Hello world", "Hello 2", "Hello (", "Hello @";
- , . ! ? ; : ) ] } ' - / go against the word and take the space: "Hello,",
  then w types "Hello,w";
- return types "Hello
", space one space, a second swipe " world";
- delete right after the swipe takes the whole word; after "Hello w", delete
  takes w, then the space;
- shift tapped after the swipe gives "Hello W".

FullKeyboardEngine.spaceAfterSwipedWord types that space before a tapped key or
accent when the swiped word is still right before the cursor and the key is not
in swipeJoiners. It never autocorrects the swiped word, keeps a shift you set and
refreshes auto-capitalization only when shift is off. Replaces the old rule "a tap
after a swipe adds to its word" (testASwipeTypesItsWordAndTheNextOneASpaceLater,
the second-finger, delete and Adaptive touch tests updated to match).

Verified: xcodebuild test (469 tests, 0 failures, iPhone 17 iOS 27.2 sim); live in
Messages with Hex's keyboard: swipe "hello" + tap w-o-r-l-d gives "Hello world",
swipe + "," gives "Hello,", then a swipe gives "Hello, world".

* iOS: keep the Metal Performance HUD off the dot scope, so the home well shows one label

TestFlight (build 56, iPhone19,2, iOS 27.2), "UI bug": the home well read Ready
with the Dictate pill drawn over a second, outlined "109.91" in a dark rounded box.
That text is not Hex's: the well's only labels are Dictate/Set up, Preparing
model..., Allow microphone access... and Done/Cancel, and Hex's clock (in the header
pill, not the well) is "m:ss" (HexTheme.formatClock). The box sits 10 pt in from
the top right of the DotScopeView's CAMetalLayer, under the SwiftUI pill, which is
where the Metal Performance HUD (Settings > Developer > Graphics HUD) draws a
layer's frame rate: about 110 fps on a 120 Hz iPhone. So the tester's Graphics HUD
was on, and every state cross-fade was fine.

CAMetalLayer.hidesDeveloperHUD sets developerHUDProperties mode "disabled" on the
dot scope (home well, handoff screen, keyboard dictation bar) and the home-indicator
swipe cue, unless the process was launched with MTL_HUD_ENABLED=1 (Xcode), so
developers can still profile them.

Verified: xcodebuild test (469 tests, 0 failures, iPhone 17 iOS 27.2 sim); home
well before/after screenshots on the sim for the ready, recording, transcribing,
failed and handoff fixtures and live start, quick start/stop, cancel, back from
background, microphone denied and Reduce Motion: one label per settled state in
both. Not reproduced on a device: the simulator draws no Metal HUD (launched with
MTL_HUD_ENABLED=1, nothing showed), so the HUD reading comes from the screenshot's
geometry and text.
2026-11-21 07:54:19 +00:00

258 lines
8.8 KiB
Swift

import Metal
import QuartzCore
import simd
import SwiftUI
import UIKit
// MARK: - `DotSwipeCueView` (Full-Bottom-Width Home-Indicator Return Cue)
@MainActor
final class DotSwipeCueHostView: UIView {
override static var layerClass: AnyClass { CAMetalLayer.self }
private static let fadeSpec = DotSpringSpec(duration: 0.34, bounce: 0.0)
/// Exact integer multiple of shader `period` (`1.24s * 512`) so `Float(elapsed)` never loses mantissa precision.
private static let elapsedWrapPeriod: CFTimeInterval = 1.24 * 512.0
private let proxy = DisplayLinkProxy()
private var displayLink: CADisplayLink?
private var lastTargetTimestamp: CFTimeInterval?
/// Starts mid-throw so initial paint and snapshot stills immediately read as a rightward sweep.
private var elapsed: CFTimeInterval = 1.38
private var visibilitySpring = DotSpring(at: 0.0)
private var hasSeededVisibility = false
var reduceMotion = false {
didSet {
if reduceMotion != oldValue {
updateAnimatingState()
renderCurrentFrame()
}
}
}
var isActive = true {
didSet {
if isActive != oldValue {
updateAnimatingState()
if displayLink?.isPaused != false {
renderCurrentFrame()
}
}
}
}
var isSceneActive = true {
didSet {
if isSceneActive != oldValue {
updateAnimatingState()
if displayLink?.isPaused != false {
renderCurrentFrame()
}
}
}
}
var debugElapsedTime: Float { Float(elapsed) }
var debugVisibility: Float { visibilitySpring.x }
var debugIsPaused: Bool { displayLink?.isPaused ?? true }
private var metalLayer: CAMetalLayer {
// swiftlint:disable:next force_cast
layer as! CAMetalLayer
}
override init(frame: CGRect) {
super.init(frame: frame)
isOpaque = false
backgroundColor = .clear
isUserInteractionEnabled = false
if let gpu = DotScopeMetalPipeline.shared {
metalLayer.device = gpu.device
metalLayer.pixelFormat = .bgra8Unorm
metalLayer.isOpaque = false
metalLayer.framebufferOnly = true
metalLayer.presentsWithTransaction = false
metalLayer.allowsNextDrawableTimeout = false
metalLayer.hidesDeveloperHUD()
metalLayer.maximumDrawableCount = 3
metalLayer.colorspace = CGColorSpace(name: CGColorSpace.sRGB)
}
proxy.tickHandler = { [weak self] link in
self?.stepAndRender(timestamp: link.timestamp, targetTimestamp: link.targetTimestamp)
}
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func didMoveToWindow() {
super.didMoveToWindow()
if window != nil {
seedVisibilityIfNeeded()
ensureDisplayLink()
updateAnimatingState()
renderCurrentFrame()
} else {
displayLink?.invalidate()
displayLink = nil
lastTargetTimestamp = nil
}
}
override func layoutSubviews() {
super.layoutSubviews()
seedVisibilityIfNeeded()
let scale = max(1.0, traitCollection.displayScale)
metalLayer.contentsScale = scale
let pixelSize = CGSize(width: (bounds.width * scale).rounded(), height: (bounds.height * scale).rounded())
let sizeChanged = metalLayer.drawableSize != pixelSize
if sizeChanged {
metalLayer.drawableSize = pixelSize
}
updateAnimatingState()
if sizeChanged || displayLink?.isPaused != false {
renderCurrentFrame()
}
}
private func seedVisibilityIfNeeded() {
guard !hasSeededVisibility else { return }
hasSeededVisibility = true
visibilitySpring.jump(isActive ? 1.0 : 0.0)
}
private func ensureDisplayLink() {
guard displayLink == nil else { return }
let link = CADisplayLink(target: proxy, selector: #selector(DisplayLinkProxy.tick(_:)))
if #available(iOS 15.0, *) {
// `minimum: 60` lets both 60 Hz displays/Simulators and 120 Hz ProMotion panels lock
// cleanly to hardware vsync without CoreAnimation pacing stalls.
link.preferredFrameRateRange = CAFrameRateRange(minimum: 60, maximum: 120, preferred: 120)
} else {
link.preferredFramesPerSecond = 60
}
link.isPaused = true
link.add(to: .main, forMode: .common)
displayLink = link
}
private var wantsAnimationFrames: Bool {
guard window != nil, isSceneActive else { return false }
let target: Float = isActive ? 1.0 : 0.0
let fading = abs(visibilitySpring.x - target) > 0.002 || abs(visibilitySpring.v) > 0.002
if reduceMotion {
return fading
}
return isActive || fading
}
private func updateAnimatingState() {
let shouldRun = wantsAnimationFrames
if displayLink?.isPaused == shouldRun {
lastTargetTimestamp = nil
}
displayLink?.isPaused = !shouldRun
}
func stepAndRender(timestamp: CFTimeInterval, targetTimestamp: CFTimeInterval) {
seedVisibilityIfNeeded()
let rawDelta: CFTimeInterval
if let prevTarget = lastTargetTimestamp {
rawDelta = targetTimestamp - prevTarget
} else {
rawDelta = targetTimestamp - timestamp
}
let clampedDelta = min(0.12, max(1.0 / 240.0, rawDelta))
let dt = Float(clampedDelta)
lastTargetTimestamp = targetTimestamp
let target: Float = isActive ? 1.0 : 0.0
let spec: DotSpringSpec = reduceMotion ? .reducedFade : Self.fadeSpec
visibilitySpring.step(to: target, spec: spec, dt: dt)
if !isActive, visibilitySpring.x < 0.002, abs(visibilitySpring.v) < 0.005 {
visibilitySpring.jump(0.0)
}
if !reduceMotion {
elapsed += clampedDelta
if elapsed >= Self.elapsedWrapPeriod {
elapsed -= Self.elapsedWrapPeriod
}
}
renderCurrentFrame()
updateAnimatingState()
}
private func renderCurrentFrame() {
guard bounds.width > 4, bounds.height > 4,
let gpu = DotScopeMetalPipeline.shared,
let swipePipeline = gpu.swipePipeline,
let drawable = metalLayer.nextDrawable(),
let commandBuffer = gpu.queue.makeCommandBuffer() else {
return
}
let scale = Float(max(1.0, metalLayer.contentsScale))
let resX = Float(metalLayer.drawableSize.width)
let resY = Float(metalLayer.drawableSize.height)
let pxPitch = max(3.0, (2.15 * scale).rounded())
let pitch = pxPitch / scale
let cols = max(15, Int(floor(resX / pxPitch)) - 1)
let rows = 5
let opacity = min(1.0, max(0.0, visibilitySpring.x))
var uniforms = DotSwipeCueMetalUniforms(
metrics: SIMD4<Float>(resX, resY, scale, pitch),
grid: SIMD4<Float>(Float(cols), Float(rows), Float(elapsed), reduceMotion ? 1.0 : 0.0),
base: SIMD4<Float>(0.86, 0.83, 0.77, opacity),
core: SIMD4<Float>(0.99, 0.97, 0.93, 54.0)
)
let pass = MTLRenderPassDescriptor()
pass.colorAttachments[0].texture = drawable.texture
pass.colorAttachments[0].loadAction = .clear
pass.colorAttachments[0].storeAction = .store
pass.colorAttachments[0].clearColor = MTLClearColor(red: 0, green: 0, blue: 0, alpha: 0)
if let encoder = commandBuffer.makeRenderCommandEncoder(descriptor: pass) {
encoder.setRenderPipelineState(swipePipeline)
encoder.setFragmentBytes(
&uniforms,
length: MemoryLayout<DotSwipeCueMetalUniforms>.stride,
index: 0
)
encoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 3)
encoder.endEncoding()
}
commandBuffer.present(drawable)
commandBuffer.commit()
}
}
struct DotSwipeCueView: UIViewRepresentable {
var isActive: Bool = true
@Environment(\.accessibilityReduceMotion) private var reduceMotion
@Environment(\.scenePhase) private var scenePhase
func makeUIView(context: Context) -> DotSwipeCueHostView {
let view = DotSwipeCueHostView()
view.reduceMotion = reduceMotion
view.isActive = isActive
view.isSceneActive = scenePhase == .active
return view
}
func updateUIView(_ uiView: DotSwipeCueHostView, context: Context) {
uiView.reduceMotion = reduceMotion
uiView.isActive = isActive
uiView.isSceneActive = scenePhase == .active
}
}