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
27use matrix_sdk::ruma::events::{AnyRedactionEvent, MessageLikeEvent}; 27use matrix_sdk::ruma::events::{AnyRedactionEvent, MessageLikeEvent};
28use matrix_sdk::send_queue::SendHandle; 28use matrix_sdk::send_queue::SendHandle;
29use modalkit::editing::cursor::Cursor; 29use modalkit::editing::cursor::Cursor;
30use ratatui::symbols::line::THICK_VERTICAL; 30use ratatui::symbols::line::{HORIZONTAL, THICK_VERTICAL};
31use ratatui_image::sliced::SlicedProtocol; 31use ratatui_image::sliced::SlicedProtocol;
32 32
33use crate::base::MessageEdits; 33use crate::base::{EventLocation, MessageEdits};
34use crate::message::html::{StyleTree, parse_matrix_html}; 34use crate::message::html::{StyleTree, parse_matrix_html};
35use crate::message::state::{body_cow_state, html_state}; 35use crate::message::state::{body_cow_state, html_state};
36use crate::prelude::*; 36use crate::prelude::*;
@@ -664,6 +664,9 @@ struct MessageFormatter<'a> {
664 /// The date the message was sent. 664 /// The date the message was sent.
665 date: Option<Span<'a>>, 665 date: Option<Span<'a>>,
666 666
667 /// Whether to draw the read marker's trackbar above this message.
668 trackbar: bool,
669
667 /// The users who have read up to this message. 670 /// The users who have read up to this message.
668 read: Vec<OwnedUserId>, 671 read: Vec<OwnedUserId>,
669} 672}
@@ -680,6 +683,10 @@ impl<'a> MessageFormatter<'a> {
680 line += 1; 683 line += 1;
681 } 684 }
682 685
686 if self.trackbar {
687 line += 1;
688 }
689
683 if let SenderSpan::Line(_) = self.user { 690 if let SenderSpan::Line(_) = self.user {
684 line += 1; 691 line += 1;
685 } 692 }
@@ -689,6 +696,12 @@ impl<'a> MessageFormatter<'a> {
689 696
690 #[inline] 697 #[inline]
691 fn push_spans(&mut self, prev_line: Line<'a>, style: Style, text: &mut Text<'a>) { 698 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
692 if let Some(date) = self.date.take() { 705 if let Some(date) = self.date.take() {
693 let len = date.content.as_ref().len(); 706 let len = date.content.as_ref().len();
694 let padding = self.orig.saturating_sub(len); 707 let padding = self.orig.saturating_sub(len);
@@ -921,6 +934,18 @@ pub struct Message {
921 pub html: Option<StyleTree>, 934 pub html: Option<StyleTree>,
922} 935}
923 936
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
924impl Message { 949impl Message {
925 pub fn new(event: MessageEvent, sender: OwnedUserId, timestamp: MessageTimeStamp) -> Self { 950 pub fn new(event: MessageEvent, sender: OwnedUserId, timestamp: MessageTimeStamp) -> Self {
926 let html = event.html(); 951 let html = event.html();
@@ -1019,6 +1044,35 @@ impl Message {
1019 1044
1020 !prev.timestamp.same_day(self.timestamp) 1045 !prev.timestamp.same_day(self.timestamp)
1021 } 1046 }
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
1022 pub fn message_column_width( 1076 pub fn message_column_width(
1023 viewctx: &ViewportContext<MessageCursor>, 1077 viewctx: &ViewportContext<MessageCursor>,
1024 settings: &ApplicationSettings, 1078 settings: &ApplicationSettings,
@@ -1048,6 +1102,7 @@ impl Message {
1048 ) -> MessageFormatter<'a> { 1102 ) -> MessageFormatter<'a> {
1049 let orig = width; 1103 let orig = width;
1050 let date = self.show_date(prev).then(|| self.timestamp.show_date()); 1104 let date = self.show_date(prev).then(|| self.timestamp.show_date());
1105 let trackbar = self.show_trackbar(prev, info, settings);
1051 let user_gutter = settings.tunables.user_gutter_width; 1106 let user_gutter = settings.tunables.user_gutter_width;
1052 1107
1053 if user_gutter + TIME_GUTTER + READ_GUTTER + MIN_MSG_LEN <= width && 1108 if user_gutter + TIME_GUTTER + READ_GUTTER + MIN_MSG_LEN <= width &&
@@ -1072,6 +1127,7 @@ impl Message {
1072 fill, 1127 fill,
1073 user, 1128 user,
1074 date, 1129 date,
1130 trackbar,
1075 time, 1131 time,
1076 read, 1132 read,
1077 info, 1133 info,
@@ -1090,6 +1146,7 @@ impl Message {
1090 fill, 1146 fill,
1091 user, 1147 user,
1092 date, 1148 date,
1149 trackbar,
1093 time, 1150 time,
1094 read, 1151 read,
1095 info, 1152 info,
@@ -1108,6 +1165,7 @@ impl Message {
1108 fill, 1165 fill,
1109 user, 1166 user,
1110 date, 1167 date,
1168 trackbar,
1111 time, 1169 time,
1112 read, 1170 read,
1113 info, 1171 info,
@@ -1126,6 +1184,7 @@ impl Message {
1126 fill, 1184 fill,
1127 user, 1185 user,
1128 date, 1186 date,
1187 trackbar,
1129 time, 1188 time,
1130 read, 1189 read,
1131 info, 1190 info,
@@ -1781,4 +1840,91 @@ pub mod tests {
1781 "[Attached Video: Alt text (44 kB)]".to_string().into() 1840 "[Attached Video: Alt text (44 kB)]".to_string().into()
1782 ); 1841 );
1783 } 1842 }
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 }
1784} 1930}
src/windows/room/scrollback.rs +46 −13
@@ -1384,6 +1384,7 @@ impl StatefulWidget for Scrollback<'_> {
1384 let incomplete_ok = !full || !sel; 1384 let incomplete_ok = !full || !sel;
1385 1385
1386 let includes_date_line = item.show_date(prev); 1386 let includes_date_line = item.show_date(prev);
1387 let includes_trackbar = item.show_trackbar(prev, info, settings);
1387 1388
1388 for (row, line) in txt.lines.into_iter().enumerate() { 1389 for (row, line) in txt.lines.into_iter().enumerate() {
1389 if sawit && lines.len() >= height && incomplete_ok { 1390 if sawit && lines.len() >= height && incomplete_ok {
@@ -1407,7 +1408,7 @@ impl StatefulWidget for Scrollback<'_> {
1407 let line_preview: Vec<_> = 1408 let line_preview: Vec<_> =
1408 msg_previews.extract_if(.., |(_, _, y)| *y as usize == row).collect(); 1409 msg_previews.extract_if(.., |(_, _, y)| *y as usize == row).collect();
1409 1410
1410 lines.push((key, row, line, line_preview, includes_date_line)); 1411 lines.push((key, row, line, line_preview, includes_date_line, includes_trackbar));
1411 sawit |= sel; 1412 sawit |= sel;
1412 } 1413 }
1413 1414
@@ -1416,19 +1417,17 @@ impl StatefulWidget for Scrollback<'_> {
1416 1417
1417 if lines.len() > height { 1418 if lines.len() > height {
1418 let n = lines.len() - height; 1419 let n = lines.len() - height;
1419 let previews = 1420 let previews = lines.drain(..n).zip(-(n as i16)..).flat_map(
1420 lines 1421 |((_, _, _, line_previews, _, _), y)| {
1421 .drain(..n) 1422 line_previews.into_iter().map(move |(backend, msg_x, _)| {
1422 .zip(-(n as i16)..) 1423 (area.left() + msg_x, area.top() as i16 + y, backend)
1423 .flat_map(|((_, _, _, line_previews, _), y)| { 1424 })
1424 line_previews.into_iter().map(move |(backend, msg_x, _)| { 1425 },
1425 (area.left() + msg_x, area.top() as i16 + y, backend) 1426 );
1426 })
1427 });
1428 image_previews.extend(previews); 1427 image_previews.extend(previews);
1429 } 1428 }
1430 1429
1431 if let Some((key, row, _, _, _)) = lines.first() { 1430 if let Some((key, row, _, _, _, _)) = lines.first() {
1432 state.viewctx.corner.timestamp = Some((*key).clone()); 1431 state.viewctx.corner.timestamp = Some((*key).clone());
1433 state.viewctx.corner.text_row = *row; 1432 state.viewctx.corner.text_row = *row;
1434 } 1433 }
@@ -1436,7 +1435,9 @@ impl StatefulWidget for Scrollback<'_> {
1436 let mut y = area.top(); 1435 let mut y = area.top();
1437 let x = area.left(); 1436 let x = area.left();
1438 1437
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 {
1440 let _ = buf.set_line(x, y, &txt, area.width); 1441 let _ = buf.set_line(x, y, &txt, area.width);
1441 image_previews.extend( 1442 image_previews.extend(
1442 line_preview 1443 line_preview
@@ -1444,7 +1445,9 @@ impl StatefulWidget for Scrollback<'_> {
1444 .map(|(backend, msg_x, _)| (x + msg_x, y as i16, backend)), 1445 .map(|(backend, msg_x, _)| (x + msg_x, y as i16, backend)),
1445 ); 1446 );
1446 1447
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 {
1448 state.term_cursor = (x, y); 1451 state.term_cursor = (x, y);
1449 } 1452 }
1450 1453
@@ -1504,6 +1507,8 @@ impl StatefulWidget for Scrollback<'_> {
1504mod tests { 1507mod tests {
1505 use super::*; 1508 use super::*;
1506 1509
1510 use ratatui::symbols::line::HORIZONTAL;
1511
1507 use crate::base::Need; 1512 use crate::base::Need;
1508 use crate::tests::*; 1513 use crate::tests::*;
1509 1514
@@ -1802,4 +1807,32 @@ mod tests {
1802 assert_eq!(scrollback.cursor, MSG4_KEY.clone().into()); 1807 assert_eq!(scrollback.cursor, MSG4_KEY.clone().into());
1803 assert_eq!(scrollback.viewctx.corner, MessageCursor::new(MSG3_KEY.clone(), 4)); 1808 assert_eq!(scrollback.viewctx.corner, MessageCursor::new(MSG3_KEY.clone(), 4));
1804 } 1809 }
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 }
1805} 1838}