|
| 1 | +use gtk::glib::{self, clone}; |
| 2 | +use gtk::prelude::*; |
| 3 | +use gtk::subclass::prelude::*; |
| 4 | +use gtk::CompositeTemplate; |
| 5 | + |
| 6 | +use crate::strings; |
| 7 | +use crate::tdlib::{Chat, ChatType, Message, MessageSender}; |
| 8 | +use crate::utils::spawn; |
| 9 | + |
| 10 | +mod imp { |
| 11 | + use gtk::glib::{ParamSpec, Properties, Value}; |
| 12 | + |
| 13 | + use super::*; |
| 14 | + use std::cell::{Cell, RefCell}; |
| 15 | + |
| 16 | + #[derive(Debug, Default, Properties, CompositeTemplate)] |
| 17 | + #[properties(wrapper_type = super::MessageReply)] |
| 18 | + #[template(string = r#" |
| 19 | + template MessageReply : Widget { |
| 20 | + Separator separator { |
| 21 | + width-request: 2; |
| 22 | + } |
| 23 | +
|
| 24 | + Box labels_box { |
| 25 | + orientation: vertical; |
| 26 | +
|
| 27 | + Label sender_label { |
| 28 | + ellipsize: end; |
| 29 | + xalign: 0; |
| 30 | +
|
| 31 | + styles ["caption-heading"] |
| 32 | + } |
| 33 | +
|
| 34 | + Label message_label { |
| 35 | + ellipsize: end; |
| 36 | + xalign: 0; |
| 37 | + single-line-mode: true; |
| 38 | +
|
| 39 | + styles [ |
| 40 | + "message", |
| 41 | + "small-body", |
| 42 | + ] |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + "#)] |
| 47 | + pub(crate) struct MessageReply { |
| 48 | + pub(super) sender_color_class: RefCell<Option<String>>, |
| 49 | + pub(super) bindings: RefCell<Vec<gtk::ExpressionWatch>>, |
| 50 | + pub(super) is_loading: Cell<bool>, |
| 51 | + |
| 52 | + #[property(get, set, construct_only)] |
| 53 | + pub(super) chat: RefCell<Option<Chat>>, |
| 54 | + #[property(get, set, construct_only)] |
| 55 | + pub(super) reply_id: Cell<i64>, |
| 56 | + #[property(get, set, construct_only)] |
| 57 | + pub(super) is_outgoing: Cell<bool>, |
| 58 | + |
| 59 | + #[template_child] |
| 60 | + pub(super) separator: TemplateChild<gtk::Separator>, |
| 61 | + #[template_child] |
| 62 | + pub(super) labels_box: TemplateChild<gtk::Box>, |
| 63 | + #[template_child] |
| 64 | + pub(super) sender_label: TemplateChild<gtk::Label>, |
| 65 | + #[template_child] |
| 66 | + pub(super) message_label: TemplateChild<gtk::Label>, |
| 67 | + } |
| 68 | + |
| 69 | + #[glib::object_subclass] |
| 70 | + impl ObjectSubclass for MessageReply { |
| 71 | + const NAME: &'static str = "MessageReply"; |
| 72 | + type Type = super::MessageReply; |
| 73 | + type ParentType = gtk::Widget; |
| 74 | + |
| 75 | + fn class_init(klass: &mut Self::Class) { |
| 76 | + Self::bind_template(klass); |
| 77 | + klass.set_layout_manager_type::<gtk::BoxLayout>(); |
| 78 | + klass.set_css_name("messagereply"); |
| 79 | + } |
| 80 | + |
| 81 | + fn instance_init(obj: &glib::subclass::InitializingObject<Self>) { |
| 82 | + obj.init_template(); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + impl ObjectImpl for MessageReply { |
| 87 | + fn properties() -> &'static [ParamSpec] { |
| 88 | + Self::derived_properties() |
| 89 | + } |
| 90 | + |
| 91 | + fn set_property(&self, id: usize, value: &Value, pspec: &ParamSpec) { |
| 92 | + self.derived_set_property(id, value, pspec) |
| 93 | + } |
| 94 | + |
| 95 | + fn property(&self, id: usize, pspec: &ParamSpec) -> Value { |
| 96 | + self.derived_property(id, pspec) |
| 97 | + } |
| 98 | + |
| 99 | + fn constructed(&self) { |
| 100 | + self.is_loading.set(true); |
| 101 | + self.message_label |
| 102 | + .set_label(&gettextrs::gettext("Loading ...")); |
| 103 | + self.obj().load_message(); |
| 104 | + } |
| 105 | + |
| 106 | + fn dispose(&self) { |
| 107 | + self.dispose_template(); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + impl WidgetImpl for MessageReply {} |
| 112 | +} |
| 113 | + |
| 114 | +glib::wrapper! { |
| 115 | + pub(crate) struct MessageReply(ObjectSubclass<imp::MessageReply>) |
| 116 | + @extends gtk::Widget; |
| 117 | +} |
| 118 | + |
| 119 | +impl MessageReply { |
| 120 | + pub(crate) fn new(chat: Chat, reply_id: i64, is_outgoing: bool) -> Self { |
| 121 | + glib::Object::builder() |
| 122 | + .property("chat", chat) |
| 123 | + .property("reply-id", reply_id) |
| 124 | + .property("is-outgoing", is_outgoing) |
| 125 | + .build() |
| 126 | + } |
| 127 | + |
| 128 | + fn load_message(&self) { |
| 129 | + let imp = self.imp(); |
| 130 | + |
| 131 | + let reply_id = imp.reply_id.get(); |
| 132 | + let chat = self.chat().unwrap(); |
| 133 | + |
| 134 | + if let Some(message) = chat.message(reply_id) { |
| 135 | + self.update_from_message(&message); |
| 136 | + } else { |
| 137 | + spawn(clone!(@weak self as obj => async move { |
| 138 | + let chat = obj.imp().chat.borrow().clone().unwrap(); |
| 139 | + if let Ok(message) = chat.fetch_message(reply_id).await { |
| 140 | + obj.update_from_message(&message); |
| 141 | + } else { |
| 142 | + // Message doesn't exist, so we should remove "Loading..." caption |
| 143 | + // TODO: Impelent it properly using signals |
| 144 | + obj.imp().message_label.set_label("Deleted message"); |
| 145 | + } |
| 146 | + })); |
| 147 | + } |
| 148 | + |
| 149 | + imp.is_loading.set(false); |
| 150 | + } |
| 151 | + |
| 152 | + pub(crate) fn set_max_char_width(&self, n_chars: i32) { |
| 153 | + self.imp().message_label.set_max_width_chars(n_chars); |
| 154 | + self.imp().sender_label.set_max_width_chars(n_chars); |
| 155 | + } |
| 156 | + |
| 157 | + fn update_from_message(&self, message: &Message) { |
| 158 | + let imp = self.imp(); |
| 159 | + let mut bindings = imp.bindings.borrow_mut(); |
| 160 | + while let Some(binding) = bindings.pop() { |
| 161 | + binding.unwatch(); |
| 162 | + } |
| 163 | + |
| 164 | + // Remove the previous color css class |
| 165 | + let mut sender_color_class = imp.sender_color_class.borrow_mut(); |
| 166 | + if let Some(class) = sender_color_class.as_ref() { |
| 167 | + self.remove_css_class(class); |
| 168 | + } |
| 169 | + // Show sender label, if needed |
| 170 | + let show_sender = !matches!( |
| 171 | + message.chat().type_(), |
| 172 | + ChatType::Supergroup(data) if data.is_channel() |
| 173 | + ); |
| 174 | + if show_sender { |
| 175 | + let sender_name_expression = message.sender_name_expression(); |
| 176 | + let sender_binding = |
| 177 | + sender_name_expression.bind(&*imp.sender_label, "label", glib::Object::NONE); |
| 178 | + |
| 179 | + bindings.push(sender_binding); |
| 180 | + |
| 181 | + if !imp.is_outgoing.get() { |
| 182 | + // Color sender label |
| 183 | + if let MessageSender::User(user) = message.sender() { |
| 184 | + let classes = vec![ |
| 185 | + "sender-text-red", |
| 186 | + "sender-text-orange", |
| 187 | + "sender-text-violet", |
| 188 | + "sender-text-green", |
| 189 | + "sender-text-cyan", |
| 190 | + "sender-text-blue", |
| 191 | + "sender-text-pink", |
| 192 | + ]; |
| 193 | + |
| 194 | + let color_class = classes[user.id() as usize % classes.len()]; |
| 195 | + self.add_css_class(color_class); |
| 196 | + |
| 197 | + *sender_color_class = Some(color_class.into()); |
| 198 | + } |
| 199 | + } |
| 200 | + imp.sender_label.set_visible(true); |
| 201 | + } |
| 202 | + |
| 203 | + // Set content label expression |
| 204 | + |
| 205 | + let caption = strings::message_content(message.clone().as_ref()); |
| 206 | + imp.message_label.set_label(&caption); |
| 207 | + |
| 208 | + self.imp().is_loading.set(false); |
| 209 | + } |
| 210 | +} |
0 commit comments