[libwnck/wip/muktupavels/icons: 1/2] class-group: get icons in getters




commit 023e3c59ff89983b9b0e9972bb48fef7e15f0473
Author: Alberts Muktupāvels <alberts muktupavels gmail com>
Date:   Thu May 13 10:21:48 2021 +0300

    class-group: get icons in getters
    
    libwnck users might not use icons at all or might use only normal
    or mini icon. Redo code to get icons only when getters are used.
    
    In future commits similar change will be done for WnckApplication
    and WnckWindow to reduce memory usage when one or both icons are
    not used.

 libwnck/class-group.c | 158 ++++++++++++++++++++++++++++++--------------------
 1 file changed, 94 insertions(+), 64 deletions(-)
---
diff --git a/libwnck/class-group.c b/libwnck/class-group.c
index f4afc31..7a463ce 100644
--- a/libwnck/class-group.c
+++ b/libwnck/class-group.c
@@ -379,109 +379,135 @@ set_name (WnckClassGroup *class_group)
     }
 }
 
-/* Walks the list of applications, trying to get an icon from them */
 static void
-get_icons_from_applications (WnckClassGroup *class_group, GdkPixbuf **icon, GdkPixbuf **mini_icon)
+ensure_icon (WnckClassGroup *self)
 {
+  GdkPixbuf *icon;
   GList *l;
 
-  *icon = NULL;
-  *mini_icon = NULL;
+  if (self->priv->icon != NULL)
+    return;
 
-  for (l = class_group->priv->windows; l; l = l->next)
+  icon = NULL;
+
+  for (l = self->priv->windows; l != NULL; l = l->next)
     {
       WnckWindow *window;
       WnckApplication *app;
 
       window = WNCK_WINDOW (l->data);
       app = wnck_window_get_application (window);
-      if (app)
-       {
-         *icon = wnck_application_get_icon (app);
-         *mini_icon = wnck_application_get_mini_icon (app);
-
-         if (*icon && *mini_icon)
-           return;
-         else
-           {
-             *icon = NULL;
-             *mini_icon = NULL;
-           }
-       }
+
+      if (app == NULL)
+        continue;
+
+      icon = wnck_application_get_icon (app);
+
+      if (icon != NULL)
+        break;
+    }
+
+  if (icon == NULL)
+    {
+      for (l = self->priv->windows; l != NULL; l = l->next)
+        {
+          WnckWindow *window;
+
+          window = WNCK_WINDOW (l->data);
+
+          icon = wnck_window_get_icon (window);
+
+          if (icon != NULL)
+            break;
+        }
+    }
+
+  if (icon == NULL)
+    {
+      _wnck_get_fallback_icons (&icon,
+                                _wnck_get_default_icon_size (),
+                                NULL,
+                                0);
+    }
+  else
+    {
+      g_object_ref (icon);
     }
+
+  g_assert (icon != NULL);
+  self->priv->icon = icon;
 }
 
-/* Walks the list of windows, trying to get an icon from them */
 static void
-get_icons_from_windows (WnckClassGroup *class_group, GdkPixbuf **icon, GdkPixbuf **mini_icon)
+ensure_mini_icon (WnckClassGroup *self)
 {
+  GdkPixbuf *mini_icon;
   GList *l;
 
-  *icon = NULL;
-  *mini_icon = NULL;
+  if (self->priv->mini_icon != NULL)
+    return;
 
-  for (l = class_group->priv->windows; l; l = l->next)
+  mini_icon = NULL;
+
+  for (l = self->priv->windows; l != NULL; l = l->next)
     {
       WnckWindow *window;
+      WnckApplication *app;
 
       window = WNCK_WINDOW (l->data);
+      app = wnck_window_get_application (window);
 
-      *icon = wnck_window_get_icon (window);
-      *mini_icon = wnck_window_get_mini_icon (window);
+      if (app == NULL)
+        continue;
 
-      if (*icon && *mini_icon)
-       return;
-      else
-       {
-         *icon = NULL;
-         *mini_icon = NULL;
-       }
+      mini_icon = wnck_application_get_mini_icon (app);
+
+      if (mini_icon != NULL)
+        break;
     }
-}
 
-/* Gets a sensible icon and mini_icon for the class group from the application
- * group leaders or from individual windows.
- */
-static void
-set_icon (WnckClassGroup *class_group)
-{
-  GdkPixbuf *icon, *mini_icon;
-  gboolean icons_reffed = FALSE;
+  if (mini_icon == NULL)
+    {
+      for (l = self->priv->windows; l != NULL; l = l->next)
+        {
+          WnckWindow *window;
 
-  get_icons_from_applications (class_group, &icon, &mini_icon);
+          window = WNCK_WINDOW (l->data);
 
-  if (!icon || !mini_icon)
-    get_icons_from_windows (class_group, &icon, &mini_icon);
+          mini_icon = wnck_window_get_mini_icon (window);
 
-  if (!icon || !mini_icon)
+          if (mini_icon != NULL)
+            break;
+        }
+    }
+
+  if (mini_icon == NULL)
     {
-      _wnck_get_fallback_icons (&icon,
-                                _wnck_get_default_icon_size (),
+      _wnck_get_fallback_icons (NULL,
+                                0,
                                 &mini_icon,
                                 _wnck_get_default_mini_icon_size ());
-      icons_reffed = TRUE;
     }
-
-  g_assert (icon && mini_icon);
-
-  if (class_group->priv->icon)
-    g_object_unref (class_group->priv->icon);
-
-  if (class_group->priv->mini_icon)
-    g_object_unref (class_group->priv->mini_icon);
-
-  class_group->priv->icon = icon;
-  class_group->priv->mini_icon = mini_icon;
-
-  if (!icons_reffed)
+  else
     {
-      g_object_ref (class_group->priv->icon);
-      g_object_ref (class_group->priv->mini_icon);
+      g_object_ref (mini_icon);
     }
 
-  g_signal_emit (G_OBJECT (class_group), signals[ICON_CHANGED], 0);
+  g_assert (mini_icon != NULL);
+  self->priv->mini_icon = mini_icon;
 }
 
+/* Gets a sensible icon and mini_icon for the class group from the application
+ * group leaders or from individual windows.
+ */
+static void
+set_icon (WnckClassGroup *class_group)
+{
+  g_clear_object (&class_group->priv->icon);
+  g_clear_object (&class_group->priv->mini_icon);
+
+  g_signal_emit (G_OBJECT (class_group), signals[ICON_CHANGED], 0);
+}
 
 /* Handle window's icon_changed signal, update class group icon */
 static void
@@ -711,6 +737,8 @@ wnck_class_group_get_icon (WnckClassGroup *class_group)
 {
   g_return_val_if_fail (class_group != NULL, NULL);
 
+  ensure_icon (class_group);
+
   return class_group->priv->icon;
 }
 
@@ -733,5 +761,7 @@ wnck_class_group_get_mini_icon (WnckClassGroup *class_group)
 {
   g_return_val_if_fail (class_group != NULL, NULL);
 
+  ensure_mini_icon (class_group);
+
   return class_group->priv->mini_icon;
 }


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