[nautilus/wip/antoniof/three-menus-no-more: 3/6] files-view: Add sort menu for grid view




commit dfac6f4fa32c8970f6dc7e63e8f29d4c0c55cd32
Author: António Fernandes <antoniof gnome org>
Date:   Sun Jul 31 21:09:27 2022 +0100

    files-view: Add sort menu for grid view
    
    In list view mode we have a clear indication of sort order, thanks to
    the list column headers, which also allow to change sort order.
    
    Implement a sort header for grid view mode, which matches the visuals
    of the list one and provides indication of the current sort order.

 po/POTFILES.in                          |  1 +
 src/nautilus-files-view.c               | 29 ++++++++++++++-
 src/nautilus-files-view.h               |  6 ++--
 src/nautilus-grid-view.c                | 62 +++++++++++++++++++++++++++++++++
 src/resources/css/Adwaita.css           | 19 +++++++++-
 src/resources/ui/nautilus-files-view.ui | 15 +++++++-
 6 files changed, 127 insertions(+), 5 deletions(-)
---
diff --git a/po/POTFILES.in b/po/POTFILES.in
index eb0f8f29d..ab1b37ba2 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -28,6 +28,7 @@ src/nautilus-compress-dialog-controller.c
 src/nautilus-dbus-launcher.c
 src/nautilus-directory.c
 src/nautilus-dnd.c
+src/nautilus-grid-view.c
 src/nautilus-error-reporting.c
 src/nautilus-file.c
 src/nautilus-file-conflict-dialog.c
diff --git a/src/nautilus-files-view.c b/src/nautilus-files-view.c
index 7be5722f7..92acb4aa4 100644
--- a/src/nautilus-files-view.c
+++ b/src/nautilus-files-view.c
@@ -246,6 +246,7 @@ typedef struct
 
     GtkWidget *stack;
 
+    GtkWidget *sort_menu_button;
     GtkWidget *scrolled_window;
 
     /* Empty states */
@@ -348,7 +349,7 @@ static gboolean nautilus_files_view_is_read_only (NautilusFilesView *view);
 
 G_DEFINE_TYPE_WITH_CODE (NautilusFilesView,
                          nautilus_files_view,
-                         ADW_TYPE_BIN,
+                         GTK_TYPE_BOX,
                          G_IMPLEMENT_INTERFACE (NAUTILUS_TYPE_VIEW, nautilus_files_view_iface_init)
                          G_ADD_PRIVATE (NautilusFilesView));
 
@@ -4842,6 +4843,15 @@ nautilus_files_view_get_content_widget (NautilusFilesView *view)
     return priv->scrolled_window;
 }
 
