[fractal/fractal-next] components: Allow to use UserPill also for rooms



commit a9cbcd7d332a6684c642f2d9cbd1b136965cc246
Author: Julian Sparber <julian sparber net>
Date:   Wed May 26 10:59:34 2021 +0200

    components: Allow to use UserPill also for rooms

 data/resources/style.css       |  4 +++
 data/resources/ui/user-pill.ui |  6 +---
 src/components/user_pill.rs    | 80 +++++++++++++++++++++++++++++++++++-------
 src/session/mod.rs             |  2 +-
 4 files changed, 74 insertions(+), 18 deletions(-)
---
diff --git a/data/resources/style.css b/data/resources/style.css
index b4e3ba64..294d5bac 100644
--- a/data/resources/style.css
+++ b/data/resources/style.css
@@ -14,6 +14,10 @@
   padding-right: 6px;
 }
 
+.app-notification .user-pill {
+  background-color: alpha(@theme_fg_color, 0.35);
+}
+
 /* Login */
 .login {
   min-width: 250px;
diff --git a/data/resources/ui/user-pill.ui b/data/resources/ui/user-pill.ui
index aff200ae..fe06abff 100644
--- a/data/resources/ui/user-pill.ui
+++ b/data/resources/ui/user-pill.ui
@@ -2,6 +2,7 @@
 <interface>
   <template class="UserPill" parent="AdwBin">
     <property name="focusable">True</property>
+    <property name="valign">center</property>
     <style>
       <class name="user-pill"/>
     </style>
@@ -19,11 +20,6 @@
           <object class="GtkLabel" id="display_name">
             <property name="ellipsize">middle</property>
             <property name="max-width-chars">30</property>
-            <binding name="label">
-              <lookup name="display-name">
-                <lookup name="user">UserPill</lookup>
-              </lookup>
-            </binding>
           </object>
         </child>
       </object>
diff --git a/src/components/user_pill.rs b/src/components/user_pill.rs
index cfe92d4e..723e9874 100644
--- a/src/components/user_pill.rs
+++ b/src/components/user_pill.rs
@@ -3,7 +3,7 @@ use gtk::prelude::*;
 use gtk::subclass::prelude::*;
 use gtk::{glib, CompositeTemplate};
 
-use crate::session::User;
+use crate::session::{Room, User};
 
 mod imp {
     use super::*;
@@ -15,10 +15,13 @@ mod imp {
     pub struct UserPill {
         /// The user displayed by this widget
         pub user: RefCell<Option<User>>,
+        /// The room displayed by this widget
+        pub room: RefCell<Option<Room>>,
         #[template_child]
         pub display_name: TemplateChild<gtk::Label>,
         #[template_child]
         pub avatar: TemplateChild<adw::Avatar>,
+        pub bindings: RefCell<Vec<glib::Binding>>,
     }
 
     #[glib::object_subclass]
@@ -40,13 +43,22 @@ mod imp {
         fn properties() -> &'static [glib::ParamSpec] {
             use once_cell::sync::Lazy;
             static PROPERTIES: Lazy<Vec<glib::ParamSpec>> = Lazy::new(|| {
-                vec![glib::ParamSpec::new_object(
-                    "user",
-                    "User",
-                    "The user displayed by this widget",
-                    User::static_type(),
-                    glib::ParamFlags::READWRITE | glib::ParamFlags::EXPLICIT_NOTIFY,
-                )]
+                vec![
+                    glib::ParamSpec::new_object(
+                        "user",
+                        "User",
+                        "The user displayed by this widget",
+                        User::static_type(),
+                        glib::ParamFlags::READWRITE | glib::ParamFlags::EXPLICIT_NOTIFY,
+                    ),
+                    glib::ParamSpec::new_object(
+                        "room",
+                        "Room",
+                        "The room displayed by this widget",
+                        Room::static_type(),
+                        glib::ParamFlags::READWRITE | glib::ParamFlags::EXPLICIT_NOTIFY,
+                    ),
+                ]
             });
 
             PROPERTIES.as_ref()
@@ -60,10 +72,8 @@ mod imp {
             pspec: &glib::ParamSpec,
         ) {
             match pspec.name() {
-                "user" => {
-                    let user = value.get().unwrap();
-                    obj.set_user(user);
-                }
+                "user" => obj.set_user(value.get().unwrap()),
+                "room" => obj.set_room(value.get().unwrap()),
                 _ => unimplemented!(),
             }
         }
@@ -71,6 +81,7 @@ mod imp {
         fn property(&self, obj: &Self::Type, _id: usize, pspec: &glib::ParamSpec) -> glib::Value {
             match pspec.name() {
                 "user" => obj.user().to_value(),
+                "room" => obj.room().to_value(),
                 _ => unimplemented!(),
             }
         }
@@ -99,6 +110,20 @@ impl UserPill {
             return;
         }
 
+        while let Some(binding) = priv_.bindings.borrow_mut().pop() {
+            binding.unbind();
+        }
+
+        if let Some(ref user) = user {
+            let display_name_binding = user
+                .bind_property("display-name", &*priv_.display_name, "label")
+                .flags(glib::BindingFlags::SYNC_CREATE)
+                .build()
+                .unwrap();
+
+            priv_.bindings.borrow_mut().push(display_name_binding);
+        }
+
         priv_.user.replace(user);
 
         self.notify("user");
@@ -108,4 +133,35 @@ impl UserPill {
         let priv_ = imp::UserPill::from_instance(self);
         priv_.user.borrow().clone()
     }
+
+    pub fn set_room(&self, room: Option<Room>) {
+        let priv_ = imp::UserPill::from_instance(self);
+
+        if *priv_.room.borrow() == room {
+            return;
+        }
+
+        while let Some(binding) = priv_.bindings.borrow_mut().pop() {
+            binding.unbind();
+        }
+
+        if let Some(ref room) = room {
+            let display_name_binding = room
+                .bind_property("display-name", &*priv_.display_name, "label")
+                .flags(glib::BindingFlags::SYNC_CREATE)
+                .build()
+                .unwrap();
+
+            priv_.bindings.borrow_mut().push(display_name_binding);
+        }
+
+        priv_.room.replace(room);
+
+        self.notify("room");
+    }
+
+    pub fn room(&self) -> Option<Room> {
+        let priv_ = imp::UserPill::from_instance(self);
+        priv_.room.borrow().clone()
+    }
 }
diff --git a/src/session/mod.rs b/src/session/mod.rs
index 21b3e2b4..a3285ad4 100644
--- a/src/session/mod.rs
+++ b/src/session/mod.rs
@@ -7,7 +7,7 @@ mod user;
 
 use self::categories::Categories;
 use self::content::Content;
-use self::room::Room;
+pub use self::room::Room;
 use self::room_list::RoomList;
 use self::sidebar::Sidebar;
 pub use self::user::User;


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