[fractal/multi-account: 1/4] Add account switcher




commit b4583c6c0c5ce420d5de17c65b6254a4bac4a757
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             |   3 +
 data/resources/style.css                           |  22 +++
 data/resources/ui/add-account-row.ui               |  23 +++
 data/resources/ui/components-avatar-overlaid.ui    |  12 ++
 data/resources/ui/components-avatar.ui             |   4 +-
 data/resources/ui/sidebar.ui                       |  15 ++
 data/resources/ui/user-entry-row.ui                |  35 ++++
 src/components/avatar.rs                           |  14 +-
 src/components/avatar_overlaid.rs                  | 109 ++++++++++++
 src/components/mod.rs                              |   2 +
 src/meson.build                                    |   5 +
 src/session/mod.rs                                 |   7 +-
 .../sidebar/account_switcher/add_account.rs        |  43 +++++
 src/session/sidebar/account_switcher/item.rs       | 152 ++++++++++++++++
 src/session/sidebar/account_switcher/mod.rs        |   3 +
 src/session/sidebar/account_switcher/user_entry.rs | 194 +++++++++++++++++++++
 src/session/sidebar/mod.rs                         |   1 +
 src/session/sidebar/sidebar.rs                     |  72 +++++++-
 src/window.rs                                      |   1 +
 19 files changed, 711 insertions(+), 6 deletions(-)
---
diff --git a/data/resources/resources.gresource.xml b/data/resources/resources.gresource.xml
index 2a6d9825..a810be13 100644
--- a/data/resources/resources.gresource.xml
+++ b/data/resources/resources.gresource.xml
@@ -1,6 +1,8 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <gresources>
   <gresource prefix="/org/gnome/FractalNext/">
+    <file compressed="true" preprocess="xml-stripblanks" 
alias="add-account-row.ui">ui/add-account-row.ui</file>
+    <file compressed="true" preprocess="xml-stripblanks" 
alias="user-entry-row.ui">ui/user-entry-row.ui</file>
     <file compressed="true" preprocess="xml-stripblanks" alias="shortcuts.ui">ui/shortcuts.ui</file>
     <file compressed="true" preprocess="xml-stripblanks" alias="content.ui">ui/content.ui</file>
     <file compressed="true" preprocess="xml-stripblanks" 
alias="content-room-history.ui">ui/content-room-history.ui</file>
@@ -29,6 +31,7 @@
     <file compressed="true" preprocess="xml-stripblanks" 
alias="spinner-button.ui">ui/spinner-button.ui</file>
     <file compressed="true" preprocess="xml-stripblanks" 
alias="in-app-notification.ui">ui/in-app-notification.ui</file>
     <file compressed="true" preprocess="xml-stripblanks" 
alias="components-avatar.ui">ui/components-avatar.ui</file>
+    <file compressed="true" preprocess="xml-stripblanks" 
alias="components-avatar-overlaid.ui">ui/components-avatar-overlaid.ui</file>
     <file compressed="true">style.css</file>
     <file preprocess="xml-stripblanks">icons/scalable/actions/send-symbolic.svg</file>
     <file preprocess="xml-stripblanks">icons/scalable/status/welcome.svg</file>
diff --git a/data/resources/style.css b/data/resources/style.css
index e5728086..355cf490 100644
--- a/data/resources/style.css
+++ b/data/resources/style.css
@@ -41,6 +41,28 @@ headerbar.flat {
   border: none;
 }
 
