Adjust color bar to be visible for capacity >= 1%
authorHugo Villeneuve <hugo@hugovil.com>
Thu, 18 Jul 2013 00:14:15 +0000 (20:14 -0400)
committerHugo Villeneuve <hugo@hugovil.com>
Sun, 2 Jan 2022 22:43:16 +0000 (17:43 -0500)
src/batmon.c

index 05095c8..ed056ea 100644 (file)
@@ -51,6 +51,7 @@
 #include "dockapp.h"
 #include "dockapp-mask.xpm"
 
+#define BAT_BORDER 2
 #define BAT_W 35
 #define BAT_H 49
 #define BAT_TIP_W (BAT_W / 2)
@@ -234,14 +235,15 @@ display_capacity(cairo_t *cr)
 static void
 draw_background(GtkWidget *clock, cairo_t *cr)
 {
-       int y;
+       int y_start;
+       int height;
 
        read_ac_status();
        read_battery_status();
 
        cairo_save(cr);
 
-       cairo_set_line_width(cr, 2);
+       cairo_set_line_width(cr, BAT_BORDER);
 
        /* Draw battery tip. */
        color_set_gray(cr);
@@ -270,12 +272,20 @@ draw_background(GtkWidget *clock, cairo_t *cr)
                color_set_green(cr);
        }
 
-       y = BAT_START_Y + BAT_TIP_H + BAT_H - ((BAT_H * capacity) / 100);
+       y_start = BAT_START_Y + BAT_TIP_H + BAT_H;
 
-       /* Draw remaining capacity. */
-       cairo_rectangle(cr, BAT_START_X, y, BAT_W,
-                       BAT_START_Y + BAT_TIP_H + BAT_H - y);
+       /* Compute colored height (no borders). */
+       height = ((BAT_H - BAT_BORDER) * capacity) / 100;
+
+       /* Make sure capacity > 0 gets a colored line. */
+       if ((capacity != 0) && (height == 0))
+               height = 1;
 
+       /* Add border width: */
+       height += BAT_BORDER;
+
+       /* Draw remaining capacity. */
+       cairo_rectangle(cr, BAT_START_X, y_start, BAT_W, -height);
        cairo_fill_preserve(cr);
        color_set_black(cr);
        cairo_stroke(cr);