Commit 88af9bfec3

88af9bfec38d8118ffc26e411f55191b9e99cb5d

parent: 999399a70f

Unregistered key

Benjamin Grosse <ste3ls@gmail.com> · 2024-01-28 07:35 UTC
committer: <noreply@github.com>

Fix crash on small image preview (#198)

Layout: unified · split

src/message/mod.rs +96 −7
@@ -841,10 +841,10 @@ impl Message {
841 if let Some(placeholder) = match &self.image_preview { 841 if let Some(placeholder) = match &self.image_preview {
842 ImageStatus::None => None, 842 ImageStatus::None => None,
843 ImageStatus::Downloading(image_preview_size) => { 843 ImageStatus::Downloading(image_preview_size) => {
844 Some(Message::placeholder_frame(Some("Downloading..."), image_preview_size)) 844 Message::placeholder_frame(Some("Downloading..."), width, image_preview_size)
845 }, 845 },
846 ImageStatus::Loaded(backend) => { 846 ImageStatus::Loaded(backend) => {
847 Some(Message::placeholder_frame(None, &backend.rect().into())) 847 Message::placeholder_frame(None, width, &backend.rect().into())
848 }, 848 },
849 ImageStatus::Error(err) => Some(format!("[Image error: {err}]\n")), 849 ImageStatus::Error(err) => Some(format!("[Image error: {err}]\n")),
850 } { 850 } {
@@ -864,21 +864,32 @@ impl Message {
864 } 864 }
865 865
866 /// Before the image is loaded, already display a placeholder frame of the image size. 866 /// Before the image is loaded, already display a placeholder frame of the image size.
867 fn placeholder_frame(text: Option<&str>, image_preview_size: &ImagePreviewSize) -> String { 867 fn placeholder_frame(
868 text: Option<&str>,
869 outer_width: usize,
870 image_preview_size: &ImagePreviewSize,
871 ) -> Option<String> {
868 let ImagePreviewSize { width, height } = image_preview_size; 872 let ImagePreviewSize { width, height } = image_preview_size;
873 if outer_width < *width || (*width < 2 || *height < 2) {
874 return None;
875 }
869 let mut placeholder = "\u{230c}".to_string(); 876 let mut placeholder = "\u{230c}".to_string();
870 placeholder.push_str(&" ".repeat(width - 2)); 877 placeholder.push_str(&" ".repeat(width - 2));
871 placeholder.push_str("\u{230d}\n"); 878 placeholder.push_str("\u{230d}\n");
872 placeholder.push(' '); 879 if *height > 2 {
873 if let Some(text) = text { 880 if let Some(text) = text {
874 placeholder.push_str(text); 881 if text.width() <= width - 2 {
882 placeholder.push(' ');
883 placeholder.push_str(text);
884 }
885 }
875 } 886 }
876 887
877 placeholder.push_str(&"\n".repeat(height - 2)); 888 placeholder.push_str(&"\n".repeat(height - 2));
878 placeholder.push('\u{230e}'); 889 placeholder.push('\u{230e}');
879 placeholder.push_str(&" ".repeat(width - 2)); 890 placeholder.push_str(&" ".repeat(width - 2));
880 placeholder.push_str("\u{230f}\n"); 891 placeholder.push_str("\u{230f}\n");
881 placeholder 892 Some(placeholder)
882 } 893 }
883 894
884 fn show_sender<'a>( 895 fn show_sender<'a>(
@@ -1122,4 +1133,82 @@ pub mod tests {
1122 "<h1>Heading</h1>\n<h2>Subheading</h2>\n<p>text</p>\n" 1133 "<h1>Heading</h1>\n<h2>Subheading</h2>\n<p>text</p>\n"
1123 ); 1134 );
1124 } 1135 }
1136
1137 #[test]
1138 fn test_placeholder_frame() {
1139 fn pretty_frame_test(str: &str) -> Option<String> {
1140 Some(str[1..].to_string())
1141 }
1142
1143 assert_eq!(
1144 Message::placeholder_frame(None, 4, &ImagePreviewSize { width: 4, height: 4 }),
1145 pretty_frame_test(
1146 r#"
1147⌌ ⌍
1148
1149
1150⌎ ⌏
1151"#
1152 )
1153 );
1154
1155 assert_eq!(
1156 Message::placeholder_frame(None, 2, &ImagePreviewSize { width: 4, height: 4 }),
1157 None
1158 );
1159 assert_eq!(
1160 Message::placeholder_frame(None, 4, &ImagePreviewSize { width: 1, height: 4 }),
1161 None
1162 );
1163
1164 assert_eq!(
1165 Message::placeholder_frame(None, 4, &ImagePreviewSize { width: 4, height: 1 }),
1166 None
1167 );
1168
1169 assert_eq!(
1170 Message::placeholder_frame(Some("OK"), 4, &ImagePreviewSize { width: 4, height: 4 }),
1171 pretty_frame_test(
1172 r#"
1173⌌ ⌍
1174 OK
1175
1176⌎ ⌏
1177"#
1178 )
1179 );
1180 assert_eq!(
1181 Message::placeholder_frame(Some("idontfit"), 4, &ImagePreviewSize {
1182 width: 4,
1183 height: 4,
1184 }),
1185 pretty_frame_test(
1186 r#"
1187⌌ ⌍
1188
1189
1190⌎ ⌏
1191"#
1192 )
1193 );
1194 assert_eq!(
1195 Message::placeholder_frame(Some("OK"), 4, &ImagePreviewSize { width: 4, height: 2 }),
1196 pretty_frame_test(
1197 r#"
1198⌌ ⌍
1199⌎ ⌏
1200"#
1201 )
1202 );
1203 assert_eq!(
1204 Message::placeholder_frame(Some("OK"), 4, &ImagePreviewSize { width: 2, height: 3 }),
1205 pretty_frame_test(
1206 r#"
1207⌌⌍
1208
1209⌎⌏
1210"#
1211 )
1212 );
1213 }
1125} 1214}