[nautilus/gtk4-preparation-trunk: 15/64] list-view: Add style class for thumbnails




commit 5eabb2a6720a776cc7b371eb321346435e129593
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 | 76 +++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 72 insertions(+), 4 deletions(-)
---
diff --git a/src/nautilus-list-view.c b/src/nautilus-list-view.c
index 242a45490..c42ecbdcb 100644
--- a/src/nautilus-list-view.c
+++ b/src/nautilus-list-view.c
@@ -1571,6 +1571,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,
@@ -1609,6 +1619,64 @@ 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;
+    g_autofree gchar *thumbnail_path = 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);
+
+    if (file != NULL)
+    {
+        thumbnail_path = nautilus_file_get_thumbnail_path (file);
+    }
+    /* 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 (thumbnail_path != NULL &&
+        zoom_level_is_enough_for_thumbnails (view) &&
+        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,
@@ -2178,10 +2246,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]