[fractal/multi-account: 2/3] Add account switcher




commit 6ab85148fe078886a612d038447044be83d4e7a2
Author: Alejandro Domínguez <adomu net-c com>
Date:   Tue Jul 13 15:34:37 2021 +0200

    Add account switcher

 data/resources/resources.gresource.xml             |  1 +
 data/resources/style.css                           | 11 ++++++
 data/resources/ui/sidebar-account-switcher-item.ui | 37 +++++++++++++++++
 data/resources/ui/sidebar.ui                       | 46 ++++++++++++++++++++++
 src/session/mod.rs                                 |  7 +++-
 src/session/sidebar/sidebar.rs                     | 10 ++++-
 src/window.rs                                      |  1 +
 7 files changed, 111 insertions(+), 2 deletions(-)
---
diff --git a/data/resources/resources.gresource.xml b/data/resources/resources.gresource.xml
index 2a6d9825..54ac0678 100644
--- a/data/resources/resources.gresource.xml
+++ b/data/resources/resources.gresource.xml
@@ -18,6 +18,7 @@
     <file compressed="true" preprocess="xml-stripblanks" alias="login.ui">ui/login.ui</file>
     <file compressed="true" preprocess="xml-stripblanks" alias="session.ui">ui/session.ui</file>
     <file compressed="true" preprocess="xml-stripblanks" alias="sidebar.ui">ui/sidebar.ui</file>
+    <file compressed="true" preprocess="xml-stripblanks" 
alias="sidebar-account-switcher-item.ui">ui/sidebar-account-switcher-item.ui</file>
     <file compressed="true" preprocess="xml-stripblanks" alias="sidebar-item.ui">ui/sidebar-item.ui</file>
     <file compressed="true" preprocess="xml-stripblanks" 
alias="sidebar-category-row.ui">ui/sidebar-category-row.ui</file>
     <file compressed="true" preprocess="xml-stripblanks" 
alias="sidebar-entry-row.ui">ui/sidebar-entry-row.ui</file>
diff --git a/data/resources/style.css b/data/resources/style.css
index ea4085e7..aec9cc4d 100644
--- a/data/resources/style.css
+++ b/data/resources/style.css
@@ -40,6 +40,17 @@ headerbar.flat {
   border: none;
 }
 
