@@ -30,38 +30,35 @@ live_design! {
3030 width: 150 , height: Fit ,
3131 flow: Right ,
3232 spacing: 10 ,
33- margin: { left: 20 , top: 20 }
34- align: { y: 0.5 }
33+ margin: { left: 20 , top: 40 }
34+ align: { y: 0.5 }
3535
3636 avatar = <Avatar > {
37- width: 40 ,
38- height: 40 ,
37+ width: 40 , height: 40 ,
3938 }
4039
4140 content = <View > {
4241 width: Fill , height: Fit ,
4342 flow: Down ,
4443 spacing: 4 ,
45- align: { x: 0.0 }
4644
4745 username = <Label > {
4846 width: Fill , height: Fit ,
4947 draw_text: {
5048 text_style: <REGULAR_TEXT >{ font_size: 14 } ,
5149 color: ( COLOR_TEXT )
5250 }
53- text: ""
5451 }
5552 timestamp_view = <View > {
5653 width: Fill , height: Fit
5754 timestamp = <Timestamp > {
5855 width: Fill , height: Fit ,
59- margin: { left: 5 }
56+ margin: { left: 5 }
6057 }
6158 }
62-
6359 }
6460 }
61+
6562 image_name_and_size = <Label > {
6663 width: Fill , height: Fit ,
6764 margin: { top: 40 }
@@ -72,24 +69,32 @@ live_design! {
7269 wrap: Word
7370 }
7471 }
72+
7573 empty_right_container = <View > {
7674 // equal width as the top-left container to keep the image name centered.
7775 width: 150 , height: Fit ,
7876 }
7977 }
8078}
8179
80+ /// A room image message detail widget that displays a user's avatar, username, and message date.
8281#[ derive( Live , LiveHook , Widget ) ]
8382pub struct RoomImageMessageDetail {
84- #[ deref] view : View ,
83+ #[ deref] view : View ,
84+ /// The sender of the message
8585 #[ rust] sender : Option < OwnedUserId > ,
86+ /// The profile of the sender
8687 #[ rust] sender_profile : Option < TimelineDetails < Profile > > ,
88+ /// The room ID
8789 #[ rust] room_id : Option < OwnedRoomId > ,
90+ /// The event ID
8891 #[ rust] event_id : Option < OwnedEventId > ,
92+ /// The sender's avatar has been drawn. Will not be drawn again if set to `true`.
8993 #[ rust] avatar_drawn : bool ,
94+ /// The name of the image.
9095 #[ rust] image_name : String ,
96+ /// The size of the image in bytes.
9197 #[ rust] image_size : i32 ,
92- #[ rust] is_desktop : bool
9398}
9499
95100/// Convert bytes to human-readable file size format
@@ -120,14 +125,12 @@ fn format_file_size(bytes: i32) -> String {
120125 }
121126}
122127
123- /// Maximum image name length for desktop display
124- const MAX_IMAGE_NAME_LENGTH_DESKTOP : usize = 50 ;
125- /// Maximum image name length for mobile display
126- const MAX_IMAGE_NAME_LENGTH_MOBILE : usize = 10 ;
128+ /// Maximum image name length to be displayed
129+ const MAX_IMAGE_NAME_LENGTH : usize = 50 ;
127130
128- /// Truncate image name based on display context while preserving file extension
129- fn truncate_image_name ( image_name : & str , is_desktop : bool ) -> String {
130- let max_length = if is_desktop { MAX_IMAGE_NAME_LENGTH_DESKTOP } else { MAX_IMAGE_NAME_LENGTH_MOBILE } ;
131+ /// Truncate image name while preserving file extension
132+ fn truncate_image_name ( image_name : & str ) -> String {
133+ let max_length = MAX_IMAGE_NAME_LENGTH ;
131134
132135 if image_name. len ( ) <= max_length {
133136 return image_name. to_string ( ) ;
@@ -159,24 +162,22 @@ impl Widget for RoomImageMessageDetail {
159162 }
160163
161164 fn draw_walk ( & mut self , cx : & mut Cx2d , scope : & mut Scope , walk : Walk ) -> DrawStep {
162- let is_desktop = cx. display_context . is_desktop ( ) ;
163- if self . is_desktop != is_desktop && !self . image_name . is_empty ( ) {
164- let truncated_name = truncate_image_name ( & self . image_name , is_desktop) ;
165- let human_readable_size = format_file_size ( self . image_size ) ;
166- let display_text = format ! ( "{} ({})" , truncated_name, human_readable_size) ;
167- self . label ( id ! ( image_name_and_size) ) . set_text ( cx, & display_text) ;
168- self . is_desktop = is_desktop;
169- }
170-
171165 if !self . avatar_drawn {
172- let avatar_ref = self . avatar ( id ! ( top_left_container. avatar) ) ;
166+ let avatar_ref = self . avatar ( ids ! ( top_left_container. avatar) ) ;
173167 let Some ( room_id) = & self . room_id else { return DrawStep :: done ( ) } ;
174168 let Some ( sender) = & self . sender else { return DrawStep :: done ( ) } ;
175169 let ( username, avatar_drawn) = avatar_ref. set_avatar_and_get_username ( cx, room_id, sender, self . sender_profile . as_ref ( ) , self . event_id . as_deref ( ) ) ;
176- self . label ( id ! ( top_left_container. username) ) . set_text ( cx, & username) ;
170+ self . label ( ids ! ( top_left_container. username) ) . set_text ( cx, & username) ;
177171 self . avatar_drawn = avatar_drawn;
178172 let is_desktop = cx. display_context . is_desktop ( ) ;
179- self . is_desktop = is_desktop;
173+ let truncated_name = truncate_image_name ( & self . image_name ) ;
174+ let human_readable_size = format_file_size ( self . image_size ) ;
175+ let display_text = format ! ( "{} ({})" , truncated_name, human_readable_size) ;
176+ self . label ( ids ! ( image_name_and_size) ) . set_text ( cx, & display_text) ;
177+ let empty_right_container_width = if is_desktop { 150 } else { 0 } ;
178+ self . view ( ids ! ( empty_right_container) ) . apply_over ( cx, live ! {
179+ width: ( empty_right_container_width)
180+ } ) ;
180181 }
181182
182183 self . view . draw_walk ( cx, scope, walk)
@@ -200,16 +201,15 @@ impl MatchEvent for RoomImageMessageDetail {
200201 self . event_id = event_id. clone ( ) ;
201202 self . avatar_drawn = false ;
202203 // Format and display image name and size
203- let is_desktop = cx. display_context . is_desktop ( ) ;
204- let truncated_name = truncate_image_name ( & image_name, is_desktop) ;
204+ let truncated_name = truncate_image_name ( & image_name) ;
205205 let human_readable_size = format_file_size ( image_size) ;
206206 let display_text = format ! ( "{} ({})" , truncated_name, human_readable_size) ;
207207 self . image_name = image_name;
208208 self . image_size = image_size;
209- self . label ( id ! ( image_name_and_size) ) . set_text ( cx, & display_text) ;
209+ self . label ( ids ! ( image_name_and_size) ) . set_text ( cx, & display_text) ;
210210 if let Some ( dt) = unix_time_millis_to_datetime ( timestamp_millis) {
211- self . view ( id ! ( timestamp_view) ) . set_visible ( cx, true ) ;
212- self . timestamp ( id ! ( timestamp) ) . set_date_time ( cx, dt) ;
211+ self . view ( ids ! ( timestamp_view) ) . set_visible ( cx, true ) ;
212+ self . timestamp ( ids ! ( timestamp) ) . set_date_time ( cx, dt) ;
213213 }
214214 }
215215 }
@@ -227,9 +227,9 @@ impl RoomImageMessageDetail {
227227 self . image_size = 0 ;
228228
229229 // Clear the UI elements
230- self . label ( id ! ( top_left_container. username) ) . set_text ( cx, "" ) ;
231- self . label ( id ! ( image_name_and_size) ) . set_text ( cx, "" ) ;
232- self . view ( id ! ( timestamp_view) ) . set_visible ( cx, false ) ;
230+ self . label ( ids ! ( top_left_container. username) ) . set_text ( cx, "" ) ;
231+ self . label ( ids ! ( image_name_and_size) ) . set_text ( cx, "" ) ;
232+ self . view ( ids ! ( timestamp_view) ) . set_visible ( cx, false ) ;
233233 }
234234}
235235
@@ -263,4 +263,4 @@ pub enum RoomImageMessageDetailAction {
263263 image_size : i32
264264 } ,
265265 None ,
266- }
266+ }
0 commit comments