[fractal/multi-account: 9/9] Change between logged-in users




commit 1b013cc72b0d4ebf95eba46b25bda7df1a7db803
Author: Alejandro Domínguez <adomu net-c com>
Date:   Tue Jul 13 17:31:24 2021 +0200

    Change between logged-in users

 src/session/mod.rs             |  7 ++++---
 src/session/sidebar/sidebar.rs | 20 +++++++++++++-------
 src/window.rs                  | 21 ++++++++++++++++++---
 3 files changed, 35 insertions(+), 13 deletions(-)
---
diff --git a/src/session/mod.rs b/src/session/mod.rs
index 25a343a4..fff1dcc8 100644
--- a/src/session/mod.rs
+++ b/src/session/mod.rs
@@ -21,11 +21,12 @@ use crate::Error;
 use crate::RUNTIME;
 
 use crate::session::content::ContentType;
+use crate::Window;
 use adw;
 use adw::subclass::prelude::BinImpl;
 use gtk::subclass::prelude::*;
 use gtk::{self, prelude::*};
-use gtk::{gio, glib, glib::clone, glib::SyncSender, CompositeTemplate, SelectionModel};
+use gtk::{gio, glib, glib::clone, glib::SyncSender, CompositeTemplate};
 use gtk_macros::send;
 use log::error;
 use matrix_sdk::ruma::{
@@ -432,8 +433,8 @@ impl Session {
         self.room_list().handle_response_rooms(response.rooms);
     }
 
-    pub fn set_logged_in_users(&self, logged_in_users: &SelectionModel) {
+    pub fn set_logged_in_users(&self, window: &Window) {
         let priv_ = &imp::Session::from_instance(self);
-        priv_.sidebar.set_logged_in_users(logged_in_users);
+        priv_.sidebar.set_logged_in_users(window);
     }
 }
diff --git a/src/session/sidebar/sidebar.rs b/src/session/sidebar/sidebar.rs
index 0362e9ac..0d3bbcc2 100644
--- a/src/session/sidebar/sidebar.rs
+++ b/src/session/sidebar/sidebar.rs
@@ -1,12 +1,13 @@
 use adw::subclass::prelude::BinImpl;
-use gtk::{gio, glib, prelude::*, subclass::prelude::*, CompositeTemplate, SelectionModel};
+use gtk::{gio, glib, glib::clone, prelude::*, subclass::prelude::*, CompositeTemplate};
 
 use crate::session::{
     content::ContentType,
     room::Room,
     sidebar::{Category, Entry, ItemList, RoomRow, Row, Selection},
-    RoomList,
+    RoomList, User,
 };
+use crate::Window;
 
 use super::account_switcher::AccountSwitcher;
 
@@ -259,12 +260,17 @@ impl Sidebar {
         self.notify("selected-room");
     }
 
-    pub fn set_logged_in_users(&self, logged_in_users: &SelectionModel) {
+    pub fn set_logged_in_users(&self, window: &Window) {
         let priv_ = imp::Sidebar::from_instance(self);
 
-        priv_
-            .account_switcher
-            .users()
-            .set_model(Some(logged_in_users));
+        let users = priv_.account_switcher.users();
+        users.set_model(Some(&window.stack_pages()));
+        users.connect_activate(clone!(@weak window => move |list, index| {
+            list.model()
+                .and_then(|m| m.item(index))
+                .and_then(|obj| obj.downcast::<User>().ok())
+                .map(|user| user.user_id())
+                .map(|user_id| window.switch_to_session(&user_id));
+        }));
     }
 }
diff --git a/src/window.rs b/src/window.rs
index 570e1492..15c5578b 100644
--- a/src/window.rs
+++ b/src/window.rs
@@ -9,8 +9,9 @@ use gio::PropertyAction;
 use glib::signal::Inhibit;
 use gtk::subclass::prelude::*;
 use gtk::{self, prelude::*};
-use gtk::{gio, glib, glib::clone, CompositeTemplate};
+use gtk::{gio, glib, glib::clone, CompositeTemplate, SelectionModel};
 use log::warn;
+use matrix_sdk::ruma::identifiers::UserId;
 
 mod imp {
     use super::*;
@@ -102,8 +103,10 @@ impl Window {
 
     fn add_session(&self, session: &Session) {
         let priv_ = &imp::Window::from_instance(self);
-        session.set_logged_in_users(&priv_.main_stack.pages());
-        priv_.main_stack.add_child(session);
+        session.set_logged_in_users(self);
+        priv_
+            .main_stack
+            .add_named(session, Some(session.user().user_id().as_str()));
         priv_.main_stack.set_visible_child(session);
         self.install_session_actions(session);
     }
@@ -172,4 +175,16 @@ impl Window {
             self.set_default_widget(gtk::NONE_WIDGET);
         }
     }
+
+    pub fn stack_pages(&self) -> SelectionModel {
+        let priv_ = &imp::Window::from_instance(self);
+
+        priv_.main_stack.pages()
+    }
+
+    pub fn switch_to_session(&self, user_id: &UserId) {
+        let priv_ = &imp::Window::from_instance(self);
+
+        priv_.main_stack.set_visible_child_name(user_id.as_str());
+    }
 }


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