diff --git a/src/components/pill.rs b/src/components/pill.rs index cd7d8b8f..0e1f4f4f 100644 --- a/src/components/pill.rs +++ b/src/components/pill.rs @@ -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 { diff --git a/src/main.rs b/src/main.rs index 9417d707..cb6cbd71 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ mod application; #[rustfmt::skip] mod config; +mod prelude; mod components; mod error; diff --git a/src/prelude.rs b/src/prelude.rs new file mode 100644 index 00000000..e9c522d6 --- /dev/null +++ b/src/prelude.rs @@ -0,0 +1 @@ +pub use crate::session::UserExt; diff --git a/src/session/content/message_row.rs b/src/session/content/message_row.rs index ffe2db39..dfc8d362 100644 --- a/src/session/content/message_row.rs +++ b/src/session/content/message_row.rs @@ -17,6 +17,7 @@ use matrix_sdk::ruma::events::{ }; use sourceview::prelude::*; +use crate::prelude::*; use crate::session::room::Event; mod imp { diff --git a/src/session/mod.rs b/src/session/mod.rs index 136b3dae..3084f7cd 100644 --- a/src/session/mod.rs +++ b/src/session/mod.rs @@ -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; diff --git a/src/session/room/room.rs b/src/session/room/room.rs index ba59fb3e..e8e3ad05 100644 --- a/src/session/room/room.rs +++ b/src/session/room/room.rs @@ -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, diff --git a/src/session/user.rs b/src/session/user.rs index 389f991b..ab0c87d8 100644 --- a/src/session/user.rs +++ b/src/session/user.rs @@ -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 { + 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) { - let priv_ = imp::User::from_instance(self); + fn set_display_name(&self, display_name: Option) { + 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) { + fn set_avatar_url(&self, url: Option) { 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) { - let priv_ = imp::User::from_instance(self); + fn update_from_member_event(&self, event: &SyncStateEvent) { + 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, - ) { + fn update_from_stripped_member_event(&self, event: &StrippedStateEvent) { 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> UserExt for T {} + +unsafe impl IsSubclassable for User { + fn class_init(class: &mut glib::Class) { + >::class_init(class.upcast_ref_mut()); + } + + fn instance_init(instance: &mut glib::subclass::InitializingObject) { + >::instance_init(instance); + } +}