Commit d3b717d1be
Unregistered key
Layout: unified · split
src/message/mod.rs +86 −94
| @@ -142,6 +142,35 @@ pub fn text_to_message(input: String) -> RoomMessageEventContent { | |||
| 142 | RoomMessageEventContent::new(msg) | 142 | RoomMessageEventContent::new(msg) |
| 143 | } | 143 | } |
| 144 | 144 | ||
| 145 | /// Before the image is loaded, already display a placeholder frame of the image size. | ||
| 146 | fn 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 | |||
| 145 | #[inline] | 174 | #[inline] |
| 146 | fn millis_to_datetime(ms: UInt) -> DateTime<LocalTz> { | 175 | fn millis_to_datetime(ms: UInt) -> DateTime<LocalTz> { |
| 147 | let time = i64::from(ms) / 1000; | 176 | let time = i64::from(ms) / 1000; |
| @@ -531,6 +560,16 @@ enum MessageColumns { | |||
| 531 | One, | 560 | One, |
| 532 | } | 561 | } |
| 533 | 562 | ||
| 563 | impl 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 | |||
| 534 | struct MessageFormatter<'a> { | 573 | struct MessageFormatter<'a> { |
| 535 | settings: &'a ApplicationSettings, | 574 | settings: &'a ApplicationSettings, |
| 536 | 575 | ||
| @@ -649,7 +688,7 @@ impl<'a> MessageFormatter<'a> { | |||
| 649 | let width = self.width(); | 688 | let width = self.width(); |
| 650 | let w = width.saturating_sub(2); | 689 | let w = width.saturating_sub(2); |
| 651 | let shortcodes = self.settings.tunables.message_shortcode_display; | 690 | 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); |
| 653 | let mut sender = msg.sender_span(info, self.settings); | 692 | let mut sender = msg.sender_span(info, self.settings); |
| 654 | let sender_width = UnicodeWidthStr::width(sender.content.as_ref()); | 693 | let sender_width = UnicodeWidthStr::width(sender.content.as_ref()); |
| 655 | let trailing = w.saturating_sub(sender_width + 1); | 694 | let trailing = w.saturating_sub(sender_width + 1); |
| @@ -881,46 +920,17 @@ impl Message { | |||
| 881 | } | 920 | } |
| 882 | } | 921 | } |
| 883 | 922 | ||
| 884 | /// Get the image preview Protocol and x,y offset, based on get_render_format. | 923 | /// Render the message as a [Text] object for the terminal. |
| 885 | pub fn line_preview<'a>( | 924 | /// |
| 886 | &'a self, | 925 | /// This will also get the image preview Protocol with an x/y offset. |
| 887 | prev: Option<&Message>, | 926 | pub fn show_with_preview<'a>( |
| 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>( | ||
| 917 | &'a self, | 927 | &'a self, |
| 918 | prev: Option<&Message>, | 928 | prev: Option<&Message>, |
| 919 | selected: bool, | 929 | selected: bool, |
| 920 | vwctx: &ViewportContext<MessageCursor>, | 930 | vwctx: &ViewportContext<MessageCursor>, |
| 921 | info: &'a RoomInfo, | 931 | info: &'a RoomInfo, |
| 922 | settings: &'a ApplicationSettings, | 932 | settings: &'a ApplicationSettings, |
| 923 | ) -> Text<'a> { | 933 | ) -> (Text<'a>, Option<(&dyn Protocol, u16, u16)>) { |
| 924 | let width = vwctx.get_width(); | 934 | let width = vwctx.get_width(); |
| 925 | 935 | ||
| 926 | let style = self.get_render_style(selected, settings); | 936 | let style = self.get_render_style(selected, settings); |
| @@ -939,12 +949,20 @@ impl Message { | |||
| 939 | } | 949 | } |
| 940 | 950 | ||
| 941 | // Now show the message contents, and the inlined reply if we couldn't find it above. | 951 | // 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( |
| 943 | width, | 953 | width, |
| 944 | style, | 954 | style, |
| 945 | reply.is_some(), | 955 | reply.is_some(), |
| 946 | settings.tunables.message_shortcode_display, | 956 | settings.tunables.message_shortcode_display, |
| 947 | ); | 957 | ); |
| 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 | |||
| 948 | fmt.push_text(msg, style, &mut text); | 966 | fmt.push_text(msg, style, &mut text); |
| 949 | 967 | ||
| 950 | if text.lines.is_empty() { | 968 | if text.lines.is_empty() { |
| @@ -961,18 +979,29 @@ impl Message { | |||
| 961 | fmt.push_thread_reply_count(thread.len(), &mut text); | 979 | fmt.push_thread_reply_count(thread.len(), &mut text); |
| 962 | } | 980 | } |
| 963 | 981 | ||
| 964 | text | 982 | (text, proto) |
| 965 | } | 983 | } |
| 966 | 984 | ||
| 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( | ||
| 968 | &self, | 997 | &self, |
| 969 | width: usize, | 998 | width: usize, |
| 970 | style: Style, | 999 | style: Style, |
| 971 | hide_reply: bool, | 1000 | hide_reply: bool, |
| 972 | emoji_shortcodes: bool, | 1001 | emoji_shortcodes: bool, |
| 973 | ) -> Text { | 1002 | ) -> (Text, Option<&dyn Protocol>) { |
| 974 | if let Some(html) = &self.html { | 1003 | 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) |
| 976 | } else { | 1005 | } else { |
| 977 | let mut msg = self.event.body(); | 1006 | let mut msg = self.event.body(); |
| 978 | if emoji_shortcodes { | 1007 | if emoji_shortcodes { |
| @@ -983,20 +1012,24 @@ impl Message { | |||
| 983 | msg.to_mut().push_str(" \u{2705}"); | 1012 | msg.to_mut().push_str(" \u{2705}"); |
| 984 | } | 1013 | } |
| 985 | 1014 | ||
| 986 | if let Some(placeholder) = match &self.image_preview { | 1015 | let mut proto = None; |
| 1016 | let placeholder = match &self.image_preview { | ||
| 987 | ImageStatus::None => None, | 1017 | ImageStatus::None => None, |
| 988 | ImageStatus::Downloading(image_preview_size) => { | 1018 | ImageStatus::Downloading(image_preview_size) => { |
| 989 | Message::placeholder_frame(Some("Downloading..."), width, image_preview_size) | 1019 | placeholder_frame(Some("Downloading..."), width, image_preview_size) |
| 990 | }, | 1020 | }, |
| 991 | ImageStatus::Loaded(backend) => { | 1021 | 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()) | ||
| 993 | }, | 1024 | }, |
| 994 | ImageStatus::Error(err) => Some(format!("[Image error: {err}]\n")), | 1025 | ImageStatus::Error(err) => Some(format!("[Image error: {err}]\n")), |
| 995 | } { | 1026 | }; |
| 1027 | |||
| 1028 | if let Some(placeholder) = placeholder { | ||
| 996 | msg.to_mut().insert_str(0, &placeholder); | 1029 | msg.to_mut().insert_str(0, &placeholder); |
| 997 | } | 1030 | } |
| 998 | 1031 | ||
| 999 | wrapped_text(msg, width, style) | 1032 | (wrapped_text(msg, width, style), proto) |
| 1000 | } | 1033 | } |
| 1001 | } | 1034 | } |
| 1002 | 1035 | ||
| @@ -1008,35 +1041,6 @@ impl Message { | |||
| 1008 | settings.get_user_span(self.sender.as_ref(), info) | 1041 | settings.get_user_span(self.sender.as_ref(), info) |
| 1009 | } | 1042 | } |
| 1010 | 1043 | ||
| 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 | |||
| 1040 | fn show_sender<'a>( | 1044 | fn show_sender<'a>( |
| 1041 | &'a self, | 1045 | &'a self, |
| 1042 | prev: Option<&Message>, | 1046 | prev: Option<&Message>, |
| @@ -1287,7 +1291,7 @@ pub mod tests { | |||
| 1287 | } | 1291 | } |
| 1288 | 1292 | ||
| 1289 | assert_eq!( | 1293 | assert_eq!( |
| 1290 | Message::placeholder_frame(None, 4, &ImagePreviewSize { width: 4, height: 4 }), | 1294 | placeholder_frame(None, 4, &ImagePreviewSize { width: 4, height: 4 }), |
| 1291 | pretty_frame_test( | 1295 | pretty_frame_test( |
| 1292 | r#" | 1296 | r#" |
| 1293 | ⌌ ⌍ | 1297 | ⌌ ⌍ |
| @@ -1298,22 +1302,13 @@ pub mod tests { | |||
| 1298 | ) | 1302 | ) |
| 1299 | ); | 1303 | ); |
| 1300 | 1304 | ||
| 1301 | assert_eq!( | 1305 | assert_eq!(placeholder_frame(None, 2, &ImagePreviewSize { width: 4, height: 4 }), None); |
| 1302 | Message::placeholder_frame(None, 2, &ImagePreviewSize { width: 4, height: 4 }), | 1306 | assert_eq!(placeholder_frame(None, 4, &ImagePreviewSize { width: 1, height: 4 }), None); |
| 1303 | None | ||
| 1304 | ); | ||
| 1305 | assert_eq!( | ||
| 1306 | Message::placeholder_frame(None, 4, &ImagePreviewSize { width: 1, height: 4 }), | ||
| 1307 | None | ||
| 1308 | ); | ||
| 1309 | 1307 | ||
| 1310 | assert_eq!( | 1308 | assert_eq!(placeholder_frame(None, 4, &ImagePreviewSize { width: 4, height: 1 }), None); |
| 1311 | Message::placeholder_frame(None, 4, &ImagePreviewSize { width: 4, height: 1 }), | ||
| 1312 | None | ||
| 1313 | ); | ||
| 1314 | 1309 | ||
| 1315 | assert_eq!( | 1310 | 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 }), |
| 1317 | pretty_frame_test( | 1312 | pretty_frame_test( |
| 1318 | r#" | 1313 | r#" |
| 1319 | ⌌ ⌍ | 1314 | ⌌ ⌍ |
| @@ -1324,10 +1319,7 @@ pub mod tests { | |||
| 1324 | ) | 1319 | ) |
| 1325 | ); | 1320 | ); |
| 1326 | assert_eq!( | 1321 | assert_eq!( |
| 1327 | Message::placeholder_frame(Some("idontfit"), 4, &ImagePreviewSize { | 1322 | placeholder_frame(Some("idontfit"), 4, &ImagePreviewSize { width: 4, height: 4 }), |
| 1328 | width: 4, | ||
| 1329 | height: 4, | ||
| 1330 | }), | ||
| 1331 | pretty_frame_test( | 1323 | pretty_frame_test( |
| 1332 | r#" | 1324 | r#" |
| 1333 | ⌌ ⌍ | 1325 | ⌌ ⌍ |
| @@ -1338,7 +1330,7 @@ pub mod tests { | |||
| 1338 | ) | 1330 | ) |
| 1339 | ); | 1331 | ); |
| 1340 | assert_eq!( | 1332 | 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 }), |
| 1342 | pretty_frame_test( | 1334 | pretty_frame_test( |
| 1343 | r#" | 1335 | r#" |
| 1344 | ⌌ ⌍ | 1336 | ⌌ ⌍ |
| @@ -1347,7 +1339,7 @@ pub mod tests { | |||
| 1347 | ) | 1339 | ) |
| 1348 | ); | 1340 | ); |
| 1349 | assert_eq!( | 1341 | 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 }), |
| 1351 | pretty_frame_test( | 1343 | pretty_frame_test( |
| 1352 | r#" | 1344 | r#" |
| 1353 | ⌌⌍ | 1345 | ⌌⌍ |
src/windows/room/scrollback.rs +2 −8
| @@ -1292,7 +1292,6 @@ impl<'a> StatefulWidget for Scrollback<'a> { | |||
| 1292 | fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) { | 1292 | fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) { |
| 1293 | let info = self.store.application.rooms.get_or_default(state.room_id.clone()); | 1293 | let info = self.store.application.rooms.get_or_default(state.room_id.clone()); |
| 1294 | let settings = &self.store.application.settings; | 1294 | let settings = &self.store.application.settings; |
| 1295 | let picker = &self.store.application.picker; | ||
| 1296 | let area = if state.cursor.timestamp.is_some() { | 1295 | let area = if state.cursor.timestamp.is_some() { |
| 1297 | render_jump_to_recent(area, buf, self.focused) | 1296 | render_jump_to_recent(area, buf, self.focused) |
| 1298 | } else { | 1297 | } else { |
| @@ -1343,13 +1342,8 @@ impl<'a> StatefulWidget for Scrollback<'a> { | |||
| 1343 | 1342 | ||
| 1344 | for (key, item) in thread.range(&corner_key..) { | 1343 | for (key, item) in thread.range(&corner_key..) { |
| 1345 | let sel = key == cursor_key; | 1344 | let sel = key == cursor_key; |
| 1346 | let txt = item.show(prev, foc && sel, &state.viewctx, info, settings); | 1345 | let (txt, mut msg_preview) = |
| 1347 | 1346 | item.show_with_preview(prev, foc && sel, &state.viewctx, info, settings); | |
| 1348 | let mut msg_preview = if picker.is_some() { | ||
| 1349 | item.line_preview(prev, &state.viewctx, info, settings) | ||
| 1350 | } else { | ||
| 1351 | None | ||
| 1352 | }; | ||
| 1353 | 1347 | ||
| 1354 | let incomplete_ok = !full || !sel; | 1348 | let incomplete_ok = !full || !sel; |
| 1355 | 1349 | ||