Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
scroll_to_bottom_button: Update the button when the feed changes.
Aman Agrawale3b235e28279
diff --git a/web/src/compose_setup.ts b/web/src/compose_setup.tsindex 9c78509e954..275889447c4 100644--- a/web/src/compose_setup.ts+++ b/web/src/compose_setup.ts@@ -36,6 +36,7 @@ import * as resize from "./resize.ts";import {unresolve_name} from "./resolved_topic.ts";import * as rows from "./rows.ts";import * as scheduled_messages from "./scheduled_messages.ts";+import * as scroll_to_bottom_button from "./scroll_to_bottom_button.ts";import {realm} from "./state_data.ts";import * as stream_data from "./stream_data.ts";import * as stream_settings_components from "./stream_settings_components.ts";@@ -165,11 +166,12 @@ export function initialize(): void {resize.watch_manual_resize("#compose-textarea");message_viewport.register_resize_handler(message_edit.maybe_autosize_message_edit_box);- // Updates compose max-height and scroll to bottom button position when+ // Updates compose max-height and the scroll to bottom button when// there is a change in compose height like when a compose banner is displayed.const update_compose_max_height = new ResizeObserver((_entries) => {requestAnimationFrame(() => {resize.reset_compose_message_max_height();+ scroll_to_bottom_button.update();});});update_compose_max_height.observe(document.querySelector("#compose")!);diff --git a/web/src/message_fetch.ts b/web/src/message_fetch.tsindex 2663cbeb9a1..e2028f6f429 100644--- a/web/src/message_fetch.ts+++ b/web/src/message_fetch.ts@@ -27,6 +27,7 @@ import {recent_view_messages_data} from "./recent_view_messages_data.ts";import * as recent_view_ui from "./recent_view_ui.ts";import * as recent_view_util from "./recent_view_util.ts";import {get_retry_backoff_seconds} from "./retry_backoff.ts";+import * as scroll_to_bottom_button from "./scroll_to_bottom_button.ts";import {narrow_operator_schema} from "./state_data.ts";import type {NarrowTerm} from "./state_data.ts";import * as stream_data from "./stream_data.ts";@@ -299,6 +300,10 @@ function get_messages_success(data: MessageFetchResponse, opts: MessageFetchOpti// even though the user is looking at the bottom of the view.unread_ops.process_visible();}++ if (opts.msg_list?.is_current_message_list()) {+ scroll_to_bottom_button.update();+ }}// This function modifies the narrow data to use integer IDs instead ofdiff --git a/web/src/message_scroll.ts b/web/src/message_scroll.tsindex 026696c1d17..f38645974ee 100644--- a/web/src/message_scroll.ts+++ b/web/src/message_scroll.ts@@ -14,7 +14,7 @@ import * as unread_ui from "./unread_ui.ts";export function scroll_finished(): void {message_scroll_state.set_actively_scrolling(false);- scroll_to_bottom_button.hide_scroll_to_bottom();+ scroll_to_bottom_button.update();if (message_lists.current === undefined) {return;@@ -93,9 +93,7 @@ export function initialize(): void {_.throttle(() => {if (message_lists.current === undefined) {// When in a non-message view, we don't need to process- // message scroll events. We just hide the scroll-to-bottom- // button instantly, if it is already visible.- scroll_to_bottom_button.hide_scroll_to_bottom();+ // message scroll events.return;}diff --git a/web/src/message_view.ts b/web/src/message_view.tsindex 9ac1aa1de6b..e1ad07203b6 100644--- a/web/src/message_view.ts+++ b/web/src/message_view.ts@@ -53,6 +53,7 @@ import * as recent_view_ui from "./recent_view_ui.ts";import * as recent_view_util from "./recent_view_util.ts";import * as resize from "./resize.ts";import * as scheduled_messages_feed_ui from "./scheduled_messages_feed_ui.ts";+import * as scroll_to_bottom_button from "./scroll_to_bottom_button.ts";import {message_edit_history_visibility_policy_values,web_mark_read_on_scroll_policy_values,@@ -324,6 +325,7 @@ function handle_post_message_list_change(}render_message_list_with_selected_message(render_opts);}+ scroll_to_bottom_button.update();handle_post_view_change(msg_list, opts);diff --git a/web/src/scroll_to_bottom_button.ts b/web/src/scroll_to_bottom_button.tsindex 453b37f932f..b60ec2905e3 100644--- a/web/src/scroll_to_bottom_button.ts+++ b/web/src/scroll_to_bottom_button.ts@@ -3,6 +3,7 @@ import assert from "minimalistic-assert";import type * as tippy from "tippy.js";import * as message_lists from "./message_lists.ts";+import * as message_scroll_state from "./message_scroll_state.ts";import * as message_viewport from "./message_viewport.ts";import * as narrow_state from "./narrow_state.ts";import {the} from "./util.ts";@@ -45,8 +46,7 @@ function mouse_is_over_button(): boolean {}function hide_once_not_hovered(): void {- stop_hide_timer();- hide_timer = setInterval(() => {+ hide_timer ??= setInterval(() => {// Check if the user is hovered over the scroll-to-bottom// button every 3 seconds to allow time for interaction.// If the user is not hovered on the button, hide the button@@ -58,19 +58,29 @@ function hide_once_not_hovered(): void {}, 3000);}-export function hide_scroll_to_bottom(): void {+// Safe to call whenever the scroll position or the message feed may+// have changed.+export function update(): void {if (message_lists.current === undefined) {// Scroll to bottom button is not for non-message views.hide();return;}+ if (message_scroll_state.actively_scrolling) {+ // The feed is not in its final position yet; we are called+ // again once the scroll finishes.+ return;+ }+if (!above_bottom_of_view()) {hide();return;}- hide_once_not_hovered();+ if (is_shown()) {+ hide_once_not_hovered();+ }}export function show_scroll_to_bottom_button(): void {diff --git a/web/src/views_util.ts b/web/src/views_util.tsindex c593a165db0..f5a1700bf61 100644--- a/web/src/views_util.ts+++ b/web/src/views_util.ts@@ -19,6 +19,7 @@ import * as pm_list from "./pm_list.ts";import * as popovers from "./popovers.ts";import * as popup_banners from "./popup_banners.ts";import * as resize from "./resize.ts";+import * as scroll_to_bottom_button from "./scroll_to_bottom_button.ts";import * as sidebar_ui from "./sidebar_ui.ts";import * as stream_list from "./stream_list.ts";import * as unread_ui from "./unread_ui.ts";@@ -110,6 +111,7 @@ export function show(opts: {opts.highlight_view_in_left_sidebar();unread_ui.hide_unread_banner();+ scroll_to_bottom_button.update();opts.update_compose();narrow_title.update_narrow_title(narrow_state.filter());message_view_header.render_title_area();diff --git a/web/tests/scroll_to_bottom_button.test.cjs b/web/tests/scroll_to_bottom_button.test.cjsindex a3ab71ec707..c5ffa8f967a 100644--- a/web/tests/scroll_to_bottom_button.test.cjs+++ b/web/tests/scroll_to_bottom_button.test.cjs@@ -15,6 +15,9 @@ class TransitionEvent {set_global("TransitionEvent", TransitionEvent);const message_lists = mock_esm("../src/message_lists", {current: undefined});+const message_scroll_state = mock_esm("../src/message_scroll_state", {+ actively_scrolling: false,+});const message_viewport = mock_esm("../src/message_viewport");const narrow_state = mock_esm("../src/narrow_state");@@ -45,7 +48,7 @@ function is_shown() {function mouse_scroll() {scroll_to_bottom_button.show_scroll_to_bottom_button();- scroll_to_bottom_button.hide_scroll_to_bottom();+ scroll_to_bottom_button.update();}function keydown(modifiers = {}) {@@ -93,7 +96,30 @@ test("hidden outside message views", ({override}) => {override(message_lists, "current", undefined);$container().addClass("show");- scroll_to_bottom_button.hide_scroll_to_bottom();+ scroll_to_bottom_button.update();+ assert.ok(!is_shown());+});++test("hidden outside message views even during a scroll", ({override}) => {+ override(message_lists, "current", undefined);+ override(message_scroll_state, "actively_scrolling", true);+ $container().addClass("show");++ scroll_to_bottom_button.update();+ assert.ok(!is_shown());+});++test("not updated until the scroll has finished", ({override}) => {+ set_feed(override, {bottom_visible: false});+ mouse_scroll();++ set_feed(override);+ override(message_scroll_state, "actively_scrolling", true);+ scroll_to_bottom_button.update();+ assert.ok(is_shown());++ override(message_scroll_state, "actively_scrolling", false);+ scroll_to_bottom_button.update();assert.ok(!is_shown());});@@ -104,7 +130,7 @@ test("hidden at the bottom of the feed", ({override}) => {assert.ok(!is_shown());$container().addClass("show");- scroll_to_bottom_button.hide_scroll_to_bottom();+ scroll_to_bottom_button.update();assert.ok(!is_shown());});@@ -115,7 +141,7 @@ test("hidden in an empty feed", ({override}) => {assert.ok(!is_shown());$container().addClass("show");- scroll_to_bottom_button.hide_scroll_to_bottom();+ scroll_to_bottom_button.update();assert.ok(!is_shown());});@@ -146,14 +172,14 @@ test("shown by a mouse scroll above the bottom", ({override}) => {scroll_to_bottom_button.show_scroll_to_bottom_button();assert.ok(is_shown());- scroll_to_bottom_button.hide_scroll_to_bottom();+ scroll_to_bottom_button.update();assert.ok(is_shown());});test("not shown by a keyboard scroll", ({override}) => {set_feed(override, {bottom_visible: false});- scroll_to_bottom_button.hide_scroll_to_bottom();+ scroll_to_bottom_button.update();assert.ok(!is_shown());});@@ -189,6 +215,36 @@ test("stays while the mouse is over it", ({override}) => {assert.ok(!is_shown());});+test("the three seconds start when the scroll finishes", ({override}) => {+ set_feed(override, {bottom_visible: false});+ scroll_to_bottom_button.show_scroll_to_bottom_button();++ clock.tick(5000);+ assert.ok(is_shown());++ scroll_to_bottom_button.update();+ clock.tick(2999);+ assert.ok(is_shown());++ clock.tick(1);+ assert.ok(!is_shown());+});++test("an update does not restart the three seconds", ({override}) => {+ set_feed(override, {bottom_visible: false});+ mouse_scroll();++ clock.tick(2000);+ scroll_to_bottom_button.update();+ assert.equal(clock.countTimers(), 1);+ clock.tick(999);+ assert.ok(is_shown());++ clock.tick(1);+ assert.ok(!is_shown());+ assert.equal(clock.countTimers(), 0);+});+test("another mouse scroll restarts the three seconds", ({override}) => {set_feed(override, {bottom_visible: false});mouse_scroll();@@ -208,7 +264,7 @@ test("a keyboard scroll does not make a later hide come early", ({override}) =>clock.tick(1000);keydown();- scroll_to_bottom_button.hide_scroll_to_bottom();+ scroll_to_bottom_button.update();clock.tick(4500);mouse_scroll();