[fractal/get-widgets-from-ui-struct: 13/19] Move stuff in state from AppOp to UI




commit 724af296ab838ea6da7674e284ea9d6e585883fc
Author: Alejandro Domínguez <adomu net-c com>
Date:   Sat Dec 5 15:45:02 2020 +0100

    Move stuff in state from AppOp to UI

 fractal-gtk/src/appop/state.rs | 155 +++++++----------------------------------
 fractal-gtk/src/meson.build    |   1 +
 fractal-gtk/src/ui/mod.rs      |   1 +
 fractal-gtk/src/ui/state.rs    | 106 ++++++++++++++++++++++++++++
 4 files changed, 133 insertions(+), 130 deletions(-)
---
diff --git a/fractal-gtk/src/appop/state.rs b/fractal-gtk/src/appop/state.rs
index e92b5729..09233c94 100644
--- a/fractal-gtk/src/appop/state.rs
+++ b/fractal-gtk/src/appop/state.rs
@@ -1,26 +1,38 @@
-use gio::prelude::*;
-use gtk::prelude::*;
-use libhandy::prelude::*;
-
 use super::RoomSearchPagination;
 use crate::actions::AppState;
 use crate::appop::AppOp;
+use gtk::prelude::*;
 
 impl AppOp {
     pub fn set_state(&mut self, state: AppState) {
         self.state = state;
 
         match self.state {
-            AppState::Login => self.set_stack_state("login"),
-            AppState::NoRoom | AppState::Room => {
-                self.set_stack_state("main_view");
-                self.set_chat_state(state);
+            AppState::Login => self.ui.set_stack_state("login"),
+            AppState::NoRoom => {
+                self.ui.set_stack_state("main_view");
+                self.ui.set_chat_state(None);
+                self.active_room = None;
+                self.clear_tmp_msgs();
+            }
+            AppState::Room => {
+                let msg = if let Some(active_room_id) = self.active_room.as_ref() {
+                    self.unsent_messages
+                        .get(active_room_id)
+                        .map(|(msg_text, msg_number)| (msg_text.as_str(), msg_number.clone()))
+                        .unwrap_or_default()
+                } else {
+                    Default::default()
+                };
+
+                self.ui.set_stack_state("main_view");
+                self.ui.set_chat_state(Some(msg));
             }
-            AppState::Directory => self.set_deck_state(Some("directory"), state),
-            AppState::Loading => self.set_stack_state("loading"),
-            AppState::AccountSettings => self.set_deck_state(Some("account-settings"), state),
-            AppState::RoomSettings => self.set_deck_state(Some("room-settings"), state),
-            AppState::MediaViewer => self.set_deck_state(Some("media-viewer"), state),
+            AppState::Directory => self.ui.set_deck_state(Some("directory"), state),
+            AppState::Loading => self.ui.set_stack_state("loading"),
+            AppState::AccountSettings => self.ui.set_deck_state(Some("account-settings"), state),
+            AppState::RoomSettings => self.ui.set_deck_state(Some("room-settings"), state),
+            AppState::MediaViewer => self.ui.set_deck_state(Some("media-viewer"), state),
         };
 
         //set focus for room directory
@@ -34,121 +46,4 @@ impl AppOp {
             self.search_rooms();
         }
     }
-
-    fn set_deck_state(&self, view: Option<&str>, state: AppState) {
-        let deck = self
-            .ui
-            .builder
-            .get_object::<libhandy::Deck>("main_deck")
-            .expect("Could not find main_deck in ui file");
-        let stack = self
-            .ui
-            .builder
-            .get_object::<gtk::Stack>("subview_stack")
-            .expect("Could not find subview_stack in ui file");
-        let app = gio::Application::get_default().unwrap();
-
-        let global_back = app.lookup_action("back");
-
-        let direction = match state {
-            AppState::Room | AppState::NoRoom => libhandy::NavigationDirection::Back,
-            _ => libhandy::NavigationDirection::Forward,
-        };
-
-        if let Some(v) = view {
-            stack.set_visible_child_name(v);
-        }
-
-        if deck.get_adjacent_child(direction).is_some() {
-            deck.navigate(direction);
-            if direction == libhandy::NavigationDirection::Forward {
-                // Disable global back while in a subview
-                global_back.map(|a| a.set_property("enabled", &false));
-            }
-        }
-    }
-
-    fn set_stack_state(&self, state: &str) {
-        self.ui
-            .builder
-            .get_object::<gtk::Stack>("main_content_stack")
-            .expect("Can't find main_content_stack in ui file.")
-            .set_visible_child_name(state);
-    }
-
-    fn set_chat_state(&mut self, state: AppState) {
-        let deck = self
-            .ui
-            .builder
-            .get_object::<libhandy::Deck>("main_deck")
-            .expect("Could not find main_deck in ui file");
-        let stack = self
-            .ui
-            .builder
-            .get_object::<gtk::Stack>("room_view_stack")
-            .expect("Can't find room_view_stack in ui file.");
-        let headerbar = self
-            .ui
-            .builder
-            .get_object::<libhandy::HeaderBar>("room_header_bar")
-            .expect("Can't find room_header_bar in ui file.");
-
-        match state {
-            AppState::NoRoom => {
-                self.set_state_no_room(&headerbar);
-                self.ui
-                    .leaflet
-                    .navigate(libhandy::NavigationDirection::Back);
-                stack.set_visible_child_name("noroom");
-            }
-            AppState::Room => {
-                self.set_state_room(&headerbar);
-                self.ui
-                    .leaflet
-                    .navigate(libhandy::NavigationDirection::Forward);
-                stack.set_visible_child_name("room_view");
-            }
-            _ => (),
-        }
-
-        if deck
-            .get_adjacent_child(libhandy::NavigationDirection::Back)
-            .is_some()
-        {
-            deck.navigate(libhandy::NavigationDirection::Back);
-        }
-    }
-
-    fn set_state_room(&self, headerbar: &libhandy::HeaderBar) {
-        for ch in headerbar.get_children().iter() {
-            ch.show();
-        }
-
-        self.ui.sventry.view.grab_focus();
-
-        let msg = self
-            .active_room
-            .as_ref()
-            .and_then(|active_room_id| self.unsent_messages.get(active_room_id))
-            .cloned()
-            .unwrap_or_default();
-        if let Some(buffer) = self.ui.sventry.view.get_buffer() {
-            buffer.set_text(&msg.0);
-
-            let iter = buffer.get_iter_at_offset(msg.1);
-            buffer.place_cursor(&iter);
-        }
-    }
-
-    // WORKAROUND this is needed because NoRoom isn't a real app state
-    fn set_state_no_room(&mut self, headerbar: &libhandy::HeaderBar) {
-        for ch in headerbar.get_children().iter() {
-            ch.hide();
-
-            // Select new active room in the sidebar
-            self.ui.roomlist.unselect();
-        }
-        self.active_room = None;
-        self.clear_tmp_msgs();
-    }
 }
diff --git a/fractal-gtk/src/meson.build b/fractal-gtk/src/meson.build
index 5f1d687b..d10bc490 100644
--- a/fractal-gtk/src/meson.build
+++ b/fractal-gtk/src/meson.build
@@ -108,6 +108,7 @@ app_sources = files(
   'ui/notify.rs',
   'ui/room_settings.rs',
   'ui/start_chat.rs',
+  'ui/state.rs',
   'ui/user.rs',
   'util/i18n.rs',
   'util/mod.rs',
diff --git a/fractal-gtk/src/ui/mod.rs b/fractal-gtk/src/ui/mod.rs
index d4599e32..21caf751 100644
--- a/fractal-gtk/src/ui/mod.rs
+++ b/fractal-gtk/src/ui/mod.rs
@@ -17,6 +17,7 @@ pub mod member;
 pub mod notify;
 pub mod room_settings;
 pub mod start_chat;
+pub mod state;
 pub mod user;
 
 pub struct UI {
diff --git a/fractal-gtk/src/ui/state.rs b/fractal-gtk/src/ui/state.rs
new file mode 100644
index 00000000..a03da58b
--- /dev/null
+++ b/fractal-gtk/src/ui/state.rs
@@ -0,0 +1,106 @@
+use super::UI;
+use crate::actions::AppState;
+use gio::prelude::*;
+use gtk::prelude::*;
+use libhandy::prelude::*;
+
+impl UI {
+    pub fn set_deck_state(&self, view: Option<&str>, state: AppState) {
+        let deck = self
+            .builder
+            .get_object::<libhandy::Deck>("main_deck")
+            .expect("Could not find main_deck in ui file");
+        let stack = self
+            .builder
+            .get_object::<gtk::Stack>("subview_stack")
+            .expect("Could not find subview_stack in ui file");
+        let app = gio::Application::get_default().unwrap();
+
+        let global_back = app.lookup_action("back");
+
+        let direction = match state {
+            AppState::Room | AppState::NoRoom => libhandy::NavigationDirection::Back,
+            _ => libhandy::NavigationDirection::Forward,
+        };
+
+        if let Some(v) = view {
+            stack.set_visible_child_name(v);
+        }
+
+        if deck.get_adjacent_child(direction).is_some() {
+            deck.navigate(direction);
+            if direction == libhandy::NavigationDirection::Forward {
+                // Disable global back while in a subview
+                global_back.map(|a| a.set_property("enabled", &false));
+            }
+        }
+    }
+
+    pub fn set_stack_state(&self, state: &str) {
+        self.builder
+            .get_object::<gtk::Stack>("main_content_stack")
+            .expect("Can't find main_content_stack in ui file.")
+            .set_visible_child_name(state);
+    }
+
+    pub fn set_chat_state(&mut self, msg: Option<(&str, i32)>) {
+        let deck = self
+            .builder
+            .get_object::<libhandy::Deck>("main_deck")
+            .expect("Could not find main_deck in ui file");
+        let stack = self
+            .builder
+            .get_object::<gtk::Stack>("room_view_stack")
+            .expect("Can't find room_view_stack in ui file.");
+        let headerbar = self
+            .builder
+            .get_object::<libhandy::HeaderBar>("room_header_bar")
+            .expect("Can't find room_header_bar in ui file.");
+
+        match msg {
+            None => {
+                self.set_state_no_room(&headerbar);
+                self.leaflet.navigate(libhandy::NavigationDirection::Back);
+                stack.set_visible_child_name("noroom");
+            }
+            Some((msg_text, msg_number)) => {
+                self.set_state_room(&headerbar, msg_text, msg_number);
+                self.leaflet
+                    .navigate(libhandy::NavigationDirection::Forward);
+                stack.set_visible_child_name("room_view");
+            }
+        }
+
+        if deck
+            .get_adjacent_child(libhandy::NavigationDirection::Back)
+            .is_some()
+        {
+            deck.navigate(libhandy::NavigationDirection::Back);
+        }
+    }
+
+    fn set_state_room(&self, headerbar: &libhandy::HeaderBar, msg_text: &str, msg_number: i32) {
+        for ch in headerbar.get_children().iter() {
+            ch.show();
+        }
+
+        self.sventry.view.grab_focus();
+
+        if let Some(buffer) = self.sventry.view.get_buffer() {
+            buffer.set_text(msg_text);
+
+            let iter = buffer.get_iter_at_offset(msg_number);
+            buffer.place_cursor(&iter);
+        }
+    }
+
+    // WORKAROUND this is needed because NoRoom isn't a real app state
+    fn set_state_no_room(&mut self, headerbar: &libhandy::HeaderBar) {
+        for ch in headerbar.get_children().iter() {
+            ch.hide();
+
+            // Select new active room in the sidebar
+            self.roomlist.unselect();
+        }
+    }
+}


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