[nautilus/wip/antoniof/new-list-view-continuation: 2/4] files-view: Allow sorting in recent and search




commit f3bce08ab6dd27e90964c52984874a35786a90a1
Author: António Fernandes <antoniof gnome org>
Date:   Sat Apr 9 12:03:59 2022 +0100

    files-view: Allow sorting in recent and search
    
    Now that both list and grid view have a sort menu, we can finally
    enable sorting the recent and search directories while providing
    in the menu a way to restore their natural sort order (Recency
    and Relevance, respectively).
    
    Fixes https://gitlab.gnome.org/GNOME/nautilus/-/issues/374

 src/nautilus-files-model-view.c                | 43 --------------------------
 src/nautilus-files-view.c                      | 24 ++++++++------
 src/nautilus-ui-utilities.c                    |  2 ++
 src/resources/ui/nautilus-toolbar-view-menu.ui | 14 +++++++++
 4 files changed, 31 insertions(+), 52 deletions(-)
---
diff --git a/src/nautilus-files-model-view.c b/src/nautilus-files-model-view.c
index 869a389a3..e4f3e01df 100644
--- a/src/nautilus-files-model-view.c
+++ b/src/nautilus-files-model-view.c
@@ -438,48 +438,6 @@ real_reveal_selection (NautilusFilesView *files_view)
     nautilus_files_model_view_scroll_to_item (self, get_first_selected_item (self));
 }
 
