]> git.mdlowis.com Git - proto/labwc.git/commitdiff
common/graphic-helpers: Add cairo helpers
authorConsolatis <35009135+Consolatis@users.noreply.github.com>
Sat, 20 Aug 2022 18:11:58 +0000 (20:11 +0200)
committerJohan Malm <johanmalm@users.noreply.github.com>
Sat, 20 Aug 2022 21:16:52 +0000 (22:16 +0100)
include/common/graphic-helpers.h
src/common/graphic-helpers.c
src/osd.c
src/workspaces.c

index ab66de0540bce93690b2204d25e9b09b03db8ad8..786cb2fd6912e9f316e7dc10dbd1b9fbae09e031 100644 (file)
@@ -32,3 +32,13 @@ struct multi_rect *multi_rect_create(struct wlr_scene_tree *parent,
                float *colors[3], int line_width);
 
 void multi_rect_set_size(struct multi_rect *rect, int width, int height);
+
+/**
+ * Sets the cairo color.
+ * Splits a float[4] single color array into its own arguments
+ */
+void set_cairo_color(cairo_t *cairo, float *color);
+
+/* Draws a border with a specified line width */
+void draw_cairo_border(cairo_t *cairo, double width, double height,
+               double line_width);
index 0dfcd021d7043dbdc3b741e8fae437ede4c6c941..eb14789354294824ca98614eca6188197acdf5d5 100644 (file)
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0-only
 
 #include <assert.h>
+#include <cairo.h>
 #include <stdlib.h>
 #include <wlr/types/wlr_scene.h>
 #include "common/graphic-helpers.h"
@@ -58,3 +59,28 @@ multi_rect_set_size(struct multi_rect *rect, int width, int height)
                        line_width, height - i * line_width * 2);
        }
 }
+
+/* Draws a border with a specified line width */
+void
+draw_cairo_border(cairo_t *cairo, double width, double height, double line_width)
+{
+       cairo_save(cairo);
+
+       double x, y, w, h;
+       /* The anchor point of a line is in the center */
+       x = y = line_width / 2;
+       w = width - line_width;
+       h = height - line_width;
+       cairo_set_line_width(cairo, line_width);
+       cairo_rectangle(cairo, x, y, w, h);
+       cairo_stroke(cairo);
+
+       cairo_restore(cairo);
+}
+
+/* Sets the cairo color. Splits the single color channels */
+void
+set_cairo_color(cairo_t *cairo, float *c)
+{
+       cairo_set_source_rgba(cairo, c[0], c[1], c[2], c[3]);
+}
index 496e7e1ca162a84be07da194a6c8a65c02da1160..0764f4536e5c9c9e0896f7dbf2f8c4f593e0254d 100644 (file)
--- a/src/osd.c
+++ b/src/osd.c
@@ -7,6 +7,7 @@
 #include "buffer.h"
 #include "common/buf.h"
 #include "common/font.h"
+#include "common/graphic-helpers.h"
 #include "config/rcxml.h"
 #include "labwc.h"
 #include "theme.h"
 #define OSD_TAB1 (120)
 #define OSD_TAB2 (300)
 
-static void
-set_source(cairo_t *cairo, float *c)
-{
-       cairo_set_source_rgba(cairo, c[0], c[1], c[2], c[3]);
-}
-
-/* Draws a border with a specified line width */
-static void
-draw_border(cairo_t *cairo, double width, double height, double line_width)
-{
-       cairo_save(cairo);
-
-       double x, y, w, h;
-       /* The anchor point of a line is in the center */
-       x = y = line_width / 2;
-       w = width - line_width;
-       h = height - line_width;
-       cairo_set_line_width(cairo, line_width);
-       cairo_rectangle(cairo, x, y, w, h);
-       cairo_stroke(cairo);
-
-       cairo_restore(cairo);
-}
-
 /* is title different from app_id/class? */
 static int
 is_title_different(struct view *view)
@@ -155,13 +132,13 @@ osd_update(struct server *server)
                cairo_surface_t *surf = cairo_get_target(cairo);
 
                /* background */
-               set_source(cairo, theme->osd_bg_color);
+               set_cairo_color(cairo, theme->osd_bg_color);
                cairo_rectangle(cairo, 0, 0, w, h);
                cairo_fill(cairo);
 
                /* Border */
