2021-05-20 07:22:47 +00:00
|
|
|
use adw::subclass::prelude::*;
|
2022-07-22 09:25:51 +00:00
|
|
|
use gtk::{glib, prelude::*, CompositeTemplate};
|
2021-05-20 07:22:47 +00:00
|
|
|
|
2022-01-20 08:24:22 +00:00
|
|
|
use crate::{
|
|
|
|
components::Avatar,
|
|
|
|
prelude::*,
|
2023-05-21 14:52:51 +00:00
|
|
|
session::model::{Room, User},
|
2022-01-20 08:24:22 +00:00
|
|
|
};
|
2021-05-20 07:22:47 +00:00
|
|
|
|
|
|
|
mod imp {
|
|
|
|
use std::cell::RefCell;
|
|
|
|
|
2022-01-20 08:24:22 +00:00
|
|
|
use glib::subclass::InitializingObject;
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
2021-05-20 07:22:47 +00:00
|
|
|
#[derive(Debug, Default, CompositeTemplate)]
|
2023-05-21 21:11:45 +00:00
|
|
|
#[template(resource = "/org/gnome/Fractal/ui/components/pill.ui")]
|
2021-05-28 13:18:10 +00:00
|
|
|
pub struct Pill {
|
2021-05-20 07:22:47 +00:00
|
|
|
/// The user displayed by this widget
|
|
|
|
pub user: RefCell<Option<User>>,
|
2021-05-26 08:59:34 +00:00
|
|
|
/// The room displayed by this widget
|
|
|
|
pub room: RefCell<Option<Room>>,
|
2021-05-20 07:22:47 +00:00
|
|
|
#[template_child]
|
|
|
|
pub display_name: TemplateChild<gtk::Label>,
|
|
|
|
#[template_child]
|
2021-06-01 15:14:28 +00:00
|
|
|
pub avatar: TemplateChild<Avatar>,
|
2021-05-26 08:59:34 +00:00
|
|
|
pub bindings: RefCell<Vec<glib::Binding>>,
|
2021-05-20 07:22:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[glib::object_subclass]
|
2021-05-28 13:18:10 +00:00
|
|
|
impl ObjectSubclass for Pill {
|
|
|
|
const NAME: &'static str = "Pill";
|
|
|
|
type Type = super::Pill;
|
2021-05-20 07:22:47 +00:00
|
|
|
type ParentType = adw::Bin;
|
|
|
|
|
|
|
|
fn class_init(klass: &mut Self::Class) {
|
|
|
|
Self::bind_template(klass);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn instance_init(obj: &InitializingObject<Self>) {
|
|
|
|
obj.init_template();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-28 13:18:10 +00:00
|
|
|
impl ObjectImpl for Pill {
|
2021-05-20 07:22:47 +00:00
|
|
|
fn properties() -> &'static [glib::ParamSpec] {
|
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
static PROPERTIES: Lazy<Vec<glib::ParamSpec>> = Lazy::new(|| {
|
2021-05-26 08:59:34 +00:00
|
|
|
vec![
|
2022-10-26 10:25:41 +00:00
|
|
|
glib::ParamSpecObject::builder::<User>("user")
|
|
|
|
.explicit_notify()
|
|
|
|
.build(),
|
|
|
|
glib::ParamSpecObject::builder::<Room>("room")
|
|
|
|
.explicit_notify()
|
|
|
|
.build(),
|
2021-05-26 08:59:34 +00:00
|
|
|
]
|
2021-05-20 07:22:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
PROPERTIES.as_ref()
|
|
|
|
}
|
|
|
|
|
2022-10-25 11:59:39 +00:00
|
|
|
fn set_property(&self, _id: usize, value: &glib::Value, pspec: &glib::ParamSpec) {
|
|
|
|
let obj = self.obj();
|
|
|
|
|
2021-05-20 07:22:47 +00:00
|
|
|
match pspec.name() {
|
2021-05-26 08:59:34 +00:00
|
|
|
"user" => obj.set_user(value.get().unwrap()),
|
|
|
|
"room" => obj.set_room(value.get().unwrap()),
|
2021-05-20 07:22:47 +00:00
|
|
|
_ => unimplemented!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-25 11:59:39 +00:00
|
|
|
fn property(&self, _id: usize, pspec: &glib::ParamSpec) -> glib::Value {
|
|
|
|
let obj = self.obj();
|
|
|
|
|
2021-05-20 07:22:47 +00:00
|
|
|
match pspec.name() {
|
|
|
|
"user" => obj.user().to_value(),
|
2021-05-26 08:59:34 +00:00
|
|
|
"room" => obj.room().to_value(),
|
2021-05-20 07:22:47 +00:00
|
|
|
_ => unimplemented!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-28 13:18:10 +00:00
|
|
|
impl WidgetImpl for Pill {}
|
2021-05-20 07:22:47 +00:00
|
|
|
|
2021-05-28 13:18:10 +00:00
|
|
|
impl BinImpl for Pill {}
|
2021-05-20 07:22:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
glib::wrapper! {
|
2021-08-05 11:27:03 +00:00
|
|
|
/// Inline widget displaying an emphasized `User` or `Room`.
|
2021-05-28 13:18:10 +00:00
|
|
|
pub struct Pill(ObjectSubclass<imp::Pill>)
|
2021-05-20 07:22:47 +00:00
|
|
|
@extends gtk::Widget, adw::Bin, @implements gtk::Accessible;
|
|
|
|
}
|
|
|
|
|
2021-05-28 13:18:10 +00:00
|
|
|
impl Pill {
|
2022-02-16 12:58:59 +00:00
|
|
|
pub fn for_user(user: &User) -> Self {
|
2022-10-25 11:59:39 +00:00
|
|
|
glib::Object::builder().property("user", user).build()
|
2022-02-16 12:58:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn for_room(room: &Room) -> Self {
|
2022-10-25 11:59:39 +00:00
|
|
|
glib::Object::builder().property("room", room).build()
|
2021-05-20 07:22:47 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 10:25:41 +00:00
|
|
|
/// Set the user displayed by this widget.
|
2022-11-06 11:40:13 +00:00
|
|
|
///
|
|
|
|
/// This removes the room, if one was set.
|
2021-05-20 07:22:47 +00:00
|
|
|
pub fn set_user(&self, user: Option<User>) {
|
2022-10-26 10:43:59 +00:00
|
|
|
let imp = self.imp();
|
2021-05-20 07:22:47 +00:00
|
|
|
|
2022-10-26 10:43:59 +00:00
|
|
|
if *imp.user.borrow() == user {
|
2021-05-20 07:22:47 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-26 10:43:59 +00:00
|
|
|
while let Some(binding) = imp.bindings.borrow_mut().pop() {
|
2021-05-26 08:59:34 +00:00
|
|
|
binding.unbind();
|
|
|
|
}
|
2022-11-06 11:40:13 +00:00
|
|
|
self.set_room(None);
|
2021-05-26 08:59:34 +00:00
|
|
|
|
|
|
|
if let Some(ref user) = user {
|
|
|
|
let display_name_binding = user
|
2022-10-26 10:43:59 +00:00
|
|
|
.bind_property("display-name", &*imp.display_name, "label")
|
2021-05-26 08:59:34 +00:00
|
|
|
.flags(glib::BindingFlags::SYNC_CREATE)
|
2022-01-19 22:15:39 +00:00
|
|
|
.build();
|
2021-05-26 08:59:34 +00:00
|
|
|
|
2022-10-26 10:43:59 +00:00
|
|
|
imp.bindings.borrow_mut().push(display_name_binding);
|
2021-05-26 08:59:34 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 10:43:59 +00:00
|
|
|
imp.avatar
|
2023-05-02 08:59:55 +00:00
|
|
|
.set_data(user.as_ref().map(|user| user.avatar_data().clone()));
|
2022-10-26 10:43:59 +00:00
|
|
|
imp.user.replace(user);
|
2021-05-20 07:22:47 +00:00
|
|
|
|
|
|
|
self.notify("user");
|
|
|
|
}
|
|
|
|
|
2022-10-26 10:25:41 +00:00
|
|
|
/// The user displayed by this widget.
|
2021-05-20 07:22:47 +00:00
|
|
|
pub fn user(&self) -> Option<User> {
|
2022-01-24 15:49:41 +00:00
|
|
|
self.imp().user.borrow().clone()
|
2021-05-20 07:22:47 +00:00
|
|
|
}
|
2021-05-26 08:59:34 +00:00
|
|
|
|
2022-10-26 10:25:41 +00:00
|
|
|
/// Set the room displayed by this widget.
|
2022-11-06 11:40:13 +00:00
|
|
|
///
|
|
|
|
/// This removes the user, if one was set.
|
2021-05-26 08:59:34 +00:00
|
|
|
pub fn set_room(&self, room: Option<Room>) {
|
2022-10-26 10:43:59 +00:00
|
|
|
let imp = self.imp();
|
2021-05-26 08:59:34 +00:00
|
|
|
|
2022-10-26 10:43:59 +00:00
|
|
|
if *imp.room.borrow() == room {
|
2021-05-26 08:59:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-26 10:43:59 +00:00
|
|
|
while let Some(binding) = imp.bindings.borrow_mut().pop() {
|
2021-05-26 08:59:34 +00:00
|
|
|
binding.unbind();
|
|
|
|
}
|
2022-11-06 11:40:13 +00:00
|
|
|
self.set_user(None);
|
2021-05-26 08:59:34 +00:00
|
|
|
|
|
|
|
if let Some(ref room) = room {
|
|
|
|
let display_name_binding = room
|
2022-10-26 10:43:59 +00:00
|
|
|
.bind_property("display-name", &*imp.display_name, "label")
|
2021-05-26 08:59:34 +00:00
|
|
|
.flags(glib::BindingFlags::SYNC_CREATE)
|
2022-01-19 22:15:39 +00:00
|
|
|
.build();
|
2021-05-26 08:59:34 +00:00
|
|
|
|
2022-10-26 10:43:59 +00:00
|
|
|
imp.bindings.borrow_mut().push(display_name_binding);
|
2021-05-26 08:59:34 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 10:43:59 +00:00
|
|
|
imp.avatar
|
2023-05-02 08:59:55 +00:00
|
|
|
.set_data(room.as_ref().map(|room| room.avatar_data().clone()));
|
2022-10-26 10:43:59 +00:00
|
|
|
imp.room.replace(room);
|
2021-05-26 08:59:34 +00:00
|
|
|
|
|
|
|
self.notify("room");
|
|
|
|
}
|
|
|
|
|
2022-10-26 10:25:41 +00:00
|
|
|
/// The room displayed by this widget.
|
2021-05-26 08:59:34 +00:00
|
|
|
pub fn room(&self) -> Option<Room> {
|
2022-01-24 15:49:41 +00:00
|
|
|
self.imp().room.borrow().clone()
|
2021-05-26 08:59:34 +00:00
|
|
|
}
|
2021-05-20 07:22:47 +00:00
|
|
|
}
|