diff options
| author | Federico Angelilli <code@fedang.net> | 2024-03-15 21:24:05 +0100 |
|---|---|---|
| committer | Federico Angelilli <code@fedang.net> | 2024-03-15 21:24:05 +0100 |
| commit | 056dfd80b930abe56e151d4ebafae248fb0fa5a0 (patch) | |
| tree | d1f0d8f67077ca5477b3410bfc7a6e5ae328fccd | |
| parent | 501270a76aea256fcf8ebe8330be3cbf96f64f22 (diff) | |
Add support for layouts with children
| -rw-r--r-- | src/draw.c | 52 |
1 files changed, 29 insertions, 23 deletions
@@ -104,33 +104,21 @@ static void paint_text(cairo_t *cr, const Layout *layout) pango_cairo_show_layout(cr, layout->pl); } -void draw_paint(Drawer *draw, Window *win) +static void paint_button_list(cairo_t *cr, GList *layouts) { - int width, height; - compute_width(draw, win, &width, &height); - - 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_OVER); - - // Fill the background - cairo_set_source_rgba(cr, draw->background.r, draw->background.g, draw->background.b, draw->background.a); - cairo_paint(cr); - - for (GList *it = draw->layouts; it; it = it->next) { + for (GList *it = layouts; it; it = it->next) { Layout *layout = it->data; Button *btn = layout->btn; cairo_push_group(cr); - paint_button(cr, layout); if (btn->simple) { + g_assert_null(layout->children); paint_text(cr, layout); } else { - // TODO + g_assert_nonnull(layout->children); + paint_button_list(cr, layout->children); } if (btn->anim != NULL && btn->anim->paint_func != NULL) { @@ -147,10 +135,27 @@ void draw_paint(Drawer *draw, Window *win) cairo_pop_group_to_source(cr); cairo_paint(cr); } +} +void draw_paint(Drawer *draw, Window *win) +{ + int width, height; + compute_width(draw, win, &width, &height); + + 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_OVER); + + // Fill the background + cairo_set_source_rgba(cr, draw->background.r, draw->background.g, draw->background.b, draw->background.a); + cairo_paint(cr); + + // Treat the top-level buttons similarly to a button group + paint_button_list(cr, draw->layouts); cairo_destroy(cr); - // TODO: Move these somewhere else window_move(win, draw->left_pad, draw->top_pad); window_resize(win, width, draw->height); @@ -158,9 +163,10 @@ void draw_paint(Drawer *draw, Window *win) cairo_surface_destroy(surface); } -// Works only for simple buttons -static void layout_text(Layout *layout, PangoFontDescription *desc, int height, int radius) +static void layout_set_text(Layout *layout, PangoFontDescription *desc, int height, int radius) { + g_assert(layout->btn->simple); + const char *text = CAST(layout->btn, ButtonSimple)->text; pango_layout_set_font_description(layout->pl, desc); @@ -246,10 +252,10 @@ void draw_compute_layout(Drawer *draw, Window *win, GList *btns) child->y = 0; child->pl = pango_cairo_create_layout(window_get_context(win)); - layout_text(child, draw->desc, height, radius); + layout_set_text(child, draw->desc, height, radius); child->width += child->line_w; - draw->layouts = g_list_prepend(draw->layouts, child); + layout->children = g_list_prepend(layout->children, child); x += child->width; if (it->next != NULL) x += sep; @@ -274,7 +280,7 @@ void draw_compute_layout(Drawer *draw, Window *win, GList *btns) if (layout->btn->simple) { layout->pl = pango_cairo_create_layout(window_get_context(win)); - layout_text(layout, draw->desc, height, radius); + layout_set_text(layout, draw->desc, height, radius); // TODO: make it work for groups if (btn->anim != NULL && btn->anim->layout_func != NULL) { |
