From 950571be817163659c10bbfda0f3b2bda67a9cb0 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Mon, 3 Jun 2024 17:52:05 +0200 Subject: [PATCH] Switch from gtk::Label to a custom chat tab widget This one supports avatars now! --- src/main.rs | 12 +++--- src/widgets/chat_tab.rs | 81 +++++++++++++++++++++++++++++++++++++++++ src/widgets/chat_tab.ui | 24 ++++++++++++ src/widgets/mod.rs | 19 ++++++++++ 4 files changed, 130 insertions(+), 6 deletions(-) create mode 100644 src/widgets/chat_tab.rs create mode 100644 src/widgets/chat_tab.ui create mode 100644 src/widgets/mod.rs diff --git a/src/main.rs b/src/main.rs index e5f5f14..b532a10 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,6 +20,7 @@ use gtk::{gio, glib}; mod message; mod poezio_logs; mod tab; +mod widgets; mod window; mod xmpp_client; @@ -119,18 +120,17 @@ fn main() { let tabs_factory = gtk::SignalListItemFactory::new(); tabs_factory.connect_setup(move |_, list_item| { - let label = gtk::Label::new(None); - label.set_halign(gtk::Align::Start); + let tab_widget = widgets::ChatTab::new(); let list_item: >k::ListItem = list_item.downcast_ref().unwrap(); - list_item.set_child(Some(&label)); + list_item.set_child(Some(&tab_widget)); }); tabs_factory.connect_bind(move |_, list_item| { let list_item: >k::ListItem = list_item.downcast_ref().unwrap(); let tab: Tab = list_item.item().and_downcast().unwrap(); - let label: gtk::Label = list_item.child().and_downcast().unwrap(); + let tab_widget: widgets::ChatTab = list_item.child().and_downcast().unwrap(); - label.set_label(&tab.name()); - label.set_tooltip_text(Some(&tab.jid())); + tab_widget.set_name(&tab.name()); + tab_widget.set_jid(&tab.jid()); }); win.tabs().set_factory(Some(&tabs_factory)); diff --git a/src/widgets/chat_tab.rs b/src/widgets/chat_tab.rs new file mode 100644 index 0000000..cee8853 --- /dev/null +++ b/src/widgets/chat_tab.rs @@ -0,0 +1,81 @@ +// xmpp-client: A sample GTK client in Rust +// Copyright (C) 2024 Link Mauve +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +use adw::prelude::*; +use adw::subclass::prelude::*; +use gtk::glib; + +mod imp { + use adw::subclass::prelude::*; + use gtk::glib; + + /// The private struct, which can hold widgets and other data. + #[derive(Debug, Default, gtk::CompositeTemplate)] + #[template(file = "chat_tab.ui")] + pub struct ChatTab { + #[template_child] + pub avatar: TemplateChild, + #[template_child] + pub name: TemplateChild, + } + + #[glib::object_subclass] + impl ObjectSubclass for ChatTab { + const NAME: &'static str = "ChatTab"; + type Type = super::ChatTab; + type ParentType = gtk::Box; + + fn class_init(class: &mut Self::Class) { + class.bind_template(); + } + + fn instance_init(obj: &glib::subclass::InitializingObject) { + obj.init_template(); + } + } + + impl ObjectImpl for ChatTab { + fn constructed(&self) { + self.parent_constructed(); + } + } + + impl WidgetImpl for ChatTab {} + impl BoxImpl for ChatTab {} +} + +glib::wrapper! { + pub struct ChatTab(ObjectSubclass) + @extends gtk::Widget, gtk::Box; +} + +impl ChatTab { + pub fn new() -> Self { + glib::Object::builder().build() + } + + pub fn set_jid(&self, jid: &str) { + let hash = "123456789abcdef123456789abcdef123456789a"; + self.imp().avatar.set_from_file(Some(format!( + "/home/linkmauve/cache/poezio/avatars/{jid}/{hash}" + ))); + self.set_tooltip_text(Some(jid)); + } + + pub fn set_name(&self, name: &str) { + self.imp().name.set_label(name); + } +} diff --git a/src/widgets/chat_tab.ui b/src/widgets/chat_tab.ui new file mode 100644 index 0000000..992eb42 --- /dev/null +++ b/src/widgets/chat_tab.ui @@ -0,0 +1,24 @@ + + + + diff --git a/src/widgets/mod.rs b/src/widgets/mod.rs new file mode 100644 index 0000000..89a3c22 --- /dev/null +++ b/src/widgets/mod.rs @@ -0,0 +1,19 @@ +// xmpp-client: A sample GTK client in Rust +// Copyright (C) 2024 Link Mauve +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +mod chat_tab; + +pub use chat_tab::ChatTab;