+void
+nautilus_files_view_set_sort_label (NautilusFilesView *view,
+                                    const gchar       *label)
+{
+    NautilusFilesViewPrivate *priv = nautilus_files_view_get_instance_private (view);
+
+    gtk_menu_button_set_label (GTK_MENU_BUTTON (priv->sort_menu_button), label);
+}
+
 /* home_dir_in_selection()
  *
  * Return TRUE if the home directory is in the selection.
@@ -9348,6 +9358,17 @@ nautilus_files_view_is_loading (NautilusView *view)
     return priv->loading;
 }
 
+static void
+nautilus_files_view_constructed (GObject *object)
+{
+    NautilusFilesView *self = NAUTILUS_FILES_VIEW (object);
+    NautilusFilesViewPrivate *priv = nautilus_files_view_get_instance_private (self);
+
+    G_OBJECT_CLASS (nautilus_files_view_parent_class)->constructed (object);
+
+    gtk_widget_set_visible (priv->sort_menu_button, NAUTILUS_IS_GRID_VIEW (self));
+}
+
 static void
 nautilus_files_view_iface_init (NautilusViewInterface *iface)
 {
@@ -9376,6 +9397,7 @@ nautilus_files_view_class_init (NautilusFilesViewClass *klass)
     widget_class = GTK_WIDGET_CLASS (klass);
     oclass = G_OBJECT_CLASS (klass);
 
+    oclass->constructed = nautilus_files_view_constructed;
     oclass->dispose = nautilus_files_view_dispose;
     oclass->finalize = nautilus_files_view_finalize;
     oclass->get_property = nautilus_files_view_get_property;
@@ -9493,6 +9515,7 @@ nautilus_files_view_class_init (NautilusFilesViewClass *klass)
     gtk_widget_class_set_template_from_resource (widget_class,
                                                  "/org/gnome/nautilus/ui/nautilus-files-view.ui");
 
+    gtk_widget_class_bind_template_child_private (widget_class, NautilusFilesView, sort_menu_button);
     gtk_widget_class_bind_template_child_private (widget_class, NautilusFilesView, overlay);
     gtk_widget_class_bind_template_child_private (widget_class, NautilusFilesView, stack);
     gtk_widget_class_bind_template_child_private (widget_class, NautilusFilesView, empty_view_page);
@@ -9603,6 +9626,9 @@ nautilus_files_view_init (NautilusFilesView *view)
     g_type_ensure (NAUTILUS_TYPE_FLOATING_BAR);
     gtk_widget_init_template (GTK_WIDGET (view));
 
+    gtk_menu_button_set_menu_model (GTK_MENU_BUTTON (priv->sort_menu_button),
+                                    priv->toolbar_menu_sections->sort_section);
+
     controller = gtk_event_controller_scroll_new (GTK_EVENT_CONTROLLER_SCROLL_VERTICAL |
                                                   GTK_EVENT_CONTROLLER_SCROLL_DISCRETE);
     gtk_widget_add_controller (priv->scrolled_window, controller);
@@ -9686,6 +9712,7 @@ nautilus_files_view_init (NautilusFilesView *view)
     gtk_widget_insert_action_group (GTK_WIDGET (view),
                                     "view",
                                     G_ACTION_GROUP (priv->view_action_group));
+
     app = g_application_get_default ();
 
     /* NOTE: Please do not add any key here that could interfere with
diff --git a/src/nautilus-files-view.h b/src/nautilus-files-view.h
index e1882b7ba..dc2609a75 100644
--- a/src/nautilus-files-view.h
+++ b/src/nautilus-files-view.h
@@ -38,10 +38,10 @@ G_BEGIN_DECLS
 
 #define NAUTILUS_TYPE_FILES_VIEW nautilus_files_view_get_type()
 
-G_DECLARE_DERIVABLE_TYPE (NautilusFilesView, nautilus_files_view, NAUTILUS, FILES_VIEW, AdwBin)
+G_DECLARE_DERIVABLE_TYPE (NautilusFilesView, nautilus_files_view, NAUTILUS, FILES_VIEW, GtkBox)
 
 struct _NautilusFilesViewClass {
-        AdwBinClass parent_class;
+        GtkBoxClass parent_class;
 
         /* The 'clear' signal is emitted to empty the view of its contents.
          * It must be replaced by each subclass.
@@ -321,5 +321,7 @@ void              nautilus_files_view_action_show_hidden_files   (NautilusFilesV
 
 GActionGroup *    nautilus_files_view_get_action_group           (NautilusFilesView      *view);
 GtkWidget*        nautilus_files_view_get_content_widget         (NautilusFilesView      *view);
+void              nautilus_files_view_set_sort_label             (NautilusFilesView      *view,
+                                                                  const gchar            *label);
 
 G_END_DECLS
diff --git a/src/nautilus-grid-view.c b/src/nautilus-grid-view.c
index 1e8e54569..a23bbd7d6 100644
--- a/src/nautilus-grid-view.c
+++ b/src/nautilus-grid-view.c
@@ -7,6 +7,8 @@
 #include "nautilus-list-base-private.h"
 #include "nautilus-grid-view.h"
 
+#include <glib/gi18n.h>
+
 #include "nautilus-grid-cell.h"
 #include "nautilus-global-preferences.h"
 
@@ -261,6 +263,64 @@ real_sort_directories_first_changed (NautilusFilesView *files_view)
     nautilus_view_model_set_sorter (model, GTK_SORTER (sorter));
 }
 
+static void
+update_sort_menu_label (NautilusGridView *self)
+{
+    const gchar *label;
+
+    switch (self->sort_type)
+    {
+        case NAUTILUS_FILE_SORT_BY_DISPLAY_NAME:
+        {
+            label = self->reversed ? _("Z-A") : _("A-Z");
+        }
+        break;
+
+        case NAUTILUS_FILE_SORT_BY_MTIME:
+        {
+            label = self->reversed ? _("Last Modified") : _("First Modified");
+        }
+        break;
+
+        case NAUTILUS_FILE_SORT_BY_SIZE:
+        {
+            label = self->reversed ? _("Size") : _("Size (ascending)");
+        }
+        break;
+
+        case NAUTILUS_FILE_SORT_BY_TYPE:
+        {
+            label = self->reversed ? _("Type (reversed)") : _("Type");
+        }
+        break;
+
+        case NAUTILUS_FILE_SORT_BY_TRASHED_TIME:
+        {
+            label = self->reversed ? _("Last Trashed") : _("First Trashed");
+        }
+        break;
+
+        case NAUTILUS_FILE_SORT_BY_SEARCH_RELEVANCE:
+        {
+            label = self->reversed ? _("Relevance") : _("Least Relevant");
+        }
+        break;
+
+        case NAUTILUS_FILE_SORT_BY_RECENCY:
+        {
+            label = self->reversed ? _("Recency") : _("Recency (reversed)");
+        }
+        break;
+
+        default:
+        {
+            label = _("Sort");
+        }
+    }
+
+    nautilus_files_view_set_sort_label (NAUTILUS_FILES_VIEW (self), label);
+}
+
 static void
 action_sort_order_changed (GSimpleAction *action,
                            GVariant      *value,
@@ -287,6 +347,8 @@ action_sort_order_changed (GSimpleAction *action,
                                  target_name,
                                  self->reversed);
 
+    update_sort_menu_label (self);
+
     g_simple_action_set_state (action, value);
 }
 
diff --git a/src/resources/css/Adwaita.css b/src/resources/css/Adwaita.css
index f2628f3a9..a6a6466a1 100644
--- a/src/resources/css/Adwaita.css
+++ b/src/resources/css/Adwaita.css
@@ -144,8 +144,25 @@
 }
 
 /* Grid view */
