[fractal/fractal-next] member: Add power-level property



commit 22f697c1490bbc73e351e048646048764ad54cf3
Author: Kai A. Hiller <V02460 gmail com>
Date:   Mon Aug 9 10:05:37 2021 +0200

    member: Add power-level property

 src/session/room/member.rs | 47 ++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 45 insertions(+), 2 deletions(-)
---
diff --git a/src/session/room/member.rs b/src/session/room/member.rs
index c848ad81..33f5e2af 100644
--- a/src/session/room/member.rs
+++ b/src/session/room/member.rs
@@ -1,4 +1,5 @@
 use gtk::glib;
+use gtk::prelude::*;
 use gtk::subclass::prelude::*;
 use matrix_sdk::ruma::events::room::member::MemberEventContent;
 use matrix_sdk::ruma::events::{StrippedStateEvent, SyncStateEvent};
@@ -10,9 +11,13 @@ use crate::session::{Room, User};
 
 mod imp {
     use super::*;
+    use once_cell::sync::Lazy;
+    use std::cell::Cell;
 
     #[derive(Debug, Default)]
-    pub struct Member {}
+    pub struct Member {
+        pub power_level: Cell<u32>,
+    }
 
     #[glib::object_subclass]
     impl ObjectSubclass for Member {
@@ -21,7 +26,30 @@ mod imp {
         type ParentType = User;
     }
 
-    impl ObjectImpl for Member {}
+    impl ObjectImpl for Member {
+        fn properties() -> &'static [glib::ParamSpec] {
+            static PROPERTIES: Lazy<Vec<glib::ParamSpec>> = Lazy::new(|| {
+                vec![glib::ParamSpec::new_uint(
+                    "power-level",
+                    "Power level",
+                    "Power level of the member in its room.",
+                    0,
+                    100,
+                    0,
+                    glib::ParamFlags::READABLE | glib::ParamFlags::EXPLICIT_NOTIFY,
+                )]
+            });
+
+            PROPERTIES.as_ref()
+        }
+
+        fn property(&self, obj: &Self::Type, _id: usize, pspec: &glib::ParamSpec) -> glib::Value {
+            match pspec.name() {
+                "power-level" => obj.power_level().to_value(),
+                _ => unimplemented!(),
+            }
+        }
+    }
 }
 
 glib::wrapper! {
@@ -36,6 +64,20 @@ impl Member {
             .expect("Failed to create Member")
     }
 
+    pub fn power_level(&self) -> u32 {
+        let priv_ = imp::Member::from_instance(self);
+        priv_.power_level.get()
+    }
+
+    fn set_power_level(&self, power_level: u32) {
+        if self.power_level() == power_level {
+            return;
+        }
+        let priv_ = imp::Member::from_instance(self);
+        priv_.power_level.replace(power_level);
+        self.notify("power-level");
+    }
+
     /// Update the user based on the the room member state event
     pub fn update_from_room_member(&self, member: &RoomMember) {
         if member.user_id() != self.user_id() {
@@ -45,6 +87,7 @@ impl Member {
 
         self.set_display_name(member.display_name().map(String::from));
         self.avatar().set_url(member.avatar_url().cloned());
+        self.set_power_level(member.power_level().clamp(0, 100) as u32);
     }
 
     /// Update the user based on the the room member state event


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]