From 457544e0d28882e8c848e6820569e5ef70e31065 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Mon, 2 Oct 2023 14:16:51 -0700 Subject: [PATCH] message_scroll: Move state flags to new module message_scroll_state. Signed-off-by: Anders Kaseorg --- tools/test-js-with-node | 1 + web/src/hotkey.js | 14 +++++++------- web/src/message_scroll.js | 25 +++++-------------------- web/src/message_scroll_state.ts | 15 +++++++++++++++ web/src/message_viewport.js | 8 ++++---- 5 files changed, 32 insertions(+), 31 deletions(-) create mode 100644 web/src/message_scroll_state.ts diff --git a/tools/test-js-with-node b/tools/test-js-with-node index 4a3229a198..6e7638f784 100755 --- a/tools/test-js-with-node +++ b/tools/test-js-with-node @@ -125,6 +125,7 @@ EXEMPT_FILES = make_set( "web/src/message_lists.js", "web/src/message_live_update.js", "web/src/message_scroll.js", + "web/src/message_scroll_state.ts", "web/src/message_util.js", "web/src/message_view_header.js", "web/src/message_viewport.js", diff --git a/web/src/hotkey.js b/web/src/hotkey.js index c2ab1ace68..cc7ded6dd4 100644 --- a/web/src/hotkey.js +++ b/web/src/hotkey.js @@ -27,7 +27,7 @@ import * as list_util from "./list_util"; import * as message_edit from "./message_edit"; import * as message_edit_history from "./message_edit_history"; import * as message_lists from "./message_lists"; -import * as message_scroll from "./message_scroll"; +import * as message_scroll_state from "./message_scroll_state"; import * as narrow from "./narrow"; import * as narrow_state from "./narrow_state"; import * as navigate from "./navigate"; @@ -958,33 +958,33 @@ export function process_hotkey(e, hotkey) { switch (event_name) { case "down_arrow": case "vim_down": - message_scroll.mark_keyboard_triggered_current_scroll(); + message_scroll_state.set_keyboard_triggered_current_scroll(true); navigate.down(true); // with_centering return true; case "up_arrow": case "vim_up": - message_scroll.mark_keyboard_triggered_current_scroll(); + message_scroll_state.set_keyboard_triggered_current_scroll(true); navigate.up(); return true; case "home": - message_scroll.mark_keyboard_triggered_current_scroll(); + message_scroll_state.set_keyboard_triggered_current_scroll(true); navigate.to_home(); return true; case "end": case "G_end": - message_scroll.mark_keyboard_triggered_current_scroll(); + message_scroll_state.set_keyboard_triggered_current_scroll(true); navigate.to_end(); return true; case "page_up": case "vim_page_up": case "shift_spacebar": - message_scroll.mark_keyboard_triggered_current_scroll(); + message_scroll_state.set_keyboard_triggered_current_scroll(true); navigate.page_up(); return true; case "page_down": case "vim_page_down": case "spacebar": - message_scroll.mark_keyboard_triggered_current_scroll(); + message_scroll_state.set_keyboard_triggered_current_scroll(true); navigate.page_down(); return true; case "copy_with_c": diff --git a/web/src/message_scroll.js b/web/src/message_scroll.js index 7312ccf22f..428cabd5bc 100644 --- a/web/src/message_scroll.js +++ b/web/src/message_scroll.js @@ -4,6 +4,7 @@ import _ from "lodash"; import * as compose_banner from "./compose_banner"; import * as message_fetch from "./message_fetch"; import * as message_lists from "./message_lists"; +import * as message_scroll_state from "./message_scroll_state"; import * as message_viewport from "./message_viewport"; import * as narrow_state from "./narrow_state"; import * as unread from "./unread"; @@ -12,22 +13,6 @@ import * as unread_ui from "./unread_ui"; let actively_scrolling = false; -// Tracks whether the next scroll that will complete is initiated by -// code, not the user, and thus should avoid moving the selected -// message. -let update_selection_on_next_scroll = true; - -export function suppress_selection_update_on_next_scroll() { - update_selection_on_next_scroll = false; -} - -// Whether a keyboard shortcut is triggering a message feed scroll event. -let keyboard_triggered_current_scroll = false; - -export function mark_keyboard_triggered_current_scroll() { - keyboard_triggered_current_scroll = true; -} - let hide_scroll_to_bottom_timer; export function hide_scroll_to_bottom() { const $show_scroll_to_bottom_button = $("#scroll-to-bottom-button-container"); @@ -94,10 +79,10 @@ export function scroll_finished() { } } - if (update_selection_on_next_scroll) { + if (message_scroll_state.update_selection_on_next_scroll) { message_viewport.keep_pointer_in_view(); } else { - update_selection_on_next_scroll = true; + message_scroll_state.set_update_selection_on_next_scroll(true); } if (message_viewport.at_top()) { @@ -125,10 +110,10 @@ function scroll_finish() { // Don't present the "scroll to bottom" widget if the current // scroll was triggered by the keyboard. - if (!keyboard_triggered_current_scroll) { + if (!message_scroll_state.keyboard_triggered_current_scroll) { show_scroll_to_bottom_button(); } - keyboard_triggered_current_scroll = false; + message_scroll_state.set_keyboard_triggered_current_scroll(false); clearTimeout(scroll_timer); scroll_timer = setTimeout(scroll_finished, 100); diff --git a/web/src/message_scroll_state.ts b/web/src/message_scroll_state.ts new file mode 100644 index 0000000000..c59149c762 --- /dev/null +++ b/web/src/message_scroll_state.ts @@ -0,0 +1,15 @@ +// Tracks whether the next scroll that will complete is initiated by +// code, not the user, and thus should avoid moving the selected +// message. +export let update_selection_on_next_scroll = true; + +export function set_update_selection_on_next_scroll(value: boolean): void { + update_selection_on_next_scroll = value; +} + +// Whether a keyboard shortcut is triggering a message feed scroll event. +export let keyboard_triggered_current_scroll = false; + +export function set_keyboard_triggered_current_scroll(value: boolean): void { + keyboard_triggered_current_scroll = value; +} diff --git a/web/src/message_viewport.js b/web/src/message_viewport.js index e5dce5930a..d7ddede6bc 100644 --- a/web/src/message_viewport.js +++ b/web/src/message_viewport.js @@ -2,7 +2,7 @@ import $ from "jquery"; import * as blueslip from "./blueslip"; import * as message_lists from "./message_lists"; -import * as message_scroll from "./message_scroll"; +import * as message_scroll_state from "./message_scroll_state"; import * as popover_menus from "./popover_menus"; import * as rows from "./rows"; import * as util from "./util"; @@ -156,7 +156,7 @@ export function set_message_position(message_top, message_height, viewport_info, const new_scroll_top = message_top - message_offset; - message_scroll.suppress_selection_update_on_next_scroll(); + message_scroll_state.set_update_selection_on_next_scroll(false); scrollTop(new_scroll_top); } @@ -334,7 +334,7 @@ export function stop_auto_scrolling() { } export function system_initiated_animate_scroll(scroll_amount) { - message_scroll.suppress_selection_update_on_next_scroll(); + message_scroll_state.set_update_selection_on_next_scroll(false); const viewport_offset = scrollTop(); in_stoppable_autoscroll = true; $scroll_container.animate({ @@ -346,7 +346,7 @@ export function system_initiated_animate_scroll(scroll_amount) { } export function user_initiated_animate_scroll(scroll_amount) { - message_scroll.suppress_selection_update_on_next_scroll(); + message_scroll_state.set_update_selection_on_next_scroll(false); in_stoppable_autoscroll = false; // defensive const viewport_offset = scrollTop();