[PATCH 7/9] clock: binary: refactor into algorithm funcs

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


No need for each different algorithm to do any drawing.

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

diff --git a/plugins/clock/clock-binary.c b/plugins/clock/clock-binary.c
index 6f785013..e790f4d5 100644
--- a/plugins/clock/clock-binary.c
+++ b/plugins/clock/clock-binary.c
@@ -257,32 +257,11 @@ xfce_clock_binary_finalize (GObject *object)
 
 
 static void
-xfce_clock_binary_draw_true_binary (XfceClockBinary *binary,
-                                    cairo_t         *cr,
-                                    GtkAllocation   *alloc)
+binary_coded_sexadecimal (gint *table, GDateTime *time, gint cols, gint rows)
 {
-  GDateTime        *time;
-  gint              row, rows;
-  static gint       binary_table[] = { 32, 16, 8, 4, 2, 1 };
-  gint              col, cols;
-  gint              w, h;
-  gint              ticks;
-  GtkStyleContext  *ctx;
-  GdkRGBA           active_rgba, inactive_rgba;
-
-  ctx = gtk_widget_get_style_context (GTK_WIDGET (binary));
-  gtk_style_context_get_color (ctx, gtk_widget_get_state_flags (GTK_WIDGET (binary)),
-                               &active_rgba);
-  inactive_rgba = active_rgba;
-  active_rgba.alpha = 1.0;
-  inactive_rgba.alpha = 0.2;
-
-  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;
+  static gint binary_table[] = { 32, 16, 8, 4, 2, 1 };
+  gint col, row;
+  gint ticks = 0;
 
   for (row = 0; row < rows; row++)
     {
@@ -298,58 +277,26 @@ xfce_clock_binary_draw_true_binary (XfceClockBinary *binary,
         {
           if (ticks >= binary_table[col])
             {
-              gdk_cairo_set_source_rgba (cr, &active_rgba);
+              table[col] |= 1 << row;
               ticks -= binary_table[col];
             }
-          else if (binary->show_inactive)
-            {
-              gdk_cairo_set_source_rgba (cr, &inactive_rgba);
-            }
           else
             {
               continue;
             }
-
-          /* draw the dot */
-          cairo_rectangle (cr, alloc->x + col * w, alloc->y + row * h, w - 1, h - 1);
-          cairo_fill (cr);
         }
     }
-
-  g_date_time_unref (time);
 }
 
 
 
 static void
-xfce_clock_binary_draw_binary (XfceClockBinary *binary,
-                                     cairo_t         *cr,
-                                     GtkAllocation   *alloc)
+binary_coded_decimal (gint *table, GDateTime *time, gint cols, gint rows)
 {
-  static gint       binary_table[] = { 80, 40, 20, 10, 8, 4, 2, 1 };
-  GDateTime        *time;
-  gint              row, rows;
-  gint              col, cols;
-  gint              digit;
-  gint              w, h;
-  gint              ticks = 0;
-  GtkStyleContext  *ctx;
-  GdkRGBA           active_rgba, inactive_rgba;
-
-  ctx = gtk_widget_get_style_context (GTK_WIDGET (binary));
-  gtk_style_context_get_color (ctx, gtk_widget_get_state_flags (GTK_WIDGET (binary)),
-                               &active_rgba);
-  inactive_rgba = active_rgba;
-  active_rgba.alpha = 1.0;
-  inactive_rgba.alpha = 0.2;
-
-  time = clock_time_get_time (binary->time);
-
-  /* make sure the cols are all equal */
-  cols = binary->show_seconds ? 6 : 4;
-  rows = G_N_ELEMENTS (binary_table) / 2;
-  w = alloc->width / cols;
-  h = alloc->height / rows;
+  static gint binary_table[] = { 80, 40, 20, 10, 8, 4, 2, 1 };
+  gint col, row;
+  gint ticks = 0;
+  gint digit;
 
   for (col = 0; col < cols; col++)
     {
@@ -366,25 +313,15 @@ xfce_clock_binary_draw_binary (XfceClockBinary *binary,
           digit = row + (4 * (col % 2));
           if (ticks >= binary_table[digit])
             {
-              gdk_cairo_set_source_rgba (cr, &active_rgba);
+              table[col] |= 1 << row;
               ticks -= binary_table[digit];
             }
-          else if (binary->show_inactive)
-            {
-              gdk_cairo_set_source_rgba (cr, &inactive_rgba);
-            }
           else
             {
               continue;
             }
-
-          /* draw the dot */
-          cairo_rectangle (cr, alloc->x + col * w, alloc->y + row * h, w - 1, h - 1);
-          cairo_fill (cr);
         }
     }
-
-  g_date_time_unref (time);
 }
 
 
@@ -402,8 +339,10 @@ xfce_clock_binary_draw (GtkWidget *widget,
   gint              pad_x, pad_y;
   gint              diff;
   GtkStyleContext  *ctx;
-  GdkRGBA           grid_rgba;
+  GdkRGBA           active_rgba, inactive_rgba, grid_rgba;
   GtkBorder         padding;
+  gint             *table;
+  GDateTime        *time;
 
   panel_return_val_if_fail (XFCE_CLOCK_IS_BINARY (binary), FALSE);
   //panel_return_val_if_fail (gtk_widget_get_has_window (widget), FALSE);
@@ -432,17 +371,18 @@ xfce_clock_binary_draw (GtkWidget *widget,
   alloc.height -= diff;
   alloc.y += diff / 2;
 
+  w = alloc.width / cols;
+  h = alloc.height / rows;
+
+  gtk_style_context_get_color (ctx, gtk_widget_get_state_flags (widget),
+                               &active_rgba);
   if (binary->show_grid)
     {
-      gtk_style_context_get_color (ctx, gtk_widget_get_state_flags (widget),
-                                   &grid_rgba);
+      grid_rgba = active_rgba;
       grid_rgba.alpha = 0.4;
       gdk_cairo_set_source_rgba (cr, &grid_rgba);
       cairo_set_line_width (cr, 1);
 
-      w = alloc.width / cols;
-      h = alloc.height / rows;
-
       x = alloc.x - 0.5;
       y = alloc.y - 0.5;
 
@@ -464,10 +404,36 @@ xfce_clock_binary_draw (GtkWidget *widget,
         }
     }
 
+  table = g_new0 (typeof(*table), cols);
+  time = clock_time_get_time (binary->time);
+
   if (binary->true_binary)
-    xfce_clock_binary_draw_true_binary (binary, cr, &alloc);
+    binary_coded_sexadecimal (table, time, cols, rows);
   else
-    xfce_clock_binary_draw_binary (binary, cr, &alloc);
+    binary_coded_decimal (table, time, cols, rows);
+
+  inactive_rgba = active_rgba;
+  active_rgba.alpha = 1.0;
+  inactive_rgba.alpha = 0.2;
+
+  for (col = 0; col < cols; col++)
+    {
+      for (row = 0; row < rows; row++)
+        {
+          if (table[col] & (1 << row))
+            gdk_cairo_set_source_rgba (cr, &active_rgba);
+          else if (binary->show_inactive)
+            gdk_cairo_set_source_rgba (cr, &inactive_rgba);
+          else
+            continue;
+
+          /* draw the dot */
+          cairo_rectangle (cr, alloc.x + col * w, alloc.y + row * h, w - 1, h - 1);
+          cairo_fill (cr);
+        }
+    }
+
+  g_date_time_unref (time);
 
   return FALSE;
 }
-- 
2.22.0.rc2.dirty



More information about the Xfce4-dev mailing list