[nautilus/wip/antoniof/gtk4-preparation-drop-libgd: 95/100] list-view: Add style class for thumbnails




commit b06a5864cbe71cb49b235d935858f8e3187d444a
Author: António Fernandes <antoniof gnome org>
Date:   Tue Nov 2 21:19:36 2021 +0000

    list-view: Add style class for thumbnails
    
    This is going to be needed to draw shadows to be defined in CSS.

 src/nautilus-list-view.c | 72 +++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 68 insertions(+), 4 deletions(-)
---
diff --git a/src/nautilus-list-view.c b/src/nautilus-list-view.c
index 3b0342a4c..9c2e75eff 100644
--- a/src/nautilus-list-view.c
+++ b/src/nautilus-list-view.c
@@ -54,6 +54,7 @@
 #include "nautilus-ui-utilities.h"
 #include "nautilus-view.h"
 #include "nautilus-tracker-utilities.h"
+#include "nautilus-thumbnails.h"
 
 struct SelectionForeachData
 {
@@ -1715,6 +1716,16 @@ starred_cell_data_func (GtkTreeViewColumn *column,
     g_autofree gchar *text = NULL;
     g_autofree gchar *uri = NULL;
     NautilusFile *file;
+    GtkStyleContext *context;
+
+    /* The "thumbnail" style class is set before rendering each icon cell with
+     * a thumbnail. However, style classes are not applied to each cell, but
+     * alwyas to the whole GtkTreeView widget. So, before the star icon is
+     * rendered, we must ensure that the style is not set, otherwise the star
+     * icon is going to get the styles meant only for thumbnail icons.
+     */
+    context = gtk_widget_get_style_context (GTK_WIDGET (view));
+    gtk_style_context_remove_class (context, "thumbnail");
 
     gtk_tree_model_get (model, iter,
                         view->details->file_name_column_num, &text,
@@ -1753,6 +1764,59 @@ starred_cell_data_func (GtkTreeViewColumn *column,
     nautilus_file_unref (file);
 }
 
+static gboolean
+zoom_level_is_enough_for_thumbnails (NautilusListView *view)
+{
+    NautilusListZoomLevel zoom_level;
+    guint icon_size;
+
+    zoom_level = view->details->zoom_level;
+    icon_size = nautilus_list_model_get_icon_size_for_zoom_level (zoom_level);
+
+    return icon_size >= NAUTILUS_THUMBNAIL_MINIMUM_ICON_SIZE;
+}
+
+static void
+icon_cell_data_func (GtkTreeViewColumn *column,
+                     GtkCellRenderer   *renderer,
+                     GtkTreeModel      *model,
+                     GtkTreeIter       *iter,
+                     NautilusListView  *view)
+{
+    cairo_surface_t *surface;
+    GtkStyleContext *context;
+    g_autoptr (NautilusFile) file = NULL;
+
+    context = gtk_widget_get_style_context (GTK_WIDGET (view));
+    gtk_tree_model_get (model,
+                        iter,
+                        nautilus_list_model_get_column_id_from_zoom_level (view->details->zoom_level),
+                        &surface,
+                        NAUTILUS_LIST_MODEL_FILE_COLUMN,
+                        &file,
+                        -1);
+
+    /* Hack: Set/unset the style class in advance of rendering. This makes a
+     * major assumption that's all but clearly stated in the documentation of
+     * GtkCellLayout: that the DataFunc is called before rendering each cell.
+     */
+    if (zoom_level_is_enough_for_thumbnails (view) &&
+        (file != NULL && (nautilus_can_thumbnail (file) &&
+                          nautilus_file_should_show_thumbnail (file))))
+    {
+        gtk_style_context_add_class (context, "thumbnail");
+    }
+    else
+    {
+        gtk_style_context_remove_class (context, "thumbnail");
+    }
+
+    g_object_set (renderer,
+                  "surface", surface,
+                  NULL);
+    cairo_surface_destroy (surface);
+}
+
 static void
 filename_cell_data_func (GtkTreeViewColumn *column,
                          GtkCellRenderer   *renderer,
@@ -2305,10 +2369,10 @@ create_and_set_up_tree_view (NautilusListView *view)
             set_up_pixbuf_size (view);
 
             gtk_tree_view_column_pack_start (view->details->file_name_column, cell, FALSE);
-            gtk_tree_view_column_set_attributes (view->details->file_name_column,
-                                                 cell,
-                                                 "surface", 
nautilus_list_model_get_column_id_from_zoom_level (view->details->zoom_level),
-                                                 NULL);
+            /* Skip regular attribute mapping in order to add shadow to thumbnails. */
+            gtk_tree_view_column_set_cell_data_func (view->details->file_name_column, cell,
+                                                     (GtkTreeCellDataFunc) icon_cell_data_func,
+                                                     view, NULL);
 
             cell = gtk_cell_renderer_text_new ();
             view->details->file_name_cell = (GtkCellRendererText *) cell;


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