浏览代码

replace [jumpTo] with [correctPixels] to reduce overhead

xuty 4 年之前
父节点
当前提交
d050950e3e
共有 1 个文件被更改,包括 22 次插入7 次删除
  1. 22 7
      lib/frontend/terminal_view.dart

+ 22 - 7
lib/frontend/terminal_view.dart

@@ -97,20 +97,32 @@ class _TerminalViewState extends State<TerminalView> {
     return widget.focusNode.hasFocus;
   }
 
-  int? _lastTerminalWidth;
-  int? _lastTerminalHeight;
-
   late CellSize _cellSize;
 
   void onTerminalChange() {
+    if (!mounted) {
+      return;
+    }
+
     final currentScrollExtent =
         _cellSize.cellHeight * widget.terminal.buffer.scrollOffsetFromTop;
 
-    widget.scrollController.jumpTo(currentScrollExtent);
-
-    if (mounted) {
-      setState(() {});
+    final maxScrollExtent = widget.scrollController.position.maxScrollExtent;
+
+    if (currentScrollExtent > maxScrollExtent) {
+      /// Ensure [maxScrollExtent] is larger than [currentScrollExtent] so
+      /// [currentScrollExtent] won't be limited.
+      ///
+      /// Calling [applyContentDimensions] has unnecessary cost, and the most
+      /// ideal way is to set [scrollController.position._maxScrollExtend]
+      /// directly, however this requires modifying flutter code.
+      widget.scrollController.position
+          .applyContentDimensions(0.0, currentScrollExtent);
     }
+
+    widget.scrollController.position.correctPixels(currentScrollExtent);
+
+    setState(() {});
   }
 
   // listen to oscillator to update mouse blink etc.
@@ -253,6 +265,9 @@ class _TerminalViewState extends State<TerminalView> {
     return Position(x, y);
   }
 
+  int? _lastTerminalWidth;
+  int? _lastTerminalHeight;
+
   void onResize(double width, double height) {
     final termWidth = (width / _cellSize.cellWidth).floor();
     final termHeight = (height / _cellSize.cellHeight).floor();