aboutsummaryrefslogtreecommitdiff
path: root/draw.c
diff options
context:
space:
mode:
authorFederico Angelilli <code@fedang.net>2023-11-17 12:58:43 +0100
committerFederico Angelilli <code@fedang.net>2023-11-17 12:58:43 +0100
commita88d3a4c61f482ffeafba89910b896a92830f95f (patch)
tree06f1a5ad5db5563379ee0bb6156e2b9f91f7094d /draw.c
parentf28d0af32e28005a4b28857fa4fd4232d6849442 (diff)
Add `Drawable` struct with drawing related state
Diffstat (limited to 'draw.c')
-rw-r--r--draw.c57
1 files changed, 32 insertions, 25 deletions
diff --git a/draw.c b/draw.c
index 1dfa773..b0c32ba 100644
--- a/draw.c
+++ b/draw.c
@@ -1,3 +1,4 @@
+#include <glib.h>
#include <math.h>
#include <pango/pangocairo.h>
#include <pango/pango-font.h>
@@ -9,26 +10,29 @@
// Idea: Either make a to_draw queue where we put things we schedule to redraw
// (this will also work for animations in the future)
// or use some flags to trigger drawing
-//
-// Anyway, a drawing context struct is required
-// Make this a state passed to draw
-int draw_height, draw_x_padding, draw_y_padding;
-PangoFontDescription *desc;
-
-void draw_init(const char *font, int height, int x_padding, int y_padding)
+Drawable *draw_create(Window *win, const char *font, int height, int left_pad, int right_pad, int top_pad, double alpha)
{
+ Drawable *draw = g_malloc(sizeof(Drawable));
+ g_assert_nonnull(draw);
+
log_debug("Pango loading font description '%s'", font);
- desc = pango_font_description_from_string(font);
- log_debug("Pango found matching font '%s'", pango_font_description_get_family(desc));
+ draw->desc = pango_font_description_from_string(font);
+ log_debug("Pango found matching font '%s'", pango_font_description_get_family(draw->desc));
- draw_height = height;
- draw_x_padding = x_padding;
- draw_y_padding = y_padding;
+ draw->win = win;
+ draw->height = height;
+ draw->left_pad = left_pad;
+ draw->right_pad = right_pad;
+ draw->top_pad = top_pad;
+ draw->alpha = alpha;
+
+ return draw;
}
-void draw(Window *win)
+void draw_paint(Drawable *draw)
{
+ Window *win = draw->win;
// FIXME: Does not work for scale != 1
//double scale = window_get_scale(win);
double scale = 1;
@@ -36,9 +40,9 @@ void draw(Window *win)
int screen_width, screen_height;
window_get_screen_size(win, &screen_width, &screen_height);
- int width0 = screen_width - 2 * draw_x_padding;
+ int width0 = screen_width - draw->right_pad - draw->left_pad;
int width = round(width0 * scale);
- int height = round(draw_height * scale);
+ int height = round(draw->height * scale);
cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
@@ -49,10 +53,7 @@ void draw(Window *win)
int radius = height / 2;
double degree = M_PI / 180.0;
- // FIXME: Alpha transparency is not working
- double alpha = 0.8;
-
- cairo_set_source_rgba(cr, 0.3, 0.3, 0.3, alpha);
+ cairo_set_source_rgba(cr, 0.3, 0.3, 0.3, draw->alpha);
// TODO: Here we should paint the shape of the bar, however there is a problem with the surface
// painted in the pixmap to mask the window shape in window_paint_corners.
@@ -71,16 +72,16 @@ void draw(Window *win)
for (int i = 0; i < 9; i++) {
PangoLayout *layout = pango_cairo_create_layout (cr);
- pango_layout_set_font_description(layout, desc);
+ pango_layout_set_font_description(layout, draw->desc);
int x = (height + cairo_get_line_width(cr) * scale * 2) * i;
// purple
- cairo_set_source_rgba(cr, 0.502, 0.168, 0.886, alpha);
+ cairo_set_source_rgba(cr, 0.502, 0.168, 0.886, 1);
cairo_arc(cr, x + radius, radius, radius, 0 * degree, 360 * degree);
cairo_fill(cr);
- cairo_set_source_rgba(cr, 0.8, 0.8, 0.8, alpha);
+ cairo_set_source_rgba(cr, 0.8, 0.8, 0.8, 1);
cairo_arc(cr, x + radius, radius, radius - 1, 0 * degree, 360 * degree);
cairo_stroke(cr);
@@ -105,15 +106,21 @@ void draw(Window *win)
cairo_destroy(cr);
- int x = draw_x_padding;
- int y = draw_y_padding;
+ int x = draw->left_pad;
+ int y = draw->top_pad;
// TODO: Move these somewhere else
window_move(win, x, y);
- window_resize(win, width0, draw_height);
+ window_resize(win, width0, draw->height);
window_paint_surface(win, surface, width, height);
cairo_surface_destroy(surface);
}
+void draw_destroy(Drawable *draw)
+{
+ pango_font_description_free(draw->desc);
+ g_free(draw);
+}
+
// vim: ts=4 sw=4 et