Two scroll-state bugs cause the viewport to snap to the bottom after streaming completes, even when the user has scrolled up to read: 1. _scrollAfterMessageRender (ui.js): when preserveScroll=true and _scrollPinned=true (re-set by the 250px near-bottom hysteresis), _followMessagesAfterDomReplace() calls scrollToBottom(), overriding the user's position. Added !_messageUserUnpinned guard so explicitly scrolled-up users get their snapshot restored instead. 2. _finishDone (messages.js): the explicit scrollToBottom() at L3302 fires when shouldFollowOnDone is true, but that flag only checks a 120px near-bottom threshold. Added _isMessagePaneNearBottom(250) gate so users reading 250px+ above bottom keep their position. Also adds a new 'auto_scroll_follow' setting (default: off) that gates scrollIfPinned() and _shouldFollowMessagesOnDomReplace(). When disabled, the viewport never auto-scrolls during streaming — the user controls scroll position manually and uses the ↓ button to jump to bottom. 7-file setting-add pattern applied: - api/config.py: _SETTINGS_DEFAULTS + _SETTINGS_BOOL_KEYS - static/boot.js: both init paths - static/ui.js: scrollIfPinned() + _shouldFollowMessagesOnDomReplace() - static/index.html: checkbox in appearance settings - static/i18n.js: en + zh-CN translations - static/panels.js: payload, autosave, populate, full save - messages.js: no additional changes (gated via _shouldFollowMessagesOnDomReplace) Tests updated: test_tars_scroll_reset_regressions, test_issue1690, test_issue3545. All 116 scroll-related tests pass.
85 lines
3.7 KiB
Python
85 lines
3.7 KiB
Python
from pathlib import Path
|
|
|
|
|
|
REPO = Path(__file__).resolve().parents[1]
|
|
UI_JS = (REPO / "static" / "ui.js").read_text(encoding="utf-8")
|
|
I18N_JS = (REPO / "static" / "i18n.js").read_text(encoding="utf-8")
|
|
STYLE_CSS = (REPO / "static" / "style.css").read_text(encoding="utf-8")
|
|
|
|
|
|
def _function_body(src: str, signature: str) -> str:
|
|
start = src.index(signature)
|
|
brace = src.index("{", start)
|
|
depth = 0
|
|
for i in range(brace, len(src)):
|
|
if src[i] == "{":
|
|
depth += 1
|
|
elif src[i] == "}":
|
|
depth -= 1
|
|
if depth == 0:
|
|
return src[start : i + 1]
|
|
raise AssertionError(f"function body not found: {signature}")
|
|
|
|
|
|
def _scroll_listener_block() -> str:
|
|
start = UI_JS.index("el.addEventListener('scroll'")
|
|
return UI_JS[start : UI_JS.index("})();", start)]
|
|
|
|
|
|
def test_preserve_scroll_unpinned_branch_shows_new_message_cue_after_restore():
|
|
helper = _function_body(UI_JS, "function _scrollAfterMessageRender")
|
|
|
|
# The preserve-scroll branch keeps master's forced follow path for pinned /
|
|
# near-bottom users (no regression on settled-response bottom-pinning), and
|
|
# only the genuinely-scrolled-up cohort restores their viewport + gets the
|
|
# new-message cue. (Codex CORE catch: scrollIfPinned() in the pinned branch
|
|
# could leave a pinned reader short of the settled response.)
|
|
assert "if(!_messageUserUnpinned && _followMessagesAfterDomReplace()) return;" in helper
|
|
assert "_restoreMessageScrollSnapshot(scrollSnapshot);" in helper
|
|
assert helper.index("_restoreMessageScrollSnapshot(scrollSnapshot)") < helper.index(
|
|
"_maybeShowNewMessageScrollCue(scrollSnapshot)"
|
|
)
|
|
# The cue is only shown on the non-follow (restore) path, after the restore.
|
|
assert helper.index("_followMessagesAfterDomReplace()") < helper.index(
|
|
"_maybeShowNewMessageScrollCue(scrollSnapshot)"
|
|
)
|
|
assert helper.count("_maybeShowNewMessageScrollCue(scrollSnapshot)") == 1
|
|
|
|
|
|
def test_scroll_cue_uses_growth_below_restored_viewport():
|
|
maybe = _function_body(UI_JS, "function _maybeShowNewMessageScrollCue")
|
|
sync = _function_body(UI_JS, "function _syncScrollToBottomCue")
|
|
|
|
assert "el.scrollHeight>previousHeight+24" in maybe
|
|
assert "distance>80" in maybe
|
|
assert "_showNewMessageScrollCue()" in maybe
|
|
assert "scroll-to-bottom-btn--new-message" in sync
|
|
assert "session_new_message" in sync
|
|
assert "session_jump_end" in sync
|
|
|
|
|
|
def test_click_near_bottom_and_resets_clear_new_message_cue():
|
|
scroll = _function_body(UI_JS, "function scrollToBottom")
|
|
reset_direction = _function_body(UI_JS, "function _resetScrollDirectionTracker")
|
|
reset_stream = _function_body(UI_JS, "function _resetStreamScrollFollow")
|
|
listener = _scroll_listener_block()
|
|
|
|
assert scroll.index("_clearNewMessageScrollCue();") < scroll.index("_scrollPinned=true")
|
|
assert "if(nearBottom) _clearNewMessageScrollCue();" in listener
|
|
assert "_syncScrollToBottomCue(showBottomButton,{newMessage:_newMessageCueVisible})" in listener
|
|
assert "_clearNewMessageScrollCue();" in reset_direction
|
|
assert "_clearNewMessageScrollCue();" in reset_stream
|
|
|
|
|
|
def test_new_message_cue_i18n_keys_exist_in_locale_blocks():
|
|
assert I18N_JS.count("session_new_message:") >= 8
|
|
assert I18N_JS.count("session_new_message_label:") >= 8
|
|
assert "session_new_message: 'New message'" in I18N_JS
|
|
assert "session_new_message_label: 'New message available, jump to end'" in I18N_JS
|
|
|
|
|
|
def test_new_message_cue_has_stable_pill_styling():
|
|
assert ".scroll-to-bottom-btn.scroll-to-bottom-btn--new-message" in STYLE_CSS
|
|
assert "max-width:min(220px,calc(100% - 40px))" in STYLE_CSS
|
|
assert ".scroll-to-bottom-btn.scroll-to-bottom-btn--new-message .session-jump-btn__text" in STYLE_CSS
|