diff --git a/lib/pages/chat_page.dart b/lib/pages/chat_page.dart index f759370..94a300a 100644 --- a/lib/pages/chat_page.dart +++ b/lib/pages/chat_page.dart @@ -12,6 +12,7 @@ import 'package:whisper_websocket_client_dart/models/media_type.dart'; import '../models/private_message.dart'; import '../models/user.dart'; import '../utils/cache_utils.dart'; +import '../utils/chat_scroll.dart'; import '../utils/chat_sender.dart'; import '../utils/media_utils.dart'; import '../utils/message_actions.dart'; @@ -57,6 +58,8 @@ class _ChatPageState extends State { bool _historyLoading = false; // Guards against overlapping "grow the window upwards" passes bool _growing = false; + // A jump walks the list itself; window growth would move the offset under it + bool _jumping = false; // Whether the newest archive page was already asked for bool _historyLoaded = false; // Cursor for the next (older) archive page, null when the archive ends @@ -250,7 +253,7 @@ class _ChatPageState extends State { /// Extend the rendered window when the user scrolls towards older messages. void _onScroll() { - if (!_scrollController.hasClients) { + if (_jumping || !_scrollController.hasClients) { return; } final position = _scrollController.position; @@ -480,33 +483,68 @@ class _ChatPageState extends State { }); } - /// Scrolls to the message a reply quotes, widening the window when it is - /// older than what is rendered. + /// Scrolls to [sentAt], used by the reply quote, the search hits and the + /// date jump. + /// + /// Widening the rendered window is not enough: a list only builds what is + /// near the viewport, so growing the tail never builds an old message. The + /// fast path therefore cuts the window down to the target, which puts it one + /// screen from the top, and `jumpTo(0)` lands on it. The slow path walks up a + /// viewport at a time for a target already inside a large window but far + /// below the screen. Future _jumpToMessage(DateTime sentAt) async { final micros = sentAt.microsecondsSinceEpoch; - final context = _messageKeys[micros]?.currentContext; - if (context != null) { - await Scrollable.ensureVisible(context, - alignment: 0.3, duration: const Duration(milliseconds: 250)); - return; - } - // Not rendered: open the window up to it, then try once more. - final index = _messages.indexWhere((message) => message.sentAt == sentAt); - if (index < 0) { - return; - } - final needed = _messages.length - index + 5; - if (needed > _visibleCount) { - await _growWindow(needed - _visibleCount); - } - await WidgetsBinding.instance.endOfFrame; - if (!mounted) { - return; - } - final retry = _messageKeys[micros]?.currentContext; - if (retry != null && retry.mounted) { - await Scrollable.ensureVisible(retry, - alignment: 0.3, duration: const Duration(milliseconds: 250)); + _jumping = true; + try { + final index = _messages.indexWhere((message) => message.sentAt == sentAt); + if (index < 0) { + return; + } + final needed = windowForAddition(_messages.length, index); + if (needed > _visibleCount) { + setState(() { + _visibleCount = needed; + }); + await WidgetsBinding.instance.endOfFrame; + if (!mounted || !_scrollController.hasClients) { + return; + } + _scrollController.jumpTo(0); + await WidgetsBinding.instance.endOfFrame; + if (!mounted) { + return; + } + } + final context = _messageKeys[micros]?.currentContext; + if (context != null && context.mounted) { + await Scrollable.ensureVisible(context, + alignment: 0.2, duration: const Duration(milliseconds: 250)); + return; + } + for (var attempt = 0; attempt < 50; attempt++) { + if (!_scrollController.hasClients) { + return; + } + final position = _scrollController.position; + final next = + nextJumpOffset(position.pixels, position.viewportDimension); + if (next == position.pixels) { + return; + } + _scrollController.jumpTo(next); + await WidgetsBinding.instance.endOfFrame; + if (!mounted) { + return; + } + final reached = _messageKeys[micros]?.currentContext; + if (reached != null && reached.mounted) { + await Scrollable.ensureVisible(reached, + alignment: 0.3, duration: const Duration(milliseconds: 250)); + return; + } + } + } finally { + _jumping = false; } } diff --git a/lib/utils/chat_scroll.dart b/lib/utils/chat_scroll.dart new file mode 100644 index 0000000..07d828f --- /dev/null +++ b/lib/utils/chat_scroll.dart @@ -0,0 +1,23 @@ +/// How many of the newest messages must be rendered for the message at +/// [index] to exist in the list at all. +/// +/// The rendered window is a slice of the tail, so reaching an older message +/// always means widening that slice. [margin] leaves a few messages above the +/// target, so it does not sit alone on the first line. +int windowForAddition(int messageCount, int index, {int margin = 5}) { + final needed = messageCount - index + margin; + if (needed < 1) { + return 1; + } + return needed > messageCount ? messageCount : needed; +} + +/// Next scroll offset while walking up the list towards a message that is not +/// built yet: one viewport at a time, never below the top. +double nextJumpOffset(double pixels, double viewport) { + final next = pixels - viewport; + if (next < 0) { + return 0; + } + return next > pixels ? pixels : next; +} diff --git a/test/chat_scroll_test.dart b/test/chat_scroll_test.dart new file mode 100644 index 0000000..d8c3112 --- /dev/null +++ b/test/chat_scroll_test.dart @@ -0,0 +1,37 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:whisper/utils/chat_scroll.dart'; + +void main() { + group('windowForAddition', () { + test('includes the target plus a margin of newer messages', () { + // 100 messages, target at 40, 5 above it: 100 - 40 + 5. + expect(windowForAddition(100, 40), 65); + }); + + test('the newest message needs only the margin', () { + expect(windowForAddition(100, 99), 6); + }); + + test('the oldest message needs the whole conversation', () { + expect(windowForAddition(100, 0), 100); + }); + + test('never asks for more than there is', () { + expect(windowForAddition(3, 0), 3); + }); + }); + + group('nextJumpOffset', () { + test('moves one viewport up', () { + expect(nextJumpOffset(1000, 600), 400); + }); + + test('stops at the top instead of going negative', () { + expect(nextJumpOffset(200, 600), 0); + }); + + test('returns the same offset when already at the top', () { + expect(nextJumpOffset(0, 600), 0); + }); + }); +}