From 7f6ecb8d49854c8cfe83457c396de622918bbddb Mon Sep 17 00:00:00 2001 From: David Phillips Date: Wed, 6 Jun 2018 22:30:41 +1200 Subject: Only update battery icon on change --- bat_tray.c | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/bat_tray.c b/bat_tray.c index c6e9f45..48a8488 100644 --- a/bat_tray.c +++ b/bat_tray.c @@ -9,6 +9,7 @@ static GtkStatusIcon* tray; static char tooltip_text[1024]; +static int old_rounded; static int bat_num; // Shortcoming: we only detect one battery static char CHARGE_VALUE_PATH[1024]; static char CHARGE_STATE_PATH[1024]; @@ -23,6 +24,27 @@ int get_bat_percent() /*********************************************************************** + * Get battery level percentage, rounded to one of: 0, 20, 40, 60, 80 + * or 100. These values match the icon files + **********************************************************************/ +int get_bat_percent_rounded() +{ + int rounded = 0; + + rounded = 20 * (int)((get_bat_percent() + 10) / 20); + + /* cap rounded value to safe bounds */ + if (rounded < 0) + rounded = 0; + + if (rounded > 100) + rounded = 100; + + return rounded; +} + + +/*********************************************************************** * Return number of seconds until battery is fully charged/discharged **********************************************************************/ long get_bat_seconds_left() @@ -140,15 +162,13 @@ static gboolean update() char icon_file[1024]; int rounded = 0; - /* Round percentage to 0, 20, 40, 60, 80 or 100 */ - rounded = 20 * (int)((get_bat_percent() + 10) / 20); - - /* cap rounded value to safe bounds */ - if (rounded < 0) - rounded = 0; + update_tooltip_cache(); - if (rounded > 100) - rounded = 100; + /* if rounded hasn't changed, don't spend time changing icon */ + rounded = get_bat_percent_rounded(); + if (rounded == old_rounded) { + return true; + } switch (get_battery_state()) { @@ -165,7 +185,6 @@ static gboolean update() break; } - update_tooltip_cache(); gtk_status_icon_set_from_file(tray, icon_file); return true; } @@ -192,7 +211,10 @@ void bat_tray_init() g_signal_connect(G_OBJECT(tray), "query-tooltip", GTK_SIGNAL_FUNC(show_tooltip), NULL); g_timeout_add(120000, update, NULL); - /* Force useful cached tooltip text, force meaningful icon */ + /* trigger icon refresh in update() below */ + old_rounded = get_bat_percent_rounded() - 1; + + /* pre-load cached tooltip text */ update(); } -- cgit v1.1