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 terminal tail following and swarm close zoom

deehw4d23d508a335
diff --git a/desktop/lib/widgets/pane_grid.dart b/desktop/lib/widgets/pane_grid.dart
index 7c0724773..3ea01d8ce 100644
--- a/desktop/lib/widgets/pane_grid.dart
+++ b/desktop/lib/widgets/pane_grid.dart
@@ -1157,6 +1157,7 @@ class _PaneContent extends StatelessWidget {
builder: (context, constraints) => TerminalPanel(
notifier: notifier,
session: session,
+ viewportSize: constraints.biggest,
focused: visible && notifier.isPaneFocused(pane.id),
focusRequest: notifier.isPaneFocused(pane.id)
? notifier.paneFocusRequest
diff --git a/desktop/lib/widgets/terminal_panel.dart b/desktop/lib/widgets/terminal_panel.dart
index 8fc941cb0..d965875f1 100644
--- a/desktop/lib/widgets/terminal_panel.dart
+++ b/desktop/lib/widgets/terminal_panel.dart
@@ -60,6 +60,7 @@ class TerminalPanel extends StatefulWidget {
/// Only the focused grid tile may claim keyboard focus on mount/rebuild.
final bool focused;
final bool visible;
+ final Size? viewportSize;
final bool compactHeader;
final int focusRequest;
@@ -85,6 +86,7 @@ class TerminalPanel extends StatefulWidget {
required this.session,
required this.focused,
this.visible = true,
+ this.viewportSize,
this.compactHeader = false,
this.focusRequest = 0,
this.composerVisible = false,
@@ -149,6 +151,7 @@ class _TerminalPanelState extends State<TerminalPanel>
String? _pressedLink;
bool _openingLink = false;
bool _linkRefreshPending = false;
+ bool _followTail = true;
bool _observingLinkModifiers = false;
late final RemoteMediaDownloader _mediaDownloader;
MediaDownloadCancellation? _previewCancellation;
@@ -161,7 +164,7 @@ class _TerminalPanelState extends State<TerminalPanel>
super.initState();
_viewTerminal = widget.session.terminal;
_viewTerminal.addListener(_scheduleLinkRefresh);
- _scrollController.addListener(_scheduleLinkRefresh);
+ _scrollController.addListener(_onScrollChanged);
_terminalViewKey = GlobalKey<TerminalViewState>();
_linkOpener = widget.linkOpener ?? TerminalLinkOpener();
_mediaDownloader = widget.mediaDownloader ?? RemoteMediaDownloader();
@@ -238,6 +241,7 @@ class _TerminalPanelState extends State<TerminalPanel>
_afterTerminalMounted();
}
if (oldWidget.visible && !widget.visible) {
+ _rememberFollowTail();
_focusNode.unfocus();
_composerFocus.unfocus();
_cancelDialInertia();
@@ -246,6 +250,12 @@ class _TerminalPanelState extends State<TerminalPanel>
_pressedLink = null;
_observeLinkModifiers(false);
}
+ if (!oldWidget.visible && widget.visible) {
+ _afterTerminalMounted(scrollToEnd: _followTail);
+ }
+ if (oldWidget.viewportSize != widget.viewportSize) {
+ _afterTerminalMounted(scrollToEnd: _followTail);
+ }
if (widget.focused &&
(!oldWidget.focused || oldWidget.focusRequest != widget.focusRequest)) {
_claimFocusAfterFrame();
@@ -266,7 +276,7 @@ class _TerminalPanelState extends State<TerminalPanel>
_tickerMode?.removeListener(_syncCursorBlink);
_previewCancellation?.cancel();
_viewTerminal.removeListener(_scheduleLinkRefresh);
- _scrollController.removeListener(_scheduleLinkRefresh);
+ _scrollController.removeListener(_onScrollChanged);
_observeLinkModifiers(false);
widget.session.setCursorBlinkPhase(true);
widget.session.removeListener(_onSessionChanged);
@@ -426,7 +436,8 @@ class _TerminalPanelState extends State<TerminalPanel>
)..addListener(_onFindChanged);
_find!.setQuery(_lastFindQuery, caseSensitive: _lastFindCaseSensitive);
}
- _afterTerminalMounted(scrollToEnd: false);
+ _followTail = atEnd;
+ _afterTerminalMounted(scrollToEnd: atEnd);
}
/// Typing in the composer focuses the tile, exactly like clicking into the terminal does.
@@ -554,12 +565,22 @@ class _TerminalPanelState extends State<TerminalPanel>
void _afterTerminalMounted({
bool clearSelection = false,
bool scrollToEnd = true,
+ int retries = 2,
}) {
WidgetsBinding.instance.addPostFrameCallback((_) {
if (!mounted || !widget.visible) return;
if (clearSelection) _controller.clearSelection();
final view = _laidOutTerminalView();
- if (view == null) return;
+ if (view == null) {
+ if (retries > 0) {
+ _afterTerminalMounted(
+ clearSelection: clearSelection,
+ scrollToEnd: scrollToEnd,
+ retries: retries - 1,
+ );
+ }
+ return;
+ }
final renderTerminal = view.renderTerminal;
final cellSize = renderTerminal.cellSize;
final renderSize = renderTerminal.size;
@@ -571,6 +592,7 @@ class _TerminalPanelState extends State<TerminalPanel>
}
if (scrollToEnd && _scrollController.hasClients) {
_scrollController.jumpTo(_scrollController.position.maxScrollExtent);
+ _followTail = true;
}
// Never over the composer: a rebuild that re-focuses this tile while someone is typing into
// the box would pull the caret out from under them mid-sentence.
@@ -933,6 +955,17 @@ class _TerminalPanelState extends State<TerminalPanel>
});
}
+ void _rememberFollowTail() {
+ if (!_scrollController.hasClients) return;
+ final position = _scrollController.position;
+ _followTail = position.maxScrollExtent - position.pixels < 1;
+ }
+
+ void _onScrollChanged() {
+ _rememberFollowTail();
+ _scheduleLinkRefresh();
+ }
+
String? _linkAtPointer(Offset globalPosition) {
final view = _laidOutTerminalView();
if (view == null) return null;
diff --git a/desktop/macos/Runner/SwarmTitlebar.swift b/desktop/macos/Runner/SwarmTitlebar.swift
index 3b914304f..bb7f27ce8 100644
--- a/desktop/macos/Runner/SwarmTitlebar.swift
+++ b/desktop/macos/Runner/SwarmTitlebar.swift
@@ -666,6 +666,7 @@ private final class SwarmTabStrip: NSView {
private var revealActiveAfterLayout = false
private var tabOrderChanged = false
private var actionsEnabled = false
+ private var lastBackgroundClick: (time: TimeInterval, point: NSPoint)?
override var mouseDownCanMoveWindow: Bool { true }
override init(frame: NSRect) {
@@ -801,8 +802,22 @@ private final class SwarmTabStrip: NSView {
}
override func mouseDown(with event: NSEvent) {
- if event.clickCount == 2 { window?.performZoom(nil) }
- else { window?.performDrag(with: event) }
+ if ownsBackgroundDoubleClick(event) { window?.performZoom(nil) }
+ else if event.clickCount == 1 { window?.performDrag(with: event) }
+ }
+ fileprivate func ownsBackgroundDoubleClick(_ event: NSEvent) -> Bool {
+ if event.clickCount == 1 {
+ lastBackgroundClick = (event.timestamp, event.locationInWindow)
+ return false
+ }
+ guard event.clickCount == 2, let first = lastBackgroundClick else {
+ lastBackgroundClick = nil
+ return false
+ }
+ lastBackgroundClick = nil
+ return event.timestamp - first.time <= NSEvent.doubleClickInterval &&
+ hypot(event.locationInWindow.x - first.point.x,
+ event.locationInWindow.y - first.point.y) <= 4
}
@objc private func newSwarm() {
if actionsEnabled && newButton.isEnabled { emit?("new", nil) }
diff --git a/desktop/test/terminal_initial_output_test.dart b/desktop/test/terminal_initial_output_test.dart
index db2cfc24d..1b43e4990 100644
--- a/desktop/test/terminal_initial_output_test.dart
+++ b/desktop/test/terminal_initial_output_test.dart
@@ -153,4 +153,54 @@ void main() {
},
);
}
+ testWidgets(
+ 'a fresh hidden terminal follows its keyframe when its swarm is opened',
+ (tester) async {
+ final app = createApp();
+ final session = terminal('a0', []);
+ app.adoptSessionForTest(session);
+ final original = app.activeSwarmId;
+ await mount(tester, app);
+ app.newSwarm();
+ await tester.pump();
+ final history = List.generate(500, (i) => 'Output line $i\r\n').join();
+ await output(session, 0, history, keyframe: true);
+ await tester.pump();
+ app.selectSwarm(original);
+ await tester.pump();
+ final scroll = terminalView(tester, session).widget.scrollController!;
+ expect(scroll.position.maxScrollExtent, greaterThan(0));
+ expect(scroll.offset, scroll.position.maxScrollExtent);
+ await tester.pumpWidget(const SizedBox());
+ app.dispose();
+ },
+ );
+
+ testWidgets('a relayout keeps tail-following panes at latest output', (
+ tester,
+ ) async {
+ final app = createApp();
+ final sessions = [for (var i = 0; i < 4; i++) terminal('a$i', [])];
+ final history = List.generate(500, (i) => 'Output line $i\r\n').join();
+ for (final session in sessions) {
+ session.terminal.write(history);
+ app.adoptSessionForTest(session);
+ }
+ await mount(tester, app);
+ final reading = terminalView(
+ tester,
+ sessions.first,
+ ).widget.scrollController!;
+ reading.jumpTo(100);
+ await tester.pump();
+ app.setPreset(4, PanePreset.rows);
+ await tester.pump();
+ expect(reading.offset, 100);
+ for (final session in sessions.skip(1)) {
+ final scroll = terminalView(tester, session).widget.scrollController!;
+ expect(scroll.offset, scroll.position.maxScrollExtent);
+ }
+ await tester.pumpWidget(const SizedBox());
+ app.dispose();
+ });
}
diff --git a/desktop/tool/swarm_titlebar_checks.swift b/desktop/tool/swarm_titlebar_checks.swift
index 8fa8b2786..40d57c008 100644
--- a/desktop/tool/swarm_titlebar_checks.swift
+++ b/desktop/tool/swarm_titlebar_checks.swift
@@ -77,6 +77,17 @@ private extension SwarmTabStrip {
update(state(rows, active: "swarm-11"))
let hover = NSEvent.mouseEvent(with: .mouseMoved, location: .zero, modifierFlags: [],
timestamp: 0, windowNumber: 0, context: nil, eventNumber: 0, clickCount: 0, pressure: 0)!
+ func click(_ count: Int, at point: NSPoint, time: TimeInterval) -> NSEvent {
+ NSEvent.mouseEvent(with: .leftMouseDown, location: point, modifierFlags: [],
+ timestamp: time, windowNumber: 0, context: nil, eventNumber: count,
+ clickCount: count, pressure: 1)!
+ }
+ try checkTitlebar(!ownsBackgroundDoubleClick(click(2, at: NSPoint(x: 40, y: 20), time: 1)),
+ "A second click displaced from a closed tab cannot zoom the window")
+ try checkTitlebar(!ownsBackgroundDoubleClick(click(1, at: NSPoint(x: 40, y: 20), time: 2)),
+ "The first titlebar-background click starts a local sequence")
+ try checkTitlebar(ownsBackgroundDoubleClick(click(2, at: NSPoint(x: 40, y: 20), time: 2.1)),
+ "A double-click wholly on titlebar background still zooms the window")
try checkTitlebar(tabs[0].showsDivider && tabs[1].showsDivider, "Idle neighboring tabs have separators")
tabs[1].mouseEntered(with: hover)
try checkTitlebar(!tabs[0].showsDivider && !tabs[1].showsDivider, "Hover clears separators on both sides of the tab")
@@ -473,8 +484,10 @@ private extension SwarmTitlebar {
try checkTitlebar(!validateMenuItem(create), "Native New Swarm respects the tab capacity")
canCreateSwarm = true
try checkTitlebar(validateMenuItem(create), "Native New Swarm returns below capacity")
- let recentRows: [[String: Any]] = (0..<20).map {
- ["id": "agent:\($0)", "title": "Agent \($0) — Machine", "detail": "Project \($0)", "current": $0 == 0, "engine": $0 == 0 ? "claude" : "codex"]
+ let recentRows: [[String: Any]] = (0..<20).map { index -> [String: Any] in
+ ["id": "agent:\(index)", "title": "Agent \(index) — Machine",
+ "detail": "Project \(index)", "current": index == 0,
+ "engine": index == 0 ? "claude" : "codex"]
} + [["id": "swarm:recent", "title": "Recent Swarm", "swarm": true]]
let closedRows: [[String: Any]] = (0..<14).map {
["id": "closed-\($0)", "title": "Closed Swarm \($0)", "detail": "3 agents", "swarm": true, "canReopen": true]