[fractal/get-widgets-from-ui-struct: 14/19] Get main_window directly instead of by address




commit 1cba9b5213bf4e118e16194eefa65a582f5ec467
Author: Alejandro Domínguez <adomu net-c com>
Date:   Sun Dec 6 20:18:20 2020 +0100

    Get main_window directly instead of by address

 fractal-gtk/src/actions/global.rs     |  7 +------
 fractal-gtk/src/actions/message.rs    | 27 ++++-----------------------
 fractal-gtk/src/appop/account.rs      | 29 ++++-------------------------
 fractal-gtk/src/appop/media_viewer.rs | 13 ++++++-------
 fractal-gtk/src/appop/message.rs      | 16 ++++------------
 fractal-gtk/src/appop/mod.rs          | 19 ++++++-------------
 fractal-gtk/src/appop/room.rs         |  7 +------
 7 files changed, 26 insertions(+), 92 deletions(-)
---
diff --git a/fractal-gtk/src/actions/global.rs b/fractal-gtk/src/actions/global.rs
index bda620a6..f9c2a392 100644
--- a/fractal-gtk/src/actions/global.rs
+++ b/fractal-gtk/src/actions/global.rs
@@ -414,12 +414,7 @@ pub fn activate_action(
     action_name: &'static str,
 ) {
     app_runtime.update_state_with(move |state| {
-        let main_window = state
-            .ui
-            .builder
-            .get_object::<gtk::Window>("main_window")
-            .expect("Can't find main_window in ui file.");
-        if let Some(action_group) = main_window.get_action_group(action_group_name) {
+        if let Some(action_group) = state.ui.main_window.get_action_group(action_group_name) {
             action_group.activate_action(action_name, None);
         }
     });
diff --git a/fractal-gtk/src/actions/message.rs b/fractal-gtk/src/actions/message.rs
index 0b9b1d5d..4a2ce92a 100644
--- a/fractal-gtk/src/actions/message.rs
+++ b/fractal-gtk/src/actions/message.rs
@@ -51,13 +51,8 @@ pub fn new(app_runtime: AppRuntime) -> gio::SimpleActionGroup {
     show_source.connect_activate(clone!(@strong app_runtime => move |_, data| {
         let data = data.cloned();
         app_runtime.update_state_with(move |state| {
-            let parent: gtk::Window = state
-                .ui
-                .builder
-                .get_object("main_window")
-                .expect("Can't find main_window in ui file.");
             let viewer = SourceDialog::new();
-            viewer.set_parent_window(&parent);
+            viewer.set_parent_window(state.ui.main_window.upcast_ref());
             if let Some(m) = get_message(state, data.as_ref()) {
                 let error = i18n("This message has no source.");
                 let source = m.source.as_ref().unwrap_or(&error);
@@ -70,14 +65,9 @@ pub fn new(app_runtime: AppRuntime) -> gio::SimpleActionGroup {
     reply.connect_activate(clone!(@strong app_runtime => move |_, data| {
         let data = data.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") {
+                if let Some(action_group) = state.ui.main_window.get_action_group("app") {
                     action_group.activate_action("back", None);
                 } else {
                     error!("The action group app is not attached to the main window.");
@@ -124,11 +114,6 @@ pub fn new(app_runtime: AppRuntime) -> gio::SimpleActionGroup {
     save_as.connect_activate(clone!(@strong app_runtime => move |_, data| {
         let data = data.cloned();
         app_runtime.update_state_with(move |state| {
-            let window = state
-                .ui
-                .builder
-                .get_object::<gtk::Window>("main_window")
-                .expect("Couldn't find main_window in ui file.");
             let (url, name) = unwrap_or_unit_return!(
                 get_message(state, data.as_ref()).and_then(|m| Some((m.url?, m.body)))
             );
@@ -139,6 +124,7 @@ pub fn new(app_runtime: AppRuntime) -> gio::SimpleActionGroup {
                 media::get_media(session_client, &url).await
             });
 
+            let window = state.ui.main_window.clone();
             glib::MainContext::default().spawn_local(async move {
                 match response.await {
                     Err(_) => {
@@ -209,14 +195,9 @@ pub fn new(app_runtime: AppRuntime) -> gio::SimpleActionGroup {
     delete.connect_activate(clone!(@strong app_runtime => move |_, data| {
         let data = data.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") {
+                if let Some(action_group) = state.ui.main_window.get_action_group("app") {
                     action_group.activate_action("back", None);
                 } else {
                     error!("The action group app is not attached to the main window.");
diff --git a/fractal-gtk/src/appop/account.rs b/fractal-gtk/src/appop/account.rs
index 7b5610b3..bbfc8ccb 100644
--- a/fractal-gtk/src/appop/account.rs
+++ b/fractal-gtk/src/appop/account.rs
@@ -75,17 +75,12 @@ impl AppOp {
 
     pub fn show_phone_dialog(&self, sid: String, secret: String) {
         let login_data = unwrap_or_unit_return!(self.login_data.clone());
-        let parent = self
-            .ui
-            .builder
-            .get_object::<gtk::Window>("main_window")
-            .expect("Can't find main_window in ui file.");
 
         let entry = gtk::Entry::new();
         let msg = i18n("Enter the code received via SMS");
         let flags = gtk::DialogFlags::MODAL | gtk::DialogFlags::DESTROY_WITH_PARENT;
         let dialog = gtk::MessageDialog::new(
-            Some(&parent),
+            Some(&self.ui.main_window),
             flags,
             gtk::MessageType::Error,
             gtk::ButtonsType::None,
@@ -142,16 +137,11 @@ impl AppOp {
 
     pub fn show_email_dialog(&self, sid: String, secret: String) {
         let login_data = unwrap_or_unit_return!(self.login_data.clone());
-        let parent = self
-            .ui
-            .builder
-            .get_object::<gtk::Window>("main_window")
-            .expect("Can't find main_window in ui file.");
 
         let msg = i18n("In order to add this email address, go to your inbox and follow the link you 
received. Once you’ve done that, click Continue.");
         let flags = gtk::DialogFlags::MODAL | gtk::DialogFlags::DESTROY_WITH_PARENT;
         let dialog = gtk::MessageDialog::new(
-            Some(&parent),
+            Some(&self.ui.main_window),
             flags,
             gtk::MessageType::Error,
             gtk::ButtonsType::None,
@@ -205,16 +195,10 @@ impl AppOp {
     }
 
     pub fn create_error_dialog(&self, error: String) -> gtk::MessageDialog {
-        let parent = self
-            .ui
-            .builder
-            .get_object::<gtk::Window>("main_window")
-            .expect("Can't find main_window in ui file.");
-
         let msg = error;
         let flags = gtk::DialogFlags::MODAL | gtk::DialogFlags::DESTROY_WITH_PARENT;
         let dialog = gtk::MessageDialog::new(
-            Some(&parent),
+            Some(&self.ui.main_window),
             flags,
             gtk::MessageType::Error,
             gtk::ButtonsType::None,
@@ -770,16 +754,11 @@ impl AppOp {
             .builder
             .get_object::<gtk::CheckButton>("account_settings_delete_check")
             .expect("Can't find account_settings_delete_check in ui file.");
-        let parent = self
-            .ui
-            .builder
-            .get_object::<gtk::Window>("main_window")
-            .expect("Can't find main_window in ui file.");
 
         let msg = i18n("Are you sure you want to delete your account?");
         let flags = gtk::DialogFlags::MODAL | gtk::DialogFlags::DESTROY_WITH_PARENT;
         let dialog = gtk::MessageDialog::new(
-            Some(&parent),
+            Some(&self.ui.main_window),
             flags,
             gtk::MessageType::Warning,
             gtk::ButtonsType::None,
diff --git a/fractal-gtk/src/appop/media_viewer.rs b/fractal-gtk/src/appop/media_viewer.rs
index 1332885b..1e8df794 100644
--- a/fractal-gtk/src/appop/media_viewer.rs
+++ b/fractal-gtk/src/appop/media_viewer.rs
@@ -19,16 +19,15 @@ impl AppOp {
             .get_object::<gtk::Stack>("subview_stack")
             .expect("Can't find subview_stack in ui file.");
 
-        let main_window = self
-            .ui
-            .builder
-            .get_object::<gtk::Window>("main_window")
-            .expect("Can't find main_window in ui file.");
-
         {
             let room_id = self.active_room.as_ref()?;
             let room = self.rooms.get(room_id)?;
-            let mut panel = widgets::MediaViewer::new(main_window, room, &msg, login_data.uid);
+            let mut panel = widgets::MediaViewer::new(
+                self.ui.main_window.clone().upcast(),
+                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 = Some(panel);
diff --git a/fractal-gtk/src/appop/message.rs b/fractal-gtk/src/appop/message.rs
index 60e6b729..e63e0d25 100644
--- a/fractal-gtk/src/appop/message.rs
+++ b/fractal-gtk/src/appop/message.rs
@@ -131,12 +131,7 @@ impl AppOp {
 
     pub fn mark_last_message_as_read(&mut self, Force(force): Force) -> Option<()> {
         let login_data = self.login_data.clone()?;
-        let window: gtk::Window = self
-            .ui
-            .builder
-            .get_object("main_window")
-            .expect("Can't find main_window in ui file.");
-        if window.is_active() || force {
+        if self.ui.main_window.is_active() || force {
             /* Move the last viewed mark to the last message */
             let active_room_id = self.active_room.as_ref()?;
             let room = self.rooms.get_mut(active_room_id)?;
@@ -384,12 +379,9 @@ impl AppOp {
                     || self.rooms.get(&msg.room).map_or(false, |r| r.direct));
 
             if should_notify {
-                let window: gtk::Window = self
-                    .ui
-                    .builder
-                    .get_object("main_window")
-                    .expect("Can't find main_window in ui file.");
-                if let (Some(app), Some(event_id)) = (window.get_application(), msg.id.clone()) {
+                if let (Some(app), Some(event_id)) =
+                    (self.ui.main_window.get_application(), msg.id.clone())
+                {
                     self.notify(app, msg.room.clone(), event_id);
                 }
             }
diff --git a/fractal-gtk/src/appop/mod.rs b/fractal-gtk/src/appop/mod.rs
index fdf51fe5..134d6235 100644
--- a/fractal-gtk/src/appop/mod.rs
+++ b/fractal-gtk/src/appop/mod.rs
@@ -158,29 +158,22 @@ impl AppOp {
         }
     }
 
-    fn get_window(&self) -> gtk::Window {
-        self.ui
-            .builder
-            .get_object("main_window")
-            .expect("Couldn't find main_window in ui file.")
-    }
-
     pub fn activate(&self) {
-        let window = self.get_window();
-        window.show();
-        window.present();
+        self.ui.main_window.show();
+        self.ui.main_window.present();
     }
 
     pub fn update_title(&mut self) {
         let unread = self.ui.roomlist.rooms_with_notifications();
         if self.unread_rooms != unread {
-            let window = self.get_window();
             if unread == 0 {
-                window.set_title(&i18n::i18n("Fractal"));
+                self.ui.main_window.set_title(&i18n::i18n("Fractal"));
             } else {
                 // Translators: The placeholder is for the number of unread messages in the
                 // application
-                window.set_title(&i18n::i18n_f("Fractal [{}]", &[&unread.to_string()]));
+                self.ui
+                    .main_window
+                    .set_title(&i18n::i18n_f("Fractal [{}]", &[&unread.to_string()]));
             }
             self.unread_rooms = unread;
         }
diff --git a/fractal-gtk/src/appop/room.rs b/fractal-gtk/src/appop/room.rs
index 5a61d637..2e6780e5 100644
--- a/fractal-gtk/src/appop/room.rs
+++ b/fractal-gtk/src/appop/room.rs
@@ -357,13 +357,8 @@ impl AppOp {
     }
 
     pub fn kicked_room(&self, room_name: String, reason: String, kicker: Option<&Member>) {
-        let parent: gtk::Window = self
-            .ui
-            .builder
-            .get_object("main_window")
-            .expect("Can't find main_window in ui file.");
         let viewer = widgets::KickedDialog::new();
-        viewer.set_parent_window(&parent);
+        viewer.set_parent_window(self.ui.main_window.upcast_ref());
         let kicker_str = kicker
             .map(|k| {
                 k.alias


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