diff options
| author | Federico Angelilli <code@fedang.net> | 2023-12-11 16:42:08 +0100 |
|---|---|---|
| committer | Federico Angelilli <code@fedang.net> | 2023-12-11 16:42:08 +0100 |
| commit | ffdfcf5d8ad1265e6beeea5780bda895221cd506 (patch) | |
| tree | 2adb77de81a560f564fec9b98e4cb84977edaa46 | |
| parent | 4b83ba02939ad7692ebd7e71161d37eabe4b73e3 (diff) | |
Simplify draw_compute_layout
| -rw-r--r-- | src/comet.c | 2 | ||||
| -rw-r--r-- | src/draw.c | 65 |
2 files changed, 32 insertions, 35 deletions
diff --git a/src/comet.c b/src/comet.c index 4945bb2..ec7295b 100644 --- a/src/comet.c +++ b/src/comet.c @@ -329,7 +329,7 @@ int main(int argc, char **argv) // Menu button(s) Color grey = { 0.5, 0.5, 0.5, 1 }; - Button *group = button_group_create(PANGO_ALIGN_LEFT, grey, line_color); + Button *group = button_group_create(PANGO_ALIGN_CENTER, grey, line_color); Button *child1 = button_simple_create(PANGO_ALIGN_CENTER, color, line_color); button_set_padding(child1, 1); @@ -169,22 +169,22 @@ void draw_compute_layout(Drawable *draw, Window *win, GList *btns) //double scale = window_get_scale(win); double scale = 1; + // Remove this... int width, height; compute_width(draw, win, &width, &height); + // Order by alignment + btns = g_list_sort(btns, align_compare); + Button *prev = NULL; + int x = 0; int radius = height / 2; int sep = 10 * scale; int line_w = draw->line_w * scale; - btns = g_list_sort(btns, align_compare); - - GList *adjust_center = NULL; - GList *adjust_right = NULL; - - int end_xs[3] = { 0 }; - Button *prev = NULL; + GList *layout_start[3] = { NULL }; + int layout_end[3] = { 0 }; for (GList *it = btns; it; it = it->next) { Button *btn = it->data; @@ -199,6 +199,10 @@ void draw_compute_layout(Drawable *draw, Window *win, GList *btns) draw->layouts = g_list_prepend(draw->layouts, layout); + if (prev == NULL || prev->align != btn->align) { + layout_start[btn->align] = draw->layouts; + } + if (!btn->simple) { ButtonGroup *group = CAST(btn, ButtonGroup); g_assert_nonnull(group->children); @@ -247,56 +251,49 @@ void draw_compute_layout(Drawable *draw, Window *win, GList *btns) layout->width -= 1; } if (fix_right) layout->width -= 1; - goto end; } } - layout->pl = pango_cairo_create_layout(window_get_context(win)); - layout_text(layout, draw->desc, scale, height, radius); - x += layout->width + line_w * 2; - - layout->width -= 2 * btn->padding; - layout->height -= 2 * btn->padding; + if (layout->btn->simple) { + layout->pl = pango_cairo_create_layout(window_get_context(win)); + layout_text(layout, draw->desc, scale, height, radius); + x += layout->width + line_w * 2; -end: - if (prev != NULL && prev->align != btn->align) { - if (btn->align == PANGO_ALIGN_CENTER) adjust_center = draw->layouts; - else if (btn->align == PANGO_ALIGN_RIGHT) adjust_right = draw->layouts; + layout->width -= 2 * btn->padding; + layout->height -= 2 * btn->padding; } prev = btn; - end_xs[btn->align] = x; + layout_end[btn->align] = x; } draw->layouts = g_list_reverse(draw->layouts); - int widths[3] = { - end_xs[0], - end_xs[1] - end_xs[0], - end_xs[2] - MAX(end_xs[1], end_xs[0]), + int layout_width[3] = { + layout_end[PANGO_ALIGN_LEFT], + layout_end[PANGO_ALIGN_CENTER] - layout_end[PANGO_ALIGN_LEFT], + layout_end[PANGO_ALIGN_RIGHT] - MAX(layout_end[PANGO_ALIGN_CENTER], layout_end[PANGO_ALIGN_LEFT]), }; - int start_xs[3] = { - 0, - end_xs[0], - MAX(end_xs[1], end_xs[0]), - }; + if (layout_end[PANGO_ALIGN_RIGHT] > width) { + log_error("LENGHT %d on %d", layout_end[PANGO_ALIGN_RIGHT], width); + } int center = round(width / 2); - int center_off = (center - widths[1] / 2) - start_xs[1]; - log_debug("Aligning center layout [x=%d, off=%d]", center, center_off); + int center_off = center - round(layout_width[PANGO_ALIGN_CENTER] / 2) - layout_end[PANGO_ALIGN_LEFT]; + log_debug("Aligning center layout [center=%d, off=%d]", center, center_off); - for (GList *it = adjust_center; it != adjust_right; it = it->next) { + for (GList *it = layout_start[PANGO_ALIGN_CENTER]; it != layout_start[PANGO_ALIGN_RIGHT]; it = it->next) { Layout *layout = it->data; layout->x += center_off; } - int right = width - widths[2]; + int right = width - layout_width[PANGO_ALIGN_LEFT]; // The width is off by 2 line width - int right_off = right - start_xs[2] + 2 * line_w; + int right_off = right - MAX(layout_end[PANGO_ALIGN_LEFT], layout_end[PANGO_ALIGN_CENTER]) + 2 * line_w; log_debug("Aligning right layout [x=%d, off=%d]", right, right_off); - for (GList *it = adjust_right; it != NULL; it = it->next) { + for (GList *it = layout_start[PANGO_ALIGN_RIGHT]; it != NULL; it = it->next) { Layout *layout = it->data; layout->x += right_off; } |