+listview.account-switcher row {
+  border-radius: 10px;
+  margin-top: 3px;
+  margin-bottom: 3px;
+}
+
+listview.account-switcher row box {
+  padding-top: 5px;
+  padding-bottom: 5px;
+}
+
 /* Sidebar */
 .sidebar row {
   padding-left: 10px;
diff --git a/data/resources/ui/sidebar-account-switcher-item.ui 
b/data/resources/ui/sidebar-account-switcher-item.ui
new file mode 100644
index 00000000..c311c0cb
--- /dev/null
+++ b/data/resources/ui/sidebar-account-switcher-item.ui
@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <template class="GtkListItem">
+    <property name="child">
+      <object class="GtkBox">
+        <property name="spacing">10</property>
+        <child type="start">
+          <object class="ComponentsAvatar">
+            <property name="size">40</property>
+            <binding name="item">
+              <lookup name="avatar" type="User">
+                <lookup name="user" type="Session">
+                  <lookup name="child" type="GtkStackPage">
+                    <lookup name="item">GtkListItem</lookup>
+                  </lookup>
+                </lookup>
+              </lookup>
+            </binding>
+          </object>
+        </child>
+        <child type="start">
+          <object class="GtkLabel">
+            <binding name="label">
+              <lookup name="display-name" type="User">
+                <lookup name="user" type="Session">
+                  <lookup name="child" type="GtkStackPage">
+                    <lookup name="item">GtkListItem</lookup>
+                  </lookup>
+                </lookup>
+              </lookup>
+            </binding>
+          </object>
+        </child>
+      </object>
+    </property>
+  </template>
+</interface>
diff --git a/data/resources/ui/sidebar.ui b/data/resources/ui/sidebar.ui
index 5de728b5..bf02da07 100644
--- a/data/resources/ui/sidebar.ui
+++ b/data/resources/ui/sidebar.ui
@@ -29,6 +29,52 @@
               <object class="AdwWindowTitle"></object>
             </property>
             <property name="show-end-title-buttons" bind-source="Sidebar" bind-property="compact" 
bind-flags="sync-create"/>
+            <child type="start">
+              <object class="GtkMenuButton" id="accounts_button">
+                <property name="icon-name">system-users-symbolic</property>
+                <property name="popover">
+                  <object class="GtkPopover">
+                    <child>
+                      <object class="GtkBox">
+                        <property name="orientation">vertical</property>
+                        <child>
+                          <object class="GtkListView" id="account_switcher">
+                            <style>
+                              <class name="account-switcher" />
+                            </style>
+                            <property name="factory">
+                              <object class="GtkBuilderListItemFactory">
+                                <property 
name="resource">/org/gnome/FractalNext/sidebar-account-switcher-item.ui</property>
+                              </object>
+                            </property>
+                          </object>
+                        </child>
+                        <child>
+                          <object class="GtkSeparator"></object>
+                        </child>
+                        <child>
+                          <object class="GtkBox">
+                            <property name="orientation">horizontal</property>
+                            <property name="spacing">10</property>
+                            <child>
+                              <object class="GtkButton">
+                                <property name="icon-name">list-add-symbolic</property>
+                                <property name="action-name">app.new-login</property>
+                              </object>
+                            </child>
+                            <child>
+                              <object class="GtkLabel">
+                                <property name="label" translatable="yes">_Add account</property>
+                              </object>
+                            </child>
+                          </object>
+                        </child>
+                      </object>
+                    </child>
+                  </object>
+                </property>
+              </object>
+            </child>
             <child type="end">
               <object class="GtkMenuButton" id="appmenu_button">
                 <property name="icon-name">open-menu-symbolic</property>
diff --git a/src/session/mod.rs b/src/session/mod.rs
index 10512fc5..69ad7d4d 100644
--- a/src/session/mod.rs
+++ b/src/session/mod.rs
@@ -26,7 +26,7 @@ use adw;
 use adw::subclass::prelude::BinImpl;
 use gtk::subclass::prelude::*;
 use gtk::{self, prelude::*};
-use gtk::{gio, glib, glib::clone, glib::SyncSender, CompositeTemplate};
+use gtk::{gio, glib, glib::clone, glib::SyncSender, CompositeTemplate, SelectionModel};
 use gtk_macros::send;
 use log::error;
 use matrix_sdk::ruma::{
@@ -432,4 +432,9 @@ impl Session {
     fn handle_sync_response(&self, response: SyncResponse) {
         self.room_list().handle_response_rooms(response.rooms);
     }
+
+    pub fn set_logged_in_users(&self, sessions_stack_pages: &SelectionModel) {
+        let priv_ = &imp::Session::from_instance(self);
+        priv_.sidebar.set_logged_in_users(sessions_stack_pages);
+    }
 }
diff --git a/src/session/sidebar/sidebar.rs b/src/session/sidebar/sidebar.rs
index 04eebb41..e9584a55 100644
--- a/src/session/sidebar/sidebar.rs
+++ b/src/session/sidebar/sidebar.rs
@@ -1,5 +1,5 @@
 use adw::subclass::prelude::BinImpl;
-use gtk::{gio, glib, prelude::*, subclass::prelude::*, CompositeTemplate};
+use gtk::{gio, glib, prelude::*, subclass::prelude::*, CompositeTemplate, SelectionModel};
 
 use crate::session::{
     content::ContentType,
@@ -23,6 +23,8 @@ mod imp {
         #[template_child]
         pub headerbar: TemplateChild<adw::HeaderBar>,
         #[template_child]
+        pub account_switcher: TemplateChild<gtk::ListView>,
+        #[template_child]
         pub listview: TemplateChild<gtk::ListView>,
         #[template_child]
         pub room_search_entry: TemplateChild<gtk::SearchEntry>,
@@ -254,4 +256,10 @@ impl Sidebar {
         priv_.selected_room.replace(selected_room);
         self.notify("selected-room");
     }
+
+    pub fn set_logged_in_users(&self, sessions_stack_pages: &SelectionModel) {
+        let account_switcher = imp::Sidebar::from_instance(self).account_switcher.get();
+
+        account_switcher.set_model(Some(sessions_stack_pages));
+    }
 }
diff --git a/src/window.rs b/src/window.rs
index 91c85dee..d76773a0 100644
--- a/src/window.rs
+++ b/src/window.rs
@@ -105,6 +105,7 @@ impl Window {
 
     fn add_session(&self, session: &Session) {
         let priv_ = &imp::Window::from_instance(self);
+        session.set_logged_in_users(&priv_.sessions.pages());
         priv_.sessions.add_child(session);
         priv_.sessions.set_visible_child(session);
         self.install_session_actions(session);


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