|
| 1 | +use adw::prelude::*; |
| 2 | +use glib::clone; |
| 3 | +use gtk::subclass::prelude::*; |
| 4 | +use gtk::{glib, CompositeTemplate}; |
| 5 | +use tdlib::enums::MessageContent; |
| 6 | +use tdlib::types::File; |
| 7 | + |
| 8 | +use crate::session::components::StickerPreview; |
| 9 | +use crate::session::content::message_row::{MessageBase, MessageBaseImpl, MessageIndicators}; |
| 10 | +use crate::tdlib::Message; |
| 11 | + |
| 12 | +use super::base::MessageBaseExt; |
| 13 | + |
| 14 | +mod imp { |
| 15 | + use super::*; |
| 16 | + use once_cell::sync::Lazy; |
| 17 | + use std::cell::RefCell; |
| 18 | + |
| 19 | + #[derive(Debug, Default, CompositeTemplate)] |
| 20 | + #[template(resource = "/com/github/melix99/telegrand/ui/content-message-animated-emoji.ui")] |
| 21 | + pub(crate) struct MessageAnimatedEmoji { |
| 22 | + pub(super) message: RefCell<Option<Message>>, |
| 23 | + #[template_child] |
| 24 | + pub(super) gtk_box: TemplateChild<gtk::Box>, |
| 25 | + #[template_child] |
| 26 | + pub(super) click: TemplateChild<gtk::GestureClick>, |
| 27 | + #[template_child] |
| 28 | + pub(super) bin: TemplateChild<adw::Bin>, |
| 29 | + #[template_child] |
| 30 | + pub(super) indicators: TemplateChild<MessageIndicators>, |
| 31 | + } |
| 32 | + |
| 33 | + #[glib::object_subclass] |
| 34 | + impl ObjectSubclass for MessageAnimatedEmoji { |
| 35 | + const NAME: &'static str = "ContentMessageAnimatedEmoji"; |
| 36 | + type Type = super::MessageAnimatedEmoji; |
| 37 | + type ParentType = MessageBase; |
| 38 | + |
| 39 | + fn class_init(klass: &mut Self::Class) { |
| 40 | + Self::bind_template(klass); |
| 41 | + klass.set_css_name("messagesticker"); |
| 42 | + } |
| 43 | + |
| 44 | + fn instance_init(obj: &glib::subclass::InitializingObject<Self>) { |
| 45 | + obj.init_template(); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + impl ObjectImpl for MessageAnimatedEmoji { |
| 50 | + fn properties() -> &'static [glib::ParamSpec] { |
| 51 | + static PROPERTIES: Lazy<Vec<glib::ParamSpec>> = Lazy::new(|| { |
| 52 | + vec![glib::ParamSpecObject::new( |
| 53 | + "message", |
| 54 | + "Message", |
| 55 | + "The message represented by this row", |
| 56 | + Message::static_type(), |
| 57 | + glib::ParamFlags::READWRITE | glib::ParamFlags::EXPLICIT_NOTIFY, |
| 58 | + )] |
| 59 | + }); |
| 60 | + PROPERTIES.as_ref() |
| 61 | + } |
| 62 | + |
| 63 | + fn set_property(&self, _id: usize, value: &glib::Value, pspec: &glib::ParamSpec) { |
| 64 | + match pspec.name() { |
| 65 | + "message" => self.obj().set_message(value.get().unwrap()), |
| 66 | + _ => unimplemented!(), |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + fn property(&self, _id: usize, pspec: &glib::ParamSpec) -> glib::Value { |
| 71 | + match pspec.name() { |
| 72 | + "message" => self.message.borrow().to_value(), |
| 73 | + _ => unimplemented!(), |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + fn constructed(&self) { |
| 78 | + self.click |
| 79 | + .connect_released(clone!(@to-owned self as imp => move |_, _, _, _| { |
| 80 | + if let Some(animation) = imp.bin.child() { |
| 81 | + if let Ok(animation) = animation.downcast::<rlt::Animation>() { |
| 82 | + animation.play(); |
| 83 | + } |
| 84 | + } |
| 85 | + })); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + impl WidgetImpl for MessageAnimatedEmoji { |
| 90 | + fn measure(&self, orientation: gtk::Orientation, for_size: i32) -> (i32, i32, i32, i32) { |
| 91 | + const SIZE: i32 = 120; |
| 92 | + let min = self.gtk_box.measure(orientation, for_size).0; |
| 93 | + if orientation == gtk::Orientation::Horizontal { |
| 94 | + (SIZE, SIZE, -1, -1) |
| 95 | + } else { |
| 96 | + (SIZE + min, SIZE + min, -1, -1) |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + fn size_allocate(&self, width: i32, height: i32, baseline: i32) { |
| 101 | + self.gtk_box.allocate(width, height, baseline, None); |
| 102 | + } |
| 103 | + } |
| 104 | + impl MessageBaseImpl for MessageAnimatedEmoji {} |
| 105 | +} |
| 106 | + |
| 107 | +glib::wrapper! { |
| 108 | + pub(crate) struct MessageAnimatedEmoji(ObjectSubclass<imp::MessageAnimatedEmoji>) |
| 109 | + @extends gtk::Widget, MessageBase; |
| 110 | +} |
| 111 | + |
| 112 | +impl MessageBaseExt for MessageAnimatedEmoji { |
| 113 | + type Message = Message; |
| 114 | + |
| 115 | + fn set_message(&self, message: Self::Message) { |
| 116 | + let imp = self.imp(); |
| 117 | + |
| 118 | + if imp.message.borrow().as_ref() == Some(&message) { |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + imp.indicators.set_message(message.clone().upcast()); |
| 123 | + |
| 124 | + if let MessageContent::MessageAnimatedEmoji(data) = message.content().0 { |
| 125 | + let data = data.animated_emoji; |
| 126 | + |
| 127 | + let sticker = data.sticker.unwrap(); |
| 128 | + |
| 129 | + let preview = StickerPreview::new(sticker.outline.clone()); |
| 130 | + self.imp().bin.set_child(Some(&preview)); |
| 131 | + |
| 132 | + let file = sticker.sticker; |
| 133 | + |
| 134 | + let looped = matches!( |
| 135 | + sticker.full_type, |
| 136 | + tdlib::enums::StickerFullType::CustomEmoji(_) |
| 137 | + ); |
| 138 | + |
| 139 | + if file.local.is_downloading_completed { |
| 140 | + self.load_sticker(&file.local.path, looped) |
| 141 | + } else { |
| 142 | + let (sender, receiver) = |
| 143 | + glib::MainContext::sync_channel::<File>(Default::default(), 5); |
| 144 | + |
| 145 | + receiver.attach( |
| 146 | + None, |
| 147 | + clone!(@weak self as obj => @default-return glib::Continue(false), move |file| { |
| 148 | + if file.local.is_downloading_completed { |
| 149 | + obj.load_sticker(&file.local.path, looped); |
| 150 | + } |
| 151 | + |
| 152 | + glib::Continue(true) |
| 153 | + }), |
| 154 | + ); |
| 155 | + |
| 156 | + message.chat().session().download_file(file.id, sender); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + imp.message.replace(Some(message)); |
| 161 | + |
| 162 | + self.notify("message"); |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +impl MessageAnimatedEmoji { |
| 167 | + fn load_sticker(&self, path: &str, looped: bool) { |
| 168 | + let path = path.to_owned(); |
| 169 | + |
| 170 | + let animation = rlt::Animation::from_filename(&path); |
| 171 | + animation.set_loop(looped); |
| 172 | + animation.use_cache(looped); |
| 173 | + animation.play(); |
| 174 | + |
| 175 | + self.imp().bin.set_child(Some(&animation)); |
| 176 | + } |
| 177 | +} |
0 commit comments