@@ -10,8 +10,8 @@ use std::ops::{Deref, DerefMut};
1010
1111use chrono:: { DateTime , Local as LocalTz } ;
1212use humansize:: { format_size, DECIMAL } ;
13- use image:: ImageReader ;
1413use matrix_sdk:: ruma:: events:: receipt:: ReceiptThread ;
14+ use matrix_sdk:: ruma:: events:: room:: MediaSource ;
1515use matrix_sdk:: ruma:: room_version_rules:: RedactionRules ;
1616use serde_json:: json;
1717use unicode_width:: UnicodeWidthStr ;
@@ -59,6 +59,7 @@ use modalkit::prelude::*;
5959use ratatui_image:: protocol:: Protocol ;
6060
6161use crate :: config:: ImagePreviewSize ;
62+ use crate :: preview:: { ImageStatus , PreviewManager } ;
6263use crate :: {
6364 base:: RoomInfo ,
6465 config:: ApplicationSettings ,
@@ -732,6 +733,7 @@ impl<'a> MessageFormatter<'a> {
732733 text : & mut Text < ' a > ,
733734 info : & ' a RoomInfo ,
734735 settings : & ' a ApplicationSettings ,
736+ previews : & ' a PreviewManager ,
735737 ) -> Option < ProtocolPreview < ' a > > {
736738 let reply_style = if settings. tunables . message_user_color {
737739 style. patch ( settings. get_user_color ( & msg. sender ) )
@@ -741,7 +743,7 @@ impl<'a> MessageFormatter<'a> {
741743
742744 let width = self . width ( ) ;
743745 let w = width. saturating_sub ( 2 ) ;
744- let ( mut replied, proto) = msg. show_msg ( w, reply_style, true , settings) ;
746+ let ( mut replied, proto) = msg. show_msg ( w, reply_style, true , settings, previews ) ;
745747 let mut sender = msg. sender_span ( info, self . settings ) ;
746748 let sender_width = UnicodeWidthStr :: width ( sender. content . as_ref ( ) ) ;
747749 let trailing = w. saturating_sub ( sender_width + 1 ) ;
@@ -842,21 +844,13 @@ impl<'a> MessageFormatter<'a> {
842844 }
843845}
844846
845- pub enum ImageStatus {
846- None ,
847- Downloading ( ImagePreviewSize ) ,
848- Loading ( Option < ImageReader < std:: io:: Cursor < Vec < u8 > > > > , ImagePreviewSize ) ,
849- Loaded ( Protocol ) ,
850- Error ( String ) ,
851- }
852-
853847pub struct Message {
854848 pub event : MessageEvent ,
855849 pub sender : OwnedUserId ,
856850 pub timestamp : MessageTimeStamp ,
857851 pub downloaded : bool ,
858852 pub html : Option < StyleTree > ,
859- pub image_preview : ImageStatus ,
853+ pub image_preview : Option < MediaSource > ,
860854}
861855
862856impl Message {
@@ -870,7 +864,7 @@ impl Message {
870864 timestamp,
871865 downloaded,
872866 html,
873- image_preview : ImageStatus :: None ,
867+ image_preview : None ,
874868 }
875869 }
876870
@@ -895,7 +889,7 @@ impl Message {
895889 }
896890 }
897891
898- fn thread_root ( & self ) -> Option < OwnedEventId > {
892+ pub fn thread_root ( & self ) -> Option < OwnedEventId > {
899893 let content = match & self . event {
900894 MessageEvent :: EncryptedOriginal ( _) => return None ,
901895 MessageEvent :: EncryptedRedacted ( _) => return None ,
@@ -1002,6 +996,7 @@ impl Message {
1002996 vwctx : & ViewportContext < MessageCursor > ,
1003997 info : & ' a RoomInfo ,
1004998 settings : & ' a ApplicationSettings ,
999+ previews : & ' a PreviewManager ,
10051000 ) -> ( Text < ' a > , [ Option < ProtocolPreview < ' a > > ; 2 ] ) {
10061001 let width = vwctx. get_width ( ) ;
10071002
@@ -1017,11 +1012,11 @@ impl Message {
10171012 . and_then ( |e| info. get_event ( & e) ) ;
10181013 let proto_reply = reply. as_ref ( ) . and_then ( |r| {
10191014 // Format the reply header, push it into the `Text` buffer, and get any image.
1020- fmt. push_in_reply ( r, style, & mut text, info, settings)
1015+ fmt. push_in_reply ( r, style, & mut text, info, settings, previews )
10211016 } ) ;
10221017
10231018 // Now show the message contents, and the inlined reply if we couldn't find it above.
1024- let ( msg, proto) = self . show_msg ( width, style, reply. is_some ( ) , settings) ;
1019+ let ( msg, proto) = self . show_msg ( width, style, reply. is_some ( ) , settings, previews ) ;
10251020
10261021 // Given our text so far, determine the image offset.
10271022 let proto_main = proto. map ( |p| {
@@ -1059,8 +1054,9 @@ impl Message {
10591054 vwctx : & ViewportContext < MessageCursor > ,
10601055 info : & ' a RoomInfo ,
10611056 settings : & ' a ApplicationSettings ,
1057+ previews : & ' a PreviewManager ,
10621058 ) -> Text < ' a > {
1063- self . show_with_preview ( prev, selected, vwctx, info, settings) . 0
1059+ self . show_with_preview ( prev, selected, vwctx, info, settings, previews ) . 0
10641060 }
10651061
10661062 fn show_msg < ' a > (
@@ -1069,6 +1065,7 @@ impl Message {
10691065 style : Style ,
10701066 hide_reply : bool ,
10711067 settings : & ' a ApplicationSettings ,
1068+ previews : & ' a PreviewManager ,
10721069 ) -> ( Text < ' a > , Option < & ' a Protocol > ) {
10731070 if let Some ( html) = & self . html {
10741071 ( html. to_text ( width, style, hide_reply, settings) , None )
@@ -1083,20 +1080,21 @@ impl Message {
10831080 }
10841081
10851082 let mut proto = None ;
1086- let placeholder = match & self . image_preview {
1087- ImageStatus :: None => None ,
1088- ImageStatus :: Downloading ( image_preview_size) => {
1089- placeholder_frame ( Some ( "Downloading..." ) , width, image_preview_size)
1090- } ,
1091- ImageStatus :: Loading ( _, image_preview_size) => {
1092- placeholder_frame ( Some ( "Loading..." ) , width, image_preview_size)
1093- } ,
1094- ImageStatus :: Loaded ( backend) => {
1095- proto = Some ( backend) ;
1096- placeholder_frame ( Some ( "No Space..." ) , width, & backend. area ( ) . into ( ) )
1097- } ,
1098- ImageStatus :: Error ( err) => Some ( format ! ( "[Image error: {err}]\n " ) ) ,
1099- } ;
1083+ let placeholder =
1084+ match self . image_preview . as_ref ( ) . and_then ( |source| previews. get ( source) ) {
1085+ None => None ,
1086+ Some ( ImageStatus :: Queued ( image_preview_size) ) => {
1087+ placeholder_frame ( Some ( "Queued..." ) , width, image_preview_size)
1088+ } ,
1089+ Some ( ImageStatus :: Downloading ( image_preview_size) ) => {
1090+ placeholder_frame ( Some ( "Downloading..." ) , width, image_preview_size)
1091+ } ,
1092+ Some ( ImageStatus :: Loaded ( backend) ) => {
1093+ proto = Some ( backend) ;
1094+ placeholder_frame ( Some ( "No Space..." ) , width, & backend. area ( ) . into ( ) )
1095+ } ,
1096+ Some ( ImageStatus :: Error ( err) ) => Some ( format ! ( "[Image error: {err}]\n " ) ) ,
1097+ } ;
11001098
11011099 if let Some ( placeholder) = placeholder {
11021100 msg. to_mut ( ) . insert_str ( 0 , & placeholder) ;
@@ -1148,7 +1146,7 @@ impl Message {
11481146 self . event . redact ( redaction, rules) ;
11491147 self . html = None ;
11501148 self . downloaded = false ;
1151- self . image_preview = ImageStatus :: None ;
1149+ self . image_preview = None ;
11521150 }
11531151}
11541152
0 commit comments