Switch from gtk::Label to a custom chat tab widget

This one supports avatars now!
This commit is contained in:
Emmanuel Gil Peyrot 2024-06-03 17:52:05 +02:00
parent 130d6bebfc
commit 950571be81
4 changed files with 130 additions and 6 deletions

View File

@ -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: &gtk::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: &gtk::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));

81
src/widgets/chat_tab.rs Normal file
View File

@ -0,0 +1,81 @@
// xmpp-client: A sample GTK client in Rust
// Copyright (C) 2024 Link Mauve <linkmauve@linkmauve.fr>
//
// 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 <http://www.gnu.org/licenses/>.
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<gtk::Image>,
#[template_child]
pub name: TemplateChild<gtk::Label>,
}
#[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<Self>) {
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<imp::ChatTab>)
@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);
}
}

24
src/widgets/chat_tab.ui Normal file
View File

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<template class="ChatTab" parent="GtkBox">
<child>
<object class="GtkImage" id="avatar">
<property name="width-request">32</property>
<property name="height-request">32</property>
<property name="margin-top">4</property>
<property name="margin-bottom">4</property>
<property name="margin-start">4</property>
<property name="margin-end">4</property>
</object>
</child>
<child>
<object class="GtkLabel" id="name">
<property name="halign">start</property>
<property name="margin-top">4</property>
<property name="margin-bottom">4</property>
<property name="margin-start">4</property>
<property name="margin-end">4</property>
</object>
</child>
</template>
</interface>

19
src/widgets/mod.rs Normal file
View File

@ -0,0 +1,19 @@
// xmpp-client: A sample GTK client in Rust
// Copyright (C) 2024 Link Mauve <linkmauve@linkmauve.fr>
//
// 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 <http://www.gnu.org/licenses/>.
mod chat_tab;
pub use chat_tab::ChatTab;