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: throttle background terminal repaints

0xdiego250ed2ec1d8730
diff --git a/desktop/lib/widgets/pane_grid.dart b/desktop/lib/widgets/pane_grid.dart
index 4c437a66f..c73f4be09 100644
--- a/desktop/lib/widgets/pane_grid.dart
+++ b/desktop/lib/widgets/pane_grid.dart
@@ -1332,6 +1332,20 @@ class _PaneContent extends StatelessWidget {
// Availability changes the header and input permission, never the renderer's
// ancestry. Retained output, selection, scroll and Find stay in this view.
if (session != null) {
+ // A software renderer (notably WSLg's llvmpipe) cannot keep up when
+ // every visible terminal schedules a full text layout for every output
+ // chunk. Keep the active tile realtime and coalesce background output
+ // only once the grid has three terminal panes. The buffer remains live,
+ // so the next paint contains every intervening chunk.
+ final terminalPaneCount = notifier.panes
+ .where((candidate) => candidate.session != null)
+ .length;
+ final throttleBackgroundOutput =
+ swarmMode &&
+ visible &&
+ !notifier.isPaneFocused(pane.id) &&
+ notifier.zoomedPaneId == null &&
+ terminalPaneCount >= 3;
final TerminalNotice? notice;
if (machine == null) {
notice = (
@@ -1384,6 +1398,9 @@ class _PaneContent extends StatelessWidget {
notifier.zoomedPaneId,
),
focused: visible && notifier.isPaneFocused(pane.id),
+ outputRepaintInterval: throttleBackgroundOutput
+ ? const Duration(milliseconds: 80)
+ : null,
focusRequest: notifier.isPaneFocused(pane.id)
? notifier.paneFocusRequest
: 0,
diff --git a/desktop/lib/widgets/terminal_panel.dart b/desktop/lib/widgets/terminal_panel.dart
index b2fbbadc4..481bd3ed1 100644
--- a/desktop/lib/widgets/terminal_panel.dart
+++ b/desktop/lib/widgets/terminal_panel.dart
@@ -63,6 +63,9 @@ class TerminalPanel extends StatefulWidget {
/// Only the focused grid tile may claim keyboard focus on mount/rebuild.
final bool focused;
final bool visible;
+
+ /// Coalesces streaming output for an unfocused tile without delaying input.
+ final Duration? outputRepaintInterval;
final Size? viewportSize;
/// A shared terminal can move to another tab without being remounted.
@@ -102,6 +105,7 @@ class TerminalPanel extends StatefulWidget {
required this.session,
required this.focused,
this.visible = true,
+ this.outputRepaintInterval,
this.viewportSize,
this.paneLocation,
this.layoutRequest,
@@ -1263,6 +1267,7 @@ class _TerminalPanelState extends State<TerminalPanel>
autoResize: widget.visible && !session.readOnly,
resizeBuffer: false,
renderingEnabled: widget.visible,
+ outputRepaintInterval: widget.outputRepaintInterval,
scrollController: _scrollController,
focusNode: _focusNode,
autofocus: widget.focused && !showComposer,
diff --git a/desktop/test/terminal_render_suspension_test.dart b/desktop/test/terminal_render_suspension_test.dart
index 6988263dd..9c66230e8 100644
--- a/desktop/test/terminal_render_suspension_test.dart
+++ b/desktop/test/terminal_render_suspension_test.dart
@@ -63,6 +63,42 @@ Future<Color> _firstCellColor(
}
void main() {
+ testWidgets('background terminal output is coalesced before repainting', (
+ tester,
+ ) async {
+ final buffer = Terminal()..write('\x1b[41m \x1b[0m');
+ final interval = ValueNotifier<Duration?>(const Duration(milliseconds: 80));
+ await tester.pumpWidget(
+ MaterialApp(
+ home: RepaintBoundary(
+ key: const ValueKey('terminal pixels'),
+ child: ValueListenableBuilder<Duration?>(
+ valueListenable: interval,
+ builder: (_, value, _) =>
+ TerminalView(buffer, outputRepaintInterval: value),
+ ),
+ ),
+ ),
+ );
+ await tester.pump();
+ final view = tester.state<TerminalViewState>(find.byType(TerminalView));
+ expect(await _firstCellColor(tester, view), view.widget.theme.red);
+
+ buffer.write('\x1b[H\x1b[42m \x1b[0m');
+ expect(view.renderTerminal.debugNeedsLayout, isFalse);
+ expect(tester.binding.hasScheduledFrame, isFalse);
+ expect(await _firstCellColor(tester, view), view.widget.theme.red);
+
+ // Focusing this tile removes the coalescing delay and paints output that
+ // arrived while it was in the background without waiting for another
+ // terminal chunk.
+ interval.value = null;
+ await tester.pump();
+ expect(await _firstCellColor(tester, view), view.widget.theme.green);
+ await tester.pumpWidget(const SizedBox());
+ interval.dispose();
+ });
+
testWidgets(
'hidden output does no rendering and reveals the latest on return',
(tester) async {
diff --git a/desktop/third_party/xterm/lib/src/terminal_view.dart b/desktop/third_party/xterm/lib/src/terminal_view.dart
index 66760f36d..621e3e051 100644
--- a/desktop/third_party/xterm/lib/src/terminal_view.dart
+++ b/desktop/third_party/xterm/lib/src/terminal_view.dart
@@ -35,6 +35,7 @@ class TerminalView extends StatefulWidget {
this.autoResize = true,
this.resizeBuffer = true,
this.renderingEnabled = true,
+ this.outputRepaintInterval,
this.backgroundOpacity = 1,
this.focusNode,
this.autofocus = false,
@@ -90,6 +91,10 @@ class TerminalView extends StatefulWidget {
/// An enclosing disabled [TickerMode] also suspends rendering updates.
final bool renderingEnabled;
+ /// Coalesces terminal-output repaints to at most one per interval. Input and
+ /// scrolling remain immediate. Null keeps every output update responsive.
+ final Duration? outputRepaintInterval;
+
/// Opacity of the terminal background. Set to 0 to make the terminal
/// background transparent.
final double backgroundOpacity;
@@ -275,6 +280,7 @@ class TerminalViewState extends State<TerminalView> {
autoResize: widget.autoResize,
resizeBuffer: widget.resizeBuffer,
renderingEnabled: widget.renderingEnabled,
+ outputRepaintInterval: widget.outputRepaintInterval,
textStyle: widget.textStyle,
textScaler: widget.textScaler ?? MediaQuery.textScalerOf(context),
theme: widget.theme,
@@ -662,6 +668,7 @@ class _TerminalView extends LeafRenderObjectWidget {
required this.autoResize,
required this.resizeBuffer,
required this.renderingEnabled,
+ this.outputRepaintInterval,
required this.textStyle,
required this.textScaler,
required this.theme,
@@ -687,6 +694,8 @@ class _TerminalView extends LeafRenderObjectWidget {
final bool renderingEnabled;
+ final Duration? outputRepaintInterval;
+
final TerminalStyle textStyle;
final TextScaler textScaler;
@@ -716,6 +725,7 @@ class _TerminalView extends LeafRenderObjectWidget {
resizeBuffer: resizeBuffer,
renderingEnabled:
renderingEnabled && TickerMode.valuesOf(context).enabled,
+ outputRepaintInterval: outputRepaintInterval,
textStyle: textStyle,
textScaler: textScaler,
theme: theme,
@@ -733,6 +743,7 @@ class _TerminalView extends LeafRenderObjectWidget {
renderObject
..renderingEnabled =
renderingEnabled && TickerMode.valuesOf(context).enabled
+ ..outputRepaintInterval = outputRepaintInterval
..terminal = terminal
..controller = controller
..offset = offset
diff --git a/desktop/third_party/xterm/lib/src/ui/render.dart b/desktop/third_party/xterm/lib/src/ui/render.dart
index 4ed9ef71c..28e0d9d56 100644
--- a/desktop/third_party/xterm/lib/src/ui/render.dart
+++ b/desktop/third_party/xterm/lib/src/ui/render.dart
@@ -1,3 +1,4 @@
+import 'dart:async';
import 'dart:math' show max;
import 'package:flutter/rendering.dart';
import 'package:flutter/scheduler.dart';
@@ -27,6 +28,7 @@ class RenderTerminal extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
required bool autoResize,
bool resizeBuffer = true,
bool renderingEnabled = true,
+ Duration? outputRepaintInterval,
required TerminalStyle textStyle,
required TextScaler textScaler,
required TerminalTheme theme,
@@ -43,6 +45,7 @@ class RenderTerminal extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
_autoResize = autoResize,
_resizeBuffer = resizeBuffer,
_renderingEnabled = renderingEnabled,
+ _outputRepaintInterval = outputRepaintInterval,
_focusNode = focusNode,
_cursorType = cursorType,
_alwaysShowCursor = alwaysShowCursor,
@@ -112,6 +115,10 @@ class RenderTerminal extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
set renderingEnabled(bool value) {
if (value == _renderingEnabled) return;
_renderingEnabled = value;
+ if (!value) {
+ _outputRepaintTimer?.cancel();
+ _outputRepaintTimer = null;
+ }
if (attached) {
if (value) {
_terminal.addListener(_onTerminalChange);
@@ -124,6 +131,19 @@ class RenderTerminal extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
if (value) markNeedsLayout();
}
+ Duration? _outputRepaintInterval;
+ Timer? _outputRepaintTimer;
+
+ set outputRepaintInterval(Duration? value) {
+ if (value == _outputRepaintInterval) return;
+ _outputRepaintInterval = value;
+ _outputRepaintTimer?.cancel();
+ _outputRepaintTimer = null;
+ // A focused tile resumes immediately even if its former background timer
+ // was waiting; there may be no later output chunk to wake it up.
+ if (value == null && attached && _renderingEnabled) markNeedsLayout();
+ }
+
set textStyle(TerminalStyle value) {
if (value == _painter.textStyle) return;
_painter.textStyle = value;
@@ -227,6 +247,17 @@ class RenderTerminal extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
}
void _onTerminalChange() {
+ final interval = _outputRepaintInterval;
+ if (interval != null && interval > Duration.zero) {
+ if (_outputRepaintTimer?.isActive ?? false) return;
+ _outputRepaintTimer = Timer(interval, () {
+ _outputRepaintTimer = null;
+ if (!attached || !_renderingEnabled) return;
+ markNeedsLayout();
+ _scheduleEditableRect();
+ });
+ return;
+ }
markNeedsLayout();
_scheduleEditableRect();
}
@@ -249,6 +280,8 @@ class RenderTerminal extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
@override
void detach() {
+ _outputRepaintTimer?.cancel();
+ _outputRepaintTimer = null;
super.detach();
_offset.removeListener(_onScroll);
_terminal.removeListener(_onTerminalChange);