Open long chats bottom-anchored so only the rows that show are laid out

This commit is contained in:
2026-11-02 00:23:46 +00:00
parent b80c52b3e1
commit a5f0eb3811
8 changed files with 532 additions and 39 deletions
@@ -74,27 +74,35 @@ String chatJumpToLatestLabel(int newMessages) {
/// Where a conversation's transcript is placed when it first shows.
///
/// No saved position means the operator has none of their own: open at the
/// newest content. A saved offset is kept, clamped to the current end, so a
/// newest content. A saved offset is kept, clamped to the current extent, so a
/// conversation whose content grew or shrank while it was closed still lands on
/// a real position.
/// a real position. A bottom-anchored transcript measures older rows above its
/// centre, so its offsets (and [minScrollExtent]) can be negative.
double chatRestoreOffset({
required double? savedOffset,
required double maxScrollExtent,
double minScrollExtent = 0,
}) {
if (savedOffset == null) return maxScrollExtent;
return savedOffset.clamp(0.0, maxScrollExtent).toDouble();
return savedOffset.clamp(minScrollExtent, maxScrollExtent).toDouble();
}
/// A saved transcript position: the scroll offset and the message id of the
/// row the transcript was split at (see `chatTranscriptAnchor`), or null for a
/// top-anchored transcript whose offset counts from its first row. An offset
/// only means something measured from the same anchor.
typedef ChatScrollPosition = ({double offset, String? anchor});
/// Per-conversation transcript positions, kept while the chat list stays
/// mounted. A conversation opened again returns to the position the operator
/// left; one they never moved in opens at its newest message.
abstract interface class ChatScrollStore {
/// The offset the operator left this conversation at, or null when they never
/// scrolled away from its end.
double? read(String sessionId);
/// The position the operator left this conversation at, or null when they
/// never scrolled away from its end.
ChatScrollPosition? read(String sessionId);
/// Records a deliberate position away from the end.
void write(String sessionId, double offset);
void write(String sessionId, ChatScrollPosition position);
/// Forgets the position once the operator is following the end again.
void clear(String sessionId);
@@ -107,19 +115,22 @@ abstract interface class ChatScrollStore {
/// client keeps no transcript position on disk and does not import the web's
/// storage keys.
class MemoryChatScrollStore implements ChatScrollStore {
final Map<String, double> _offsets = {};
final Map<String, ChatScrollPosition> _offsets = {};
/// A snapshot of the stored offsets, so a test can tell a cleared
/// A snapshot of the stored positions, so a test can tell a cleared
/// conversation from one that was never moved.
Map<String, double> get entries => Map.unmodifiable(_offsets);
Map<String, ChatScrollPosition> get entries => Map.unmodifiable(_offsets);
@override
double? read(String sessionId) => _offsets[sessionId];
ChatScrollPosition? read(String sessionId) => _offsets[sessionId];
@override
void write(String sessionId, double offset) {
if (!offset.isFinite || offset < 0) return;
_offsets[sessionId] = offset;
void write(String sessionId, ChatScrollPosition position) {
// A top-anchored offset counts from the first row and is never negative;
// a bottom-anchored one is negative above its centre.
if (!position.offset.isFinite) return;
if (position.anchor == null && position.offset < 0) return;
_offsets[sessionId] = position;
}
@override