From dda9ca047eadff1c427aef4e2ffab570b190809a Mon Sep 17 00:00:00 2001 From: weyfonk Date: Tue, 13 Sep 2016 10:40:50 +0100 Subject: [PATCH] Detailed view: focus nearest file after deletion (Bug 9870) This commit fixes bug #9870: when deleting files in detailed list view, the focus would go back to the first element of the list, thus scrolling back to the top, which made this view poorly usable when dealing with large amounts of files. The selection is now backed up in thunar-standard-view.c --- thunar/thunar-standard-view.c | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/thunar/thunar-standard-view.c b/thunar/thunar-standard-view.c index 6b078e3..fcf628d 100644 --- a/thunar/thunar-standard-view.c +++ b/thunar/thunar-standard-view.c @@ -2505,9 +2505,45 @@ thunar_standard_view_unlink_selection (ThunarStandardView *standard_view, gboolean permanently) { ThunarApplication *application; + GList *paths; + GList *lp; + GtkTreeIter iter; + GtkTreePath *first_selected_path; + GtkTreePath *last_selected_path; _thunar_return_if_fail (THUNAR_IS_STANDARD_VIEW (standard_view)); + /* Get tree paths of all selected files */ + paths = thunar_list_model_get_paths_for_files (standard_view->model, + standard_view->priv->selected_files); + + /* determine the paths of the last selected file, and first selected file in case we need it later */ + for (last_selected_path = first_selected_path = paths->data, lp = paths; lp != NULL; lp = lp->next) + { + /* check if this path is located before the current first_selected_path */ + if (gtk_tree_path_compare (lp->data, first_selected_path) < 0) + first_selected_path = lp->data; + + /* check if this path is located after the current last_selected_path */ + if (gtk_tree_path_compare (lp->data, last_selected_path) > 0) + last_selected_path = lp->data; + } + + /* Remember the first and last selected paths to be removed so that the selection can be restored + * after the deletion. If the last row of the view is removed, select the last row + * before the first selected file; otherwise, select the first row after the selection */ + gtk_tree_path_next (last_selected_path); + if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (standard_view->model), &iter, last_selected_path) + && G_LIKELY (gtk_tree_path_prev (first_selected_path))) + { + { + if (g_list_first (paths) == g_list_last (paths)) + gtk_tree_path_prev (first_selected_path); + + last_selected_path = first_selected_path; + } + } + /* delete the selected files */ application = thunar_application_get (); thunar_application_unlink_files (application, GTK_WIDGET (standard_view), @@ -2515,8 +2551,10 @@ thunar_standard_view_unlink_selection (ThunarStandardView *standard_view, permanently); g_object_unref (G_OBJECT (application)); - /* do not select new files */ + /* do not select new files, but place the cursor on the selected path */ + (*THUNAR_STANDARD_VIEW_GET_CLASS (standard_view)->set_cursor) (standard_view, last_selected_path, FALSE); thunar_component_set_selected_files (THUNAR_COMPONENT (standard_view), NULL); + g_list_free_full (paths, (GDestroyNotify) gtk_tree_path_free); } -- 2.9.3