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.

9.1 KB
Raw Blame
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';

/// A chat's scroll: what the reader looks at never moves by itself.
///
/// Rows change height after they are laid out (a receipt line moves to the
/// newest own message or grows a note, a picture or code block finishes,
/// a page of older messages arrives above). A list keeps its offset, so all
/// of that would slide the rows on screen. Instead, at every layout the
/// first visible row is kept where it was, and a reader at the very end
/// stays at the end. Both happen during layout, before anything is painted.
class SuperTabChatScroll extends ScrollController {
  SuperTabChatScroll({required this.rows});

  /// The list's rows that are built, by message id.
  final Iterable<MapEntry<String, BuildContext>> Function() rows;

  @override
  ScrollPosition createScrollPosition(
    ScrollPhysics physics,
    ScrollContext context,
    ScrollPosition? oldPosition,
  ) => _AnchoredPosition(
    rows: rows,
    holdsEnd: () => _holdingFor != null,
    physics: physics,
    context: context,
    initialPixels: initialScrollOffset,
    keepScrollOffset: keepScrollOffset,
    oldPosition: oldPosition,
    debugLabel: debugLabel,
  );

  /// The list's dividing row moved [by] down the rows: the view and the
  /// place it keeps move with them, so nothing on screen moves.
  void moveContent(double by) {
    for (final position in positions) {
      if (position is _AnchoredPosition) position._moveContent(by);
    }
  }

  /// The list was rebuilt around a new dividing row: the view starts at it
  /// and keeps nothing of where it was. Set before the list is laid out, so
  /// no row between the old place and the new one is laid out on the way.
  void restart() {
    for (final position in positions) {
      if (position is _AnchoredPosition) position._restart();
    }
  }

  /// A jump to the end, named by [jump], is under way: a view left past the
  /// end, as an end estimated from rows not yet laid out leaves it, is put
  /// at the end before anything is painted. Without it a Mac's bouncing
  /// scroll shows no row until the view springs back (Huy, #3049). Null
  /// ends any jump, as the reader taking the list does.
  void holdEnd(int? jump) => _holdingFor = jump;

  /// Ends the hold of [jump], unless a later jump holds the end now.
  void releaseEnd(int jump) {
    if (_holdingFor == jump) _holdingFor = null;
  }

  int? _holdingFor;
}

class _AnchoredPosition extends ScrollPositionWithSingleContext {
  _AnchoredPosition({
    required this.rows,
    required this.holdsEnd,
    required super.physics,
    required super.context,
    super.initialPixels,
    super.keepScrollOffset,
    super.oldPosition,
    super.debugLabel,
  });

  final Iterable<MapEntry<String, BuildContext>> Function() rows;

  // The first visible row and its top within the content, as last laid out.
  ({String messageId, double top})? _anchor;
  // Where the last finished layout left the view.
  double? _settledPixels, _settledMax;

  /// Whether a jump to the end is under way ([SuperTabChatScroll.holdEnd]):
  /// asked of the controller, which may be told before this is attached.
  final bool Function() holdsEnd;

  bool get _wasAtEnd =>
      _settledPixels != null && _settledPixels! >= _settledMax! - .5;

  // Whether the reader is pulling the list: a drag, and the fling or bounce
  // that follows it. Only they take the view past the end of the list.
  bool _pulled = false;

  @override
  void beginActivity(ScrollActivity? newActivity) {
    if (newActivity is DragScrollActivity) {
      _pulled = true;
    } else if (newActivity is! BallisticScrollActivity) {
      _pulled = false;
    }
    super.beginActivity(newActivity);
  }

  @override
  void correctBy(double correction) {
    // The list moved every row and the view together: nothing on screen
    // moved, so the kept place moves with them.
    super.correctBy(correction);
    if (_anchor case final anchor?) {
      _anchor = (messageId: anchor.messageId, top: anchor.top + correction);
    }
    if (_settledPixels != null) _settledPixels = _settledPixels! + correction;
  }

  void _moveContent(double by) {
    correctPixels(pixels + by);
    if (_anchor case final anchor?) {
      _anchor = (messageId: anchor.messageId, top: anchor.top + by);
    }
    if (_settledPixels != null) _settledPixels = _settledPixels! + by;
    if (_settledMax != null) _settledMax = _settledMax! + by;
  }

  void _restart() {
    goIdle();
    correctPixels(0);
    _anchor = null;
    _settledPixels = _settledMax = null;
  }

