From 539735d8ed5c2de4c254ff56cc923e5a1b352f7a Mon Sep 17 00:00:00 2001
From: Avinash Sonawane <rootkea@gmail.com>
Date: Thu, 8 Jul 2021 16:53:45 +0530
Subject: [PATCH] After restoring tabs set focus on the last focused tab

MR !131
---
 thunar/thunar-application.c | 10 ++++-
 thunar/thunar-preferences.c | 46 +++++++++++++++++-----
 thunar/thunar-window.c      | 77 ++++++++++++++++++++++++++-----------
 thunar/thunar-window.h      |  2 +
 4 files changed, 100 insertions(+), 35 deletions(-)

diff --git a/thunar/thunar-application.c b/thunar/thunar-application.c
index b09b38b14..8664be711 100644
--- a/thunar/thunar-application.c
+++ b/thunar/thunar-application.c
@@ -547,6 +547,7 @@ thunar_application_command_line (GApplication            *gapp,
       gchar       **tabs_right;
       gboolean      restore_tabs;
       gboolean      has_left_tabs; /* used to check whether the split-view should be enabled */
+      gint          last_focused_tab;
 
       if (!thunar_application_process_filenames (application, cwd, cwd_list, NULL, NULL, &error, THUNAR_APPLICATION_SELECT_FILES))
         {
@@ -562,9 +563,10 @@ thunar_application_command_line (GApplication            *gapp,
           window_list = thunar_application_get_windows (application);
           window_list = g_list_last (window_list); /* this will be the topmost Window */
           window = THUNAR_WINDOW (window_list->data);
+
           /* restore left tabs */
           has_left_tabs = FALSE;
-          g_object_get (G_OBJECT (application->preferences), "last-tabs-left", &tabs_left, NULL);
+          g_object_get (G_OBJECT (application->preferences), "last-tabs-left", &tabs_left, "last-focused-tab-left", &last_focused_tab, NULL);
           if (tabs_left != NULL && g_strv_length (tabs_left) > 0)
             {
               for (guint i = 0; i < g_strv_length (tabs_left); i++)
@@ -573,11 +575,13 @@ thunar_application_command_line (GApplication            *gapp,
                   thunar_window_notebook_add_new_tab (window, directory, FALSE);
                 }
               thunar_window_notebook_remove_tab (window, 0); /* remove automatically opened tab */
+              thunar_window_notebook_set_current_tab (window, last_focused_tab);
               has_left_tabs = TRUE;
             }
+
           /* restore right tabs */
           /* (will be restored on the left if no left tabs are found) */
-          g_object_get (G_OBJECT (application->preferences), "last-tabs-right", &tabs_right, NULL);
+          g_object_get (G_OBJECT (application->preferences), "last-tabs-right", &tabs_right, "last-focused-tab-right", &last_focused_tab, NULL);
           if (tabs_right != NULL && g_strv_length (tabs_right) > 0)
             {
               if (has_left_tabs)
@@ -588,7 +592,9 @@ thunar_application_command_line (GApplication            *gapp,
                   thunar_window_notebook_add_new_tab (window, directory, FALSE);
                 }
               thunar_window_notebook_remove_tab (window, 0); /* remove automatically opened tab */
+              thunar_window_notebook_set_current_tab (window, last_focused_tab);
             }
+
           /* free memory */
           g_list_free (window_list);
           g_strfreev (tabs_left);
diff --git a/thunar/thunar-preferences.c b/thunar/thunar-preferences.c
index 029554b3a..6475f9bdb 100644
--- a/thunar/thunar-preferences.c
+++ b/thunar/thunar-preferences.c
@@ -53,9 +53,11 @@ enum
   PROP_DEFAULT_VIEW,
   PROP_HIDDEN_DEVICES,
   PROP_HIDDEN_BOOKMARKS,
+  PROP_LAST_RESTORE_TABS,
   PROP_LAST_TABS_LEFT,
   PROP_LAST_TABS_RIGHT,
-  PROP_LAST_RESTORE_TABS,
+  PROP_LAST_FOCUSED_TAB_LEFT,
+  PROP_LAST_FOCUSED_TAB_RIGHT,
   PROP_LAST_COMPACT_VIEW_ZOOM_LEVEL,
   PROP_LAST_DETAILS_VIEW_COLUMN_ORDER,
   PROP_LAST_DETAILS_VIEW_COLUMN_WIDTHS,
@@ -210,6 +212,18 @@ thunar_preferences_class_init (ThunarPreferencesClass *klass)
                           G_TYPE_STRV,
                           EXO_PARAM_READWRITE);
 
+  /**
+   * ThunarPreferences:last-restore-tabs:
+   *
+   * %TRUE to restore the tabs as they were before closing Thunar.
+   **/
+  preferences_props[PROP_LAST_RESTORE_TABS] =
+      g_param_spec_boolean ("last-restore-tabs",
+                            "LastRestoreTabs",
+                            NULL,
+                            FALSE,
+                            EXO_PARAM_READWRITE);
+
   /**
    * ThunarPreferences:last-tabs-left:
    *
@@ -229,7 +243,7 @@ thunar_preferences_class_init (ThunarPreferencesClass *klass)
    * List of URI's that are used to reopen tabs on restart. There is one URI for each tab/folder that was open at the time
    * of the last program exit. This preference holds the tabs of the right split-view.
    **/
-    preferences_props[PROP_LAST_TABS_RIGHT] =
+  preferences_props[PROP_LAST_TABS_RIGHT] =
         g_param_spec_boxed ("last-tabs-right",
                             NULL,
                             NULL,
@@ -237,16 +251,28 @@ thunar_preferences_class_init (ThunarPreferencesClass *klass)
                             EXO_PARAM_READWRITE);
 
   /**
-   * ThunarPreferences:last-restore-tabs:
+   * ThunarPreferences:last-focused-tab-left:
    *
-   * %TRUE to restore the tabs as they where before closing Thunar.
+   * The index (starting from 0) of the last focused tab in left split-view
    **/
-  preferences_props[PROP_LAST_RESTORE_TABS] =
-      g_param_spec_boolean ("last-restore-tabs",
-                            "LastRestoreTabs",
-                            NULL,
-                            FALSE,
-                            EXO_PARAM_READWRITE);
+  preferences_props[PROP_LAST_FOCUSED_TAB_LEFT] =
+      g_param_spec_int ("last-focused-tab-left",
+                         "LastFocusedTabLeft",
+                         NULL,
+                         0, G_MAXINT, 0,
+                         EXO_PARAM_READWRITE);
+
+  /**
+   * ThunarPreferences:last-focused-tab-right:
+   *
+   * The index (starting from 0) of the last focused tab in right split-view
+   **/
+  preferences_props[PROP_LAST_FOCUSED_TAB_RIGHT] =
+      g_param_spec_int ("last-focused-tab-right",
+                         "LastFocusedTabRight",
+                         NULL,
+                         0, G_MAXINT, 0,
+                         EXO_PARAM_READWRITE);
 
   /**
    * ThunarPreferences:last-compact-view-zoom-level:
diff --git a/thunar/thunar-window.c b/thunar/thunar-window.c
index f6c3e88d7..5573df1a0 100644
--- a/thunar/thunar-window.c
+++ b/thunar/thunar-window.c
@@ -1339,8 +1339,9 @@ static gboolean thunar_window_delete (GtkWidget *widget,
                                       GdkEvent  *event,
                                       gpointer   data )
 {
-  gboolean      confirm_close_multiple_tabs, do_not_ask_again;
-  gint          response, n_tabs = 0, n_tabsl = 0, n_tabsr = 0;
+  gboolean      confirm_close_multiple_tabs, do_not_ask_again, restore_tabs;
+  gint          response, n_tabs, n_tabsl = 0, n_tabsr = 0;
+  gint          current_page_left = 0, current_page_right = 0;
   ThunarWindow *window = THUNAR_WINDOW (widget);
   gchar       **tab_uris_left;
   gchar       **tab_uris_right;
@@ -1348,35 +1349,49 @@ static gboolean thunar_window_delete (GtkWidget *widget,
   _thunar_return_val_if_fail (THUNAR_IS_WINDOW (widget),FALSE);
 
   if (window->notebook_left)
-    n_tabsl += gtk_notebook_get_n_pages (GTK_NOTEBOOK (window->notebook_left));
+    n_tabsl = gtk_notebook_get_n_pages (GTK_NOTEBOOK (window->notebook_left));
   if (window->notebook_right)
-    n_tabsr += gtk_notebook_get_n_pages (GTK_NOTEBOOK (window->notebook_right));
+    n_tabsr = gtk_notebook_get_n_pages (GTK_NOTEBOOK (window->notebook_right));
   n_tabs = n_tabsl + n_tabsr;
 
   /* save open tabs */
-  tab_uris_left = g_new0 (gchar *, n_tabsl + 1);
-  for (int i = 0; i < n_tabsl; i++)
+  g_object_get (G_OBJECT (window->preferences), "last-restore-tabs", &restore_tabs, NULL);
+  if (restore_tabs)
     {
-      ThunarNavigator *view = THUNAR_NAVIGATOR (gtk_notebook_get_nth_page (GTK_NOTEBOOK (window->notebook_left), i));
-      gchar *uri = g_file_get_uri (thunar_file_get_file (thunar_navigator_get_current_directory (view)));
-      tab_uris_left[i] = g_strdup (uri);
-      g_free (uri);
-    }
+      tab_uris_left = g_new0 (gchar *, n_tabsl + 1);
+      for (int i = 0; i < n_tabsl; i++)
+        {
+          ThunarNavigator *view = THUNAR_NAVIGATOR (gtk_notebook_get_nth_page (GTK_NOTEBOOK (window->notebook_left), i));
+          gchar *uri = g_file_get_uri (thunar_file_get_file (thunar_navigator_get_current_directory (view)));
+          tab_uris_left[i] = g_strdup (uri);
+          g_free (uri);
+        }
 
-  tab_uris_right = g_new0 (gchar *, n_tabsr + 1);
-  for (int i = 0; i < n_tabsr; i++)
-    {
-      ThunarNavigator *view = THUNAR_NAVIGATOR (gtk_notebook_get_nth_page (GTK_NOTEBOOK (window->notebook_right), i));
-      gchar *uri = g_file_get_uri (thunar_file_get_file (thunar_navigator_get_current_directory (view)));
-      tab_uris_right[i] = g_strdup (uri);
-      g_free (uri);
-    }
+      tab_uris_right = g_new0 (gchar *, n_tabsr + 1);
+      for (int i = 0; i < n_tabsr; i++)
+        {
+          ThunarNavigator *view = THUNAR_NAVIGATOR (gtk_notebook_get_nth_page (GTK_NOTEBOOK (window->notebook_right), i));
+          gchar *uri = g_file_get_uri (thunar_file_get_file (thunar_navigator_get_current_directory (view)));
+          tab_uris_right[i] = g_strdup (uri);
+          g_free (uri);
+        }
+
+      if (window->notebook_left)
+        current_page_left = gtk_notebook_get_current_page (GTK_NOTEBOOK (window->notebook_left));
+      if (window->notebook_right)
+        current_page_right = gtk_notebook_get_current_page (GTK_NOTEBOOK (window->notebook_right));
 
-  g_object_set (G_OBJECT (window->preferences), "last-tabs-left", tab_uris_left, NULL);
-  g_object_set (G_OBJECT (window->preferences), "last-tabs-right", tab_uris_right, NULL);
+      g_object_set (G_OBJECT (window->preferences), "last-tabs-left", tab_uris_left, NULL);
+      g_object_set (G_OBJECT (window->preferences), "last-tabs-right", tab_uris_right, NULL);
 
-  g_strfreev (tab_uris_left);
-  g_strfreev (tab_uris_right);
+      if (current_page_left > -1)
+        g_object_set (G_OBJECT (window->preferences), "last-focused-tab-left", current_page_left, NULL);
+      if (current_page_right > -1)
+        g_object_set (G_OBJECT (window->preferences), "last-focused-tab-right", current_page_right, NULL);
+
+      g_strfreev (tab_uris_left);
+      g_strfreev (tab_uris_right);
+  }
 
   /* if we don't have muliple tabs in one of the notebooks then just exit */
   if (thunar_window_split_view_is_active (window))
@@ -2400,6 +2415,22 @@ thunar_window_notebook_remove_tab (ThunarWindow *window,
 
 
 
+/**
+ * thunar_window_notebook_set_current_tab:
+ * @window      : a #ThunarWindow instance.
+ * @tab         : the page index as a #gint.
+ *
+ * Switches to the @tab page in the currently selected notebook.
+ **/
+void
+thunar_window_notebook_set_current_tab (ThunarWindow *window,
+                                        gint          tab)
+{
+  gtk_notebook_set_current_page (GTK_NOTEBOOK (window->notebook_selected), tab);
+}
+
+
+
 void
 thunar_window_update_directories (ThunarWindow *window,
                                   ThunarFile   *old_directory,
diff --git a/thunar/thunar-window.h b/thunar/thunar-window.h
index 913313461..a214220aa 100644
--- a/thunar/thunar-window.h
+++ b/thunar/thunar-window.h
@@ -125,6 +125,8 @@ void                      thunar_window_notebook_add_new_tab                (Thu
                                                                              gboolean             force_switch_to_new_tab);
 void                      thunar_window_notebook_remove_tab                 (ThunarWindow        *window,
                                                                              gint                 tab);
+void                      thunar_window_notebook_set_current_tab            (ThunarWindow        *window,
+                                                                             gint                 tab);
 gboolean                  thunar_window_has_shortcut_sidepane               (ThunarWindow        *window);
 GtkWidget*                thunar_window_get_sidepane                        (ThunarWindow        *window);
 void                      thunar_window_append_menu_item                    (ThunarWindow        *window,
-- 
GitLab