Fix iOS terminal dock keyboard pinning (bars jump to mid-screen)

Merged
AZ
4mo ago
+527-65

On iPhone, opening the software keyboard in a terminal workspace launched the shortcut toolbar and the Message composer to the middle of the screen (roughly an extra keyboard height above their correct spot, with the two bars visibly separating mid-flight), then they settled back down about a third of a second after the keyboard finished. Sometimes they never settled and stayed stranded mid-screen.

Four stacked causes, all in the terminal dock path:

  1. The composer's UIHostingController was the only hosting controller in the app without safeAreaRegions = .container, so SwiftUI keyboard avoidance ran inside the band and sizeThatFits measurements taken while the keyboard rose came back inflated by the keyboard overlap. That inflated height became composerBandHeight, and the toolbar rides the band's top edge, so both bars jumped a keyboard height too high. A later clean re-measure settled them; when none arrived they stayed stuck.
  2. handleKeyboardWillChangeFrame guarded on the cached animation target instead of the on-screen position, so repeat notifications during an interrupted transition were swallowed and the dock stayed parked wherever it was.
  3. Three animation clocks (keyboard curve, hardcoded 0.25s ease-in-out band reflow, hardcoded 0.25s animateBottomDock) ran additively on the same layers.
  4. layoutSubviews and safeAreaInsetsDidChange snapped dock frames outside any animation mid-flight.

Fix, modeled on the already-hardened agent-chat keyboard controller: a pure TerminalDockKeyboardTransitionPlanner (in CmuxMobileTerminalKit, unit-tested) decides ignore/apply/animate per notification by comparing the presentation-layer visible position against the target; the surface pins the model to the visible position and removes stale dock animations before starting each transition; every non-keyboard dock reflow joins the active keyboard animation's curve and remaining duration through one animateDockReflow helper; mid-flight bounds or safe-area changes re-pin and animate the remainder instead of snapping; the composer host opts out of SwiftUI keyboard avoidance and clamps measured band heights to 45% of the surface as a backstop; the async scheduleKeyboardHideHeightResync band-aid is replaced by a deterministic completion settle.

Verification: 106 CmuxMobileTerminalKit tests pass including 6 new planner cases covering the stuck-bar re-drive, synthesized durations for zero-duration notifications, and safe-area boundary pinning. iOS simulator build and device build compile and run; keyboard-up layout verified on the simulator with the bars flush against the keyboard top. A two-commit red/green split was not practical because the buggy logic lived in unhosted UIKit paths; the planner tests lock the new behavior instead.


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.


Summary by cubic

Fixes iOS terminal dock keyboard pinning so the toolbar and composer stay flush to the keyboard and no longer jump or get stranded mid-screen. The dock now follows keyboard transitions reliably, even during interruptions and safe-area changes.

  • Bug Fixes

    • Opt out of SwiftUI keyboard avoidance in the composer host (safeAreaRegions = .container).
    • Clamp composer band height to 45% of the surface to avoid inflated measurements during keyboard rise.
    • Use a new TerminalDockKeyboardTransitionPlanner to decide ignore/apply/animate from presentation-layer geometry.
    • Pin the dock to the visible position and remove stale animations before each transition; route all reflows through one animateDockReflow on the active keyboard curve.
    • Handle zero-duration and interrupted notifications by synthesizing durations; re-pin and continue mid-flight on bounds/safe-area changes instead of snapping.
    • Replace the async keyboard-hide resync with a deterministic completion settle.
  • Refactors

    • Added TerminalDockKeyboardTransitionInput, TerminalDockKeyboardTransitionPlan, and TerminalDockKeyboardTransitionPlanner in CmuxMobileTerminalKit, with unit tests for stuck-bar re-drive, zero-duration synthesis, and safe-area boundary pinning.
    • Extended TerminalKeyboardHeightAnimation to track target overlap, animation options, and end timestamp for shared dock timing.

Summary by CodeRabbit

  • Bug Fixes
    • Improved keyboard transitions to reduce jumps, flicker, and unnecessary layout changes.
    • Kept the terminal dock correctly positioned while the keyboard appears, disappears, or changes size.
    • Improved handling of interrupted keyboard animations and varying transition durations.
    • Prevented the composer from occupying more than 45% of the available terminal surface.
    • Improved safe-area and keyboard geometry handling for the composer field.