[gtk: 22/31] icon-theme: Add GTK_ICON_LOOKUP_LOAD_IN_THREAD flag



commit e27413a9ed5ce0d18895ec02b7f45d84b39440ee
Author: Alexander Larsson <alexl redhat com>
Date:   Fri Feb 7 17:25:18 2020 +0100

    icon-theme: Add GTK_ICON_LOOKUP_LOAD_IN_THREAD flag
    
    This causes lookup to trigger a thread that loads the icon texture.

 gtk/gtkicontheme.c | 34 ++++++++++++++++++++++++++++++++++
 gtk/gtkicontheme.h |  5 ++++-
 2 files changed, 38 insertions(+), 1 deletion(-)
---
diff --git a/gtk/gtkicontheme.c b/gtk/gtkicontheme.c
index 1377abc192..45b580e6a2 100644
--- a/gtk/gtkicontheme.c
+++ b/gtk/gtkicontheme.c
@@ -2185,6 +2185,20 @@ choose_icon (GtkIconTheme      *self,
   return icon;
 }
 
+static void
+load_icon_thread (GTask        *task,
+                  gpointer      source_object,
+                  gpointer      task_data,
+                  GCancellable *cancellable)
+{
+  GtkIconPaintable *self = GTK_ICON_PAINTABLE (source_object);
+
+  g_mutex_lock (&self->texture_lock);
+  icon_ensure_texture__locked (self, TRUE);
+  g_mutex_unlock (&self->texture_lock);
+  g_task_return_pointer (task, NULL, NULL);
+}
+
 /**
  * gtk_icon_theme_lookup_icon:
  * @self: a #GtkIconTheme
@@ -2258,6 +2272,26 @@ gtk_icon_theme_lookup_icon (GtkIconTheme       *self,
 
   gtk_icon_theme_unlock (self);
 
+  if (flags & GTK_ICON_LOOKUP_LOAD_IN_THREAD)
+    {
+      gboolean has_texture = FALSE;
+
+      /* If we fail to get the lock it is because some other thread is
+         currently loading the icon, so we need to do nothing */
+      if (g_mutex_trylock (&icon->texture_lock))
+        {
+          has_texture = icon->texture != NULL;
+          g_mutex_unlock (&icon->texture_lock);
+
+          if (!has_texture)
+            {
+              GTask *task = g_task_new (icon, NULL, NULL, NULL);
+              g_task_run_in_thread (task, load_icon_thread);
+              g_object_unref (task);
+            }
+        }
+    }
+
   return icon;
 }
 
diff --git a/gtk/gtkicontheme.h b/gtk/gtkicontheme.h
index afc79e5a18..f0fc704ed4 100644
--- a/gtk/gtkicontheme.h
+++ b/gtk/gtkicontheme.h
@@ -44,13 +44,16 @@ typedef struct _GtkIconTheme      GtkIconTheme;
  *   when symbolic icon names are given
  * @GTK_ICON_LOOKUP_FORCE_SYMBOLIC: Try to always load symbolic icons, even
  *   when regular icon names are given
+ * @GTK_ICON_LOOKUP_LOAD_IN_THREAD: Starts loading the texture in the background
+ *   so it is ready when later needed.
  *
  * Used to specify options for gtk_icon_theme_lookup_icon()
  */
 typedef enum
 {
   GTK_ICON_LOOKUP_FORCE_REGULAR    = 1 << 0,
-  GTK_ICON_LOOKUP_FORCE_SYMBOLIC   = 1 << 1
+  GTK_ICON_LOOKUP_FORCE_SYMBOLIC   = 1 << 1,
+  GTK_ICON_LOOKUP_LOAD_IN_THREAD   = 1 << 2,
 } GtkIconLookupFlags;
 
 /**


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