[nautilus/wip/antoniof/new-list-view-continuation: 8/19] view-icon-controller: Make view.sort's state a tuple




commit bd4f789f8d227908846a59f593bab68c24949f99
Author: António Fernandes <antoniof gnome org>
Date:   Mon Feb 14 00:37:01 2022 +0000

    view-icon-controller: Make view.sort's state a tuple
    
    Everywhere else in our codebase the sort attribute and reverse order are
    separate parameters. There is no reason for this action to spawn an
    abstraction which combines both when GAction can just take a tuple with
    both each parameter separately.

 src/nautilus-view-icon-controller.c            | 100 +++++++++----------------
 src/resources/ui/nautilus-toolbar-view-menu.ui |  14 ++--
 2 files changed, 42 insertions(+), 72 deletions(-)
---
diff --git a/src/nautilus-view-icon-controller.c b/src/nautilus-view-icon-controller.c
index ea85a225b..8020a6c66 100644
--- a/src/nautilus-view-icon-controller.c
+++ b/src/nautilus-view-icon-controller.c
@@ -63,7 +63,6 @@ typedef struct
     const NautilusFileSortType sort_type;
     const gchar *metadata_name;
     const gchar *action_target_name;
-    gboolean reversed;
 } SortConstants;
 
 static const SortConstants sorts_constants[] =
@@ -72,73 +71,41 @@ static const SortConstants sorts_constants[] =
         NAUTILUS_FILE_SORT_BY_DISPLAY_NAME,
         "name",
         "name",
-        FALSE,
-    },
-    {
-        NAUTILUS_FILE_SORT_BY_DISPLAY_NAME,
-        "name",
-        "name-desc",
-        TRUE,
     },
     {
         NAUTILUS_FILE_SORT_BY_SIZE,
         "size",
         "size",
-        TRUE,
     },
     {
         NAUTILUS_FILE_SORT_BY_TYPE,
         "type",
         "type",
-        FALSE,
     },
     {
         NAUTILUS_FILE_SORT_BY_MTIME,
         "modification date",
         "modification-date",
-        FALSE,
-    },
-    {
-        NAUTILUS_FILE_SORT_BY_MTIME,
-        "modification date",
-        "modification-date-desc",
-        TRUE,
     },
     {
         NAUTILUS_FILE_SORT_BY_ATIME,
         "access date",
         "access-date",
-        FALSE,
-    },
-    {
-        NAUTILUS_FILE_SORT_BY_ATIME,
-        "access date",
-        "access-date-desc",
-        TRUE,
     },
     {
         NAUTILUS_FILE_SORT_BY_BTIME,
         "creation date",
         "creation-date",
-        FALSE,
-    },
-    {
-        NAUTILUS_FILE_SORT_BY_BTIME,
-        "creation date",
-        "creation-date-desc",
-        TRUE,
     },
     {
         NAUTILUS_FILE_SORT_BY_TRASHED_TIME,
         "trashed",
         "trash-time",
-        TRUE,
     },
     {
         NAUTILUS_FILE_SORT_BY_SEARCH_RELEVANCE,
         NULL,
         "search-relevance",
-        TRUE,
     }
 };
 
@@ -161,15 +128,13 @@ get_sorts_constants_from_action_target_name (const gchar *action_target_name)
 }
 
 static const SortConstants *
