user: Make User subclassable

This commit is contained in:
Kai A. Hiller 2021-08-08 22:20:22 +02:00
parent c85ac6d191
commit 44053ca6b1
7 changed files with 37 additions and 21 deletions

View file

@ -4,6 +4,7 @@ use gtk::prelude::*;
use gtk::subclass::prelude::*;
use gtk::{glib, CompositeTemplate};
use crate::prelude::*;
use crate::session::{Room, User};
mod imp {

View file

@ -6,6 +6,7 @@
mod application;
#[rustfmt::skip]
mod config;
mod prelude;
mod components;
mod error;

1
src/prelude.rs Normal file
View file

@ -0,0 +1 @@
pub use crate::session::UserExt;

View file

@ -17,6 +17,7 @@ use matrix_sdk::ruma::events::{
};
use sourceview::prelude::*;
use crate::prelude::*;
use crate::session::room::Event;
mod imp {

View file

@ -11,7 +11,7 @@ use self::content::Content;
pub use self::room::Room;
use self::room_list::RoomList;
use self::sidebar::Sidebar;
pub use self::user::User;
pub use self::user::{User, UserExt};
use crate::components::InAppNotification;
use crate::secret;

View file

@ -30,6 +30,7 @@ use std::cell::RefCell;
use std::convert::TryFrom;
use crate::components::{LabelWithWidgets, Pill};
use crate::prelude::*;
use crate::session::{
room::{Event, HighlightFlags, RoomType, Timeline},
Avatar, Session, User,

View file

@ -120,19 +120,21 @@ impl User {
glib::Object::new(&[("session", session), ("user-id", &user_id.as_str())])
.expect("Failed to create User")
}
}
pub fn session(&self) -> &Session {
let priv_ = imp::User::from_instance(self);
pub trait UserExt: IsA<User> {
fn session(&self) -> &Session {
let priv_ = imp::User::from_instance(self.upcast_ref());
priv_.session.get().unwrap()
}
pub fn user_id(&self) -> &UserId {
let priv_ = imp::User::from_instance(self);
fn user_id(&self) -> &UserId {
let priv_ = imp::User::from_instance(self.upcast_ref());
priv_.user_id.get().unwrap()
}
pub fn display_name(&self) -> String {
let priv_ = imp::User::from_instance(self);
fn display_name(&self) -> String {
let priv_ = imp::User::from_instance(self.upcast_ref());
if let Some(display_name) = priv_.display_name.borrow().to_owned() {
display_name
@ -141,24 +143,24 @@ impl User {
}
}
pub fn set_display_name(&self, display_name: Option<String>) {
let priv_ = imp::User::from_instance(self);
fn set_display_name(&self, display_name: Option<String>) {
let priv_ = imp::User::from_instance(self.upcast_ref());
priv_.display_name.replace(display_name);
self.notify("display-name");
}
pub fn avatar(&self) -> &Avatar {
let priv_ = imp::User::from_instance(self);
fn avatar(&self) -> &Avatar {
let priv_ = imp::User::from_instance(self.upcast_ref());
priv_.avatar.get().unwrap()
}
pub fn set_avatar_url(&self, url: Option<MxcUri>) {
fn set_avatar_url(&self, url: Option<MxcUri>) {
self.avatar().set_url(url);
}
/// Update the user based on the the room member state event
pub fn update_from_room_member(&self, member: &RoomMember) {
let priv_ = imp::User::from_instance(self);
fn update_from_room_member(&self, member: &RoomMember) {
let priv_ = imp::User::from_instance(self.upcast_ref());
let user_id = priv_.user_id.get().unwrap();
if member.user_id().as_str() != user_id {
@ -175,8 +177,8 @@ impl User {
}
/// Update the user based on the the room member state event
pub fn update_from_member_event(&self, event: &SyncStateEvent<MemberEventContent>) {
let priv_ = imp::User::from_instance(self);
fn update_from_member_event(&self, event: &SyncStateEvent<MemberEventContent>) {
let priv_ = imp::User::from_instance(self.upcast_ref());
let user_id = priv_.user_id.get().unwrap();
if event.sender.as_str() != user_id {
return;
@ -200,12 +202,9 @@ impl User {
}
/// Update the user based on the the stripped room member state event
pub fn update_from_stripped_member_event(
&self,
event: &StrippedStateEvent<MemberEventContent>,
) {
fn update_from_stripped_member_event(&self, event: &StrippedStateEvent<MemberEventContent>) {
let changed = {
let priv_ = imp::User::from_instance(self);
let priv_ = imp::User::from_instance(self.upcast_ref());
let user_id = priv_.user_id.get().unwrap();
if event.sender.as_str() != user_id {
return;
@ -237,3 +236,15 @@ impl User {
}
}
}
impl<T: IsA<User>> UserExt for T {}
unsafe impl<T: ObjectImpl + 'static> IsSubclassable<T> for User {
fn class_init(class: &mut glib::Class<Self>) {
<glib::Object as IsSubclassable<T>>::class_init(class.upcast_ref_mut());
}
fn instance_init(instance: &mut glib::subclass::InitializingObject<T>) {
<glib::Object as IsSubclassable<T>>::instance_init(instance);
}
}