[fractal/fractal-next] room_details: Hide edit buttons when not eligible



commit 128159ac79fe91bb3a7ed6233ac94abd0dec37ba
Author: Kai A. Hiller <V02460 gmail com>
Date:   Thu Aug 12 16:04:14 2021 +0200

    room_details: Hide edit buttons when not eligible

 data/resources/ui/content-room-details.ui        |  4 +--
 src/session/content/room_details/room_details.rs | 32 ++++++++++++++++++++++++
 src/utils.rs                                     |  1 -
 3 files changed, 34 insertions(+), 3 deletions(-)
---
diff --git a/data/resources/ui/content-room-details.ui b/data/resources/ui/content-room-details.ui
index 4250759f..f978b11e 100644
--- a/data/resources/ui/content-room-details.ui
+++ b/data/resources/ui/content-room-details.ui
@@ -27,7 +27,7 @@
                   </object>
                 </child>
                 <child type="overlay">
-                  <object class="GtkButton">
+                  <object class="GtkButton" id="avatar_remove_button">
                     <property name="icon-name">window-close-symbolic</property>
                     <property name="action-name">details.remove-avatar</property>
                     <property name="halign">end</property>
@@ -39,7 +39,7 @@
                   </object>
                 </child>
                 <child type="overlay">
-                  <object class="GtkButton">
+                  <object class="GtkButton" id="avatar_edit_button">
                     <property name="icon-name">document-edit-symbolic</property>
                     <property name="action-name">details.choose-avatar</property>
                     <property name="halign">end</property>
diff --git a/src/session/content/room_details/room_details.rs 
b/src/session/content/room_details/room_details.rs
index 4f2b0504..2fa5b2bb 100644
--- a/src/session/content/room_details/room_details.rs
+++ b/src/session/content/room_details/room_details.rs
@@ -6,9 +6,12 @@ use gtk::{
     subclass::prelude::*,
     CompositeTemplate,
 };
+use matrix_sdk::ruma::events::EventType;
 
 use crate::components::CustomEntry;
+use crate::session::room::RoomAction;
 use crate::session::Room;
+use crate::utils::or_expr;
 
 mod imp {
     use super::*;
@@ -21,6 +24,10 @@ mod imp {
         pub room: OnceCell<Room>,
         pub avatar_chooser: OnceCell<gtk::FileChooserNative>,
         #[template_child]
+        pub avatar_remove_button: TemplateChild<gtk::Button>,
+        #[template_child]
+        pub avatar_edit_button: TemplateChild<gtk::Button>,
+        #[template_child]
         pub edit_toggle: TemplateChild<gtk::ToggleButton>,
         #[template_child]
         pub room_name_entry: TemplateChild<gtk::Entry>,
@@ -89,6 +96,7 @@ mod imp {
         fn constructed(&self, obj: &Self::Type) {
             self.parent_constructed(obj);
 
+            obj.init_avatar();
             obj.init_edit_toggle();
             obj.init_avatar_chooser();
         }
@@ -123,6 +131,20 @@ impl RoomDetails {
         priv_.room.set(room).expect("Room already initialized");
     }
 
+    fn init_avatar(&self) {
+        let priv_ = imp::RoomDetails::from_instance(self);
+        let avatar_remove_button = &priv_.avatar_remove_button;
+        let avatar_edit_button = &priv_.avatar_edit_button;
+
+        // Hide avatar controls when the user is not eligible to perform the actions.
+        let room = self.room();
+        let room_avatar_changeable =
+            room.new_allowed_expr(RoomAction::StateEvent(EventType::RoomAvatar));
+
+        room_avatar_changeable.bind(&avatar_remove_button.get(), "visible", None);
+        room_avatar_changeable.bind(&avatar_edit_button.get(), "visible", None);
+    }
+
     fn init_edit_toggle(&self) {
         let priv_ = imp::RoomDetails::from_instance(self);
         let edit_toggle = &priv_.edit_toggle;
@@ -157,6 +179,16 @@ impl RoomDetails {
                 let priv_ = imp::RoomDetails::from_instance(&this);
                 priv_.edit_toggle.set_active(false);
             }));
+
+        // Hide edit controls when the user is not eligible to perform the actions.
+        let room = self.room();
+        let room_name_changeable =
+            room.new_allowed_expr(RoomAction::StateEvent(EventType::RoomName));
+        let room_topic_changeable =
+            room.new_allowed_expr(RoomAction::StateEvent(EventType::RoomTopic));
+
+        let edit_toggle_visible = or_expr(room_name_changeable, room_topic_changeable);
+        edit_toggle_visible.bind(&edit_toggle.get(), "visible", None);
     }
 
     fn init_avatar_chooser(&self) {
diff --git a/src/utils.rs b/src/utils.rs
index fb0d93f0..9bc5622f 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -77,7 +77,6 @@ pub fn and_expr(a_expr: gtk::Expression, b_expr: gtk::Expression) -> gtk::Expres
 }
 
 // Returns an expression that is the or’ed result of the given boolean expressions.
-#[allow(dead_code)]
 pub fn or_expr(a_expr: gtk::Expression, b_expr: gtk::Expression) -> gtk::Expression {
     gtk::ClosureExpression::new(
         move |args| {


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