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.

fix(mobile): hide the keyboard when swiping between agents

tony5dd70f2ba68d
diff --git a/mobile/lib/phone/agent_swipe.dart b/mobile/lib/phone/agent_swipe.dart
index 3656b0dbf..618c911c5 100644
--- a/mobile/lib/phone/agent_swipe.dart
+++ b/mobile/lib/phone/agent_swipe.dart
@@ -222,38 +222,53 @@ class _AgentSwipeHostState extends State<AgentSwipeHost> {
isActive: true,
);
}
- return PageView.builder(
- controller: controller,
- // The terminal below scrolls vertically and selects text only with a mouse, so the horizontal
- // axis is free — and here it is the pager's ALONE. The route's own edge-swipe back used to
- // compete for it and win at the left margin, being registered above this in the tree: a drag
- // started near the edge to reach the previous AGENT left the screen instead. The route is
- // pushed without that gesture now (see `phoneRoute`'s `swipeToGoBack`), so going back is the
- // header's back band, or Android's back button.
- physics: const PageScrollPhysics(),
- // No count is what makes it endless: the builder answers for any page, and the modulo below
- // wraps it back onto the list. A one-agent list keeps its single page instead — see
- // [AgentSwipeList.wraps].
- itemCount: neighbours.wraps ? null : neighbours.entries.length,
- onPageChanged: _onPageChanged,
- itemBuilder: (context, i) {
- final entry = neighbours.entries[i % neighbours.entries.length];
- return TerminalPage(
- // ⚠️ Keyed by PAGE, not by agent, and the difference only shows once the pager wraps: an
- // endless run holds several pages for the same agent — the lap before and the lap after —
- // and an agent-shaped key would make Flutter treat two live pages as one widget, which
- // throws on a duplicate key the moment both are mounted. Each page then learns its own
- // `_hadPane`, which is what that flag wants anyway: it is about this page's attach, not
- // about the agent.
- key: ValueKey(i),
- notifier: widget.notifier,
- machineId: entry.machineId,
- agentId: entry.agent.id,
- voice: _voice,
- // Exactly one mounted page, by page number — see [_page].
- isActive: i == _page,
- );
+ // ⚠️ **The keyboard goes at the START of the drag, not at [_onPageChanged].**
+ // That callback fires at the halfway point of a SETTLED swipe, so a keyboard
+ // dismissed there would sit over the terminal for the whole gesture and then
+ // vanish once the new agent had already arrived — and it would never go at
+ // all for a drag that was pulled part way and let go.
+ //
+ // [ScrollStartNotification] is the first frame of the finger's movement,
+ // which is the moment the person has shown they are leaving this terminal.
+ return NotificationListener<ScrollStartNotification>(
+ onNotification: (_) {
+ dismissKeyboardForSwipe();
+ // Let it bubble: the pager's own scroll machinery is listening too.
+ return false;
},
+ child: PageView.builder(
+ controller: controller,
+ // The terminal below scrolls vertically and selects text only with a mouse, so the horizontal
+ // axis is free — and here it is the pager's ALONE. The route's own edge-swipe back used to
+ // compete for it and win at the left margin, being registered above this in the tree: a drag
+ // started near the edge to reach the previous AGENT left the screen instead. The route is
+ // pushed without that gesture now (see `phoneRoute`'s `swipeToGoBack`), so going back is the
+ // header's back band, or Android's back button.
+ physics: const PageScrollPhysics(),
+ // No count is what makes it endless: the builder answers for any page, and the modulo below
+ // wraps it back onto the list. A one-agent list keeps its single page instead — see
+ // [AgentSwipeList.wraps].
+ itemCount: neighbours.wraps ? null : neighbours.entries.length,
+ onPageChanged: _onPageChanged,
+ itemBuilder: (context, i) {
+ final entry = neighbours.entries[i % neighbours.entries.length];
+ return TerminalPage(
+ // ⚠️ Keyed by PAGE, not by agent, and the difference only shows once the pager wraps: an
+ // endless run holds several pages for the same agent — the lap before and the lap after —
+ // and an agent-shaped key would make Flutter treat two live pages as one widget, which
+ // throws on a duplicate key the moment both are mounted. Each page then learns its own
+ // `_hadPane`, which is what that flag wants anyway: it is about this page's attach, not
+ // about the agent.
+ key: ValueKey(i),
+ notifier: widget.notifier,
+ machineId: entry.machineId,
+ agentId: entry.agent.id,
+ voice: _voice,
+ // Exactly one mounted page, by page number — see [_page].
+ isActive: i == _page,
+ );
+ },
+ ),
);
}
@@ -268,6 +283,12 @@ class _AgentSwipeHostState extends State<AgentSwipeHost> {
// and finds none.
_attached.add(arrived);
widget.notifier.lastOpenedAgent.remember(arrived);
+ // ⚠️ A second dismissal, and not a redundant one. The [ScrollStartNotification] above catches
+ // the finger, which is the usual way here and the one that matters for how it looks — but a page
+ // reached any other way never raised that notification, and the incoming terminal would claim
+ // the keyboard the moment it built. Landing on a new agent is the invariant; the drag is only
+ // where it is felt. Both are cheap: clearing a flag and unfocusing what is already unfocused.
+ dismissKeyboardForSwipe();
// Before the setState, so a host that rebuilds this pager in response already names the agent
// swiped to — told afterwards, it would rebuild still pointing at the previous one.
widget.onAgentChanged?.call(arrived);
diff --git a/mobile/lib/phone/terminal_page.dart b/mobile/lib/phone/terminal_page.dart
index d152daf75..53cc9b2e5 100644
--- a/mobile/lib/phone/terminal_page.dart
+++ b/mobile/lib/phone/terminal_page.dart
@@ -83,6 +83,23 @@ class TerminalPage extends StatefulWidget {
/// inset, so they all write the same value.
bool _keyboardIsUp = false;
+/// Set while a swipe has asked the keyboard to go and the platform has not
+/// finished taking it away.
+///
+/// ⚠️ **Without this, dismissing on a swipe silently does nothing.** The
+/// keyboard leaves over an animation, so for the ~250ms after `unfocus()` the
+/// inset is still above zero — and every mounted page's [didChangeMetrics] is
+/// firing on every frame of that animation, each one writing `_keyboardIsUp =
+/// true` again. The page being swiped to then reads the flag it was supposed to
+/// have lost, passes `focused: true` to [TerminalPanel], and the panel's
+/// `didUpdateWidget` claims the input connection back. The keyboard never goes,
+/// and nothing in the code looks wrong.
+///
+/// So the writes are held off until the inset actually reaches zero, which is
+/// the platform confirming the keyboard is gone. From that tick on, the flag
+/// tracks the truth again as it always did.
+bool _keyboardDismissing = false;
+
/// Forgets what the keyboard did during the last run of terminal pages.
///
/// Called when a pager opens. A pager popped with the keyboard up is disposed
@@ -90,6 +107,22 @@ bool _keyboardIsUp = false;
/// pager would otherwise open believing the keyboard is up, and summon it.
void resetKeyboardSession() {
_keyboardIsUp = false;
+ _keyboardDismissing = false;
+}
+
+/// Puts the keyboard away for a swipe between agents, and keeps it away.
+///
+/// ⚠️ **Dropping focus alone does nothing here, and neither does clearing the
+/// flag.** [_keyboardIsUp] exists to HOLD the keyboard across a swipe — that is
+/// what it was written for — so the incoming page reads it and claims the
+/// keyboard straight back. Clearing it is therefore necessary, but not enough on
+/// its own: the inset is still falling, and the metrics ticks of that fall put
+/// it back. [_keyboardDismissing] is what makes the clear stick until the
+/// platform agrees.
+void dismissKeyboardForSwipe() {
+ _keyboardIsUp = false;
+ _keyboardDismissing = true;
+ FocusManager.instance.primaryFocus?.unfocus();
}
class _TerminalPageState extends State<TerminalPage>
@@ -198,7 +231,16 @@ class _TerminalPageState extends State<TerminalPage>
final up = inset > 0;
// Every mounted page writes it, and they all see the same inset — so a page
// that was parked while the keyboard came and went still reads the truth.
- _keyboardIsUp = up;
+ //
+ // ⚠️ Except while a swipe is putting the keyboard away: the inset is still
+ // falling then, and writing `true` from those ticks is exactly what used to
+ // undo the dismissal. See [_keyboardDismissing]. Zero is the platform
+ // saying the keyboard has finished leaving, which ends the hold-off.
+ if (_keyboardDismissing) {
+ if (!up) _keyboardDismissing = false;
+ } else {
+ _keyboardIsUp = up;
+ }
// The FIRST frame of the keyboard rising spends the request — it need not
// finish. Spending it this early is the point: it is off long before any
// Back press can arrive.