-               set_source(cairo, theme->osd_border_color);
-               draw_border(cairo, w, h, theme->osd_border_width);
+               set_cairo_color(cairo, theme->osd_border_color);
+               draw_cairo_border(cairo, w, h, theme->osd_border_width);
 
                int y = OSD_BORDER_WIDTH;
 
@@ -177,7 +154,7 @@ osd_update(struct server *server)
                                continue;
                        }
                        if (view == server->cycle_view) {
-                               set_source(cairo, theme->osd_label_text_color);
+                               set_cairo_color(cairo, theme->osd_label_text_color);
                                cairo_rectangle(cairo, OSD_BORDER_WIDTH, y,
                                        OSD_ITEM_WIDTH, OSD_ITEM_HEIGHT);
                                cairo_stroke(cairo);
@@ -187,7 +164,7 @@ osd_update(struct server *server)
                }
 
                /* text */
-               set_source(cairo, theme->osd_label_text_color);
+               set_cairo_color(cairo, theme->osd_label_text_color);
                PangoLayout *layout = pango_cairo_create_layout(cairo);
                pango_layout_set_width(layout,
                        (OSD_ITEM_WIDTH - 2 * OSD_ITEM_PADDING) * PANGO_SCALE);
index 1c029b8a95d9c0e54be8503018f78a366d0caf73..9e61f4b903083f16885010f1790cb1986ea235a1 100644 (file)
@@ -9,6 +9,7 @@
 #include <strings.h>
 #include "labwc.h"
 #include "common/font.h"
+#include "common/graphic-helpers.h"
 #include "common/zfree.h"
 #include "workspaces.h"
 
@@ -45,33 +46,6 @@ parse_workspace_index(const char *name)
        return index;
 }
 
-/*
- * TODO: set_source and draw_border are straight up copies from src/osd.c
- * find some proper place for them instead of duplicating stuff.
- */
-static void
-set_source(cairo_t *cairo, float *c)
-{
-       cairo_set_source_rgba(cairo, c[0], c[1], c[2], c[3]);
-}
-
-static void
-draw_border(cairo_t *cairo, double width, double height, double line_width)
-{
-       cairo_save(cairo);
-
-       double x, y, w, h;
-       /* The anchor point of a line is in the center */
-       x = y = line_width / 2;
-       w = width - line_width;
-       h = height - line_width;
-       cairo_set_line_width(cairo, line_width);
-       cairo_rectangle(cairo, x, y, w, h);
-       cairo_stroke(cairo);
-
-       cairo_restore(cairo);
-}
-
 static void
 _osd_update(struct server *server)
 {
@@ -110,18 +84,18 @@ _osd_update(struct server *server)
                cairo = buffer->cairo;
 
                /* Background */
-               set_source(cairo, theme->osd_bg_color);
+               set_cairo_color(cairo, theme->osd_bg_color);
                cairo_rectangle(cairo, 0, 0, width, height);
                cairo_fill(cairo);
 
                /* Border */
-               set_source(cairo, theme->osd_border_color);
-               draw_border(cairo, width, height, theme->osd_border_width);
+               set_cairo_color(cairo, theme->osd_border_color);
+               draw_cairo_border(cairo, width, height, theme->osd_border_width);
 
                uint16_t x = (width - marker_width) / 2;
                wl_list_for_each(workspace, &server->workspaces, link) {
                        bool active =  workspace == server->workspace_current;
-                       set_source(cairo, server->theme->osd_label_text_color);
+                       set_cairo_color(cairo, server->theme->osd_label_text_color);
                        cairo_rectangle(cairo, x, margin,
                                rect_width - padding, rect_height);
                        cairo_stroke(cairo);
@@ -134,7 +108,7 @@ _osd_update(struct server *server)
                }
 
                /* Text */
-               set_source(cairo, server->theme->osd_label_text_color);
+               set_cairo_color(cairo, server->theme->osd_label_text_color);
                PangoLayout *layout = pango_cairo_create_layout(cairo);
                pango_layout_set_width(layout, (width - 2 * margin) * PANGO_SCALE);
                pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END);