[fractal/ui-refactor: 8/9] Remove Rc<RefCell<T>> pattern in ui::UI




commit 1f0b3d9275ebb898a9fa6945a614733488b24093
Author: Alejandro Domínguez <adomu net-c com>
Date:   Wed Nov 18 00:42:09 2020 +0100

    Remove Rc<RefCell<T>> pattern in ui::UI

 fractal-gtk/src/actions/global.rs     | 18 +++++++++---------
 fractal-gtk/src/actions/message.rs    | 21 +++++----------------
 fractal-gtk/src/appop/login.rs        |  2 +-
 fractal-gtk/src/appop/media_viewer.rs |  5 ++---
 fractal-gtk/src/appop/room.rs         |  3 +--
 fractal-gtk/src/ui/mod.rs             | 10 +++++-----
 6 files changed, 23 insertions(+), 36 deletions(-)
---
diff --git a/fractal-gtk/src/actions/global.rs b/fractal-gtk/src/actions/global.rs
index c8d42522..94c03467 100644
--- a/fractal-gtk/src/actions/global.rs
+++ b/fractal-gtk/src/actions/global.rs
@@ -237,14 +237,14 @@ pub fn new(appop: &AppOp) {
     account.connect_activate(clone!(@strong app_runtime => move |_, _| {
         app_runtime.update_state_with(|state| {
             state.show_account_settings_dialog();
-            state.ui.room_back_history.borrow_mut().push(AppState::AccountSettings);
+            state.ui.room_back_history.push(AppState::AccountSettings);
         });
     }));
 
     directory.connect_activate(clone!(@strong app_runtime => move |_, _| {
         app_runtime.update_state_with(|state| {
             state.set_state(AppState::Directory);
-            state.ui.room_back_history.borrow_mut().push(AppState::Directory);
+            state.ui.room_back_history.push(AppState::Directory);
         });
     }));
 
@@ -259,13 +259,13 @@ pub fn new(appop: &AppOp) {
                 state.activate();
             }
             // Push a new state only if the current state is not already Room
-            let push = if let Some(last) = state.ui.room_back_history.borrow().last() {
+            let push = if let Some(last) = state.ui.room_back_history.last() {
                 last != &AppState::Room
             } else {
                 true
             };
             if push {
-                state.ui.room_back_history.borrow_mut().push(AppState::Room);
+                state.ui.room_back_history.push(AppState::Room);
             }
         });
     }));