-get_sorts_constants_from_sort_type (NautilusFileSortType sort_type,
-                                    gboolean             reversed)
+get_sorts_constants_from_sort_type (NautilusFileSortType sort_type)
 {
     guint i;
 
     for (i = 0; i < G_N_ELEMENTS (sorts_constants); i++)
     {
-        if (sort_type == sorts_constants[i].sort_type
-            && reversed == sorts_constants[i].reversed)
+        if (sort_type == sorts_constants[i].sort_type)
         {
             return &sorts_constants[i];
         }
@@ -179,15 +144,13 @@ get_sorts_constants_from_sort_type (NautilusFileSortType sort_type,
 }
 
 static const SortConstants *
-get_sorts_constants_from_metadata_text (const char *metadata_name,
-                                        gboolean    reversed)
+get_sorts_constants_from_metadata_text (const char *metadata_name)
 {
     guint i;
 
     for (i = 0; i < G_N_ELEMENTS (sorts_constants); i++)
     {
-        if (g_strcmp0 (sorts_constants[i].metadata_name, metadata_name) == 0
-            && reversed == sorts_constants[i].reversed)
+        if (g_strcmp0 (sorts_constants[i].metadata_name, metadata_name) == 0)
         {
             return &sorts_constants[i];
         }
@@ -197,44 +160,46 @@ get_sorts_constants_from_metadata_text (const char *metadata_name,
 }
 
 static const SortConstants *
-get_default_sort_order (NautilusFile *file)
+get_default_sort_order (NautilusFile *file,
+                        gboolean     *reversed)
 {
     NautilusFileSortType sort_type;
-    gboolean reversed;
 
-    sort_type = nautilus_file_get_default_sort_type (file, &reversed);
+    sort_type = nautilus_file_get_default_sort_type (file, reversed);
 
-    return get_sorts_constants_from_sort_type (sort_type, reversed);
+    return get_sorts_constants_from_sort_type (sort_type);
 }
 
 static const SortConstants *
-get_directory_sort_by (NautilusFile *file)
+get_directory_sort_by (NautilusFile *file,
+                       gboolean     *reversed)
 {
     const SortConstants *default_sort;
     g_autofree char *sort_by = NULL;
-    gboolean reversed;
 
-    default_sort = get_default_sort_order (file);
+    default_sort = get_default_sort_order (file, reversed);
     g_return_val_if_fail (default_sort != NULL, NULL);
 
     sort_by = nautilus_file_get_metadata (file,
                                           NAUTILUS_METADATA_KEY_ICON_VIEW_SORT_BY,
                                           default_sort->metadata_name);
 
-    reversed = nautilus_file_get_boolean_metadata (file,
-                                                   NAUTILUS_METADATA_KEY_ICON_VIEW_SORT_REVERSED,
-                                                   default_sort->reversed);
+    *reversed = nautilus_file_get_boolean_metadata (file,
+                                                    NAUTILUS_METADATA_KEY_ICON_VIEW_SORT_REVERSED,
+                                                    *reversed);
 
-    return get_sorts_constants_from_metadata_text (sort_by, reversed);
+    return get_sorts_constants_from_metadata_text (sort_by);
 }
 
 static void
 set_directory_sort_metadata (NautilusFile        *file,
-                             const SortConstants *sort)
+                             const SortConstants *sort,
+                             gboolean             reversed)
 {
     const SortConstants *default_sort;
+    gboolean default_reversed;
 
-    default_sort = get_default_sort_order (file);
+    default_sort = get_default_sort_order (file, &default_reversed);
 
     nautilus_file_set_metadata (file,
                                 NAUTILUS_METADATA_KEY_ICON_VIEW_SORT_BY,
@@ -242,8 +207,8 @@ set_directory_sort_metadata (NautilusFile        *file,
                                 sort->metadata_name);
     nautilus_file_set_boolean_metadata (file,
                                         NAUTILUS_METADATA_KEY_ICON_VIEW_SORT_REVERSED,
-                                        default_sort->reversed,
-                                        sort->reversed);
+                                        default_reversed,
+                                        reversed);
 }
 
 static void
@@ -251,12 +216,16 @@ update_sort_order_from_metadata_and_preferences (NautilusViewIconController *sel
 {
     const SortConstants *default_directory_sort;
     GActionGroup *view_action_group;
+    gboolean reversed;
 
-    default_directory_sort = get_directory_sort_by (nautilus_files_view_get_directory_as_file 
(NAUTILUS_FILES_VIEW (self)));
+    default_directory_sort = get_directory_sort_by (nautilus_files_view_get_directory_as_file 
(NAUTILUS_FILES_VIEW (self)),
+                                                    &reversed);
     view_action_group = nautilus_files_view_get_action_group (NAUTILUS_FILES_VIEW (self));
     g_action_group_change_action_state (view_action_group,
                                         "sort",
-                                        g_variant_new_string (get_sorts_constants_from_sort_type 
(default_directory_sort->sort_type, default_directory_sort->reversed)->action_target_name));
+                                        g_variant_new ("(sb)",
+                                                       default_directory_sort->action_target_name,
+                                                       reversed));
 }
 
 static gint
@@ -960,19 +929,20 @@ real_compare_files (NautilusFilesView *files_view,
     GActionGroup *view_action_group;
     GAction *action;
     const gchar *target_name;
+    gboolean reversed;
     const SortConstants *sort_constants;
     gboolean directories_first;
 
     view_action_group = nautilus_files_view_get_action_group (files_view);
     action = g_action_map_lookup_action (G_ACTION_MAP (view_action_group), "sort");
-    target_name = g_variant_get_string (g_action_get_state (action), NULL);
+    g_variant_get (g_action_get_state (action), "(&sb)", &target_name, &reversed);
     sort_constants = get_sorts_constants_from_action_target_name (target_name);
     directories_first = nautilus_files_view_should_sort_directories_first (files_view);
 
     return nautilus_file_compare_for_sort (file1, file2,
                                            sort_constants->sort_type,
                                            directories_first,
-                                           sort_constants->reversed);
+                                           reversed);
 }
 
 static void
@@ -1165,22 +1135,22 @@ action_sort_order_changed (GSimpleAction *action,
     g_autoptr (GtkCustomSorter) sorter = NULL;
 
     /* Don't resort if the action is in the same state as before */
-    if (g_strcmp0 (g_variant_get_string (value, NULL), g_variant_get_string (g_action_get_state (G_ACTION 
(action)), NULL)) == 0)
+    if (g_variant_equal (value, g_action_get_state (G_ACTION (action))))
     {
         return;
     }
 
     self = NAUTILUS_VIEW_ICON_CONTROLLER (user_data);
-    target_name = g_variant_get_string (value, NULL);
+    g_variant_get (value, "(&sb)", &target_name, &self->reversed);
     sort_constants = get_sorts_constants_from_action_target_name (target_name);
     self->sort_type = sort_constants->sort_type;
-    self->reversed = sort_constants->reversed;
     self->directories_first = nautilus_files_view_should_sort_directories_first (NAUTILUS_FILES_VIEW (self));
 
     sorter = gtk_custom_sorter_new (nautilus_view_icon_controller_sort, self, NULL);
     nautilus_view_model_set_sorter (self->model, GTK_SORTER (sorter));
     set_directory_sort_metadata (nautilus_files_view_get_directory_as_file (NAUTILUS_FILES_VIEW (self)),
-                                 sort_constants);
+                                 sort_constants,
+                                 self->reversed);
 
     g_simple_action_set_state (action, value);
 }
@@ -1511,7 +1481,7 @@ create_view_ui (NautilusViewIconController *self)
 
 const GActionEntry view_icon_actions[] =
 {
-    { "sort", NULL, "s", "'invalid'", action_sort_order_changed },
+    { "sort", NULL, "(sb)", "('invalid',false)", action_sort_order_changed },
     { "zoom-to-level", NULL, NULL, "100", action_zoom_to_level }
 };
 
diff --git a/src/resources/ui/nautilus-toolbar-view-menu.ui b/src/resources/ui/nautilus-toolbar-view-menu.ui
index fb9dac220..aa174889d 100644
--- a/src/resources/ui/nautilus-toolbar-view-menu.ui
+++ b/src/resources/ui/nautilus-toolbar-view-menu.ui
@@ -4,43 +4,43 @@
   <menu id="sort_section">
     <item>
       <attribute name="action">view.sort</attribute>
-      <attribute name="target">name</attribute>
+      <attribute name="target" type="(sb)">('name',false)</attribute>
       <attribute name="label" translatable="yes" context="Sort Criterion" comments="This is used to sort by 
name in the toolbar view menu">_A-Z</attribute>
       <attribute name="hidden-when">action-disabled</attribute>
     </item>
     <item>
       <attribute name="action">view.sort</attribute>
-      <attribute name="target">name-desc</attribute>
+      <attribute name="target" type="(sb)">('name',true)</attribute>
       <attribute name="label" translatable="yes" context="Sort Criterion" comments="This is used to sort by 
name, in descending order in the toolbar view menu">_Z-A</attribute>
       <attribute name="hidden-when">action-disabled</attribute>
     </item>
     <item>
       <attribute name="action">view.sort</attribute>
-      <attribute name="target">modification-date-desc</attribute>
+      <attribute name="target" type="(sb)">('modification-date',true)</attribute>
       <attribute name="label" translatable="yes">Last _Modified</attribute>
       <attribute name="hidden-when">action-disabled</attribute>
     </item>
     <item>
       <attribute name="action">view.sort</attribute>
-      <attribute name="target">modification-date</attribute>
+      <attribute name="target" type="(sb)">('modification-date',false)</attribute>
       <attribute name="label" translatable="yes">_First Modified</attribute>
       <attribute name="hidden-when">action-disabled</attribute>
     </item>
     <item>
       <attribute name="action">view.sort</attribute>
-      <attribute name="target">size</attribute>
+      <attribute name="target" type="(sb)">('size',true)</attribute>
       <attribute name="label" translatable="yes">_Size</attribute>
       <attribute name="hidden-when">action-disabled</attribute>
     </item>
     <item>
       <attribute name="action">view.sort</attribute>
-      <attribute name="target">type</attribute>
+      <attribute name="target" type="(sb)">('type',false)</attribute>
       <attribute name="label" translatable="yes">_Type</attribute>
       <attribute name="hidden-when">action-disabled</attribute>
     </item>
     <item>
       <attribute name="action">view.sort</attribute>
-      <attribute name="target">trash-time</attribute>
+      <attribute name="target" type="(sb)">('trash-time',true)</attribute>
       <attribute name="label" translatable="yes">Last _Trashed</attribute>
       <attribute name="hidden-when">action-disabled</attribute>
       <attribute name="nautilus-menu-item">last_trashed</attribute>


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