From 4ce61977e6778bf95f71dc1543a3796627c9acf6 Mon Sep 17 00:00:00 2001 From: Felix Kratz Date: Sun, 29 May 2022 19:26:54 +0200 Subject: [PATCH] allow popup.background.image and align popup elements properly --- src/misc/helpers.h | 2 ++ src/popup.c | 56 ++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 49 insertions(+), 9 deletions(-) diff --git a/src/misc/helpers.h b/src/misc/helpers.h index 5ee51e6..f187107 100644 --- a/src/misc/helpers.h +++ b/src/misc/helpers.h @@ -27,6 +27,8 @@ struct rgba_color { float a; }; +static struct rgba_color g_transparent = { 0 }; + struct token { char *text; unsigned int length; diff --git a/src/popup.c b/src/popup.c index a6bbd37..5bcb777 100644 --- a/src/popup.c +++ b/src/popup.c @@ -22,8 +22,40 @@ void popup_init(struct popup* popup) { void popup_calculate_bounds(struct popup* popup) { uint32_t y = popup->background.border_width; uint32_t x = 0; + uint32_t total_item_width = 0; uint32_t width = 0; uint32_t height = 0; + + if (popup->background.enabled + && popup->background.image.enabled) { + width = popup->background.image.bounds.size.width; + } + + if (popup->horizontal) { + for (int j = 0; j < popup->num_items; j++) { + struct bar_item* bar_item = popup->items[j]; + if (!bar_item->drawing) continue; + uint32_t cell_height = bar_item_get_height(bar_item) > popup->cell_size + ? bar_item_get_height(bar_item) + : popup->cell_size; + + total_item_width += bar_item->background.padding_right + + bar_item->background.padding_left + + bar_item_get_length(bar_item, false); + + if (cell_height > height && popup->horizontal) height = cell_height; + } + + if (popup->background.enabled + && popup->background.image.enabled) { + if (popup->background.image.bounds.size.height > height) + height = popup->background.image.bounds.size.height; + + x = (width - total_item_width) / 2; + } + + } + for (int j = 0; j < popup->num_items; j++) { struct bar_item* bar_item = NULL; if (popup->horizontal) bar_item = popup->items[j]; @@ -36,18 +68,23 @@ void popup_calculate_bounds(struct popup* popup) { uint32_t item_width = bar_item->background.padding_right + bar_item->background.padding_left + bar_item_calculate_bounds(bar_item, - cell_height, + popup->horizontal + ? height + : cell_height, x, - y + cell_height / 2); + y + (popup->horizontal + ? height + : cell_height) / 2); if (item_width > width && !popup->horizontal) width = item_width; - if (cell_height > height && popup->horizontal) height = cell_height; if (popup->horizontal) x += item_width; else y += cell_height; } if (popup->horizontal) { - width = x; + if (!popup->background.enabled || !popup->background.image.enabled) { + width = x; + } y += height; } y += popup->background.border_width; @@ -136,11 +173,12 @@ void popup_draw(struct popup* popup) { popup->background.bounds.size.height}}); draw_rect(popup->window.context, popup->background.bounds, - &popup->background.color, - popup->background.corner_radius, - popup->background.border_width, - &popup->background.border_color, - true ); + &g_transparent, + 0, + 0, + &g_transparent, + true ); + background_draw(&popup->background, popup->window.context); for (int i = 0; i < popup->num_items; i++) { struct bar_item* bar_item = popup->items[i];