+/* Account switcher */
+#account-switcher row {
+  border-radius: 10px;
+  margin-top: 2px;
+  margin-bottom: 2px;
+  padding-top: 7px;
+  padding-bottom: 7px;
+}
+
+#account-switcher .user-id {
+  font-size: 12px;
+}
+
+#new-login-icon {
+  /*
+   *  2 * padding + pixel-size = size (of avatar)
+   */
+  padding: 10px;
+  background-color: lightgrey;
+  border-radius: 9999px;
+}
+
 /* Sidebar */
 .sidebar row {
   padding-left: 10px;
diff --git a/data/resources/ui/add-account-row.ui b/data/resources/ui/add-account-row.ui
new file mode 100644
index 00000000..367fe5ca
--- /dev/null
+++ b/data/resources/ui/add-account-row.ui
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <template class="AddAccountRow" parent="AdwBin">
+    <child>
+      <object class="GtkBox">
+        <property name="spacing">10</property>
+        <child>
+          <object class="GtkImage">
+            <property name="name">new-login-icon</property>
+            <property name="icon-name">list-add-symbolic</property>
+            <property name="pixel-size">20</property>
+          </object>
+        </child>
+        <child>
+          <object class="GtkLabel">
+            <property name="use-underline">true</property>
+            <property name="label">_Add Account</property>
+          </object>
+        </child>
+      </object>
+    </child>
+  </template>
+</interface>
diff --git a/data/resources/ui/components-avatar-overlaid.ui b/data/resources/ui/components-avatar-overlaid.ui
new file mode 100644
index 00000000..6997f20f
--- /dev/null
+++ b/data/resources/ui/components-avatar-overlaid.ui
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <template class="ComponentsAvatarOverlaid" parent="AdwBin">
+    <property name="child">
+      <object class="GtkOverlay" id="checkmark_overlay">
+        <child>
+          <object class="ComponentsAvatar" id="child_avatar"></object>
+        </child>
+      </object>
+    </property>
+  </template>
+</interface>
diff --git a/data/resources/ui/components-avatar.ui b/data/resources/ui/components-avatar.ui
index 3c4489e2..c8121edf 100644
--- a/data/resources/ui/components-avatar.ui
+++ b/data/resources/ui/components-avatar.ui
@@ -6,12 +6,12 @@
         <property name="show-initials">True</property>
         <binding name="custom-image">
           <lookup name="image" type="Avatar">
-              <lookup name="item">ComponentsAvatar</lookup>
+            <lookup name="item">ComponentsAvatar</lookup>
           </lookup>
         </binding>
         <binding name="text">
           <lookup name="display-name" type="Avatar">
-              <lookup name="item">ComponentsAvatar</lookup>
+            <lookup name="item">ComponentsAvatar</lookup>
           </lookup>
         </binding>
       </object>
diff --git a/data/resources/ui/sidebar.ui b/data/resources/ui/sidebar.ui
index 5de728b5..eb9f13d3 100644
--- a/data/resources/ui/sidebar.ui
+++ b/data/resources/ui/sidebar.ui
@@ -29,6 +29,21 @@
               <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="GtkListView" id="account_switcher">
+                        <property name="name">account-switcher</property>
+                        <property name="single-click-activate">true</property>
+                      </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/data/resources/ui/user-entry-row.ui b/data/resources/ui/user-entry-row.ui
new file mode 100644
index 00000000..21c459f2
--- /dev/null
+++ b/data/resources/ui/user-entry-row.ui
@@ -0,0 +1,35 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <template class="UserEntryRow" parent="AdwBin">
+    <child>
+      <object class="GtkBox">
+        <property name="spacing">10</property>
+        <child>
+          <object class="ComponentsAvatarOverlaid" id="avatar_component">
+            <property name="size">40</property>
+          </object>
+        </child>
+        <child>
+          <object class="GtkBox">
+            <property name="spacing">5</property>
+            <property name="orientation">vertical</property>
+            <child>
+              <object class="GtkLabel" id="display_name">
+                <property name="xalign">0.0</property>
+              </object>
+            </child>
+            <child>
+              <object class="GtkLabel" id="user_id">
+                <property name="xalign">0.0</property>
+                <style>
+                  <class name="dim-label" />
+                  <class name="user-id" />
+                </style>
+              </object>
+            </child>
+          </object>
+        </child>
+      </object>
+    </child>
+  </template>
+</interface>
diff --git a/src/components/avatar.rs b/src/components/avatar.rs
index 49e3e90b..c6ff6291 100644
--- a/src/components/avatar.rs
+++ b/src/components/avatar.rs
@@ -6,6 +6,7 @@ use crate::session::Avatar as AvatarItem;
 mod imp {
     use super::*;
     use glib::subclass::InitializingObject;
+    use once_cell::sync::Lazy;
     use std::cell::RefCell;
 
     #[derive(Debug, Default, CompositeTemplate)]
@@ -34,7 +35,6 @@ mod imp {
 
     impl ObjectImpl for Avatar {
         fn properties() -> &'static [glib::ParamSpec] {
-            use once_cell::sync::Lazy;
             static PROPERTIES: Lazy<Vec<glib::ParamSpec>> = Lazy::new(|| {
                 vec![
                     glib::ParamSpec::new_object(
@@ -76,7 +76,7 @@ mod imp {
         fn property(&self, obj: &Self::Type, _id: usize, pspec: &glib::ParamSpec) -> glib::Value {
             match pspec.name() {
                 "item" => obj.item().to_value(),
-                "size" => self.avatar.size().to_value(),
+                "size" => obj.size().to_value(),
                 _ => unimplemented!(),
             }
         }
@@ -105,6 +105,11 @@ impl Avatar {
         glib::Object::new(&[]).expect("Failed to create Avatar")
     }
 
+    pub fn set_size(&self, size: i32) {
+        let priv_ = imp::Avatar::from_instance(self);
+        priv_.avatar.set_size(size);
+    }
+
     pub fn set_item(&self, item: Option<AvatarItem>) {
         let priv_ = imp::Avatar::from_instance(self);
 
@@ -121,6 +126,11 @@ impl Avatar {
         self.notify("item");
     }
 
+    pub fn size(&self) -> i32 {
+        let priv_ = imp::Avatar::from_instance(self);
+        priv_.avatar.size()
+    }
+
     pub fn item(&self) -> Option<AvatarItem> {
         let priv_ = imp::Avatar::from_instance(self);
         priv_.item.borrow().clone()
diff --git a/src/components/avatar_overlaid.rs b/src/components/avatar_overlaid.rs
new file mode 100644
index 00000000..cf1e9257
--- /dev/null
+++ b/src/components/avatar_overlaid.rs
@@ -0,0 +1,109 @@
+use adw::subclass::prelude::*;
+use gtk::{glib, prelude::*, subclass::prelude::*, CompositeTemplate};
+
+use super::Avatar;
+
+mod imp {
+    use super::*;
+    use glib::subclass::InitializingObject;
+    use once_cell::sync::{Lazy, OnceCell};
+
+    #[derive(Debug, Default, CompositeTemplate)]
+    #[template(resource = "/org/gnome/FractalNext/components-avatar-overlaid.ui")]
+    pub struct AvatarOverlaid {
+        #[template_child]
+        pub child_avatar: TemplateChild<Avatar>,
+        #[template_child]
+        pub checkmark_overlay: TemplateChild<gtk::Overlay>,
+        pub checkmark: OnceCell<gtk::Image>,
+    }
+
+    #[glib::object_subclass]
+    impl ObjectSubclass for AvatarOverlaid {
+        const NAME: &'static str = "ComponentsAvatarOverlaid";
+        type Type = super::AvatarOverlaid;
+        type ParentType = adw::Bin;
+
+        fn class_init(klass: &mut Self::Class) {
+            Avatar::static_type();
+            Self::bind_template(klass);
+        }
+
+        fn instance_init(obj: &InitializingObject<Self>) {
+            obj.init_template();
+        }
+    }
+
+    impl ObjectImpl for AvatarOverlaid {
+        fn properties() -> &'static [glib::ParamSpec] {
+            static PROPERTIES: Lazy<Vec<glib::ParamSpec>> = Lazy::new(|| {
+                vec![
+                    glib::ParamSpec::new_int(
+                        "size",
+                        "Size",
+                        "The size of the Avatar",
+                        -1,
+                        i32::MAX,
+                        -1,
+                        glib::ParamFlags::READWRITE,
+                    ),
+                    glib::ParamSpec::new_boolean(
+                        "selected",
+                        "Selected",
+                        "Style helper for the inner Avatar",
+                        false,
+                        glib::ParamFlags::WRITABLE,
+                    ),
+                ]
+            });
+
+            PROPERTIES.as_ref()
+        }
+
+        fn set_property(
+            &self,
+            _obj: &Self::Type,
+            _id: usize,
+            value: &glib::Value,
+            pspec: &glib::ParamSpec,
+        ) {
+            match pspec.name() {
+                "size" => self.child_avatar.set_size(value.get().unwrap()),
+                _ => unimplemented!(),
+            }
+        }
+
+        fn property(&self, _obj: &Self::Type, _id: usize, pspec: &glib::ParamSpec) -> glib::Value {
+            match pspec.name() {
+                "size" => self.child_avatar.size().to_value(),
+                _ => unimplemented!(),
+            }
+        }
+    }
+
+    impl WidgetImpl for AvatarOverlaid {}
+    impl BinImpl for AvatarOverlaid {}
+}
+
+glib::wrapper! {
+    /// A widget displaying an `Avatar` for a `Room` or `User`.
+    pub struct AvatarOverlaid(ObjectSubclass<imp::AvatarOverlaid>)
+        @extends gtk::Widget, adw::Bin, @implements gtk::Accessible;
+}
+
+impl AvatarOverlaid {
+    pub fn new() -> Self {
+        glib::Object::new(&[]).expect("Failed to create AvatarOverlaid")
+    }
+
+    pub fn avatar(&self) -> &Avatar {
+        let priv_ = imp::AvatarOverlaid::from_instance(self);
+        &priv_.child_avatar
+    }
+}
+
+impl Default for AvatarOverlaid {
+    fn default() -> Self {
+        Self::new()
+    }
+}
diff --git a/src/components/mod.rs b/src/components/mod.rs
index 5d2e3e74..9ac37418 100644
--- a/src/components/mod.rs
+++ b/src/components/mod.rs
@@ -1,4 +1,5 @@
 mod avatar;
+mod avatar_overlaid;
 mod context_menu_bin;
 mod in_app_notification;
 mod label_with_widgets;
@@ -7,6 +8,7 @@ mod room_title;
 mod spinner_button;
 
 pub use self::avatar::Avatar;
+pub use self::avatar_overlaid::AvatarOverlaid;
 pub use self::context_menu_bin::{ContextMenuBin, ContextMenuBinExt, ContextMenuBinImpl};
 pub use self::in_app_notification::InAppNotification;
 pub use self::label_with_widgets::LabelWithWidgets;
diff --git a/src/meson.build b/src/meson.build
index ee7f4d2a..9199cd77 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -21,6 +21,7 @@ run_command(
 sources = files(
   'application.rs',
   'components/avatar.rs',
+  'components/avatar_overlaid.rs',
   'components/context_menu_bin.rs',
   'components/label_with_widgets.rs',
   'components/mod.rs',
@@ -71,6 +72,10 @@ sources = files(
   'session/sidebar/room_row.rs',
   'session/sidebar/selection.rs',
   'session/sidebar/sidebar.rs',
+  'session/sidebar/account_switcher/add_account.rs',
+  'session/sidebar/account_switcher/item.rs',
+  'session/sidebar/account_switcher/mod.rs',
+  'session/sidebar/account_switcher/user_entry.rs',
 )
 
 custom_target(
diff --git a/src/session/mod.rs b/src/session/mod.rs
index 3084f7cd..436fa2f6 100644
--- a/src/session/mod.rs
+++ b/src/session/mod.rs
@@ -25,7 +25,7 @@ use crate::session::content::ContentType;
 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::{
@@ -449,6 +449,11 @@ 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);
+    }
 }
 
 impl Default for Session {
diff --git a/src/session/sidebar/account_switcher/add_account.rs 
b/src/session/sidebar/account_switcher/add_account.rs
new file mode 100644
index 00000000..abf9ccff
--- /dev/null
+++ b/src/session/sidebar/account_switcher/add_account.rs
@@ -0,0 +1,43 @@
+use adw::subclass::prelude::BinImpl;
+use gtk::subclass::prelude::*;
+use gtk::{self, prelude::*};
+use gtk::{glib, CompositeTemplate};
+
+mod imp {
+    use super::*;
+    use glib::subclass::InitializingObject;
+
+    #[derive(Debug, Default, CompositeTemplate)]
+    #[template(resource = "/org/gnome/FractalNext/add-account-row.ui")]
+    pub struct AddAccountRow;
+
+    #[glib::object_subclass]
+    impl ObjectSubclass for AddAccountRow {
+        const NAME: &'static str = "AddAccountRow";
+        type Type = super::AddAccountRow;
+        type ParentType = adw::Bin;
+
+        fn class_init(klass: &mut Self::Class) {
+            Self::bind_template(klass);
+        }
+
+        fn instance_init(obj: &InitializingObject<Self>) {
+            obj.init_template();
+        }
+    }
+
+    impl ObjectImpl for AddAccountRow {}
+    impl WidgetImpl for AddAccountRow {}
+    impl BinImpl for AddAccountRow {}
+}
+
+glib::wrapper! {
+    pub struct AddAccountRow(ObjectSubclass<imp::AddAccountRow>)
+        @extends gtk::Widget, adw::Bin, @implements gtk::Accessible;
+}
+
+impl AddAccountRow {
+    pub fn new() -> Self {
+        glib::Object::new(&[]).expect("Failed to create AddAccountRow")
+    }
+}
diff --git a/src/session/sidebar/account_switcher/item.rs b/src/session/sidebar/account_switcher/item.rs
new file mode 100644
index 00000000..353f89cc
--- /dev/null
+++ b/src/session/sidebar/account_switcher/item.rs
@@ -0,0 +1,152 @@
+use super::add_account::AddAccountRow;
+use super::user_entry::UserEntryRow;
+use gtk::{gio::ListStore, glib, prelude::*, subclass::prelude::*};
+use std::convert::TryFrom;
+
+mod imp {
+    use super::*;
+    use once_cell::sync::Lazy;
+    use std::cell::Cell;
+
+    #[derive(Debug, Default)]
+    pub struct ExtraItemObj(pub Cell<super::ExtraItem>);
+
+    #[glib::object_subclass]
+    impl ObjectSubclass for ExtraItemObj {
+        const NAME: &'static str = "ExtraItemObj";
+        type Type = super::ExtraItemObj;
+        type ParentType = glib::Object;
+    }
+
+    impl ObjectImpl for ExtraItemObj {
+        fn properties() -> &'static [glib::ParamSpec] {
+            static PROPERTIES: Lazy<Vec<glib::ParamSpec>> = Lazy::new(|| {
+                vec![glib::ParamSpec::new_enum(
+                    "inner",
+                    "Inner",
+                    "Inner value of ExtraItem",
+                    super::ExtraItem::static_type(),
+                    0,
+                    glib::ParamFlags::READWRITE | glib::ParamFlags::CONSTRUCT_ONLY,
+                )]
+            });
+
+            PROPERTIES.as_ref()
+        }
+
+        fn property(&self, obj: &Self::Type, _id: usize, pspec: &glib::ParamSpec) -> glib::Value {
+            match pspec.name() {
+                "inner" => obj.get().to_value(),
+                _ => unimplemented!(),
+            }
+        }
+
+        fn set_property(
+            &self,
+            _obj: &Self::Type,
+            _id: usize,
+            value: &glib::Value,
+            pspec: &glib::ParamSpec,
+        ) {
+            match pspec.name() {
+                "inner" => self.0.set(value.get().unwrap()),
+                _ => unimplemented!(),
+            }
+        }
+    }
+}
+
+#[derive(Debug, Hash, Eq, PartialEq, Clone, Copy, glib::GEnum)]
+#[repr(u32)]
+#[genum(type_name = "ExtraItem")]
+pub enum ExtraItem {
+    Separator = 0,
+    AddAccount = 1,
+}
+
+impl ExtraItem {
+    const VALUES: [Self; 2] = [Self::Separator, Self::AddAccount];
+}
+
+impl Default for ExtraItem {
+    fn default() -> Self {
+        Self::Separator
+    }
+}
+
+glib::wrapper! {
+    pub struct ExtraItemObj(ObjectSubclass<imp::ExtraItemObj>);
+}
+
+impl From<&ExtraItem> for ExtraItemObj {
+    fn from(item: &ExtraItem) -> Self {
+        glib::Object::new(&[("inner", item)]).expect("Failed to create ExtraItem")
+    }
+}
+
+impl ExtraItemObj {
+    pub fn list_store() -> ListStore {
+        ExtraItem::VALUES.iter().map(ExtraItemObj::from).fold(
+            ListStore::new(ExtraItemObj::static_type()),
+            |list_items, item| {
+                list_items.append(&item);
+                list_items
+            },
+        )
+    }
+
+    pub fn get(&self) -> ExtraItem {
+        let priv_ = imp::ExtraItemObj::from_instance(self);
+
+        priv_.0.get()
+    }
+
+    pub fn is_separator(&self) -> bool {
+        self.get() == ExtraItem::Separator
+    }
+
+    pub fn is_add_account(&self) -> bool {
+        self.get() == ExtraItem::AddAccount
+    }
+}
+
+#[derive(Debug, Clone)]
+pub enum Item {
+    User(gtk::StackPage),
+    Separator,
+    AddAccount,
+}
+
+impl From<ExtraItem> for Item {
+    fn from(extra_item: ExtraItem) -> Self {
+        match extra_item {
+            ExtraItem::Separator => Self::Separator,
+            ExtraItem::AddAccount => Self::AddAccount,
+        }
+    }
+}
+
+impl TryFrom<glib::Object> for Item {
+    type Error = glib::Object;
+
+    fn try_from(object: glib::Object) -> Result<Self, Self::Error> {
+        object
+            .downcast::<gtk::StackPage>()
+            .map(Self::User)
+            .or_else(|object| object.downcast::<ExtraItemObj>().map(|it| it.get().into()))
+    }
+}
+
+impl Item {
+    pub fn build_widget(&self) -> gtk::Widget {
+        match self {
+            Self::User(ref session_page) => {
+                let user_entry = UserEntryRow::new();
+                user_entry.bind_session_page(session_page);
+                user_entry.upcast()
+            }
+            Self::Separator => gtk::Separator::new(gtk::Orientation::Vertical).upcast(),
+            Self::AddAccount => AddAccountRow::new().upcast(),
+        }
+    }
+}
diff --git a/src/session/sidebar/account_switcher/mod.rs b/src/session/sidebar/account_switcher/mod.rs
new file mode 100644
index 00000000..d244c4b3
--- /dev/null
+++ b/src/session/sidebar/account_switcher/mod.rs
@@ -0,0 +1,3 @@
+pub mod add_account;
+pub mod item;
+pub mod user_entry;
diff --git a/src/session/sidebar/account_switcher/user_entry.rs 
b/src/session/sidebar/account_switcher/user_entry.rs
new file mode 100644
index 00000000..1b477360
--- /dev/null
+++ b/src/session/sidebar/account_switcher/user_entry.rs
@@ -0,0 +1,194 @@
+use crate::{
+    components::AvatarOverlaid,
+    session::{Avatar as AvatarItem, Session, User},
+};
+use adw::subclass::prelude::BinImpl;
+use gtk::{self, glib, prelude::*, subclass::prelude::*, CompositeTemplate};
+
+mod imp {
+    use super::*;
+    use glib::subclass::InitializingObject;
+    use once_cell::sync::Lazy;
+
+    #[derive(Debug, Default, CompositeTemplate)]
+    #[template(resource = "/org/gnome/FractalNext/user-entry-row.ui")]
+    pub struct UserEntryRow {
+        #[template_child]
+        pub avatar_component: TemplateChild<AvatarOverlaid>,
+        #[template_child]
+        pub display_name: TemplateChild<gtk::Label>,
+        #[template_child]
+        pub user_id: TemplateChild<gtk::Label>,
+    }
+
+    #[glib::object_subclass]
+    impl ObjectSubclass for UserEntryRow {
+        const NAME: &'static str = "UserEntryRow";
+        type Type = super::UserEntryRow;
+        type ParentType = adw::Bin;
+
+        fn class_init(klass: &mut Self::Class) {
+            AvatarOverlaid::static_type();
+            Self::bind_template(klass);
+        }
+
+        fn instance_init(obj: &InitializingObject<Self>) {
+            obj.init_template();
+        }
+    }
+
+    impl ObjectImpl for UserEntryRow {
+        fn properties() -> &'static [glib::ParamSpec] {
+            static PROPERTIES: Lazy<Vec<glib::ParamSpec>> = Lazy::new(|| {
+                vec![
+                    glib::ParamSpec::new_object(
+                        "avatar",
+                        "Avatar",
+                        "The avatar of the user",
+                        AvatarItem::static_type(),
+                        glib::ParamFlags::READWRITE,
+                    ),
+                    glib::ParamSpec::new_string(
+                        "display-name",
+                        "Display name",
+                        "The display name of the user",
+                        Some(""),
+                        glib::ParamFlags::READWRITE,
+                    ),
+                    glib::ParamSpec::new_string(
+                        "user-id",
+                        "User ID",
+                        "The user ID",
+                        Some(""),
+                        glib::ParamFlags::READWRITE,
+                    ),
+                    glib::ParamSpec::new_boolean(
+                        "active",
+                        "Active",
+                        "Is the active user",
+                        false,
+                        glib::ParamFlags::WRITABLE,
+                    ),
+                ]
+            });
+
+            PROPERTIES.as_ref()
+        }
+
+        fn set_property(
+            &self,
+            obj: &Self::Type,
+            _id: usize,
+            value: &glib::Value,
+            pspec: &glib::ParamSpec,
+        ) {
+            match pspec.name() {
+                "avatar" => {
+                    let avatar = value.get().unwrap();
+                    obj.set_avatar(avatar);
+                }
+                "display-name" => {
+                    let display_name = value.get().unwrap();
+                    obj.set_display_name(display_name);
+                }
+                "user-id" => {
+                    let user_id = value.get().unwrap();
+                    obj.set_user_id(user_id);
+                }
+                _ => unimplemented!(),
+            }
+        }
+
+        fn property(&self, _obj: &Self::Type, _id: usize, pspec: &glib::ParamSpec) -> glib::Value {
+            match pspec.name() {
+                "avatar" => self.avatar_component.avatar().item().to_value(),
+                "display-name" => self.display_name.label().to_value(),
+                "user-id" => self.user_id.label().to_value(),
+                _ => unimplemented!(),
+            }
+        }
+    }
+
+    impl WidgetImpl for UserEntryRow {}
+    impl BinImpl for UserEntryRow {}
+}
+
+glib::wrapper! {
+    pub struct UserEntryRow(ObjectSubclass<imp::UserEntryRow>)
+        @extends gtk::Widget, adw::Bin, @implements gtk::Accessible;
+}
+
+impl UserEntryRow {
+    pub fn new() -> Self {
+        glib::Object::new(&[]).expect("Failed to create AddAccountRow")
+    }
+
+    pub fn set_avatar(&self, avatar_item: AvatarItem) {
+        let priv_ = imp::UserEntryRow::from_instance(self);
+
+        priv_.avatar_component.avatar().set_item(Some(avatar_item));
+    }
+
+    pub fn set_display_name(&self, display_name: String) {
+        let priv_ = imp::UserEntryRow::from_instance(self);
+
+        priv_.display_name.set_label(&display_name);
+        if let Some(item) = priv_.avatar_component.avatar().item() {
+            item.set_display_name(Some(display_name));
+        }
+    }
+
+    pub fn set_user_id(&self, user_id: String) {
+        let priv_ = imp::UserEntryRow::from_instance(self);
+
+        priv_.user_id.set_label(&user_id);
+    }
+
+    pub fn bind_session_page(&self, session_page: &gtk::StackPage) {
+        let priv_ = imp::UserEntryRow::from_instance(self);
+
+        let session_exp = gtk::PropertyExpression::new::<gtk::PropertyExpression>(
+            gtk::StackPage::static_type(),
+            None,
+            "child",
+        );
+        let user_exp = gtk::PropertyExpression::new::<gtk::PropertyExpression>(
+            Session::static_type(),
+            Some(&session_exp),
+            "user",
+        );
+
+        let avatar_exp = gtk::PropertyExpression::new::<gtk::PropertyExpression>(
+            User::static_type(),
+            Some(&user_exp),
+            "avatar",
+        );
+        avatar_exp.bind::<glib::Object>(
+            self.upcast_ref(),
+            "avatar",
+            Some(session_page.upcast_ref()),
+        );
+
+        let display_name_exp = gtk::PropertyExpression::new::<gtk::PropertyExpression>(
+            User::static_type(),
+            Some(&user_exp),
+            "display-name",
+        );
+        display_name_exp.bind::<glib::Object>(
+            priv_.display_name.upcast_ref(),
+            "label",
+            Some(session_page.upcast_ref()),
+        );
+
+        let user_id_exp = gtk::PropertyExpression::new::<gtk::PropertyExpression>(
+            User::static_type(),
+            Some(&user_exp),
+            "user-id",
+        );
+        user_id_exp.bind::<glib::Object>(
+            priv_.user_id.upcast_ref(),
+            "label",
+            Some(session_page.upcast_ref()),
+        );
+    }
+}
diff --git a/src/session/sidebar/mod.rs b/src/session/sidebar/mod.rs
index e9cc2def..ac8dc48d 100644
--- a/src/session/sidebar/mod.rs
+++ b/src/session/sidebar/mod.rs
@@ -1,3 +1,4 @@
+mod account_switcher;
 mod category;
 mod category_row;
 mod entry;
diff --git a/src/session/sidebar/sidebar.rs b/src/session/sidebar/sidebar.rs
index 50ff61ce..1e54619a 100644
--- a/src/session/sidebar/sidebar.rs
+++ b/src/session/sidebar/sidebar.rs
@@ -1,6 +1,14 @@
 use adw::subclass::prelude::BinImpl;
-use gtk::{gio, glib, prelude::*, subclass::prelude::*, CompositeTemplate};
+use gtk::{
+    gio::{self, ListModel, ListStore},
+    glib,
+    prelude::*,
+    subclass::prelude::*,
+    CompositeTemplate, SelectionModel,
+};
+use std::convert::TryFrom;
 
+use super::account_switcher::item::{ExtraItemObj, Item as AccountSwitcherItem};
 use crate::session::{
     content::ContentType,
     room::Room,
@@ -23,6 +31,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>,
@@ -142,6 +152,53 @@ mod imp {
                     _ => {}
                 }
             });
+
+            // Account switcher setup
+
+            self.account_switcher.connect_activate(|list_view, index| {
+                list_view
+                    .model()
+                    .and_then(|model| model.item(index))
+                    .map(AccountSwitcherItem::try_from)
+                    .and_then(Result::ok)
+                    .map(|item| match item {
+                        AccountSwitcherItem::User(session_page) => {
+                            session_page.set_visible(true);
+                        }
+                        _ => {}
+                    });
+            });
+
+            // There is no permanent stuff to take care of,
+            // so only bind and unbind are connected.
+            let ref factory = gtk::SignalListItemFactory::new();
+            factory.connect_bind(|_, list_item| {
+                list_item.set_selectable(false);
+                let child = list_item
+                    .item()
+                    .map(AccountSwitcherItem::try_from)
+                    .and_then(Result::ok)
+                    .as_ref()
+                    .map(|item| {
+                        match item {
+                            AccountSwitcherItem::Separator => {
+                                list_item.set_activatable(false);
+                            }
+                            _ => {}
+                        }
+
+                        item
+                    })
+                    .map(AccountSwitcherItem::build_widget);
+
+                list_item.set_child(child.as_ref());
+            });
+
+            factory.connect_unbind(|_, list_item| {
+                list_item.set_child::<gtk::Widget>(None);
+            });
+
+            self.account_switcher.set_factory(Some(factory));
         }
     }
 
@@ -249,6 +306,19 @@ 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();
+
+        let ref end_items = ExtraItemObj::list_store();
+        let ref items_split = ListStore::new(ListModel::static_type());
+        items_split.append(sessions_stack_pages);
+        items_split.append(end_items);
+        let ref items = gtk::FlattenListModel::new(Some(items_split));
+        let ref selectable_items = gtk::NoSelection::new(Some(items));
+
+        account_switcher.set_model(Some(selectable_items));
+    }
 }
 
 impl Default for Sidebar {
diff --git a/src/window.rs b/src/window.rs
index 7485a1d9..ca973c6c 100644
--- a/src/window.rs
+++ b/src/window.rs
@@ -107,6 +107,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]