[fractal/fractal-next] pill: Add constructors for room or user



commit 78c890f071dc3302960bff075a8b25e0cd6c5ad9
Author: Kévin Commaille <zecakeh tedomum fr>
Date:   Wed Feb 16 13:58:59 2022 +0100

    pill: Add constructors for room or user

 src/components/pill.rs                                 | 14 ++++++--------
 src/session/content/room_details/invite_subpage/mod.rs |  3 +--
 src/session/room/mod.rs                                | 18 ++++++------------
 3 files changed, 13 insertions(+), 22 deletions(-)
---
diff --git a/src/components/pill.rs b/src/components/pill.rs
index 52cc999dd..1822a02b8 100644
--- a/src/components/pill.rs
+++ b/src/components/pill.rs
@@ -103,8 +103,12 @@ glib::wrapper! {
 }
 
 impl Pill {
-    pub fn new() -> Self {
-        glib::Object::new(&[]).expect("Failed to create Pill")
+    pub fn for_user(user: &User) -> Self {
+        glib::Object::new(&[("user", user)]).expect("Failed to create Pill")
+    }
+
+    pub fn for_room(room: &Room) -> Self {
+        glib::Object::new(&[("room", room)]).expect("Failed to create Pill")
     }
 
     pub fn set_user(&self, user: Option<User>) {
@@ -171,9 +175,3 @@ impl Pill {
         self.imp().room.borrow().clone()
     }
 }
-
-impl Default for Pill {
-    fn default() -> Self {
-        Self::new()
-    }
-}
diff --git a/src/session/content/room_details/invite_subpage/mod.rs 
b/src/session/content/room_details/invite_subpage/mod.rs
index a1f4fd8ba..9931a9095 100644
--- a/src/session/content/room_details/invite_subpage/mod.rs
+++ b/src/session/content/room_details/invite_subpage/mod.rs
@@ -253,10 +253,9 @@ impl InviteSubpage {
     fn add_user_pill(&self, user: &Invitee) {
         let priv_ = self.imp();
 
-        let pill = Pill::new();
+        let pill = Pill::for_user(user.upcast_ref());
         pill.set_margin_start(3);
         pill.set_margin_end(3);
-        pill.set_user(Some(user.clone().upcast()));
 
         let (mut start_iter, mut end_iter) = priv_.text_buffer.bounds();
 
diff --git a/src/session/room/mod.rs b/src/session/room/mod.rs
index 20de7d7d4..29b7aeea2 100644
--- a/src/session/room/mod.rs
+++ b/src/session/room/mod.rs
@@ -407,8 +407,7 @@ impl Room {
                     Err(error) => {
                             error!("Couldn’t forget the room: {}", error);
 
-                            let room_pill = Pill::new();
-                            room_pill.set_room(Some(obj.clone()));
+                            let room_pill = Pill::for_room(&obj);
                             let error = Toast::builder()
                                 .title(&gettext("Failed to forget <widget>."))
                                 .widgets(&[&room_pill])
@@ -553,8 +552,7 @@ impl Room {
                         Err(error) => {
                                 error!("Couldn’t set the room category: {}", error);
 
-                                let room_pill = Pill::new();
-                                room_pill.set_room(Some(obj.clone()));
+                                let room_pill = Pill::for_room(&obj);
                                 let error = Toast::builder()
                                     .title(&gettext!(
                                         "Failed to move <widget> from {} to {}.",
@@ -1057,8 +1055,7 @@ impl Room {
                 Err(error) => {
                     error!("Accepting invitation failed: {}", error);
 
-                    let room_pill = Pill::new();
-                    room_pill.set_room(Some(self.clone()));
+                    let room_pill = Pill::for_room(self);
                     let error = Toast::builder()
                         .title(&gettext(
                             "Failed to accept invitation for <widget>. Try again later.",
@@ -1089,8 +1086,7 @@ impl Room {
                 Err(error) => {
                     error!("Rejecting invitation failed: {}", error);
 
-                    let room_pill = Pill::new();
-                    room_pill.set_room(Some(self.clone()));
+                    let room_pill = Pill::for_room(self);
                     let error = Toast::builder()
                         .title(&gettext(
                             "Failed to reject invitation for <widget>. Try again later.",
@@ -1246,10 +1242,8 @@ impl Room {
                 } else {
                     gettext("Failed to invite <widget> and some other users to <widget>. Try again later.")
                 };
-                let user_pill = Pill::new();
-                user_pill.set_user(Some(first_failed.clone()));
-                let room_pill = Pill::new();
-                room_pill.set_room(Some(self.clone()));
+                let user_pill = Pill::for_user(first_failed);
+                let room_pill = Pill::for_room(self);
                 let error = Toast::builder()
                     .title(&error_message)
                     .widgets(&[&user_pill, &room_pill])


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