From 9655abbcaee56aa4a8d464fb4fe9a366241262e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Mon, 2 Oct 2023 10:22:43 +0200 Subject: [PATCH] message-row: Get the proper widget to show media It was using the child of the widget, while it could be deeper in the hierarchy because of replies. --- .../room_history/message_row/content.rs | 24 +++++++++++++------ .../content/room_history/message_row/mod.rs | 8 ++++++- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/session/view/content/room_history/message_row/content.rs b/src/session/view/content/room_history/message_row/content.rs index 20352094..4b2380ff 100644 --- a/src/session/view/content/room_history/message_row/content.rs +++ b/src/session/view/content/room_history/message_row/content.rs @@ -110,6 +110,20 @@ impl MessageContent { self.notify("format"); } + /// Access the widget with the own content of the event. + /// + /// This allows to access the descendant content while discarding the + /// content of a related message, like a replied-to event. + pub fn content_widget(&self) -> Option { + let child = self.child()?; + + if let Some(reply) = child.downcast_ref::() { + reply.content().child() + } else { + Some(child) + } + } + pub fn update_for_event(&self, event: &Event) { let format = self.format(); if format == ContentFormat::Natural { @@ -168,13 +182,9 @@ impl MessageContent { /// Get the texture displayed by this widget, if any. pub fn texture(&self) -> Option { - let mut content = self.child()?; - - if let Some(reply) = content.downcast_ref::() { - content = reply.content().child()?; - } - - content.downcast_ref::()?.texture() + self.content_widget()? + .downcast_ref::()? + .texture() } } diff --git a/src/session/view/content/room_history/message_row/mod.rs b/src/session/view/content/room_history/message_row/mod.rs index fafd2f44..6378b3e9 100644 --- a/src/session/view/content/room_history/message_row/mod.rs +++ b/src/session/view/content/room_history/message_row/mod.rs @@ -15,6 +15,7 @@ use gtk::{ CompositeTemplate, }; use matrix_sdk::ruma::events::room::message::MessageType; +use tracing::warn; pub use self::content::ContentFormat; use self::{content::MessageContent, media::MessageMedia, reaction_list::MessageReactionList}; @@ -244,7 +245,12 @@ impl MessageRow { }; if matches!(message, MessageType::Image(_) | MessageType::Video(_)) { - let media_widget = imp.content.child().and_downcast::().unwrap(); + let Some(media_widget) = imp.content.content_widget().and_downcast::() + else { + warn!("Trying to show media of a non-media message"); + return; + }; + window.session_view().show_media(event, &media_widget); } }