Commit beddf49e33

beddf49e33f0cee6906c79c32e04f548c15c08aa

parent: cc4822503d

Unregistered key

whiteye <62688683+maskjelly@users.noreply.github.com> · 2026-09-18 04:31 UTC
committer: <noreply@github.com>

Show a trackbar between the last-read and first-new message (#709)

Signed-off-by: whiteye <62688683+maskjelly@users.noreply.github.com>

Layout: unified · split

src/message/mod.rs +148 −2
@@ -27,10 +27,10 @@ use matrix_sdk::ruma::events::sticker::{OriginalStickerEvent, RedactedStickerEve
2727use matrix_sdk::ruma::events::{AnyRedactionEvent, MessageLikeEvent};
2828use matrix_sdk::send_queue::SendHandle;
2929use modalkit::editing::cursor::Cursor;
30use ratatui::symbols::line::THICK_VERTICAL;
30use ratatui::symbols::line::{HORIZONTAL, THICK_VERTICAL};
3131use ratatui_image::sliced::SlicedProtocol;
3232
33use crate::base::MessageEdits;
33use crate::base::{EventLocation, MessageEdits};
3434use crate::message::html::{StyleTree, parse_matrix_html};
3535use crate::message::state::{body_cow_state, html_state};
3636use crate::prelude::*;
@@ -664,6 +664,9 @@ struct MessageFormatter<'a> {
664664 /// The date the message was sent.
665665 date: Option<Span<'a>>,
666666
667 /// Whether to draw the read marker's trackbar above this message.
668 trackbar: bool,
669
667670 /// The users who have read up to this message.
668671 read: Vec<OwnedUserId>,
669672}
@@ -680,6 +683,10 @@ impl<'a> MessageFormatter<'a> {
680683 line += 1;
681684 }
682685
686 if self.trackbar {
687 line += 1;
688 }
689
683690 if let SenderSpan::Line(_) = self.user {
684691 line += 1;
685692 }
@@ -689,6 +696,12 @@ impl<'a> MessageFormatter<'a> {
689696
690697 #[inline]
691698 fn push_spans(&mut self, prev_line: Line<'a>, style: Style, text: &mut Text<'a>) {
699 if std::mem::take(&mut self.trackbar) {
700 let trackbar_style = Style::default().add_modifier(StyleModifier::DIM);
701 text.lines
702 .push(Line::from(Span::styled(HORIZONTAL.repeat(self.orig), trackbar_style)));
703 }
704
692705 if let Some(date) = self.date.take() {
693706 let len = date.content.as_ref().len();
694707 let padding = self.orig.saturating_sub(len);
@@ -921,6 +934,18 @@ pub struct Message {
921934 pub html: Option<StyleTree>,
922935}
923936
937/// Whether the read marker sits between the previous message and this one.
938///
939/// The marker's event is the last one the user has read, so the first message
940/// after it is the first unread one. A missing marker, or one whose message
941/// isn't among the loaded ones, leaves nothing to mark.
942fn is_after_read_marker(prev: Option<&Message>, marker: Option<&OwnedEventId>) -> bool {
943 match (prev.and_then(|msg| msg.event.event_id()), marker) {
944 (Some(prev), Some(marker)) => prev == marker,
945 _ => false,
946 }
947}
948
924949impl Message {
925950 pub fn new(event: MessageEvent, sender: OwnedUserId, timestamp: MessageTimeStamp) -> Self {
926951 let html = event.html();
@@ -1019,6 +1044,35 @@ impl Message {
10191044
10201045 !prev.timestamp.same_day(self.timestamp)
10211046 }
1047
1048 /// Whether the current user's read marker sits right before this message.
1049 ///
1050 /// Each thread keeps its own marker, so the rule only shows up in the view
1051 /// whose messages the marker belongs to.
1052 pub fn show_trackbar(
1053 &self,
1054 prev: Option<&Message>,
1055 info: &RoomInfo,
1056 settings: &ApplicationSettings,
1057 ) -> bool {
1058 let marker = info
1059 .user_receipts
1060 .get(&self.receipt_thread(info))
1061 .and_then(|receipts| receipts.get(&settings.profile.user_id));
1062
1063 is_after_read_marker(prev, marker)
1064 }
1065
1066 /// The read receipt thread that this message was inserted into.
1067 fn receipt_thread(&self, info: &RoomInfo) -> ReceiptThread {
1068 match self.event.event_id().and_then(|event_id| info.keys.get(event_id)) {
1069 Some(EventLocation::Message(Some(thread_root), _)) => {
1070 ReceiptThread::Thread(thread_root.clone())
1071 },
1072 _ => ReceiptThread::Main,
1073 }
1074 }
1075
10221076 pub fn message_column_width(
10231077 viewctx: &ViewportContext<MessageCursor>,
10241078 settings: &ApplicationSettings,
@@ -1048,6 +1102,7 @@ impl Message {
10481102 ) -> MessageFormatter<'a> {
10491103 let orig = width;
10501104 let date = self.show_date(prev).then(|| self.timestamp.show_date());
1105 let trackbar = self.show_trackbar(prev, info, settings);
10511106 let user_gutter = settings.tunables.user_gutter_width;
10521107
10531108 if user_gutter + TIME_GUTTER + READ_GUTTER + MIN_MSG_LEN <= width &&
@@ -1072,6 +1127,7 @@ impl Message {
10721127 fill,
10731128 user,
10741129 date,
1130 trackbar,
10751131 time,
10761132 read,
10771133 info,
@@ -1090,6 +1146,7 @@ impl Message {
10901146 fill,
10911147 user,
10921148 date,
1149 trackbar,
10931150 time,
10941151 read,
10951152 info,
@@ -1108,6 +1165,7 @@ impl Message {
11081165 fill,
11091166 user,
11101167 date,
1168 trackbar,
11111169 time,
11121170 read,
11131171 info,
@@ -1126,6 +1184,7 @@ impl Message {
11261184 fill,
11271185 user,
11281186 date,
1187 trackbar,
11291188 time,
11301189 read,
11311190 info,
@@ -1781,4 +1840,91 @@ pub mod tests {
17811840 "[Attached Video: Alt text (44 kB)]".to_string().into()
17821841 );
17831842 }
1843
1844 #[test]
1845 fn test_is_after_read_marker() {
1846 let first = mock_message1();
1847 let second = mock_message2();
1848
1849 // Without a marker there's nothing to mark.
1850 assert!(!is_after_read_marker(Some(&first), None));
1851 assert!(!is_after_read_marker(None, None));
1852
1853 // The marker names the last read message, so the message immediately
1854 // after it draws the trackbar.
1855 assert!(is_after_read_marker(Some(&first), Some(&MSG1_EVID)));
1856 assert!(is_after_read_marker(Some(&second), Some(&MSG2_EVID)));
1857
1858 // A marker on the rendered message belongs to the next message instead,
1859 // so the last loaded message never draws a trackbar when it's the
1860 // marker itself.
1861 assert!(!is_after_read_marker(Some(&first), Some(&MSG2_EVID)));
1862
1863 // The first loaded message has no predecessor to prove the marker is
1864 // its neighbor, and markers for unloaded messages aren't neighbors.
1865 assert!(!is_after_read_marker(None, Some(&MSG1_EVID)));
1866 assert!(!is_after_read_marker(Some(&second), Some(&MSG5_EVID)));
1867 }
1868
1869 fn has_trackbar(text: &Text<'_>, width: usize) -> bool {
1870 text.lines.iter().any(|line| {
1871 line.spans
1872 .iter()
1873 .any(|span| span.content.as_ref() == HORIZONTAL.repeat(width))
1874 })
1875 }
1876
1877 #[test]
1878 fn test_show_trackbar() {
1879 let settings = mock_settings();
1880 let previews = PreviewManager::new(&settings);
1881 let mut info = mock_room();
1882 let vwctx = ViewportContext { dimensions: (60, 5), ..Default::default() };
1883
1884 let prev = mock_message1();
1885 let msg = mock_message2();
1886 let user_id = settings.profile.user_id.clone();
1887
1888 // Without a marker, no trackbar is drawn.
1889 let text = msg.show(Some(&prev), false, &vwctx, &info, &settings, &previews);
1890 assert!(!has_trackbar(&text, 60));
1891
1892 // When the marker names the previous message, the rule leads the message.
1893 info.set_receipt(ReceiptThread::Main, user_id.clone(), MSG1_EVID.clone());
1894 let text = msg.show(Some(&prev), false, &vwctx, &info, &settings, &previews);
1895 assert!(has_trackbar(&text, 60));
1896 assert_eq!(text.lines[0].spans[0].content.as_ref(), HORIZONTAL.repeat(60));
1897
1898 // A marker on the message itself means the trackbar belongs to the next one.
1899 info.set_receipt(ReceiptThread::Main, user_id.clone(), MSG2_EVID.clone());
1900 let text = msg.show(Some(&prev), false, &vwctx, &info, &settings, &previews);
1901 assert!(!has_trackbar(&text, 60));
1902 let text = prev.show(Some(&msg), false, &vwctx, &info, &settings, &previews);
1903 assert!(has_trackbar(&text, 60));
1904 }
1905
1906 #[test]
1907 fn test_show_trackbar_thread() {
1908 let settings = mock_settings();
1909 let mut info = mock_room();
1910 let user_id = settings.profile.user_id.clone();
1911
1912 let prev = mock_message1();
1913 let msg = mock_message2();
1914
1915 let root = MSG5_EVID.clone();
1916 info.keys.insert(
1917 MSG2_EVID.clone(),
1918 EventLocation::Message(Some(root.clone()), MSG2_KEY.clone()),
1919 );
1920
1921 // A marker for the thread that this message belongs to is shown.
1922 info.set_receipt(ReceiptThread::Thread(root.clone()), user_id.clone(), MSG1_EVID.clone());
1923 assert!(msg.show_trackbar(Some(&prev), &info, &settings));
1924
1925 // The main timeline's marker doesn't apply to a thread's messages.
1926 info.user_receipts.remove(&ReceiptThread::Thread(root));
1927 info.set_receipt(ReceiptThread::Main, user_id, MSG1_EVID.clone());
1928 assert!(!msg.show_trackbar(Some(&prev), &info, &settings));
1929 }
17841930}
src/windows/room/scrollback.rs +46 −13
@@ -1384,6 +1384,7 @@ impl StatefulWidget for Scrollback<'_> {
13841384 let incomplete_ok = !full || !sel;
13851385
13861386 let includes_date_line = item.show_date(prev);
1387 let includes_trackbar = item.show_trackbar(prev, info, settings);
13871388
13881389 for (row, line) in txt.lines.into_iter().enumerate() {
13891390 if sawit && lines.len() >= height && incomplete_ok {
@@ -1407,7 +1408,7 @@ impl StatefulWidget for Scrollback<'_> {
14071408 let line_preview: Vec<_> =
14081409 msg_previews.extract_if(.., |(_, _, y)| *y as usize == row).collect();
14091410
1410 lines.push((key, row, line, line_preview, includes_date_line));
1411 lines.push((key, row, line, line_preview, includes_date_line, includes_trackbar));
14111412 sawit |= sel;
14121413 }
14131414
@@ -1416,19 +1417,17 @@ impl StatefulWidget for Scrollback<'_> {
14161417
14171418 if lines.len() > height {
14181419 let n = lines.len() - height;
1419 let previews =
1420 lines
1421 .drain(..n)
1422 .zip(-(n as i16)..)
1423 .flat_map(|((_, _, _, line_previews, _), y)| {
1424 line_previews.into_iter().map(move |(backend, msg_x, _)| {
1425 (area.left() + msg_x, area.top() as i16 + y, backend)
1426 })
1427 });
1420 let previews = lines.drain(..n).zip(-(n as i16)..).flat_map(
1421 |((_, _, _, line_previews, _, _), y)| {
1422 line_previews.into_iter().map(move |(backend, msg_x, _)| {
1423 (area.left() + msg_x, area.top() as i16 + y, backend)
1424 })
1425 },
1426 );
14281427 image_previews.extend(previews);
14291428 }
14301429
1431 if let Some((key, row, _, _, _)) = lines.first() {
1430 if let Some((key, row, _, _, _, _)) = lines.first() {
14321431 state.viewctx.corner.timestamp = Some((*key).clone());
14331432 state.viewctx.corner.text_row = *row;
14341433 }
@@ -1436,7 +1435,9 @@ impl StatefulWidget for Scrollback<'_> {
14361435 let mut y = area.top();
14371436 let x = area.left();
14381437
1439 for (key, row, txt, line_preview, includes_date_line) in lines.into_iter() {
1438 for (key, row, txt, line_preview, includes_date_line, includes_trackbar) in
1439 lines.into_iter()
1440 {
14401441 let _ = buf.set_line(x, y, &txt, area.width);
14411442 image_previews.extend(
14421443 line_preview
@@ -1444,7 +1445,9 @@ impl StatefulWidget for Scrollback<'_> {
14441445 .map(|(backend, msg_x, _)| (x + msg_x, y as i16, backend)),
14451446 );
14461447
1447 if key == cursor_key && row == usize::from(includes_date_line) {
1448 if key == cursor_key &&
1449 row == usize::from(includes_date_line) + usize::from(includes_trackbar)
1450 {
14481451 state.term_cursor = (x, y);
14491452 }
14501453
@@ -1504,6 +1507,8 @@ impl StatefulWidget for Scrollback<'_> {
15041507mod tests {
15051508 use super::*;
15061509
1510 use ratatui::symbols::line::HORIZONTAL;
1511
15071512 use crate::base::Need;
15081513 use crate::tests::*;
15091514
@@ -1802,4 +1807,32 @@ mod tests {
18021807 assert_eq!(scrollback.cursor, MSG4_KEY.clone().into());
18031808 assert_eq!(scrollback.viewctx.corner, MessageCursor::new(MSG3_KEY.clone(), 4));
18041809 }
1810
1811 #[tokio::test]
1812 async fn test_draw_trackbar() {
1813 let mut store = mock_store().await;
1814 store.application.settings.tunables.typing_notice_display = false;
1815
1816 let user_id = store.application.settings.profile.user_id.clone();
1817 store.application.get_room_info(TEST_ROOM1_ID.clone()).set_receipt(
1818 ReceiptThread::Main,
1819 user_id,
1820 MSG3_EVID.clone(),
1821 );
1822
1823 let mut scrollback = ScrollbackState::new(TEST_ROOM1_ID.clone(), None);
1824 let area = Rect::new(0, 0, 60, 6);
1825 let mut buffer = Buffer::empty(area);
1826 scrollback.draw(area, &mut buffer, true, &mut store);
1827
1828 // The trackbar takes a row of its own, so the view is scrolled up an
1829 // extra line compared to the same messages without a marker.
1830 assert_eq!(scrollback.viewctx.corner, MessageCursor::new(MSG4_KEY.clone(), 0));
1831
1832 // It's drawn between the marker's message and the first unread one.
1833 assert_eq!(buffer.cell((0, 0)).unwrap().symbol(), HORIZONTAL);
1834
1835 // The terminal cursor still lands on the selected message's first line.
1836 assert_eq!(scrollback.term_cursor, (0, 4));
1837 }
18051838}