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.
This commit is contained in:
Kévin Commaille 2023-10-02 10:22:43 +02:00
parent 9f5ddd9ee1
commit 9655abbcae
No known key found for this signature in database
GPG key ID: 29A48C1F03620416
2 changed files with 24 additions and 8 deletions

View file

@ -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<gtk::Widget> {
let child = self.child()?;
if let Some(reply) = child.downcast_ref::<MessageReply>() {
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<gdk::Texture> {
let mut content = self.child()?;
if let Some(reply) = content.downcast_ref::<MessageReply>() {
content = reply.content().child()?;
}
content.downcast_ref::<MessageMedia>()?.texture()
self.content_widget()?
.downcast_ref::<MessageMedia>()?
.texture()
}
}

View file

@ -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::<MessageMedia>().unwrap();
let Some(media_widget) = imp.content.content_widget().and_downcast::<MessageMedia>()
else {
warn!("Trying to show media of a non-media message");
return;
};
window.session_view().show_media(event, &media_widget);
}
}