[libwnck/wip/muktupavels/icons: 3/5] class-group: get icons in getters




commit 94906e601ac90664e6c5fe98c01b69625efeab55
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 | 152 +++++++++++++++++++++++++++++---------------------
 1 file changed, 89 insertions(+), 63 deletions(-)
---
diff --git a/libwnck/class-group.c b/libwnck/class-group.c
index 70a420a..4bb6ccf 100644
--- a/libwnck/class-group.c
+++ b/libwnck/class-group.c
@@ -379,107 +379,129 @@ 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;
-           }
-       }
-    }
-}
 
-/* 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)
-{
-  GList *l;
+      if (app == NULL)
+        continue;
 
-  *icon = NULL;
-  *mini_icon = NULL;
+      icon = wnck_application_get_icon (app);
 
-  for (l = class_group->priv->windows; l; l = l->next)
+      if (icon != NULL)
+        break;
+    }
+
+  if (icon == NULL)
     {
-      WnckWindow *window;
+      for (l = self->priv->windows; l != NULL; l = l->next)
+        {
+          WnckWindow *window;
 
-      window = WNCK_WINDOW (l->data);
+          window = WNCK_WINDOW (l->data);
 
-      *icon = wnck_window_get_icon (window);
-      *mini_icon = wnck_window_get_mini_icon (window);
+          icon = wnck_window_get_icon (window);
 
-      if (*icon && *mini_icon)
-       return;
-      else
-       {
-         *icon = NULL;
-         *mini_icon = NULL;
-       }
+          if (icon != NULL)
+            break;
+        }
+    }
+
+  if (icon == NULL)
+    {
+      _wnck_get_fallback_icon (&icon, _wnck_get_default_icon_size ());
     }
+  else
+    {
+      g_object_ref (icon);
+    }
+
+  g_assert (icon != NULL);
+  self->priv->icon = 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)
+ensure_mini_icon (WnckClassGroup *self)
 {
-  GdkPixbuf *icon, *mini_icon;
-  gboolean icons_reffed = FALSE;
+  GdkPixbuf *mini_icon;
+  GList *l;
 
-  get_icons_from_applications (class_group, &icon, &mini_icon);
+  if (self->priv->mini_icon != NULL)
+    return;
 
-  if (!icon || !mini_icon)
-    get_icons_from_windows (class_group, &icon, &mini_icon);
+  mini_icon = NULL;
 
-  if (!icon || !mini_icon)
+  for (l = self->priv->windows; l != NULL; l = l->next)
     {
-      _wnck_get_fallback_icon (&icon, _wnck_get_default_icon_size ());
-      _wnck_get_fallback_icon (&mini_icon, _wnck_get_default_mini_icon_size ());
-      icons_reffed = TRUE;
+      WnckWindow *window;
+      WnckApplication *app;
+
+      window = WNCK_WINDOW (l->data);
+      app = wnck_window_get_application (window);
+
+      if (app == NULL)
+        continue;
+
+      mini_icon = wnck_application_get_mini_icon (app);
+
+      if (mini_icon != NULL)
+        break;
     }
 
-  g_assert (icon && mini_icon);
+  if (mini_icon == NULL)
+    {
+      for (l = self->priv->windows; l != NULL; l = l->next)
+        {
+          WnckWindow *window;
 
-  if (class_group->priv->icon)
-    g_object_unref (class_group->priv->icon);
+          window = WNCK_WINDOW (l->data);
 
-  if (class_group->priv->mini_icon)
-    g_object_unref (class_group->priv->mini_icon);
+          mini_icon = wnck_window_get_mini_icon (window);
 
-  class_group->priv->icon = icon;
-  class_group->priv->mini_icon = mini_icon;
+          if (mini_icon != NULL)
+            break;
+        }
+    }
 
-  if (!icons_reffed)
+  if (mini_icon == NULL)
     {
-      g_object_ref (class_group->priv->icon);
-      g_object_ref (class_group->priv->mini_icon);
+      _wnck_get_fallback_icon (&mini_icon, _wnck_get_default_mini_icon_size ());
+    }
+  else
+    {
+      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
@@ -709,6 +731,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;
 }
 
@@ -731,5 +755,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]