[PATCH 4/9] clock: binary: trivial refactoring

Felipe Contreras felipe.contreras at gmail.com
Fri Jun 14 02:40:39 CEST 2019


There's no need to constantly recalculate the width and height of the
dots. Also, we can change the offset from the for look, no need for a
separate variable. Nor is there any need to calculate the remain.

Signed-off-by: Felipe Contreras <felipe.contreras at gmail.com>
---
 plugins/clock/clock-binary.c | 62 ++++++++++++------------------------
 1 file changed, 21 insertions(+), 41 deletions(-)

diff --git a/plugins/clock/clock-binary.c b/plugins/clock/clock-binary.c
index 1c599883..d41debd5 100644
--- a/plugins/clock/clock-binary.c
+++ b/plugins/clock/clock-binary.c
@@ -264,10 +264,9 @@ xfce_clock_binary_draw_true_binary (XfceClockBinary *binary,
   GDateTime        *time;
   gint              row, rows;
   static gint       binary_table[] = { 32, 16, 8, 4, 2, 1 };
-  gint              col, cols = G_N_ELEMENTS (binary_table);
-  gint              remain_h, remain_w;
+  gint              col, cols;
   gint              offset_x, offset_y;
-  gint              w, h, x;
+  gint              w, h;
   gint              ticks;
   GtkStyleContext  *ctx;
   GdkRGBA           active_rgba, inactive_rgba;
@@ -281,12 +280,15 @@ xfce_clock_binary_draw_true_binary (XfceClockBinary *binary,
 
   time = clock_time_get_time (binary->time);
 
+  cols = G_N_ELEMENTS (binary_table);
+  rows = binary->show_seconds ? 3 : 2;
+  w = alloc->width / cols;
+  h = alloc->height / rows;
+
   /* init sizes */
-  remain_h = alloc->height;
   offset_y = alloc->y;
 
-  rows = binary->show_seconds ? 3 : 2;
-  for (row = 0; row < rows; row++)
+  for (row = 0; row < rows; row++, offset_y += h)
     {
       /* get the time this row represents */
       if (row == 0)
@@ -297,19 +299,10 @@ xfce_clock_binary_draw_true_binary (XfceClockBinary *binary,
         ticks = g_date_time_get_second (time);
 
       /* reset sizes */
-      remain_w = alloc->width;
       offset_x = alloc->x;
-      h = remain_h / (rows - row);
-      remain_h -= h;
 
-      for (col = 0; col < cols; col++)
+      for (col = 0; col < cols; col++, offset_x += w)
         {
-          /* update sizes */
-          w = remain_w / (cols - col);
-          x = offset_x;
-          remain_w -= w;
-          offset_x += w;
-
           if (ticks >= binary_table[col])
             {
               gdk_cairo_set_source_rgba (cr, &active_rgba);
@@ -325,12 +318,9 @@ xfce_clock_binary_draw_true_binary (XfceClockBinary *binary,
             }
 
           /* draw the dot */
-          cairo_rectangle (cr, x, offset_y, w - 1, h - 1);
+          cairo_rectangle (cr, offset_x, offset_y, w - 1, h - 1);
           cairo_fill (cr);
         }
-
-      /* advance offset */
-      offset_y += h;
     }
 
   g_date_time_unref (time);
@@ -345,12 +335,11 @@ xfce_clock_binary_draw_binary (XfceClockBinary *binary,
 {
   static gint       binary_table[] = { 80, 40, 20, 10, 8, 4, 2, 1 };
   GDateTime        *time;
-  gint              row, rows = G_N_ELEMENTS (binary_table) / 2;
+  gint              row, rows;
   gint              col, cols;
   gint              digit;
-  gint              remain_h, remain_w;
   gint              offset_x, offset_y;
-  gint              w, h, y;
+  gint              w, h;
   gint              ticks = 0;
   GtkStyleContext  *ctx;
   GdkRGBA           active_rgba, inactive_rgba;
@@ -364,12 +353,15 @@ xfce_clock_binary_draw_binary (XfceClockBinary *binary,
 
   time = clock_time_get_time (binary->time);
 
-  remain_w = alloc->width;
-  offset_x = alloc->x;
-
   /* make sure the cols are all equal */
   cols = binary->show_seconds ? 6 : 4;
-  for (col = 0; col < cols; col++)
+  rows = G_N_ELEMENTS (binary_table) / 2;
+  w = alloc->width / cols;
+  h = alloc->height / rows;
+
+  offset_x = alloc->x;
+
+  for (col = 0; col < cols; col++, offset_x += w)
     {
       /* get the time this row represents */
       if (col == 0)
@@ -380,19 +372,10 @@ xfce_clock_binary_draw_binary (XfceClockBinary *binary,
         ticks = g_date_time_get_second (time);
 
       /* reset sizes */
-      remain_h = alloc->height;
       offset_y = alloc->y;
-      w = remain_w / (cols - col);
-      remain_w -= w;
 
-      for (row = 0; row < rows; row++)
+      for (row = 0; row < rows; row++, offset_y += h)
         {
-          /* update sizes */
-          h = remain_h / (rows - row);
-          remain_h -= h;
-          y = offset_y;
-          offset_y += h;
-
           digit = row + (4 * (col % 2));
           if (ticks >= binary_table[digit])
             {
@@ -409,12 +392,9 @@ xfce_clock_binary_draw_binary (XfceClockBinary *binary,
             }
 
           /* draw the dot */
-          cairo_rectangle (cr, offset_x, y, w - 1, h - 1);
+          cairo_rectangle (cr, offset_x, offset_y, w - 1, h - 1);
           cairo_fill (cr);
         }
-
-      /* advance offset */
-      offset_x += w;
     }
 
   g_date_time_unref (time);
-- 
2.22.0.rc2.dirty



More information about the Xfce4-dev mailing list