[evince/wip/gpoo/gtk4-rebase-port: 24/72] ev-sidebar-attachments: remove the icon cache and enable popup




commit d21aa083571db8e86170cdf08e579a125a3085e5
Author: Qiu Wenbo <qiuwenbo kylinos com cn>
Date:   Sat Aug 28 16:05:30 2021 +0800

    ev-sidebar-attachments: remove the icon cache and enable popup
    
    Signed-off-by: Qiu Wenbo <qiuwenbo kylinos com cn>

 shell/ev-sidebar-attachments.c      | 350 +++++-------------------------------
 shell/evince-sidebar-attachments.ui |  39 +++-
 2 files changed, 78 insertions(+), 311 deletions(-)
---
diff --git a/shell/ev-sidebar-attachments.c b/shell/ev-sidebar-attachments.c
index 48dfd2c69..a94806c42 100644
--- a/shell/ev-sidebar-attachments.c
+++ b/shell/ev-sidebar-attachments.c
@@ -38,6 +38,7 @@
 #include "ev-file-helpers.h"
 #include "ev-sidebar-attachments.h"
 #include "ev-sidebar-page.h"
+#include "ev-shell-marshal.h"
 
 enum {
        COLUMN_ICON,
@@ -74,10 +75,6 @@ static guint signals[N_SIGNALS];
 struct _EvSidebarAttachmentsPrivate {
        GtkWidget      *icon_view;
        GtkListStore   *model;
-
-       /* Icons */
-       GtkIconTheme   *icon_theme;
-       GHashTable     *icon_cache;
 };
 
 static void ev_sidebar_attachments_page_iface_init (EvSidebarPageInterface *iface);
@@ -90,133 +87,10 @@ G_DEFINE_TYPE_EXTENDED (EvSidebarAttachments,
                         G_IMPLEMENT_INTERFACE (EV_TYPE_SIDEBAR_PAGE,
                                               ev_sidebar_attachments_page_iface_init))
 
-/* Icon cache */
-static void
-ev_sidebar_attachments_icon_cache_add (EvSidebarAttachments *ev_attachbar,
-                                      const gchar          *mime_type,
-                                      const GdkPixbuf      *pixbuf)
-{
-       g_assert (mime_type != NULL);
-       g_assert (GDK_IS_PIXBUF (pixbuf));
-
-       g_hash_table_insert (ev_attachbar->priv->icon_cache,
-                            (gpointer)g_strdup (mime_type),
-                            (gpointer)pixbuf);
-
-}
-
-static GdkPixbuf *
-icon_theme_get_pixbuf_from_mime_type (GtkIconTheme *icon_theme,
-                                     const gchar  *mime_type)
-{
-       const char *separator;
-       GString *icon_name;
-       GdkPixbuf *pixbuf;
-
-       separator = strchr (mime_type, '/');
-       if (!separator)
-               return NULL; /* maybe we should return a GError with "invalid MIME-type" */
-
-       icon_name = g_string_new ("gnome-mime-");
-       g_string_append_len (icon_name, mime_type, separator - mime_type);
-       g_string_append_c (icon_name, '-');
-       g_string_append (icon_name, separator + 1);
-       // pixbuf = gtk_icon_theme_load_icon (icon_theme, icon_name->str, 48, 0, NULL);
-       g_string_free (icon_name, TRUE);
-       if (pixbuf)
-               return pixbuf;
-
-       icon_name = g_string_new ("gnome-mime-");
-       g_string_append_len (icon_name, mime_type, separator - mime_type);
-       // pixbuf = gtk_icon_theme_load_icon (icon_theme, icon_name->str, 48, 0, NULL);
-       g_string_free (icon_name, TRUE);
-
-       return pixbuf;
-}
-
-static GdkPixbuf *
-ev_sidebar_attachments_icon_cache_get (EvSidebarAttachments *ev_attachbar,
-                                      const gchar          *mime_type)
-{
-       GdkPixbuf *pixbuf = NULL;
-
-       g_assert (mime_type != NULL);
-
-       pixbuf = g_hash_table_lookup (ev_attachbar->priv->icon_cache,
-                                     mime_type);
-
-       if (GDK_IS_PIXBUF (pixbuf))
-               return pixbuf;
-
-       pixbuf = icon_theme_get_pixbuf_from_mime_type (ev_attachbar->priv->icon_theme,
-                                                      mime_type);
-
-       if (GDK_IS_PIXBUF (pixbuf))
-               ev_sidebar_attachments_icon_cache_add (ev_attachbar,
-                                                      mime_type,
-                                                      pixbuf);
-
-       return pixbuf;
-}
-
-static gboolean
-icon_cache_update_icon (gchar                *key,
-                       GdkPixbuf            *value,
-                       EvSidebarAttachments *ev_attachbar)
-{
-       GdkPixbuf *pixbuf = NULL;
-
-       pixbuf = icon_theme_get_pixbuf_from_mime_type (ev_attachbar->priv->icon_theme,
-                                                      key);
-
-       ev_sidebar_attachments_icon_cache_add (ev_attachbar,
-                                              key,
-                                              pixbuf);
-
-       return FALSE;
-}
-
-static void
-ev_sidebar_attachments_icon_cache_refresh (EvSidebarAttachments *ev_attachbar)
-{
-       g_hash_table_foreach_remove (ev_attachbar->priv->icon_cache,
-                                    (GHRFunc) icon_cache_update_icon,
-                                    ev_attachbar);
-}
-
-static EvAttachment *
-ev_sidebar_attachments_get_attachment_at_pos (EvSidebarAttachments *ev_attachbar,
-                                             gint                  x,
-                                             gint                  y)
-{
-       GtkTreePath  *path = NULL;
-       GtkTreeIter   iter;
-       EvAttachment *attachment = NULL;
-
-       path = gtk_icon_view_get_path_at_pos (GTK_ICON_VIEW (ev_attachbar->priv->icon_view),
-                                             x, y);
-       if (!path) {
-               return NULL;
-       }
-
-       gtk_tree_model_get_iter (GTK_TREE_MODEL (ev_attachbar->priv->model),
-                                &iter, path);
-       gtk_tree_model_get (GTK_TREE_MODEL (ev_attachbar->priv->model), &iter,
-                           COLUMN_ATTACHMENT, &attachment,
-                           -1);
-
-       gtk_icon_view_select_path (GTK_ICON_VIEW (ev_attachbar->priv->icon_view),
-                                  path);
-
-       gtk_tree_path_free (path);
-
-       return attachment;
-}
-
 static gboolean
 ev_sidebar_attachments_popup_menu_show (EvSidebarAttachments *ev_attachbar,
-                                       gint                  x,
-                                       gint                  y)
+                                       gdouble               x,
+                                       gdouble               y)
 {
        GtkIconView *icon_view;
        GtkTreePath *path;
@@ -263,141 +137,55 @@ ev_sidebar_attachments_popup_menu_show (EvSidebarAttachments *ev_attachbar,
        if (!attach_list)
                return FALSE;
 
-       g_signal_emit (ev_attachbar, signals[SIGNAL_POPUP_MENU], 0, attach_list);
+       g_signal_emit (ev_attachbar, signals[SIGNAL_POPUP_MENU], 0, x, y, attach_list);
 
        return TRUE;
 }
 
-static gboolean
-ev_sidebar_attachments_popup_menu (GtkWidget *widget)
-{
-       EvSidebarAttachments *ev_attachbar = EV_SIDEBAR_ATTACHMENTS (widget);
-       gint                  x, y;
-
-       ev_document_misc_get_pointer_position (widget, &x, &y);
-
-       return ev_sidebar_attachments_popup_menu_show (ev_attachbar, x, y);
-}
-#if 0
-
-static gboolean
-ev_sidebar_attachments_button_press (EvSidebarAttachments *ev_attachbar,
-                                    GdkEventButton       *event,
-                                    GtkWidget            *icon_view)
-{
-       if (!gtk_widget_has_focus (icon_view)) {
-               gtk_widget_grab_focus (icon_view);
-       }
-
-       if (event->button == 2)
-               return FALSE;
-
-       switch (event->button) {
-               case 1:
-                       if (event->type == GDK_2BUTTON_PRESS) {
-                               GError *error = NULL;
-                               EvAttachment *attachment;
-
-                               attachment = ev_sidebar_attachments_get_attachment_at_pos (ev_attachbar,
-                                                                                          event->x,
-                                                                                          event->y);
-                               if (!attachment)
-                                       return FALSE;
-
-                               ev_attachment_open (attachment,
-                                                   gtk_widget_get_screen (GTK_WIDGET (ev_attachbar)),
-                                                   event->time,
-                                                   &error);
-
-                               if (error) {
-                                       g_warning ("%s", error->message);
-                                       g_error_free (error);
-                               }
-
-                               g_object_unref (attachment);
-
-                               return TRUE;
-                       }
-                       break;
-               case 3:
-                       return ev_sidebar_attachments_popup_menu_show (ev_attachbar, event->x, event->y);
-       }
-
-       return FALSE;
-}
-
 static void
-ev_sidebar_attachments_update_icons (EvSidebarAttachments *ev_attachbar,
-                                    gpointer              user_data)
+on_icon_view_item_activated (GtkIconView               *self,
+                            GtkTreePath                *path,
+                            EvSidebarAttachments       *ev_attachbar)
 {
-       GtkTreeIter iter;
-       gboolean    valid;
-
-       ev_sidebar_attachments_icon_cache_refresh (ev_attachbar);
-
-       valid = gtk_tree_model_get_iter_first (
-               GTK_TREE_MODEL (ev_attachbar->priv->model),
-               &iter);
 
-       while (valid) {
-               EvAttachment *attachment = NULL;
-               GdkPixbuf    *pixbuf = NULL;
-               const gchar  *mime_type;
-
-               gtk_tree_model_get (GTK_TREE_MODEL (ev_attachbar->priv->model), &iter,
-                                   COLUMN_ATTACHMENT, &attachment,
-                                   -1);
-
-               mime_type = ev_attachment_get_mime_type (attachment);
+       EvAttachment *attachment;
+       GtkTreeIter iter;
+       GError *error = NULL;
 
-               if (attachment)
-                       g_object_unref (attachment);
+       gtk_tree_model_get_iter (GTK_TREE_MODEL (ev_attachbar->priv->model),
+                                &iter, path);
+       gtk_tree_model_get (GTK_TREE_MODEL (ev_attachbar->priv->model), &iter,
+                           COLUMN_ATTACHMENT, &attachment,
+                           -1);
+       gtk_icon_view_select_path (GTK_ICON_VIEW (ev_attachbar->priv->icon_view), path);
 
-               pixbuf = ev_sidebar_attachments_icon_cache_get (ev_attachbar,
-                                                               mime_type);
+       if (!attachment)
+               return;
 
-               gtk_list_store_set (ev_attachbar->priv->model, &iter,
-                                   COLUMN_ICON, pixbuf,
-                                   -1);
+       ev_attachment_open (attachment,
+                               gtk_widget_get_display (GTK_WIDGET (ev_attachbar)),
+                               GDK_CURRENT_TIME,
+                               &error);
 
-               valid = gtk_tree_model_iter_next (
-                       GTK_TREE_MODEL (ev_attachbar->priv->model),
-                       &iter);
+       if (error) {
+               g_warning ("%s", error->message);
+               g_error_free (error);
        }
+
+       g_object_unref (attachment);
 }
 
 static void
-ev_sidebar_attachments_screen_changed (GtkWidget *widget,
-                                      GdkScreen *old_screen)
+on_secondary_button_clicked (GtkGestureClick           *self,
+                            gint                        n_press,
+                            gdouble                     x,
+                            gdouble                     y,
+                            EvSidebarAttachments       *ev_attachbar)
 {
-       EvSidebarAttachments *ev_attachbar = EV_SIDEBAR_ATTACHMENTS (widget);
-       GdkScreen            *screen;
-
-       if (!ev_attachbar->priv->icon_theme)
-               return;
-
-       screen = gtk_widget_get_screen (widget);
-       if (screen == old_screen)
-               return;
-
-       if (old_screen) {
-               g_signal_handlers_disconnect_by_func (
-                       gtk_icon_theme_get_for_screen (old_screen),
-                       G_CALLBACK (ev_sidebar_attachments_update_icons),
-                       ev_attachbar);
-       }
-
-       ev_attachbar->priv->icon_theme = gtk_icon_theme_get_for_screen (screen);
-       g_signal_connect_swapped (ev_attachbar->priv->icon_theme,
-                                 "changed",
-                                 G_CALLBACK (ev_sidebar_attachments_update_icons),
-                                 (gpointer) ev_attachbar);
-
-       if (GTK_WIDGET_CLASS (ev_sidebar_attachments_parent_class)->screen_changed) {
-               GTK_WIDGET_CLASS (ev_sidebar_attachments_parent_class)->screen_changed (widget, old_screen);
-       }
+       ev_sidebar_attachments_popup_menu_show (ev_attachbar, x, y);
 }
 
+#if 0
 
 static gchar *
 read_xds_property (GdkDragContext *context)
@@ -434,7 +222,6 @@ write_xds_property (GdkDragContext *context,
        else
                gdk_property_delete (gdk_drag_context_get_source_window (context), XDS_ATOM);
 }
-
 #endif
 
 /*
@@ -667,26 +454,12 @@ ev_sidebar_attachments_dispose (GObject *object)
 {
        EvSidebarAttachments *ev_attachbar = EV_SIDEBAR_ATTACHMENTS (object);
 
-       if (ev_attachbar->priv->icon_theme) {
-#if 0
-               g_signal_handlers_disconnect_by_func (
-                       ev_attachbar->priv->icon_theme,
-                       G_CALLBACK (ev_sidebar_attachments_update_icons),
-                       ev_attachbar);
-#endif
-               ev_attachbar->priv->icon_theme = NULL;
-       }
 
        if (ev_attachbar->priv->model) {
                g_object_unref (ev_attachbar->priv->model);
                ev_attachbar->priv->model = NULL;
        }
 
-       if (ev_attachbar->priv->icon_cache) {
-               g_hash_table_destroy (ev_attachbar->priv->icon_cache);
-               ev_attachbar->priv->icon_cache = NULL;
-       }
-
        G_OBJECT_CLASS (ev_sidebar_attachments_parent_class)->dispose (object);
 }
 
@@ -701,10 +474,11 @@ ev_sidebar_attachments_class_init (EvSidebarAttachmentsClass *ev_attachbar_class
        gtk_widget_class_bind_template_child_private (widget_class, EvSidebarAttachments, model);
        gtk_widget_class_bind_template_child_private (widget_class, EvSidebarAttachments, icon_view);
 
+       gtk_widget_class_bind_template_callback (widget_class, on_icon_view_item_activated);
+       gtk_widget_class_bind_template_callback (widget_class, on_secondary_button_clicked);
+
        g_object_class->get_property = ev_sidebar_attachments_get_property;
        g_object_class->dispose = ev_sidebar_attachments_dispose;
-       // gtk_widget_class->popup_menu = ev_sidebar_attachments_popup_menu;
-       // gtk_widget_class->screen_changed = ev_sidebar_attachments_screen_changed;
 
        /* Signals */
        signals[SIGNAL_POPUP_MENU] =
@@ -713,8 +487,10 @@ ev_sidebar_attachments_class_init (EvSidebarAttachmentsClass *ev_attachbar_class
                              G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                              G_STRUCT_OFFSET (EvSidebarAttachmentsClass, popup_menu),
                              NULL, NULL,
-                             g_cclosure_marshal_VOID__POINTER,
-                             G_TYPE_NONE, 1,
+                             ev_shell_marshal_VOID__DOUBLE_DOUBLE_POINTER,
+                             G_TYPE_NONE, 3,
+                             G_TYPE_DOUBLE,
+                             G_TYPE_DOUBLE,
                              G_TYPE_POINTER);
 
        signals[SIGNAL_SAVE_ATTACHMENT] =
@@ -744,22 +520,6 @@ ev_sidebar_attachments_init (EvSidebarAttachments *ev_attachbar)
 
        gtk_widget_init_template (GTK_WIDGET (ev_attachbar));
 
-#if 0
-       g_signal_connect_swapped (ev_attachbar->priv->icon_view,
-                                 "button-press-event",
-                                 G_CALLBACK (ev_sidebar_attachments_button_press),
-                                 (gpointer) ev_attachbar);
-#endif
-
-       /* Icon Theme */
-       ev_attachbar->priv->icon_theme = NULL;
-
-       /* Icon Cache */
-       ev_attachbar->priv->icon_cache = g_hash_table_new_full (g_str_hash,
-                                                               g_str_equal,
-                                                               g_free,
-                                                               g_object_unref);
-
 #if 0
        /* Drag and Drop */
        gtk_icon_view_enable_model_drag_source (
@@ -783,13 +543,7 @@ ev_sidebar_attachments_init (EvSidebarAttachments *ev_attachbar)
 GtkWidget *
 ev_sidebar_attachments_new (void)
 {
-       GtkWidget *ev_attachbar;
-
-       ev_attachbar = g_object_new (EV_TYPE_SIDEBAR_ATTACHMENTS,
-                                     "orientation", GTK_ORIENTATION_VERTICAL,
-                                     NULL);
-
-       return ev_attachbar;
+       return GTK_WIDGET (g_object_new (EV_TYPE_SIDEBAR_ATTACHMENTS, NULL));
 }
 
 static void
@@ -801,15 +555,14 @@ job_finished_callback (EvJobAttachments     *job,
        for (l = job->attachments; l && l->data; l = g_list_next (l)) {
                EvAttachment *attachment;
                GtkTreeIter   iter;
-               GdkPixbuf    *pixbuf = NULL;
+               GIcon        *gicon;
                const gchar  *mime_type;
                gchar        *description;
 
                attachment = EV_ATTACHMENT (l->data);
 
                mime_type = ev_attachment_get_mime_type (attachment);
-               pixbuf = ev_sidebar_attachments_icon_cache_get (ev_attachbar,
-                                                               mime_type);
+               gicon = g_content_type_get_symbolic_icon (mime_type);
                description =  g_markup_printf_escaped ("%s",
                                                         ev_attachment_get_description (attachment));
 
@@ -817,7 +570,7 @@ job_finished_callback (EvJobAttachments     *job,
                gtk_list_store_set (ev_attachbar->priv->model, &iter,
                                    COLUMN_NAME, ev_attachment_get_name (attachment),
                                    COLUMN_DESCRIPTION, description,
-                                   COLUMN_ICON, pixbuf,
+                                   COLUMN_ICON, gicon,
                                    COLUMN_ATTACHMENT, attachment,
                                    -1);
                g_free (description);
@@ -841,19 +594,6 @@ ev_sidebar_attachments_document_changed_cb (EvDocumentModel      *model,
        if (!ev_document_attachments_has_attachments (EV_DOCUMENT_ATTACHMENTS (document)))
                return;
 
-       if (!ev_attachbar->priv->icon_theme) {
-#if 0
-               GdkScreen *screen;
-
-               screen = gtk_widget_get_screen (GTK_WIDGET (ev_attachbar));
-               ev_attachbar->priv->icon_theme = gtk_icon_theme_get_for_screen (screen);
-               g_signal_connect_swapped (ev_attachbar->priv->icon_theme,
-                                         "changed",
-                                         G_CALLBACK (ev_sidebar_attachments_update_icons),
-                                         (gpointer) ev_attachbar);
-#endif
-       }
-
        gtk_list_store_clear (ev_attachbar->priv->model);
 
        job = ev_job_attachments_new (document);
diff --git a/shell/evince-sidebar-attachments.ui b/shell/evince-sidebar-attachments.ui
index 8b3afda2b..914dbe878 100644
--- a/shell/evince-sidebar-attachments.ui
+++ b/shell/evince-sidebar-attachments.ui
@@ -1,8 +1,8 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <interface>
   <requires lib="gtk" version="4.0"/>
-  <requires lib="libadwaita" version="1.0"/>
   <template class="EvSidebarAttachments" parent="GtkBox">
+    <property name="name">ev-sidebar-attachments</property>
     <property name="orientation">vertical</property>
     <child type="start">
       <object class="GtkScrolledWindow" id="swindow">
@@ -16,9 +16,33 @@
             <property name="model">model</property>
             <property name="selection-mode">multiple</property>
             <property name="columns">-1</property>
-            <property name="pixbuf-column">0</property>
-            <property name="text-column">1</property>
             <property name="tooltip-column">2</property>
+            <signal name="item-activated" handler="on_icon_view_item_activated" />
+
+            <child>
+              <object class="GtkCellRendererPixbuf">
+                <property name="xalign">0.5</property>
+                <property name="yalign">0.5</property>
+                <property name="icon-size">large</property>
+              </object>
+              <attributes>
+                <attribute name="gicon">0</attribute>
+              </attributes>
+            </child>
+            <child>
+              <object class="GtkCellRendererText">
+                <property name="xalign">0.5</property>
+              </object>
+              <attributes>
+                <attribute name="text">1</attribute>
+              </attributes>
+            </child>
+            <child>
+              <object class="GtkGestureClick">
+                <property name="button">3</property>
+                <signal name="pressed" handler="on_secondary_button_clicked" />
+              </object>
+            </child>
           </object>
         </child>
       </object>
@@ -27,7 +51,10 @@
     <child type="end">
       <object class="GtkBox" id="hbox">
         <property name="orientation">horizontal</property>
-        <!-- <property name="margin">6</property> -->
+        <property name="margin-top">6</property>
+        <property name="margin-bottom">6</property>
+        <property name="margin-start">6</property>
+        <property name="margin-end">6</property>
         <property name="halign">center</property>
         <child>
           <object class="GtkButton" id="add_button">
@@ -43,11 +70,11 @@
         </child>
       </object>
     </child>
-
   </template>
+
   <object class="GtkListStore" id="model">
     <columns>
-      <column type="GdkPixbuf" />
+      <column type="GIcon" />
       <column type="gchararray" />
       <column type="gchararray" />
       <column type="EvAttachment" />


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