@@ -273,14 +273,14 @@ pub fn new(appop: &AppOp) {
     room_settings.connect_activate(clone!(@strong app_runtime => move |_, _| {
         app_runtime.update_state_with(|state| {
             state.create_room_settings();
-            state.ui.room_back_history.borrow_mut().push(AppState::RoomSettings);
+            state.ui.room_back_history.push(AppState::RoomSettings);
         });
     }));
 
     media_viewer.connect_activate(clone!(@strong app_runtime => move |_, data| {
         open_viewer(&app_runtime, data.cloned());
         app_runtime.update_state_with(|state| {
-            state.ui.room_back_history.borrow_mut().push(AppState::MediaViewer);
+            state.ui.room_back_history.push(AppState::MediaViewer);
         });
     }));
 
@@ -295,13 +295,13 @@ pub fn new(appop: &AppOp) {
 
     back.connect_activate(clone!(@strong app_runtime => move |_, _| {
         app_runtime.update_state_with(|state| {
-            if let Some(mut mv) = state.ui.media_viewer.borrow_mut().take() {
+            if let Some(mut mv) = state.ui.media_viewer.take() {
                 mv.disconnect_signal_id();
             }
 
             // Remove the current state from the store
-            state.ui.room_back_history.borrow_mut().pop();
-            let app_state = state.ui.room_back_history.borrow().last().cloned();
+            state.ui.room_back_history.pop();
+            let app_state = state.ui.room_back_history.last().cloned();
             if let Some(app_state) = app_state {
                 debug!("Go back to state {:?}", app_state);
                 state.set_state(app_state);
diff --git a/fractal-gtk/src/actions/message.rs b/fractal-gtk/src/actions/message.rs
index ba4c469b..0b9b1d5d 100644
--- a/fractal-gtk/src/actions/message.rs
+++ b/fractal-gtk/src/actions/message.rs
@@ -2,10 +2,8 @@ use crate::backend::{dw_media, media, room, ContentType};
 use glib::clone;
 use log::error;
 use matrix_sdk::identifiers::RoomId;
-use std::cell::RefCell;
 use std::fs;
 use std::process::Command;
-use std::rc::Rc;
 
 use crate::actions::AppState;
 use crate::app::{AppRuntime, RUNTIME};
@@ -26,10 +24,7 @@ use crate::widgets::FileDialog::save;
 use crate::widgets::SourceDialog;
 
 /* This creates all actions the room history can perform */
-pub fn new(
-    app_runtime: AppRuntime,
-    back_history: Rc<RefCell<Vec<AppState>>>,
-) -> gio::SimpleActionGroup {
+pub fn new(app_runtime: AppRuntime) -> gio::SimpleActionGroup {
     let actions = SimpleActionGroup::new();
     /* Action for each message */
     let reply = SimpleAction::new("reply", glib::VariantTy::new("s").ok());
@@ -72,18 +67,15 @@ pub fn new(
         });
     }));
 
-    reply.connect_activate(clone!(
-    @weak back_history,
-    @strong app_runtime
-    => move |_, data| {
+    reply.connect_activate(clone!(@strong app_runtime => move |_, data| {
         let data = data.cloned();
-        let past_state = back_history.borrow().last().cloned();
         app_runtime.update_state_with(move |state| {
             let window = state
                 .ui
                 .builder
                 .get_object::<gtk::ApplicationWindow>("main_window")
                 .expect("Couldn't find main_window in ui file.");
+            let past_state = state.ui.room_back_history.last().cloned();
             if let Some(AppState::MediaViewer) = past_state {
                 if let Some(action_group) = window.get_action_group("app") {
                     action_group.activate_action("back", None);
@@ -214,10 +206,7 @@ pub fn new(
         });
     }));
 
-    delete.connect_activate(clone!(
-    @weak back_history,
-    @strong app_runtime
-    => move |_, data| {
+    delete.connect_activate(clone!(@strong app_runtime => move |_, data| {
         let data = data.cloned();
         app_runtime.update_state_with(move |state| {
             let window = state
@@ -225,7 +214,7 @@ pub fn new(
                 .builder
                 .get_object::<gtk::ApplicationWindow>("main_window")
                 .expect("Couldn't find main_window in ui file.");
-            let past_state = back_history.borrow().last().cloned();
+            let past_state = state.ui.room_back_history.last().cloned();
             if let Some(AppState::MediaViewer) = past_state {
                 if let Some(action_group) = window.get_action_group("app") {
                     action_group.activate_action("back", None);
diff --git a/fractal-gtk/src/appop/login.rs b/fractal-gtk/src/appop/login.rs
index 61e1938b..d1bf5cc4 100644
--- a/fractal-gtk/src/appop/login.rs
+++ b/fractal-gtk/src/appop/login.rs
@@ -126,6 +126,6 @@ impl AppOp {
             }
         });
         self.bk_logout();
-        *self.ui.room_back_history.borrow_mut() = vec![];
+        self.ui.room_back_history = vec![];
     }
 }
diff --git a/fractal-gtk/src/appop/media_viewer.rs b/fractal-gtk/src/appop/media_viewer.rs
index 39bc23e4..1332885b 100644
--- a/fractal-gtk/src/appop/media_viewer.rs
+++ b/fractal-gtk/src/appop/media_viewer.rs
@@ -31,10 +31,9 @@ impl AppOp {
             let mut panel = widgets::MediaViewer::new(main_window, room, &msg, login_data.uid);
             panel.display_media_viewer(login_data.session_client.clone(), msg);
             let (body, header) = panel.create(login_data.session_client.clone())?;
-            *self.ui.media_viewer.borrow_mut() = Some(panel);
+            self.ui.media_viewer = Some(panel);
 
-            let back_history = self.ui.room_back_history.clone();
-            let actions = actions::Message::new(self.app_runtime.clone(), back_history);
+            let actions = actions::Message::new(self.app_runtime.clone());
             header.insert_action_group("message", Some(&actions));
             body.insert_action_group("message", Some(&actions));
 
diff --git a/fractal-gtk/src/appop/room.rs b/fractal-gtk/src/appop/room.rs
index fc81484a..7dccf037 100644
--- a/fractal-gtk/src/appop/room.rs
+++ b/fractal-gtk/src/appop/room.rs
@@ -297,8 +297,7 @@ impl AppOp {
             history.destroy();
         }
 
-        let back_history = self.ui.room_back_history.clone();
-        let actions = actions::Message::new(self.app_runtime.clone(), back_history);
+        let actions = actions::Message::new(self.app_runtime.clone());
         let history = widgets::RoomHistory::new(actions, active_room.clone(), &self.ui);
         self.ui.history = if let Some(mut history) = history {
             history.create(
diff --git a/fractal-gtk/src/ui/mod.rs b/fractal-gtk/src/ui/mod.rs
index c5ee61b4..02bee00c 100644
--- a/fractal-gtk/src/ui/mod.rs
+++ b/fractal-gtk/src/ui/mod.rs
@@ -5,7 +5,7 @@ use crate::widgets::{self, SVEntry};
 use chrono::prelude::{DateTime, Local};
 use gtk::{self, prelude::*};
 use matrix_sdk::identifiers::{EventId, UserId};
-use std::{cell::RefCell, path::PathBuf, rc::Rc};
+use std::path::PathBuf;
 use url::Url;
 
 pub mod connect;
@@ -19,8 +19,8 @@ pub struct UI {
     pub room_settings: Option<widgets::RoomSettings>,
     pub history: Option<widgets::RoomHistory>,
     pub roomlist: widgets::RoomList,
-    pub media_viewer: Rc<RefCell<Option<widgets::MediaViewer>>>,
-    pub room_back_history: Rc<RefCell<Vec<AppState>>>,
+    pub media_viewer: Option<widgets::MediaViewer>,
+    pub room_back_history: Vec<AppState>,
     pub invite_list: Vec<(Member, gtk::TextChildAnchor)>,
     pub leaflet: libhandy::Leaflet,
     pub deck: libhandy::Deck,
@@ -124,8 +124,8 @@ impl UI {
             room_settings: None,
             history: None,
             roomlist: widgets::RoomList::new(None, None),
-            media_viewer: Rc::new(RefCell::new(None)),
-            room_back_history: Rc::new(RefCell::new(vec![])),
+            media_viewer: None,
+            room_back_history: vec![],
             invite_list: vec![],
             leaflet,
             deck,


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