[libwnck/wip/muktupavels/icons: 22/24] class-group: get icons in getters




commit 777f8e9e4d43bf682ff98b8b38a9cf7df9223764
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 | 164 ++++++++++++++++++++++++++++++--------------------
 1 file changed, 99 insertions(+), 65 deletions(-)
---
diff --git a/libwnck/class-group.c b/libwnck/class-group.c
index cfc023ea..efdfcbd4 100644
--- a/libwnck/class-group.c
+++ b/libwnck/class-group.c
@@ -368,111 +368,141 @@ 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)
+    {
+      WnckHandle *handle;
+      gsize size;
+
+      handle = _wnck_screen_get_handle (self->priv->screen);
+      size = _wnck_handle_get_default_icon_size (handle);
+
+      _wnck_get_fallback_icon (&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)
     {
-      WnckHandle *handle;
+      WnckWindow *window;
+      WnckApplication *app;
+
+      window = WNCK_WINDOW (l->data);
+      app = wnck_window_get_application (window);
 
-      handle = _wnck_screen_get_handle (class_group->priv->screen);
+      if (app == NULL)
+        continue;
 
-      _wnck_get_fallback_icon (&icon, _wnck_handle_get_default_icon_size (handle));
-      _wnck_get_fallback_icon (&mini_icon, _wnck_handle_get_default_mini_icon_size (handle));
-      icons_reffed = TRUE;
+      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);
+      WnckHandle *handle;
+      gsize size;
+
+      handle = _wnck_screen_get_handle (self->priv->screen);
+      size = _wnck_handle_get_default_mini_icon_size (handle);
+
+      _wnck_get_fallback_icon (&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
@@ -702,6 +732,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;
 }
 
@@ -724,5 +756,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]