-static gboolean
-showing_recent_directory (NautilusFilesView *view)
-{
-    NautilusFile *file;
-
-    file = nautilus_files_view_get_directory_as_file (view);
-    if (file != NULL)
-    {
-        return nautilus_file_is_in_recent (file);
-    }
-    return FALSE;
-}
-
-static gboolean
-showing_search_directory (NautilusFilesView *view)
-{
-    NautilusFile *file;
-
-    file = nautilus_files_view_get_directory_as_file (view);
-    if (file != NULL)
-    {
-        return nautilus_file_is_in_search (file);
-    }
-    return FALSE;
-}
-
-static void
-real_update_actions_state (NautilusFilesView *files_view)
-{
-    GAction *action;
-    GActionGroup *view_action_group;
-
-    NAUTILUS_FILES_VIEW_CLASS (nautilus_files_model_view_parent_class)->update_actions_state (files_view);
-
-    view_action_group = nautilus_files_view_get_action_group (files_view);
-    action = g_action_map_lookup_action (G_ACTION_MAP (view_action_group), "sort");
-    g_simple_action_set_enabled (G_SIMPLE_ACTION (action),
-                                 !showing_recent_directory (files_view) &&
-                                 !showing_search_directory (files_view));
-}
-
-
 static int
 real_compare_files (NautilusFilesView *files_view,
                     NautilusFile      *file1,
@@ -941,7 +899,6 @@ nautilus_files_model_view_class_init (NautilusFilesModelViewClass *klass)
     files_view_class->end_loading = real_end_loading;
     files_view_class->get_first_visible_file = real_get_first_visible_file;
     files_view_class->reveal_selection = real_reveal_selection;
-    files_view_class->update_actions_state = real_update_actions_state;
     files_view_class->scroll_to_file = real_scroll_to_file;
     files_view_class->select_first = real_select_first;
     files_view_class->compute_rename_popover_pointing_to = real_compute_rename_popover_pointing_to;
diff --git a/src/nautilus-files-view.c b/src/nautilus-files-view.c
index 741057b5f..db43b3c00 100644
--- a/src/nautilus-files-view.c
+++ b/src/nautilus-files-view.c
@@ -8177,20 +8177,26 @@ nautilus_files_view_reset_view_menu (NautilusFilesView *view)
     NautilusFilesViewPrivate *priv = nautilus_files_view_get_instance_private (view);
     NautilusFile *file;
     GMenuModel *sort_section = priv->toolbar_menu_sections->sort_section;
-    const gchar *trashed_action;
+    const gchar *action;
     gint i;
 
     file = nautilus_files_view_get_directory_as_file (NAUTILUS_FILES_VIEW (view));
 
-    /* When not in Trash, set an inexistant action to hide the menu item. This
-     * works under the assumptiont that the menu item has its "hidden-when"
-     * attribute set to "action-disabled", and that an inexistant action is
-     * treated as a disabled action. */
-    trashed_action = nautilus_file_is_in_trash (file) ? "view.sort" : "doesnt-exist";
+    /* When not in the special location, set an inexistant action to hide the
+     * menu item. This works under the assumptiont that the menu item has its
+     * "hidden-when" attribute set to "action-disabled", and that an inexistant
+     * action is treated as a disabled action. */
+    action = nautilus_file_is_in_trash (file) ? "view.sort" : "doesnt-exist";
     i = nautilus_g_menu_model_find_by_string (sort_section, "nautilus-menu-item", "last_trashed");
-    g_return_if_fail (i != -1);
-    nautilus_g_menu_replace_string_in_item (G_MENU (sort_section), i,
-                                            "action", trashed_action);
+    nautilus_g_menu_replace_string_in_item (G_MENU (sort_section), i, "action", action);
+
+    action = nautilus_file_is_in_recent (file) ? "view.sort" : "doesnt-exist";
+    i = nautilus_g_menu_model_find_by_string (sort_section, "nautilus-menu-item", "recency");
+    nautilus_g_menu_replace_string_in_item (G_MENU (sort_section), i, "action", action);
+
+    action = nautilus_file_is_in_search (file) ? "view.sort" : "doesnt-exist";
+    i = nautilus_g_menu_model_find_by_string (sort_section, "nautilus-menu-item", "relevance");
+    nautilus_g_menu_replace_string_in_item (G_MENU (sort_section), i, "action", action);
 }
 
 /* Convenience function to reset the menus owned by the view but managed on
diff --git a/src/nautilus-ui-utilities.c b/src/nautilus-ui-utilities.c
index 1498488df..8e5933ee8 100644
--- a/src/nautilus-ui-utilities.c
+++ b/src/nautilus-ui-utilities.c
@@ -127,7 +127,9 @@ nautilus_g_menu_replace_string_in_item (GMenu       *menu,
 {
     g_autoptr (GMenuItem) item = NULL;
 
+    g_return_if_fail (i != -1);
     item = g_menu_item_new_from_model (G_MENU_MODEL (menu), i);
+    g_return_if_fail (item != NULL);
     g_menu_item_set_attribute (item, attribute, "s", string);
     g_menu_remove (menu, i);
     g_menu_insert_item (menu, i, item);
diff --git a/src/resources/ui/nautilus-toolbar-view-menu.ui b/src/resources/ui/nautilus-toolbar-view-menu.ui
index 32d0e95cf..03c6e8e8d 100644
--- a/src/resources/ui/nautilus-toolbar-view-menu.ui
+++ b/src/resources/ui/nautilus-toolbar-view-menu.ui
@@ -45,5 +45,19 @@
       <attribute name="hidden-when">action-disabled</attribute>
       <attribute name="nautilus-menu-item">last_trashed</attribute>
     </item>
+    <item>
+      <attribute name="action">view.sort</attribute>
+      <attribute name="target" type="(sb)">('recency',true)</attribute>
+      <attribute name="label" translatable="yes">Recency</attribute>
+      <attribute name="hidden-when">action-disabled</attribute>
+      <attribute name="nautilus-menu-item">recency</attribute>
+    </item>
+    <item>
+      <attribute name="action">view.sort</attribute>
+      <attribute name="target" type="(sb)">('search_relevance',true)</attribute>
+      <attribute name="label" translatable="yes">Relevance</attribute>
+      <attribute name="hidden-when">action-disabled</attribute>
+      <attribute name="nautilus-menu-item">relevance</attribute>
+    </item>
   </menu>
 </interface>


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