  @override
  bool applyContentDimensions(double minScrollExtent, double maxScrollExtent) {
    if (holdsEnd() && pixels > maxScrollExtent + .5) {
      // Past the end only because the end was estimated: laid out there,
      // the list knows its end exactly.
      goIdle();
      correctPixels(maxScrollExtent);
      return false;
    }
    // Nothing the chat does leaves the view past the end of the list: the
    // newest row shrinking under a kept place, or a jump to a row, would show
    // blank under it until a Mac's bouncing scroll sprang back, every few
    // seconds with no input (Scholes, #3124). Before anything is painted.
    var target = _keptPixels(maxScrollExtent);
    if (!_pulled && (target ?? pixels) > maxScrollExtent) {
      target = maxScrollExtent;
    }
    if (target != null && (target - pixels).abs() > .5) {
      correctPixels(target);
      return false;
    }
    if (!super.applyContentDimensions(minScrollExtent, maxScrollExtent)) {
      return false;
    }
    _settledPixels = pixels;
    _settledMax = maxScrollExtent;
    _anchor = _firstVisible();
    return true;
  }

  /// Where the view belongs now, or null to leave it.
  double? _keptPixels(double maxScrollExtent) {
    // Only the content changed since the reader was at the end.
    if (_wasAtEnd &&
        pixels == _settledPixels &&
        maxScrollExtent != _settledMax) {
      return maxScrollExtent;
    }
    final anchor = _anchor;
    if (anchor == null) return null;
    final edge = _rowEdges()[anchor.messageId]?.at;
    if (edge == null) return null;
    // Measured in the content, so the reader's own scrolling never counts:
    // only the rows between it and the list's dividing row changing does.
    final now = pixels + edge;
    if ((now - anchor.top).abs() <= .5) return null;
    _anchor = (messageId: anchor.messageId, top: now);
    return pixels + now - anchor.top;
  }

  /// The row at the top of the view, by the edge [_rowEdges] measures.
  ({String messageId, double top})? _firstVisible() {
    ({String messageId, double at})? first;
    for (final MapEntry(key: id, value: edge) in _rowEdges().entries) {
      final better = switch (edge) {
        // Above the dividing row (only in view when the view's top is):
        // the row whose bottom is the first below the top of the view.
        (above: true, :final at) =>
          pixels < 0 && at > 0 && (first == null || at < first.at),
        // Below it: the row containing the top of the view, else the
        // highest one in it.
        (above: false, :final at) =>
          pixels >= 0 &&
              (first == null ||
                  (at <= 0 ? first.at > 0 || at > first.at : at < first.at)),
      };
      if (better) first = (messageId: id, at: edge.at);
    }
    return first == null
        ? null
        : (messageId: first.messageId, top: pixels + first.at);
  }

  /// Each laid-out row's edge facing the list's dividing row, within the
  /// view: a row below it by its top, one above by its bottom. Nothing
  /// here reads a row's size, which only its list may during layout.
  Map<String, ({bool above, double at})> _rowEdges() {
    final edges = <String, ({bool above, double at})>{};
    for (final MapEntry(key: id, value: row) in rows()) {
      if (!row.mounted) continue;
      final box = row.findRenderObject();
      if (box is! RenderBox || !box.attached) continue;
      final viewport = RenderAbstractViewport.maybeOf(box);
      if (viewport is! RenderViewportBase || viewport.offset != this) continue;
      // The row's item in its list, laid out and not only kept alive.
      RenderObject item = box;
      while (item.parent != null &&
          item.parent is! RenderSliverMultiBoxAdaptor) {
        item = item.parent!;
      }
      final list = item.parent, data = item.parentData;
      if (list is! RenderSliverMultiBoxAdaptor ||
          item is! RenderBox ||
          data is! SliverMultiBoxAdaptorParentData ||
          data.keptAlive ||
          data.layoutOffset == null) {
        continue;
      }
      final listTop = MatrixUtils.transformPoint(
        list.getTransformTo(viewport),
        Offset.zero,
      ).dy;
      final position = list.childMainAxisPosition(item);
      if (list.constraints.growthDirection == GrowthDirection.forward) {
        final inItem = box.localToGlobal(Offset.zero, ancestor: item).dy;
        edges[id] = (above: false, at: listTop + position + inItem);
      } else {
        // The row closes its item: its bottom is the item's.
        edges[id] = (
          above: true,
          at: listTop + list.geometry!.paintExtent - position,
        );
      }
    }
    return edges;
  }
}