[gnome-flashback] system-indicators: use more fine-grained battery levels



commit 734365e2f7d59d2559ac3691fadcf4a27262aa4e
Author: Alberts Muktupāvels <alberts muktupavels gmail com>
Date:   Wed Jan 1 23:33:36 2020 +0200

    system-indicators: use more fine-grained battery levels

 system-indicators/si-indicator.c | 23 +++++++++++
 system-indicators/si-indicator.h |  3 ++
 system-indicators/si-power.c     | 87 +++++++++++++++++++++++++++++++---------
 3 files changed, 95 insertions(+), 18 deletions(-)
---
diff --git a/system-indicators/si-indicator.c b/system-indicators/si-indicator.c
index f5b1319..0d33924 100644
--- a/system-indicators/si-indicator.c
+++ b/system-indicators/si-indicator.c
@@ -325,3 +325,26 @@ si_indicator_set_icon_filename (SiIndicator *self,
 
   gtk_widget_show (priv->image);
 }
+
+void
+si_indicator_set_icon (SiIndicator *self,
+                       GIcon       *icon)
+{
+  SiIndicatorPrivate *priv;
+
+  priv = si_indicator_get_instance_private (self);
+
+  g_clear_pointer (&priv->filename, g_free);
+  gtk_image_clear (GTK_IMAGE (priv->image));
+
+  if (icon == NULL)
+    {
+      gtk_widget_hide (priv->image);
+      return;
+    }
+
+  gtk_image_set_from_gicon (GTK_IMAGE (priv->image), icon, GTK_ICON_SIZE_MENU);
+  update_icon (self);
+
+  gtk_widget_show (priv->image);
+}
diff --git a/system-indicators/si-indicator.h b/system-indicators/si-indicator.h
index 3c6de04..dc05a38 100644
--- a/system-indicators/si-indicator.h
+++ b/system-indicators/si-indicator.h
@@ -40,6 +40,9 @@ void       si_indicator_set_icon_name     (SiIndicator *self,
 void       si_indicator_set_icon_filename (SiIndicator *self,
                                            const char  *filename);
 
+void       si_indicator_set_icon          (SiIndicator *self,
+                                           GIcon       *icon);
+
 G_END_DECLS
 
 #endif
diff --git a/system-indicators/si-power.c b/system-indicators/si-power.c
index 3c96f8c..fee3376 100644
--- a/system-indicators/si-power.c
+++ b/system-indicators/si-power.c
@@ -172,41 +172,92 @@ update_indicator_label (SiPower *self)
   g_free (label);
 }
 
+static GIcon *
+get_themed_icon (SiPower  *self,
+                 gboolean  symbolic)
+{
+  const char *device_icon_name;
+  GIcon *icon;
+
+  device_icon_name = gf_upower_device_gen_get_icon_name (self->device);
+
+  if (device_icon_name != NULL && device_icon_name[0] != '\0')
+    {
+      if (!symbolic && g_str_has_suffix (device_icon_name, "-symbolic"))
+        {
+          char *icon_name;
+          char *tmp;
+
+          icon_name = g_strdup (device_icon_name);
+          tmp = g_strrstr (icon_name, "-symbolic");
+
+          g_strlcpy (tmp, "", sizeof (tmp));
+
+          icon = g_themed_icon_new (icon_name);
+          g_free (icon_name);
+        }
+      else
+        {
+          icon = g_themed_icon_new (device_icon_name);
+        }
+    }
+  else
+    {
+      if (symbolic)
+        icon = g_themed_icon_new ("battery-symbolic");
+      else
+        icon = g_themed_icon_new ("battery");
+    }
+
+  return icon;
+}
+
 static void
 update_indicator_icon (SiPower *self)
 {
   GpApplet *applet;
-  char *icon_name;
+  gboolean symbolic;
+  GIcon *icon;
 
   if (self->device == NULL)
     return;
 
   applet = si_indicator_get_applet (SI_INDICATOR (self));
-  icon_name = g_strdup (gf_upower_device_gen_get_icon_name (self->device));
+  symbolic = gp_applet_get_prefer_symbolic_icons (applet);
 
-  if (icon_name != NULL && icon_name[0] != '\0')
+  icon = get_themed_icon (self, symbolic);
+
+  if (symbolic)
     {
-      if (!gp_applet_get_prefer_symbolic_icons (applet) &&
-          g_str_has_suffix (icon_name, "-symbolic"))
+      UpDeviceState state;
+      double percentage;
+      int level;
+      char *icon_name;
+
+      state = gf_upower_device_gen_get_state (self->device);
+      percentage = gf_upower_device_gen_get_percentage (self->device);
+      level = floor (percentage / 10);
+
+      if (level == 100 || state == UP_DEVICE_STATE_FULLY_CHARGED)
+        {
+          icon_name = g_strdup ("battery-level-100-charged-symbolic");
+        }
+      else
         {
-          char *symbolic;
+          gboolean charging;
 
-          symbolic = g_strrstr (icon_name, "-symbolic");
-          g_strlcpy (symbolic, "", sizeof (symbolic));
+          charging = state == UP_DEVICE_STATE_CHARGING;
+          icon_name = g_strdup_printf ("battery-level-%d%s-symbolic",
+                                       level,
+                                       charging ? "-charging" : "");
         }
-    }
-  else
-    {
-      g_free (icon_name);
 
-      if (gp_applet_get_prefer_symbolic_icons (applet))
-        icon_name = g_strdup ("battery-symbolic");
-      else
-        icon_name = g_strdup ("battery");
+      g_themed_icon_prepend_name (G_THEMED_ICON (icon), icon_name);
+      g_free (icon_name);
     }
 
-  si_indicator_set_icon_name (SI_INDICATOR (self), icon_name);
-  g_free (icon_name);
+  si_indicator_set_icon (SI_INDICATOR (self), icon);
+  g_object_unref (icon);
 }
 
 static void


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