aboutsummaryrefslogtreecommitdiff
path: root/src/draw.c
diff options
context:
space:
mode:
authorFederico Angelilli <code@fedang.net>2024-03-16 19:34:45 +0100
committerFederico Angelilli <code@fedang.net>2024-03-16 19:34:45 +0100
commitfe293d9503a57df0236be85cc82093074e7bbccb (patch)
tree23a54b8237e8bb2f90e8d9390b5c5fd064d3b13e /src/draw.c
parent6913ec943a1c71804ec81a9adbb19cfd899a202d (diff)
Rework draw functions
Diffstat (limited to 'src/draw.c')
-rw-r--r--src/draw.c80
1 files changed, 49 insertions, 31 deletions
diff --git a/src/draw.c b/src/draw.c
index 292f2d2..968f111 100644
--- a/src/draw.c
+++ b/src/draw.c
@@ -21,33 +21,13 @@
} \
} while (false)
-Drawer *draw_create(const char *font, int height, int left_pad, int right_pad, int top_pad)
+Drawer *draw_create()
{
Drawer *draw = g_malloc0(sizeof(Drawer));
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;
-
- log_debug("Draw context created [height=%d, left_pad=%d, right_pad=%d, top_pad=%d]",
- height, left_pad, right_pad, top_pad);
-
return draw;
}
-// TODO: Remove this
-static void compute_width(Drawer *draw, Window *win)
-{
- int screen_width = win->con->screen_size->width;
- draw->width = round(screen_width - draw->right_pad - draw->left_pad);
-}
-
static void paint_button(cairo_t *cr, const Layout *layout)
{
double degree = M_PI / 180.0;
@@ -136,8 +116,6 @@ static void paint_button_list(cairo_t *cr, GList *layouts)
void draw_paint(Drawer *draw, Window *win)
{
- compute_width(draw, win);
-
// Back buffering
cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, draw->width, draw->height);
@@ -162,11 +140,18 @@ void draw_paint(Drawer *draw, Window *win)
static void layout_destroy(Layout *layout)
{
- if (layout->pl != NULL) g_object_unref(layout->pl);
+ if (layout->pl != NULL)
+ g_object_unref(layout->pl);
+
g_list_free_full(layout->children, (GDestroyNotify)layout_destroy);
g_free(layout);
}
+static void layout_invalidate(GList *layouts)
+{
+ g_list_free_full(layouts, (GDestroyNotify)layout_destroy);
+}
+
static void layout_adjust(GList *start, GList *end, int off)
{
// NOTE: end must be present in the list or segfault
@@ -195,6 +180,13 @@ static void layout_set_text(Layout *layout, PangoFontDescription *desc, int heig
layout->height = height;
}
+static void compute_width(Drawer *draw, Window *win)
+{
+ int screen_width = win->con->screen_size->width;
+ draw->width = screen_width - draw->right_pad - draw->left_pad;
+ log_debug("Draw context width calculated [width=%d]", draw->width);
+}
+
static GList *compute_group_layout(Drawer *draw, GList *btns, int bx, bool root)
{
GList *layouts = NULL;
@@ -244,7 +236,7 @@ retry:
layout->width -= 1;
}
} else {
- layout->pl = pango_layout_new(draw->ctx);
+ layout->pl = pango_layout_new(draw->context);
layout_set_text(layout, draw->desc, draw->height);
}
@@ -275,10 +267,7 @@ void draw_compute_layout(Drawer *draw, Window *win, GList *btns)
memset(draw->layout_end, 0, sizeof(draw->layout_end));
memset(draw->layout_bx, 0, sizeof(draw->layout_bx));
- g_assert_null(draw->ctx);
- draw->ctx = pango_cairo_create_context(window_get_context(win));
-
- g_list_free_full(draw->layouts, (GDestroyNotify)layout_destroy);
+ g_clear_pointer(&draw->layouts, layout_invalidate);
draw->layouts = compute_group_layout(draw, btns, 0, true);
draw->layout_width[PANGO_ALIGN_LEFT] = draw->layout_bx[PANGO_ALIGN_LEFT];
@@ -313,7 +302,6 @@ void draw_compute_layout(Drawer *draw, Window *win, GList *btns)
}
log_debug("Updated layouts");
- g_clear_pointer(&draw->ctx, g_object_unref);
}
void draw_set_background(Drawer *draw, Color background)
@@ -323,12 +311,42 @@ void draw_set_background(Drawer *draw, Color background)
void draw_set_separator(Drawer *draw, int sep)
{
+ g_assert(sep >= 0);
draw->sep = sep;
}
+void draw_set_font(Drawer *draw, const char *font)
+{
+ log_debug("Pango loading font description '%s'", font);
+ draw->desc = pango_font_description_from_string(font);
+ g_assert_nonnull(draw->desc);
+ log_debug("Pango found matching font '%s'", pango_font_description_get_family(draw->desc));
+}
+
+// NOTE: Don't call this between layout and paint!
+void draw_set_context(Drawer *draw, PangoContext *context)
+{
+ g_assert_nonnull(context);
+ draw->context = context;
+ log_debug("Pango context updated [context=%p]", context);
+}
+
+void draw_set_size(Drawer *draw, int height, int left_pad, int right_pad, int top_pad)
+{
+ g_assert(height > 0);
+
+ draw->height = height;
+ draw->left_pad = left_pad;
+ draw->right_pad = right_pad;
+ draw->top_pad = top_pad;
+
+ log_debug("Draw context size updated [height=%d, left_pad=%d, right_pad=%d, top_pad=%d]",
+ height, left_pad, right_pad, top_pad);
+}
+
void draw_destroy(Drawer *draw)
{
- g_list_free_full(draw->layouts, (GDestroyNotify)layout_destroy);
+ g_clear_pointer(&draw->layouts, layout_invalidate);
pango_font_description_free(draw->desc);
g_free(draw);
}