Commit a9c1e69a89

a9c1e69a8945d74acfce7c6c99bcd7f5db871a5c

parent: 3e45ca3d2c

Unregistered key

Andrew Collins <andyc1367@gmail.com> · 2025-05-15 04:23 UTC
committer: <noreply@github.com>

Fix image preview in replies and threads (#366)

Layout: unified · split

src/message/mod.rs +25 −11
@@ -70,6 +70,8 @@ mod printer;
7070
7171pub use self::compose::text_to_message;
7272
73type ProtocolPreview<'a> = (&'a dyn Protocol, u16, u16);
74
7375pub type MessageKey = (MessageTimeStamp, OwnedEventId);
7476
7577#[derive(Default)]
@@ -715,11 +717,12 @@ impl<'a> MessageFormatter<'a> {
715717 style: Style,
716718 text: &mut Text<'a>,
717719 info: &'a RoomInfo,
718 ) {
720 settings: &'a ApplicationSettings,
721 ) -> Option<ProtocolPreview<'a>> {
719722 let width = self.width();
720723 let w = width.saturating_sub(2);
721724 let shortcodes = self.settings.tunables.message_shortcode_display;
722 let (mut replied, _) = msg.show_msg(w, style, true, shortcodes);
725 let (mut replied, proto) = msg.show_msg(w, style, true, shortcodes);
723726 let mut sender = msg.sender_span(info, self.settings);
724727 let sender_width = UnicodeWidthStr::width(sender.content.as_ref());
725728 let trailing = w.saturating_sub(sender_width + 1);
@@ -738,12 +741,22 @@ impl<'a> MessageFormatter<'a> {
738741 text,
739742 );
740743
744 // Determine the image offset of the reply header, taking into account the formatting
745 let proto = proto.map(|p| {
746 let y_off = text.lines.len() as u16;
747 // Adjust x_off by 2 to account for the vertical line and indent
748 let x_off = self.cols.user_gutter_width(settings) + 2;
749 (p, x_off, y_off)
750 });
751
741752 for line in replied.lines.iter_mut() {
742753 line.spans.insert(0, Span::styled(THICK_VERTICAL, style));
743754 line.spans.insert(0, Span::styled(" ", style));
744755 }
745756
746757 self.push_text(replied, style, text);
758
759 proto
747760 }
748761
749762 fn push_reactions(&mut self, counts: Vec<(&'a str, usize)>, style: Style, text: &mut Text<'a>) {
@@ -961,7 +974,7 @@ impl Message {
961974 vwctx: &ViewportContext<MessageCursor>,
962975 info: &'a RoomInfo,
963976 settings: &'a ApplicationSettings,
964 ) -> (Text<'a>, Option<(&'a dyn Protocol, u16, u16)>) {
977 ) -> (Text<'a>, [Option<ProtocolPreview<'a>>; 2]) {
965978 let width = vwctx.get_width();
966979
967980 let style = self.get_render_style(selected, settings);
@@ -974,10 +987,10 @@ impl Message {
974987 .reply_to()
975988 .or_else(|| self.thread_root())
976989 .and_then(|e| info.get_event(&e));
977
978 if let Some(r) = &reply {
979 fmt.push_in_reply(r, style, &mut text, info);
980 }
990 let proto_reply = reply.as_ref().and_then(|r| {
991 // Format the reply header, push it into the `Text` buffer, and get any image.
992 fmt.push_in_reply(r, style, &mut text, info, settings)
993 });
981994
982995 // Now show the message contents, and the inlined reply if we couldn't find it above.
983996 let (msg, proto) = self.show_msg(
@@ -988,10 +1001,11 @@ impl Message {
9881001 );
9891002
9901003 // Given our text so far, determine the image offset.
991 let proto = proto.map(|p| {
1004 let proto_main = proto.map(|p| {
9921005 let y_off = text.lines.len() as u16;
9931006 let x_off = fmt.cols.user_gutter_width(settings);
994 // Adjust y_off by 1 if a date was printed before the message to account for the extra line.
1007 // Adjust y_off by 1 if a date was printed before the message to account for
1008 // the extra line we're going to print.
9951009 let y_off = if fmt.date.is_some() { y_off + 1 } else { y_off };
9961010 (p, x_off, y_off)
9971011 });
@@ -1012,7 +1026,7 @@ impl Message {
10121026 fmt.push_thread_reply_count(thread.len(), &mut text);
10131027 }
10141028
1015 (text, proto)
1029 (text, [proto_main, proto_reply])
10161030 }
10171031
10181032 pub fn show<'a>(
@@ -1053,7 +1067,7 @@ impl Message {
10531067 },
10541068 ImageStatus::Loaded(backend) => {
10551069 proto = Some(backend.as_ref());
1056 placeholder_frame(None, width, &backend.rect().into())
1070 placeholder_frame(Some("Loading..."), width, &backend.rect().into())
10571071 },
10581072 ImageStatus::Error(err) => Some(format!("[Image error: {err}]\n")),
10591073 };
src/windows/room/scrollback.rs +9 −3
@@ -1340,7 +1340,7 @@ impl<'a> StatefulWidget for Scrollback<'a> {
13401340
13411341 for (key, item) in thread.range(&corner_key..) {
13421342 let sel = key == cursor_key;
1343 let (txt, mut msg_preview) =
1343 let (txt, [mut msg_preview, mut reply_preview]) =
13441344 item.show_with_preview(prev, foc && sel, &state.viewctx, info, settings);
13451345
13461346 let incomplete_ok = !full || !sel;
@@ -1357,11 +1357,17 @@ impl<'a> StatefulWidget for Scrollback<'a> {
13571357 continue;
13581358 }
13591359
1360 // Only take the preview into the matching row number.
1361 // `reply` and `msg` previews are on rows,
1362 // so an `or` works to pick the one that matches (if any)
13601363 let line_preview = match msg_preview {
1361 // Only take the preview into the matching row number.
13621364 Some((_, _, y)) if y as usize == row => msg_preview.take(),
13631365 _ => None,
1364 };
1366 }
1367 .or(match reply_preview {
1368 Some((_, _, y)) if y as usize == row => reply_preview.take(),
1369 _ => None,
1370 });
13651371
13661372 lines.push((key, row, line, line_preview));
13671373 sawit |= sel;