]> git.sur5r.net Git - i3/i3/commitdiff
Extract cairo_clear_surface.
authorIngo Bürk <ingo.buerk@tngtech.com>
Wed, 14 Oct 2015 17:03:05 +0000 (19:03 +0200)
committerIngo Bürk <ingo.buerk@tngtech.com>
Wed, 14 Oct 2015 17:03:05 +0000 (19:03 +0200)
This commit refactors the i3bar drawing code to also define an abstraction
function for clearing a surface. This is needed to fully abstract i3bar/xcb.c's
drawing code so that we can introduce a switch to easily exchange the
underlying drawing mechanism.

i3bar/include/cairo_util.h
i3bar/src/cairo_util.c
i3bar/src/xcb.c

index 3e4ebec4f143dec0dbf1a278e0f06c9a6c448a89..fe2d06ffeba5b3a84bf5e4070968bf5408c03e40 100644 (file)
@@ -89,6 +89,13 @@ void cairo_draw_text(i3String *text, surface_t *surface, color_t fg_color, color
  */
 void cairo_draw_rectangle(surface_t *surface, color_t color, double x, double y, double w, double h);
 
+/**
+ * Clears a surface with the given color.
+ * Note that the drawing is done using CAIRO_OPERATOR_SOURCE.
+ *
+ */
+void cairo_clear_surface(surface_t *surface, color_t color);
+
 /**
  * Copies a surface onto another surface.
  * Note that the drawing is done using CAIRO_OPERATOR_SOURCE.
index 98c129b9aef17d901c244f566af9754ad924fc19..2cec76d6c5c23b649a82588eff96610f4d53acd8 100644 (file)
@@ -114,6 +114,29 @@ void cairo_draw_rectangle(surface_t *surface, color_t color, double x, double y,
     cairo_restore(surface->cr);
 }
 
+/**
+ * Clears a surface with the given color.
+ * Note that the drawing is done using CAIRO_OPERATOR_SOURCE.
+ *
+ */
+void cairo_clear_surface(surface_t *surface, color_t color) {
+    cairo_save(surface->cr);
+
+    /* Using the SOURCE operator will copy both color and alpha information directly
+     * onto the surface rather than blending it. This is a bit more efficient and
+     * allows better color control for the user when using opacity. */
+    cairo_set_operator(surface->cr, CAIRO_OPERATOR_SOURCE);
+    cairo_set_source_color(surface, color);
+
+    cairo_paint(surface->cr);
+
+    /* Make sure we flush the surface for any text drawing operations that could follow.
+     * Since we support drawing text via XCB, we need this. */
+    CAIRO_SURFACE_FLUSH(surface->surface);
+
+    cairo_restore(surface->cr);
+}
+
 /**
  * Copies a surface onto another surface.
  * Note that the drawing is done using CAIRO_OPERATOR_SOURCE.
index aa7b816caaa79041ed94de5e5e90d367bdc87c52..3b3836c1d09b134165aeef1721cb30a93071ae9b 100644 (file)
@@ -244,12 +244,7 @@ void refresh_statusline(bool use_short_text) {
         realloc_sl_buffer();
 
     /* Clear the statusline pixmap. */
-    cairo_save(statusline_surface.cr);
-    cairo_set_source_color(&statusline_surface, colors.bar_bg);
-    cairo_set_operator(statusline_surface.cr, CAIRO_OPERATOR_SOURCE);
-    cairo_paint(statusline_surface.cr);
-    CAIRO_SURFACE_FLUSH(statusline_surface.surface);
-    cairo_restore(statusline_surface.cr);
+    cairo_clear_surface(&statusline_surface, colors.bar_bg);
 
     /* Draw the text of each block. */
     uint32_t x = 0;
@@ -1797,12 +1792,7 @@ void draw_bars(bool unhide) {
         }
 
         /* First things first: clear the backbuffer */
-        cairo_save(outputs_walk->buffer.cr);
-        cairo_set_source_color(&(outputs_walk->buffer), colors.bar_bg);
-        cairo_set_operator(outputs_walk->buffer.cr, CAIRO_OPERATOR_SOURCE);
-        cairo_paint(outputs_walk->buffer.cr);
-        CAIRO_SURFACE_FLUSH(outputs_walk->buffer.surface);
-        cairo_restore(outputs_walk->buffer.cr);
+        cairo_clear_surface(&(outputs_walk->buffer), colors.bar_bg);
 
         if (!config.disable_ws) {
             i3_ws *ws_walk;