From ab92cac18652f72be12f68b5a96095ba8eb5afdf Mon Sep 17 00:00:00 2001 From: Federico Angelilli Date: Fri, 17 Nov 2023 15:42:49 +0100 Subject: Reorganize project structure --- src/draw.c | 127 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 src/draw.c (limited to 'src/draw.c') diff --git a/src/draw.c b/src/draw.c new file mode 100644 index 0000000..aed4da0 --- /dev/null +++ b/src/draw.c @@ -0,0 +1,127 @@ +#include +#include +#include +#include +#include + +#include "draw.h" +#include "log.h" + +// 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 + +Drawable *draw_create(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); + 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->left_pad = left_pad; + draw->right_pad = right_pad; + draw->top_pad = top_pad; + draw->alpha = alpha; + g_assert(alpha >= 0 && alpha <= 1); + + log_debug("Draw context created [height=%d, left_pad=%d, right_pad=%d, top_pad=%d, alpha=%.2lf]", height, left_pad, right_pad, top_pad, alpha); + + return draw; +} + +void draw_paint(Drawable *draw, Window *win) +{ + // FIXME: Does not work for scale != 1 + //double scale = window_get_scale(win); + double scale = 1; + + int screen_width, screen_height; + window_get_screen_size(win, &screen_width, &screen_height); + + int width0 = screen_width - draw->right_pad - draw->left_pad; + int width = round(width0 * scale); + int height = round(draw->height * scale); + + cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height); + + cairo_t *cr = cairo_create(surface); + cairo_set_fill_rule(cr, CAIRO_FILL_RULE_EVEN_ODD); + cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); + + int radius = height / 2; + double degree = M_PI / 180.0; + + 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. + // + // This is caused by some difference between the two shapes (that should technically be the same) + // which causes a mismatch between the two layers and leaves some black pixels visible + + //cairo_arc(cr, radius, radius, radius, 90.0 * degree, 270 * degree); + //cairo_arc(cr, width - radius, radius, radius, 270 * degree, 450 * degree); + //cairo_fill(cr); + + cairo_paint(cr); + + cairo_set_line_width(cr, 1 * scale); + + for (int i = 0; i < 9; i++) { + + PangoLayout *layout = pango_cairo_create_layout (cr); + 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, 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, 1); + cairo_arc(cr, x + radius, radius, radius - 1, 0 * degree, 360 * degree); + cairo_stroke(cr); + + cairo_set_source_rgb(cr, 0.8, 0.8, 0.8); + char btn[] = { '1' + i, '\0' }; + pango_layout_set_text(layout, btn, -1); + + int text_w, text_h; + pango_layout_get_pixel_size(layout, &text_w, &text_h); + text_w = ceil(text_w / scale); + text_h = ceil(text_h / scale); + + int text_x = x + radius - (text_w / 2); + int text_y = radius - (text_h / 2); + cairo_move_to(cr, text_x, text_y); + + pango_cairo_update_layout(cr, layout); + pango_cairo_show_layout(cr, layout); + + g_object_unref(layout); + } + + cairo_destroy(cr); + + 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_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 -- cgit v1.2.3