Open long chats bottom-anchored so only the rows that show are laid out
This commit is contained in:
@@ -35,7 +35,9 @@ import '../../../ui/adaptive/segmented_bar_overlay.dart';
|
||||
import '../../../ui/layout/app_sheet_header.dart';
|
||||
import 'chat_reading_width.dart';
|
||||
import 'chat_thread_header.dart';
|
||||
import 'chat_transcript_anchor.dart';
|
||||
import 'chat_transcript_cache.dart';
|
||||
import 'chat_transcript_scroll_view.dart';
|
||||
import 'chat_permission_card_model.dart';
|
||||
import 'chat_permission_group.dart';
|
||||
import 'chat_transcript_card.dart';
|
||||
@@ -185,7 +187,12 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
|
||||
/// The conversation's saved position, read when this view is attached and
|
||||
/// consumed by the first placement after the history arrives.
|
||||
double? _savedOffset;
|
||||
ChatScrollPosition? _savedPosition;
|
||||
|
||||
/// The message id the transcript is split at (see [chatTranscriptAnchor]),
|
||||
/// chosen once when the history first arrives; null keeps it top-anchored.
|
||||
String? _anchor;
|
||||
bool _anchorChosen = false;
|
||||
bool _placed = false;
|
||||
bool _pinScheduled = false;
|
||||
|
||||
@@ -384,7 +391,7 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_savedOffset = widget.scrollPositions?.read(widget.sessionId);
|
||||
_savedPosition = widget.scrollPositions?.read(widget.sessionId);
|
||||
_adoptCachedTranscript();
|
||||
_seedCommandNotice();
|
||||
}
|
||||
@@ -416,7 +423,9 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
_syncGeneration++;
|
||||
_commandNotices.clear();
|
||||
_seedCommandNotice();
|
||||
_savedOffset = widget.scrollPositions?.read(widget.sessionId);
|
||||
_savedPosition = widget.scrollPositions?.read(widget.sessionId);
|
||||
_anchor = null;
|
||||
_anchorChosen = false;
|
||||
_placed = false;
|
||||
_following = true;
|
||||
_distanceFromBottom = null;
|
||||
@@ -489,11 +498,8 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
store.clear(id);
|
||||
return;
|
||||
}
|
||||
if (_scroll.hasClients) {
|
||||
store.write(id, _scroll.position.pixels);
|
||||
} else {
|
||||
store.write(id, _lastOffset);
|
||||
}
|
||||
final offset = _scroll.hasClients ? _scroll.position.pixels : _lastOffset;
|
||||
store.write(id, (offset: offset, anchor: _anchor));
|
||||
}
|
||||
|
||||
/// Measured distance from the end for the current content.
|
||||
@@ -535,11 +541,21 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
/// the operator left one, otherwise the newest message.
|
||||
void _placeFromStore() {
|
||||
if (!_scroll.hasClients) return;
|
||||
final saved = _savedPosition;
|
||||
if (_anchor != null &&
|
||||
saved == null &&
|
||||
_scroll.position.minScrollExtent >= 0) {
|
||||
// The rows above the centre do not fill the viewport, so a bottom-anchored
|
||||
// list would leave a gap above them. A transcript this short is cheap to
|
||||
// lay out whole: show it top-anchored, from its first row, as before.
|
||||
setState(() => _anchor = null);
|
||||
_scheduleFollow();
|
||||
return;
|
||||
}
|
||||
_placed = true;
|
||||
final saved = _savedOffset;
|
||||
_savedOffset = null;
|
||||
_savedPosition = null;
|
||||
if (saved != null) {
|
||||
_restoreOffset = saved;
|
||||
_restoreOffset = saved.offset;
|
||||
_restoring = true;
|
||||
_following = false;
|
||||
_newestAtRelease = null;
|
||||
@@ -564,6 +580,7 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
final target = chatRestoreOffset(
|
||||
savedOffset: _restoreOffset,
|
||||
maxScrollExtent: end,
|
||||
minScrollExtent: _scroll.position.minScrollExtent,
|
||||
);
|
||||
if (_scroll.position.pixels != target) _scroll.jumpTo(target);
|
||||
_lastOffset = target;
|
||||
@@ -680,6 +697,35 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
_restoreOffset = null;
|
||||
}
|
||||
|
||||
/// The index of the row the transcript is split at, or -1 to keep it
|
||||
/// top-anchored. The anchor is chosen once, when the history first arrives:
|
||||
/// a saved position keeps the anchor it was measured from.
|
||||
int _splitFor(List<JsonMap> rows) {
|
||||
if (!_controller.loaded) return -1;
|
||||
if (!_anchorChosen) {
|
||||
_anchorChosen = true;
|
||||
final saved = _savedPosition;
|
||||
if (saved != null &&
|
||||
saved.anchor != null &&
|
||||
chatTranscriptAnchorIndex(rows, saved.anchor) < 0) {
|
||||
// The row the position was measured from is gone: the offset no longer
|
||||
// names a place in this transcript, so it opens at the newest message.
|
||||
_savedPosition = null;
|
||||
}
|
||||
_anchor = _savedPosition == null
|
||||
? chatTranscriptAnchor(rows)
|
||||
: _savedPosition!.anchor;
|
||||
}
|
||||
var index = chatTranscriptAnchorIndex(rows, _anchor);
|
||||
if (index < 0 && _anchor != null) {
|
||||
// A repair replaced the transcript without the anchor row: split the new
|
||||
// one afresh (a following view is pinned to its end again after layout).
|
||||
_anchor = chatTranscriptAnchor(rows);
|
||||
index = chatTranscriptAnchorIndex(rows, _anchor);
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => AnimatedBuilder(
|
||||
animation: Listenable.merge([
|
||||
@@ -743,6 +789,7 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
// its previous messages, keeping the element (and its open disclosures)
|
||||
// across paging and streaming.
|
||||
final keys = _rowKeys.forRows(rows);
|
||||
final split = _splitFor(rows);
|
||||
final initialLoad = !_controller.loaded && _controller.loading;
|
||||
final historyNotice = _noticeOf(
|
||||
notices,
|
||||
@@ -825,11 +872,16 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
children: [
|
||||
// At rest the first item starts below the floating bar
|
||||
// and the last one ends above the floating composer.
|
||||
ListView(
|
||||
// Bottom-anchored at the split row, the newest rows are
|
||||
// laid out first and older ones only when scrolled to.
|
||||
ChatTranscriptScrollView(
|
||||
key: ValueKey<String>(
|
||||
'chat-thread-${widget.sessionId}',
|
||||
),
|
||||
controller: _scroll,
|
||||
splitAfter: split < 0
|
||||
? null
|
||||
: ValueKey<String>(keys[split]),
|
||||
padding: EdgeInsets.fromLTRB(
|
||||
side,
|
||||
media.top + 8,
|
||||
|
||||
Reference in New Issue
Block a user