Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

Hold the terminal still while the keyboard animates

tony573d5d43ad7d
diff --git a/mobile/lib/phone/terminal_page.dart b/mobile/lib/phone/terminal_page.dart
index 77cc367eb..2097bdde3 100644
--- a/mobile/lib/phone/terminal_page.dart
+++ b/mobile/lib/phone/terminal_page.dart
@@ -107,14 +107,54 @@ class _TerminalPageState extends State<TerminalPage>
/// bottom inset is pinned at zero inside this page.
bool _keyboardUp = false;
+ /// Whether the keyboard is mid-animation, and so the pane's height is still
+ /// changing frame by frame. Handed to [TerminalPanel.settling], which freezes
+ /// the renderer and the auto-resize until this clears.
+ bool _keyboardSettling = false;
+
+ /// The last bottom inset seen, in physical pixels, and the timer that decides
+ /// the animation has stopped.
+ ///
+ /// The platform gives no "keyboard animation finished" callback on either OS —
+ /// only a stream of [didChangeMetrics] ticks — so the end is detected by the
+ /// inset going quiet. The window is a little longer than one frame at 60Hz so
+ /// a slow frame mid-animation does not read as the end of it.
+ double? _lastInset;
+ Timer? _settleTimer;
+ static const _settleWindow = Duration(milliseconds: 80);
+
+ /// Stops the settle watch, leaving the renderer live.
+ ///
+ /// ⚠️ Called from [dispose], so it must not touch [setState].
+ void _cancelSettle() {
+ _settleTimer?.cancel();
+ _settleTimer = null;
+ _lastInset = null;
+ }
+
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
+ /// Releases the freeze when this page is parked mid-animation.
+ ///
+ /// A settle that never ends would otherwise be waiting on [didChangeMetrics]
+ /// ticks that only the page ON SCREEN gets, and the pane would come back from
+ /// the pager with its renderer still gated.
+ @override
+ void didUpdateWidget(TerminalPage oldWidget) {
+ super.didUpdateWidget(oldWidget);
+ if (oldWidget.isActive && !widget.isActive) {
+ _cancelSettle();
+ if (_keyboardSettling) setState(() => _keyboardSettling = false);
+ }
+ }
+
@override
void dispose() {
+ _cancelSettle();
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@@ -133,15 +173,42 @@ class _TerminalPageState extends State<TerminalPage>
void didChangeMetrics() {
super.didChangeMetrics();
if (!mounted) return;
+ final inset = View.of(context).viewInsets.bottom;
// The FIRST frame of the keyboard rising is enough — it need not finish.
// Spending the claim this early is the point: it is off long before any
// Back press can arrive.
- final up = View.of(context).viewInsets.bottom > 0;
+ final up = inset > 0;
final claim = _claimSpent || up;
- if (up == _keyboardUp && claim == _claimSpent) return;
+
+ // Every tick that MOVES the inset is the animation still running; the run
+ // ends when one window passes without another move. Gated on a real change
+ // so the ticks this page gets for everything else — a rotation, a status
+ // bar resizing — never freeze a pane whose height is not moving.
+ //
+ // The very first tick is deliberately not a move: `_lastInset` starts null
+ // and only seeds the baseline, so arriving on this page cannot begin a
+ // settle of its own.
+ final previous = _lastInset;
+ _lastInset = inset;
+ if (previous != null && inset != previous) {
+ _settleTimer?.cancel();
+ _settleTimer = Timer(_settleWindow, () {
+ _settleTimer = null;
+ if (!mounted || !_keyboardSettling) return;
+ setState(() => _keyboardSettling = false);
+ });
+ }
+
+ final settling = _settleTimer != null;
+ if (up == _keyboardUp &&
+ claim == _claimSpent &&
+ settling == _keyboardSettling) {
+ return;
+ }
setState(() {
_keyboardUp = up;
_claimSpent = claim;
+ _keyboardSettling = settling;
});
}
@@ -337,6 +404,11 @@ class _TerminalPageState extends State<TerminalPage>
// from taking it back after Back.
focused: widget.isActive && !_claimSpent,
visible: widget.isActive,
+ // Hold the renderer still while the keyboard
+ // slides. Separate from `visible` because this
+ // must NOT release focus — the animation being
+ // waited on is the one that focus started.
+ settling: _keyboardSettling,
showHeader: false,
// No composer, and so no grip above it: the
// page hands the pane its full height and the
diff --git a/mobile/lib/widgets/terminal_panel.dart b/mobile/lib/widgets/terminal_panel.dart
index a9f67922b..19928c0d5 100644
--- a/mobile/lib/widgets/terminal_panel.dart
+++ b/mobile/lib/widgets/terminal_panel.dart
@@ -54,6 +54,24 @@ class TerminalPanel extends StatefulWidget {
/// Only the focused grid tile may claim keyboard focus on mount/rebuild.
final bool focused;
final bool visible;
+
+ /// True while the software keyboard is mid-animation and the pane's height is
+ /// still a moving target.
+ ///
+ /// Freezes the renderer for the duration, WITHOUT touching focus — that is
+ /// the whole reason this is not just `visible: false`, which releases the
+ /// keyboard this animation is raising.
+ ///
+ /// ⚠️ What actually stutters is not painting, it is the RESIZE. Every frame
+ /// of the keyboard sliding gives the view a new height, xterm re-derives rows
+ /// from it in `performLayout` and fires `onResize` → `session.resize` → a
+ /// `terminal_resize` frame and a real SIGWINCH on the far machine. A full-screen
+ /// TUI redraws for each one, and those redraws come back as keyframes that
+ /// repaint the pane while it is still moving. `renderingEnabled: false` gates
+ /// both halves of that loop in the vendored renderer (see
+ /// `RenderTerminal._resizeTerminalIfNeeded`), so the shell is asked exactly
+ /// once, for the height the keyboard settles at.
+ final bool settling;
final Size? viewportSize;
/// A shared terminal can move to another harness without being remounted.
@@ -93,6 +111,7 @@ class TerminalPanel extends StatefulWidget {
required this.session,
required this.focused,
this.visible = true,
+ this.settling = false,
this.viewportSize,
this.paneLocation,
this.layoutRequest,
@@ -280,6 +299,17 @@ class _TerminalPanelState extends State<TerminalPanel>
(!oldWidget.visible || oldWidget.paneLocation != widget.paneLocation)) {
_afterTerminalMounted();
}
+ // The keyboard has finished moving and the pane's height is final. Measure
+ // it once and ask the shell for that size — the single SIGWINCH this whole
+ // gate exists to reduce the animation to.
+ //
+ // `claimFocus: false` on purpose: the keyboard is already up, or already
+ // gone, and whoever owns the caret decided that. Re-claiming here would
+ // pull it back from the composer, or summon the keyboard again just as the
+ // user finished dismissing it.
+ if (oldWidget.settling && !widget.settling && widget.visible) {
+ _afterTerminalMounted(claimFocus: false);
+ }
if (oldWidget.viewportSize != widget.viewportSize ||
oldWidget.layoutRequest != widget.layoutRequest) {
// Geometry can change while a resize handle or another control owns
@@ -337,6 +367,13 @@ class _TerminalPanelState extends State<TerminalPanel>
/// is `remote` (see the filter in `_loadMachines`), so `isRemote` is true for every pane,
/// including this very computer. What separates them is whether the machine's computerId is this
/// one, which is what puts it on the loopback transport.
+ /// Whether the renderer is allowed to paint and to resize the remote shell.
+ ///
+ /// A parked page stops both because nobody is looking; a settling one stops
+ /// them because its height is still moving. Focus is deliberately NOT part of
+ /// this — see [TerminalPanel.settling].
+ bool get _live => widget.visible && !widget.settling;
+
bool get _showsComposer {
final machineState = widget.notifier.stateOf(widget.session.machineId);
return machineState?.isLocalMachine != true && widget.composerVisible;
@@ -586,7 +623,9 @@ class _TerminalPanelState extends State<TerminalPanel>
if (!findEnabled) _clearFindHighlight();
final enabled =
mounted &&
- widget.visible &&
+ // `_live`, not `visible`: a cursor phase is a markNeedsPaint on the
+ // terminal, and nothing should repaint it while the keyboard slides.
+ _live &&
!widget.readOnly &&
_focusNode.hasFocus &&
widget.session.acceptsInput &&
@@ -1294,9 +1333,9 @@ class _TerminalPanelState extends State<TerminalPanel>
session.terminal,
key: _terminalViewKey,
controller: _controller,
- autoResize: widget.visible,
+ autoResize: _live,
resizeBuffer: false,
- renderingEnabled: widget.visible,
+ renderingEnabled: _live,
scrollController: _scrollController,
focusNode: _focusNode,
autofocus: widget.focused && !showComposer,