fix: debounce resize and enlarge broadcast buffer for TUI resize stability

This commit is contained in:
2026-08-01 21:10:23 +08:00
parent 1ab485dffe
commit d1cd33de07
4 changed files with 116 additions and 12 deletions

View File

@@ -31,6 +31,11 @@ class _TerminalScreenState extends State<TerminalScreen> {
StreamSubscription<Uint8List>? _engineOutputSub;
StreamSubscription<Uint8List>? _backendOutputSub;
/// resize 防抖:窗口拖动时 onPtyResize 会高频触发,合并为最后一次。
Timer? _resizeDebounce;
int _pendingCols = 0;
int _pendingRows = 0;
final _host = TextEditingController(text: '127.0.0.1');
final _port = TextEditingController(text: '8080');
final _session = TextEditingController();
@@ -101,6 +106,8 @@ class _TerminalScreenState extends State<TerminalScreen> {
void _disconnect() => _tearDownSession();
void _tearDownSession() {
_resizeDebounce?.cancel();
_resizeDebounce = null;
_engineOutputSub?.cancel();
_engineOutputSub = null;
_backendOutputSub?.cancel();
@@ -121,15 +128,22 @@ class _TerminalScreenState extends State<TerminalScreen> {
}
void _handlePtyResize(int cols, int rows) {
final b = _backend;
if (b == null) return;
b.resize(rows, cols);
if (mounted) {
setState(() {
_cols = cols;
_rows = rows;
});
}
if (!mounted) return;
setState(() {
_cols = cols;
_rows = rows;
});
// 合并连续 resize只发送最后一次避免 TUI 频繁 SIGWINCH 重绘错乱。
_pendingCols = cols;
_pendingRows = rows;
_resizeDebounce?.cancel();
_resizeDebounce = Timer(const Duration(milliseconds: 80), () {
final b = _backend;
if (b == null) return;
// PtyBackend.resize(rows, columns) — 注意参数顺序为行/列。
b.resize(_pendingRows, _pendingCols);
});
}
@override