Commit d3b717d1be

d3b717d1be8c7baf5fef68f9828177d1a7d70f27

parent: 2ac71da9a6

Unregistered key

Ulyssa <git@ulyssa.dev> · 2024-03-24 02:41 UTC
committer: <noreply@github.com>

Fix image previews in replies (#225)

Layout: unified · split

src/message/mod.rs +86 −94
@@ -142,6 +142,35 @@ pub fn text_to_message(input: String) -> RoomMessageEventContent {
142142 RoomMessageEventContent::new(msg)
143143}
144144
145/// Before the image is loaded, already display a placeholder frame of the image size.
146fn placeholder_frame(
147 text: Option<&str>,
148 outer_width: usize,
149 image_preview_size: &ImagePreviewSize,
150) -> Option<String> {
151 let ImagePreviewSize { width, height } = image_preview_size;
152 if outer_width < *width || (*width < 2 || *height < 2) {
153 return None;
154 }
155 let mut placeholder = "\u{230c}".to_string();
156 placeholder.push_str(&" ".repeat(width - 2));
157 placeholder.push_str("\u{230d}\n");
158 if *height > 2 {
159 if let Some(text) = text {
160 if text.width() <= width - 2 {
161 placeholder.push(' ');
162 placeholder.push_str(text);
163 }
164 }
165 }
166
167 placeholder.push_str(&"\n".repeat(height - 2));
168 placeholder.push('\u{230e}');
169 placeholder.push_str(&" ".repeat(width - 2));
170 placeholder.push_str("\u{230f}\n");
171 Some(placeholder)
172}
173
145174#[inline]
146175fn millis_to_datetime(ms: UInt) -> DateTime<LocalTz> {
147176 let time = i64::from(ms) / 1000;
@@ -531,6 +560,16 @@ enum MessageColumns {
531560 One,
532561}
533562
563impl MessageColumns {
564 fn user_gutter_width(&self, settings: &ApplicationSettings) -> u16 {
565 if let MessageColumns::One = self {
566 0
567 } else {
568 settings.tunables.user_gutter_width as u16
569 }
570 }
571}
572
534573struct MessageFormatter<'a> {
535574 settings: &'a ApplicationSettings,
536575
@@ -649,7 +688,7 @@ impl<'a> MessageFormatter<'a> {
649688 let width = self.width();
650689 let w = width.saturating_sub(2);
651690 let shortcodes = self.settings.tunables.message_shortcode_display;
652 let mut replied = msg.show_msg(w, style, true, shortcodes);
691 let (mut replied, _) = msg.show_msg(w, style, true, shortcodes);
653692 let mut sender = msg.sender_span(info, self.settings);
654693 let sender_width = UnicodeWidthStr::width(sender.content.as_ref());
655694 let trailing = w.saturating_sub(sender_width + 1);
@@ -881,46 +920,17 @@ impl Message {
881920 }
882921 }
883922
884 /// Get the image preview Protocol and x,y offset, based on get_render_format.
885 pub fn line_preview<'a>(
886 &'a self,
887 prev: Option<&Message>,
888 vwctx: &ViewportContext<MessageCursor>,
889 info: &'a RoomInfo,
890 settings: &'a ApplicationSettings,
891 ) -> Option<(&dyn Protocol, u16, u16)> {
892 let width = vwctx.get_width();
893 let user_gutter = settings.tunables.user_gutter_width;
894 // The x position where get_render_format would render the text.
895 let x = (if user_gutter + MIN_MSG_LEN <= width {
896 user_gutter
897 } else {
898 0
899 }) as u16;
900 // See get_render_format; account for possible "date" line.
901 let date_y = match &prev {
902 Some(prev) if !prev.timestamp.same_day(&self.timestamp) => 1,
903 _ => 0,
904 };
905 if let ImageStatus::Loaded(backend) = &self.image_preview {
906 return Some((backend.as_ref(), x, date_y));
907 } else if let Some(reply) = self.reply_to().and_then(|e| info.get_event(&e)) {
908 if let ImageStatus::Loaded(backend) = &reply.image_preview {
909 // The reply should be offset a bit:
910 return Some((backend.as_ref(), x + 2, date_y + 1));
911 }
912 }
913 None
914 }
915
916 pub fn show<'a>(
923 /// Render the message as a [Text] object for the terminal.
924 ///
925 /// This will also get the image preview Protocol with an x/y offset.
926 pub fn show_with_preview<'a>(
917927 &'a self,
918928 prev: Option<&Message>,
919929 selected: bool,
920930 vwctx: &ViewportContext<MessageCursor>,
921931 info: &'a RoomInfo,
922932 settings: &'a ApplicationSettings,
923 ) -> Text<'a> {
933 ) -> (Text<'a>, Option<(&dyn Protocol, u16, u16)>) {
924934 let width = vwctx.get_width();
925935
926936 let style = self.get_render_style(selected, settings);
@@ -939,12 +949,20 @@ impl Message {
939949 }
940950
941951 // Now show the message contents, and the inlined reply if we couldn't find it above.
942 let msg = self.show_msg(
952 let (msg, proto) = self.show_msg(
943953 width,
944954 style,
945955 reply.is_some(),
946956 settings.tunables.message_shortcode_display,
947957 );
958
959 // Given our text so far, determine the image offset.
960 let proto = proto.map(|p| {
961 let y_off = text.lines.len() as u16;
962 let x_off = fmt.cols.user_gutter_width(settings);
963 (p, x_off, y_off)
964 });
965
948966 fmt.push_text(msg, style, &mut text);
949967
950968 if text.lines.is_empty() {
@@ -961,18 +979,29 @@ impl Message {
961979 fmt.push_thread_reply_count(thread.len(), &mut text);
962980 }
963981
964 text
982 (text, proto)
965983 }
966984
967 pub fn show_msg(
985 pub fn show<'a>(
986 &'a self,
987 prev: Option<&Message>,
988 selected: bool,
989 vwctx: &ViewportContext<MessageCursor>,
990 info: &'a RoomInfo,
991 settings: &'a ApplicationSettings,
992 ) -> Text<'a> {
993 self.show_with_preview(prev, selected, vwctx, info, settings).0
994 }
995
996 fn show_msg(
968997 &self,
969998 width: usize,
970999 style: Style,
9711000 hide_reply: bool,
9721001 emoji_shortcodes: bool,
973 ) -> Text {
1002 ) -> (Text, Option<&dyn Protocol>) {
9741003 if let Some(html) = &self.html {
975 html.to_text(width, style, hide_reply, emoji_shortcodes)
1004 (html.to_text(width, style, hide_reply, emoji_shortcodes), None)
9761005 } else {
9771006 let mut msg = self.event.body();
9781007 if emoji_shortcodes {
@@ -983,20 +1012,24 @@ impl Message {
9831012 msg.to_mut().push_str(" \u{2705}");
9841013 }
9851014
986 if let Some(placeholder) = match &self.image_preview {
1015 let mut proto = None;
1016 let placeholder = match &self.image_preview {
9871017 ImageStatus::None => None,
9881018 ImageStatus::Downloading(image_preview_size) => {
989 Message::placeholder_frame(Some("Downloading..."), width, image_preview_size)
1019 placeholder_frame(Some("Downloading..."), width, image_preview_size)
9901020 },
9911021 ImageStatus::Loaded(backend) => {
992 Message::placeholder_frame(None, width, &backend.rect().into())
1022 proto = Some(backend.as_ref());
1023 placeholder_frame(None, width, &backend.rect().into())
9931024 },
9941025 ImageStatus::Error(err) => Some(format!("[Image error: {err}]\n")),
995 } {
1026 };
1027
1028 if let Some(placeholder) = placeholder {
9961029 msg.to_mut().insert_str(0, &placeholder);
9971030 }
9981031
999 wrapped_text(msg, width, style)
1032 (wrapped_text(msg, width, style), proto)
10001033 }
10011034 }
10021035
@@ -1008,35 +1041,6 @@ impl Message {
10081041 settings.get_user_span(self.sender.as_ref(), info)
10091042 }
10101043
1011 /// Before the image is loaded, already display a placeholder frame of the image size.
1012 fn placeholder_frame(
1013 text: Option<&str>,
1014 outer_width: usize,
1015 image_preview_size: &ImagePreviewSize,
1016 ) -> Option<String> {
1017 let ImagePreviewSize { width, height } = image_preview_size;
1018 if outer_width < *width || (*width < 2 || *height < 2) {
1019 return None;
1020 }
1021 let mut placeholder = "\u{230c}".to_string();
1022 placeholder.push_str(&" ".repeat(width - 2));
1023 placeholder.push_str("\u{230d}\n");
1024 if *height > 2 {
1025 if let Some(text) = text {
1026 if text.width() <= width - 2 {
1027 placeholder.push(' ');
1028 placeholder.push_str(text);
1029 }
1030 }
1031 }
1032
1033 placeholder.push_str(&"\n".repeat(height - 2));
1034 placeholder.push('\u{230e}');
1035 placeholder.push_str(&" ".repeat(width - 2));
1036 placeholder.push_str("\u{230f}\n");
1037 Some(placeholder)
1038 }
1039
10401044 fn show_sender<'a>(
10411045 &'a self,
10421046 prev: Option<&Message>,
@@ -1287,7 +1291,7 @@ pub mod tests {
12871291 }
12881292
12891293 assert_eq!(
1290 Message::placeholder_frame(None, 4, &ImagePreviewSize { width: 4, height: 4 }),
1294 placeholder_frame(None, 4, &ImagePreviewSize { width: 4, height: 4 }),
12911295 pretty_frame_test(
12921296 r#"
12931297⌌ ⌍
@@ -1298,22 +1302,13 @@ pub mod tests {
12981302 )
12991303 );
13001304
1301 assert_eq!(
1302 Message::placeholder_frame(None, 2, &ImagePreviewSize { width: 4, height: 4 }),
1303 None
1304 );
1305 assert_eq!(
1306 Message::placeholder_frame(None, 4, &ImagePreviewSize { width: 1, height: 4 }),
1307 None
1308 );
1305 assert_eq!(placeholder_frame(None, 2, &ImagePreviewSize { width: 4, height: 4 }), None);
1306 assert_eq!(placeholder_frame(None, 4, &ImagePreviewSize { width: 1, height: 4 }), None);
13091307
1310 assert_eq!(
1311 Message::placeholder_frame(None, 4, &ImagePreviewSize { width: 4, height: 1 }),
1312 None
1313 );
1308 assert_eq!(placeholder_frame(None, 4, &ImagePreviewSize { width: 4, height: 1 }), None);
13141309
13151310 assert_eq!(
1316 Message::placeholder_frame(Some("OK"), 4, &ImagePreviewSize { width: 4, height: 4 }),
1311 placeholder_frame(Some("OK"), 4, &ImagePreviewSize { width: 4, height: 4 }),
13171312 pretty_frame_test(
13181313 r#"
13191314⌌ ⌍
@@ -1324,10 +1319,7 @@ pub mod tests {
13241319 )
13251320 );
13261321 assert_eq!(
1327 Message::placeholder_frame(Some("idontfit"), 4, &ImagePreviewSize {
1328 width: 4,
1329 height: 4,
1330 }),
1322 placeholder_frame(Some("idontfit"), 4, &ImagePreviewSize { width: 4, height: 4 }),
13311323 pretty_frame_test(
13321324 r#"
13331325⌌ ⌍
@@ -1338,7 +1330,7 @@ pub mod tests {
13381330 )
13391331 );
13401332 assert_eq!(
1341 Message::placeholder_frame(Some("OK"), 4, &ImagePreviewSize { width: 4, height: 2 }),
1333 placeholder_frame(Some("OK"), 4, &ImagePreviewSize { width: 4, height: 2 }),
13421334 pretty_frame_test(
13431335 r#"
13441336⌌ ⌍
@@ -1347,7 +1339,7 @@ pub mod tests {
13471339 )
13481340 );
13491341 assert_eq!(
1350 Message::placeholder_frame(Some("OK"), 4, &ImagePreviewSize { width: 2, height: 3 }),
1342 placeholder_frame(Some("OK"), 4, &ImagePreviewSize { width: 2, height: 3 }),
13511343 pretty_frame_test(
13521344 r#"
13531345⌌⌍
src/windows/room/scrollback.rs +2 −8
@@ -1292,7 +1292,6 @@ impl<'a> StatefulWidget for Scrollback<'a> {
12921292 fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
12931293 let info = self.store.application.rooms.get_or_default(state.room_id.clone());
12941294 let settings = &self.store.application.settings;
1295 let picker = &self.store.application.picker;
12961295 let area = if state.cursor.timestamp.is_some() {
12971296 render_jump_to_recent(area, buf, self.focused)
12981297 } else {
@@ -1343,13 +1342,8 @@ impl<'a> StatefulWidget for Scrollback<'a> {
13431342
13441343 for (key, item) in thread.range(&corner_key..) {
13451344 let sel = key == cursor_key;
1346 let txt = item.show(prev, foc && sel, &state.viewctx, info, settings);
1347
1348 let mut msg_preview = if picker.is_some() {
1349 item.line_preview(prev, &state.viewctx, info, settings)
1350 } else {
1351 None
1352 };
1345 let (txt, mut msg_preview) =
1346 item.show_with_preview(prev, foc && sel, &state.viewctx, info, settings);
13531347
13541348 let incomplete_ok = !full || !sel;
13551349