From aab3a13c35763a1a7486e7b2c01e09d7d68d72bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Sun, 29 Dec 2024 10:39:34 +0100 Subject: [PATCH] room-history: Do not offer to copy the thumbnail until it is loaded --- .../view/content/room_history/item_row.rs | 26 +++++++++++++++++++ .../room_history/message_row/visual_media.rs | 26 ++++++++++++++++++- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/src/session/view/content/room_history/item_row.rs b/src/session/view/content/room_history/item_row.rs index d580edf0..8934bcf8 100644 --- a/src/session/view/content/room_history/item_row.rs +++ b/src/session/view/content/room_history/item_row.rs @@ -52,6 +52,32 @@ mod imp { fn class_init(klass: &mut Self::Class) { klass.set_css_name("room-history-row"); klass.set_accessible_role(gtk::AccessibleRole::ListItem); + + klass.install_action( + "room-history-row.enable-copy-image", + Some(&bool::static_variant_type()), + |obj, _, param| { + let enable = param + .and_then(glib::Variant::get::) + .expect("The parameter should be a boolean"); + let imp = obj.imp(); + + let Some(action_group) = imp.action_group.borrow().clone() else { + error!("Could not change state of copy-image action: no action group"); + return; + }; + let Some(action) = action_group.lookup_action("copy-image") else { + error!("Could not change state of copy-image action: action not found"); + return; + }; + tracing::debug!("Action: {action:?}"); + let Some(action) = action.downcast_ref::() else { + error!("Could not change state of copy-image action: not a GSimpleAction"); + return; + }; + action.set_enabled(enable); + }, + ); } } diff --git a/src/session/view/content/room_history/message_row/visual_media.rs b/src/session/view/content/room_history/message_row/visual_media.rs index 1d80a3d8..ef38ed85 100644 --- a/src/session/view/content/room_history/message_row/visual_media.rs +++ b/src/session/view/content/room_history/message_row/visual_media.rs @@ -7,7 +7,7 @@ use gtk::{ }; use matrix_sdk::Client; use ruma::api::client::media::get_content_thumbnail::v3::Method; -use tracing::warn; +use tracing::{error, warn}; use super::ContentFormat; use crate::{ @@ -324,6 +324,11 @@ mod imp { /// Build the content for the image in the given media message. async fn build_image(&self, media_message: &VisualMediaMessage, client: Client) { + // Disable the copy-image action while the image is loading. + if matches!(media_message, VisualMediaMessage::Image(_)) { + self.enable_copy_image_action(false); + } + let scale_factor = self.obj().scale_factor(); let settings = ThumbnailSettings { @@ -364,6 +369,25 @@ mod imp { } self.set_state(LoadingState::Ready); + + // Enable the copy-image action now that the image is loaded. + if matches!(media_message, VisualMediaMessage::Image(_)) { + self.enable_copy_image_action(true); + } + } + + /// Enable or disable the context menu action to copy the image. + fn enable_copy_image_action(&self, enable: bool) { + if self + .obj() + .activate_action( + "room-history-row.enable-copy-image", + Some(&enable.to_variant()), + ) + .is_err() + { + error!("Could not change state of copy-image action: `room-history-row.enable-copy-image` action not found"); + } } /// Build the content for the video in the given media message.