Skip to content

Commit 59cfdd1

Browse files
committed
swaybar: deduplicate mode and workspace rendering code
The render_workspace_button and render_binding_mode_indicator functions are almost the same. This commit extracts the common rendering code into a new render_box function.
1 parent a25645a commit 59cfdd1

File tree

2 files changed

+58
-86
lines changed

2 files changed

+58
-86
lines changed

include/swaybar/config.h

+5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ struct box_colors {
1414
uint32_t text;
1515
};
1616

17+
struct box_size {
18+
uint32_t width;
19+
uint32_t height;
20+
};
21+
1722
struct config_output {
1823
struct wl_list link; // swaybar_config::outputs
1924
char *name;

swaybar/render.c

+53-86
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,14 @@
1313
#include "swaybar/ipc.h"
1414
#include "swaybar/render.h"
1515
#include "swaybar/status_line.h"
16-
#include "log.h"
1716
#if HAVE_TRAY
1817
#include "swaybar/tray/tray.h"
1918
#endif
2019
#include "wlr-layer-shell-unstable-v1-client-protocol.h"
2120

2221
static const int WS_HORIZONTAL_PADDING = 5;
2322
static const double WS_VERTICAL_PADDING = 1.5;
24-
static const double BORDER_WIDTH = 1;
23+
static const int BORDER_WIDTH = 1;
2524

2625
struct render_context {
2726
cairo_t *cairo;
@@ -541,62 +540,74 @@ static uint32_t render_status_line(struct render_context *ctx, double *x) {
541540
return 0;
542541
}
543542

544-
static uint32_t render_binding_mode_indicator(struct render_context *ctx,
545-
double x) {
543+
static struct box_size render_box(struct render_context *ctx, double x,
544+
struct box_colors colors, const char *label, bool pango_markup) {
546545
struct swaybar_output *output = ctx->output;
547-
const char *mode = output->bar->mode;
548-
if (!mode) {
549-
return 0;
550-
}
551-
552-
cairo_t *cairo = ctx->cairo;
553546
struct swaybar_config *config = output->bar->config;
547+
cairo_t *cairo = ctx->cairo;
548+
554549
int text_width, text_height;
555550
get_text_size(cairo, config->font_description, &text_width, &text_height, NULL,
556-
1, output->bar->mode_pango_markup,
557-
"%s", mode);
551+
1, pango_markup, "%s", label);
558552

559-
int ws_vertical_padding = WS_VERTICAL_PADDING;
560-
int ws_horizontal_padding = WS_HORIZONTAL_PADDING;
561-
int border_width = BORDER_WIDTH;
553+
uint32_t width = text_width + WS_HORIZONTAL_PADDING * 2 + BORDER_WIDTH * 2;
554+
if (width < config->workspace_min_width) {
555+
width = config->workspace_min_width;
556+
}
562557

563-
uint32_t ideal_height = text_height + ws_vertical_padding * 2
564-
+ border_width * 2;
558+
uint32_t ideal_height = text_height + WS_VERTICAL_PADDING * 2
559+
+ BORDER_WIDTH * 2;
565560
uint32_t ideal_surface_height = ideal_height;
566561
if (!output->bar->config->height &&
567562
output->height < ideal_surface_height) {
568-
return ideal_surface_height;
569-
}
570-
uint32_t width = text_width + ws_horizontal_padding * 2 + border_width * 2;
571-
if (width < config->workspace_min_width) {
572-
width = config->workspace_min_width;
563+
return (struct box_size) {
564+
.width = width,
565+
.height = ideal_surface_height,
566+
};
573567
}
574568

575569
uint32_t height = output->height;
576570
cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE);
577-
cairo_set_source_u32(cairo, config->colors.binding_mode.background);
578-
ctx->background_color = config->colors.binding_mode.background;
579-
ctx->has_transparency |= (config->colors.binding_mode.background & 0xFF) != 0xFF;
571+
cairo_set_source_u32(cairo, colors.background);
572+
ctx->background_color = colors.background;
573+
ctx->has_transparency |= (colors.background & 0xFF) != 0xFF;
580574
cairo_rectangle(cairo, x, 0, width, height);
581575
cairo_fill(cairo);
582576

583-
cairo_set_source_u32(cairo, config->colors.binding_mode.border);
584-
cairo_rectangle(cairo, x, 0, width, border_width);
577+
cairo_set_source_u32(cairo, colors.border);
578+
cairo_rectangle(cairo, x, 0, width, BORDER_WIDTH);
585579
cairo_fill(cairo);
586-
cairo_rectangle(cairo, x, 0, border_width, height);
580+
cairo_rectangle(cairo, x, 0, BORDER_WIDTH, height);
587581
cairo_fill(cairo);
588-
cairo_rectangle(cairo, x + width - border_width, 0, border_width, height);
582+
cairo_rectangle(cairo, x + width - BORDER_WIDTH, 0, BORDER_WIDTH, height);
589583
cairo_fill(cairo);
590-
cairo_rectangle(cairo, x, height - border_width, width, border_width);
584+
cairo_rectangle(cairo, x, height - BORDER_WIDTH, width, BORDER_WIDTH);
591585
cairo_fill(cairo);
592586

593587
double text_y = height / 2.0 - text_height / 2.0;
594-
cairo_set_source_u32(cairo, config->colors.binding_mode.text);
588+
cairo_set_source_u32(cairo, colors.text);
595589
cairo_move_to(cairo, x + width / 2 - text_width / 2, (int)floor(text_y));
596-
choose_text_aa_mode(ctx, config->colors.binding_mode.text);
597-
render_text(cairo, config->font_description, 1, output->bar->mode_pango_markup,
598-
"%s", mode);
599-
return output->height;
590+
choose_text_aa_mode(ctx, colors.text);
591+
render_text(cairo, config->font_description, 1, pango_markup,
592+
"%s", label);
593+
594+
return (struct box_size) {
595+
.width = width,
596+
.height = output->height,
597+
};
598+
}
599+
600+
static uint32_t render_binding_mode_indicator(struct render_context *ctx,
601+
double x) {
602+
struct swaybar_output *output = ctx->output;
603+
const char *mode = output->bar->mode;
604+
if (!mode) {
605+
return 0;
606+
}
607+
608+
struct box_size size = render_box(ctx, x, output->bar->config->colors.binding_mode,
609+
mode, output->bar->mode_pango_markup);
610+
return size.height;
600611
}
601612

602613
static enum hotspot_event_handling workspace_hotspot_callback(
@@ -618,6 +629,7 @@ static uint32_t render_workspace_button(struct render_context *ctx,
618629
struct swaybar_workspace *ws, double *x) {
619630
struct swaybar_output *output = ctx->output;
620631
struct swaybar_config *config = output->bar->config;
632+
621633
struct box_colors box_colors;
622634
if (ws->urgent) {
623635
box_colors = config->colors.urgent_workspace;
@@ -629,66 +641,21 @@ static uint32_t render_workspace_button(struct render_context *ctx,
629641
box_colors = config->colors.inactive_workspace;
630642
}
631643

632-
uint32_t height = output->height;
633-
634-
cairo_t *cairo = ctx->cairo;
635-
int text_width, text_height;
636-
get_text_size(cairo, config->font_description, &text_width, &text_height, NULL,
637-
1, config->pango_markup, "%s", ws->label);
638-
639-
int ws_vertical_padding = WS_VERTICAL_PADDING;
640-
int ws_horizontal_padding = WS_HORIZONTAL_PADDING;
641-
int border_width = BORDER_WIDTH;
642-
643-
uint32_t ideal_height = ws_vertical_padding * 2 + text_height
644-
+ border_width * 2;
645-
uint32_t ideal_surface_height = ideal_height;
646-
if (!output->bar->config->height &&
647-
output->height < ideal_surface_height) {
648-
return ideal_surface_height;
649-
}
650-
651-
uint32_t width = text_width + ws_horizontal_padding * 2 + border_width * 2;
652-
if (width < config->workspace_min_width) {
653-
width = config->workspace_min_width;
654-
}
655-
656-
cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE);
657-
cairo_set_source_u32(cairo, box_colors.background);
658-
ctx->background_color = box_colors.background;
659-
ctx->has_transparency |= (box_colors.background & 0xFF) != 0xFF;
660-
cairo_rectangle(cairo, *x, 0, width, height);
661-
cairo_fill(cairo);
662-
663-
cairo_set_source_u32(cairo, box_colors.border);
664-
cairo_rectangle(cairo, *x, 0, width, border_width);
665-
cairo_fill(cairo);
666-
cairo_rectangle(cairo, *x, 0, border_width, height);
667-
cairo_fill(cairo);
668-
cairo_rectangle(cairo, *x + width - border_width, 0, border_width, height);
669-
cairo_fill(cairo);
670-
cairo_rectangle(cairo, *x, height - border_width, width, border_width);
671-
cairo_fill(cairo);
672-
673-
double text_y = height / 2.0 - text_height / 2.0;
674-
cairo_set_source_u32(cairo, box_colors.text);
675-
cairo_move_to(cairo, *x + width / 2 - text_width / 2, (int)floor(text_y));
676-
choose_text_aa_mode(ctx, box_colors.text);
677-
render_text(cairo, config->font_description, 1, config->pango_markup,
678-
"%s", ws->label);
644+
struct box_size size = render_box(ctx, *x, box_colors,
645+
ws->label, config->pango_markup);
679646

680647
struct swaybar_hotspot *hotspot = calloc(1, sizeof(struct swaybar_hotspot));
681648
hotspot->x = *x;
682649
hotspot->y = 0;
683-
hotspot->width = width;
684-
hotspot->height = height;
650+
hotspot->width = size.width;
651+
hotspot->height = size.height;
685652
hotspot->callback = workspace_hotspot_callback;
686653
hotspot->destroy = free;
687654
hotspot->data = strdup(ws->name);
688655
wl_list_insert(&output->hotspots, &hotspot->link);
689656

690-
*x += width;
691-
return output->height;
657+
*x += size.width;
658+
return size.height;
692659
}
693660

694661
static uint32_t render_to_cairo(struct render_context *ctx) {

0 commit comments

Comments
 (0)