| 1 | use gpui_kit::*; |
| 2 | |
| 3 | use super::ConversationView; |
| 4 | use crate::read_state::{JumpTarget, jump_target}; |
| 5 | use crate::rows::Row; |
| 6 | use crate::views::shortcuts::keys_for_label; |
| 7 | |
| 8 | pub(super) const JUMP_TO_NEWEST_LABEL: &str = "Jump to newest"; |
| 9 | const JUMP_SHORTCUT_LABEL: &str = "Jump to last read or newest message"; |
| 10 | |
| 11 | pub(super) fn jump_to_newest_tooltip() -> SharedString { |
| 12 | match keys_for_label(JUMP_SHORTCUT_LABEL) { |
| 13 | Some(keys) => format!("{JUMP_TO_NEWEST_LABEL} ({keys})").into(), |
| 14 | None => JUMP_TO_NEWEST_LABEL.into(), |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | impl ConversationView { |
| 19 | pub fn jump_to_last_read_or_newest(&mut self, cx: &mut Context<Self>) { |
| 20 | if self.current.is_none() || self.in_feed() { |
| 21 | return; |
| 22 | } |
| 23 | if self.is_detached() { |
| 24 | self.leave_detached(cx); |
| 25 | return; |
| 26 | } |
| 27 | let last_read_index = self |
| 28 | .rows |
| 29 | .borrow() |
| 30 | .iter() |
| 31 | .position(|row| matches!(row, Row::Message(message) if message.new_marker)); |
| 32 | let (top_item, scrolled_up) = { |
| 33 | let scroller = self.scroller.read(cx); |
| 34 | (scroller.top_item(), scroller.is_scrolled_up()) |
| 35 | }; |
| 36 | let Some(target) = jump_target(last_read_index, top_item, scrolled_up) else { |
| 37 | return; |
| 38 | }; |
| 39 | self.scroller.update(cx, |scroller, cx| match target { |
| 40 | JumpTarget::Newest => scroller.scroll_to_end(cx), |
| 41 | JumpTarget::LastRead(index) => { |
| 42 | scroller.scroll_to_item(index, cx); |
| 43 | } |
| 44 | }); |
| 45 | } |
| 46 | } |