+.nautilus-grid-view .nautilus-sort-button button {
+  min-height: 20px;
+  margin-left: 24px;
+  margin-right: 24px;
+  background: none;
+}
+.nautilus-grid-view .nautilus-sort-button button:not(:hover):not(:checked) {
+  opacity: 0.4;
+}
+
+.nautilus-grid-view scrolledwindow undershoot.top {
+  border-top: 1px solid @borders;
+}
+
 .nautilus-grid-view gridview {
-  padding: 15px;
+  padding-top: 12px;
+  padding-bottom: 21px;
+  padding-left: 21px;
+  padding-right: 21px;
 }
 
 .nautilus-grid-view gridview > child {
diff --git a/src/resources/ui/nautilus-files-view.ui b/src/resources/ui/nautilus-files-view.ui
index eab5bddbe..db5e68779 100644
--- a/src/resources/ui/nautilus-files-view.ui
+++ b/src/resources/ui/nautilus-files-view.ui
@@ -1,11 +1,24 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <interface>
   <requires lib="gtk" version="4.0"/>
-  <template class="NautilusFilesView" parent="AdwBin">
+  <template class="NautilusFilesView" parent="GtkBox">
+    <property name="orientation">vertical</property>
     <accessibility>
       <property name="label" translatable="yes">Content View</property>
       <property name="description" translatable="yes">View of the current folder</property>
     </accessibility>
+    <child>
+      <object class="GtkMenuButton" id="sort_menu_button">
+        <property name="halign">start</property>
+        <property name="valign">start</property>
+        <property name="always-show-arrow">true</property>
+        <property name="label">Sort</property>
+        <style>
+          <class name="caption-heading"/>
+          <class name="nautilus-sort-button"/>
+        </style>
+      </object>
+    </child>
     <child>
       <object class="GtkOverlay" id="overlay">
         <property name="hexpand">True</property>


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