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.
This commit is contained in:
Konstantin Pospelov 2025-03-27 20:11:59 +01:00 committed by Simon Ser
parent 63689bfb83
commit 7e7994dbb2
2 changed files with 73 additions and 101 deletions

View file

@ -14,6 +14,11 @@ struct box_colors {
uint32_t text; uint32_t text;
}; };
struct box_size {
uint32_t width;
uint32_t height;
};
struct config_output { struct config_output {
struct wl_list link; // swaybar_config::outputs struct wl_list link; // swaybar_config::outputs
char *name; char *name;

View file

@ -13,7 +13,6 @@
#include "swaybar/ipc.h" #include "swaybar/ipc.h"
#include "swaybar/render.h" #include "swaybar/render.h"
#include "swaybar/status_line.h" #include "swaybar/status_line.h"
#include "log.h"
#if HAVE_TRAY #if HAVE_TRAY
#include "swaybar/tray/tray.h" #include "swaybar/tray/tray.h"
#endif #endif
@ -21,7 +20,7 @@
static const int WS_HORIZONTAL_PADDING = 5; static const int WS_HORIZONTAL_PADDING = 5;
static const double WS_VERTICAL_PADDING = 1.5; static const double WS_VERTICAL_PADDING = 1.5;
static const double BORDER_WIDTH = 1; static const int BORDER_WIDTH = 1;
struct render_context { struct render_context {
cairo_t *cairo; cairo_t *cairo;
@ -541,6 +540,63 @@ static uint32_t render_status_line(struct render_context *ctx, double *x) {
return 0; return 0;
} }
static struct box_size render_box(struct render_context *ctx, double x,
struct box_colors colors, const char *label, bool pango_markup) {
struct swaybar_output *output = ctx->output;
struct swaybar_config *config = output->bar->config;
cairo_t *cairo = ctx->cairo;
int text_width, text_height;
get_text_size(cairo, config->font_description, &text_width, &text_height, NULL,
1, pango_markup, "%s", label);
uint32_t width = text_width + WS_HORIZONTAL_PADDING * 2 + BORDER_WIDTH * 2;
if (width < config->workspace_min_width) {
width = config->workspace_min_width;
}
uint32_t ideal_height = text_height + WS_VERTICAL_PADDING * 2
+ BORDER_WIDTH * 2;
uint32_t ideal_surface_height = ideal_height;
if (!output->bar->config->height &&
output->height < ideal_surface_height) {
return (struct box_size) {
.width = width,
.height = ideal_surface_height,
};
}
uint32_t height = output->height;
cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE);
cairo_set_source_u32(cairo, colors.background);
ctx->background_color = colors.background;
ctx->has_transparency |= (colors.background & 0xFF) != 0xFF;
cairo_rectangle(cairo, x, 0, width, height);
cairo_fill(cairo);
cairo_set_source_u32(cairo, colors.border);
cairo_rectangle(cairo, x, 0, width, BORDER_WIDTH);
cairo_fill(cairo);
cairo_rectangle(cairo, x, 0, BORDER_WIDTH, height);
cairo_fill(cairo);
cairo_rectangle(cairo, x + width - BORDER_WIDTH, 0, BORDER_WIDTH, height);
cairo_fill(cairo);
cairo_rectangle(cairo, x, height - BORDER_WIDTH, width, BORDER_WIDTH);
cairo_fill(cairo);
double text_y = height / 2.0 - text_height / 2.0;
cairo_set_source_u32(cairo, colors.text);
cairo_move_to(cairo, x + width / 2 - text_width / 2, (int)floor(text_y));
choose_text_aa_mode(ctx, colors.text);
render_text(cairo, config->font_description, 1, pango_markup,
"%s", label);
return (struct box_size) {
.width = width,
.height = output->height,
};
}
static uint32_t render_binding_mode_indicator(struct render_context *ctx, static uint32_t render_binding_mode_indicator(struct render_context *ctx,
double x) { double x) {
struct swaybar_output *output = ctx->output; struct swaybar_output *output = ctx->output;
@ -549,54 +605,9 @@ static uint32_t render_binding_mode_indicator(struct render_context *ctx,
return 0; return 0;
} }
cairo_t *cairo = ctx->cairo; struct box_size size = render_box(ctx, x, output->bar->config->colors.binding_mode,
struct swaybar_config *config = output->bar->config; mode, output->bar->mode_pango_markup);
int text_width, text_height; return size.height;
get_text_size(cairo, config->font_description, &text_width, &text_height, NULL,
1, output->bar->mode_pango_markup,
"%s", mode);
int ws_vertical_padding = WS_VERTICAL_PADDING;
int ws_horizontal_padding = WS_HORIZONTAL_PADDING;
int border_width = BORDER_WIDTH;
uint32_t ideal_height = text_height + ws_vertical_padding * 2
+ border_width * 2;
uint32_t ideal_surface_height = ideal_height;
if (!output->bar->config->height &&
output->height < ideal_surface_height) {
return ideal_surface_height;
}
uint32_t width = text_width + ws_horizontal_padding * 2 + border_width * 2;
if (width < config->workspace_min_width) {
width = config->workspace_min_width;
}
uint32_t height = output->height;
cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE);
cairo_set_source_u32(cairo, config->colors.binding_mode.background);
ctx->background_color = config->colors.binding_mode.background;
ctx->has_transparency |= (config->colors.binding_mode.background & 0xFF) != 0xFF;
cairo_rectangle(cairo, x, 0, width, height);
cairo_fill(cairo);
cairo_set_source_u32(cairo, config->colors.binding_mode.border);
cairo_rectangle(cairo, x, 0, width, border_width);
cairo_fill(cairo);
cairo_rectangle(cairo, x, 0, border_width, height);
cairo_fill(cairo);
cairo_rectangle(cairo, x + width - border_width, 0, border_width, height);
cairo_fill(cairo);
cairo_rectangle(cairo, x, height - border_width, width, border_width);
cairo_fill(cairo);
double text_y = height / 2.0 - text_height / 2.0;
cairo_set_source_u32(cairo, config->colors.binding_mode.text);
cairo_move_to(cairo, x + width / 2 - text_width / 2, (int)floor(text_y));
choose_text_aa_mode(ctx, config->colors.binding_mode.text);
render_text(cairo, config->font_description, 1, output->bar->mode_pango_markup,
"%s", mode);
return output->height;
} }
static enum hotspot_event_handling workspace_hotspot_callback( static enum hotspot_event_handling workspace_hotspot_callback(
@ -618,6 +629,7 @@ static uint32_t render_workspace_button(struct render_context *ctx,
struct swaybar_workspace *ws, double *x) { struct swaybar_workspace *ws, double *x) {
struct swaybar_output *output = ctx->output; struct swaybar_output *output = ctx->output;
struct swaybar_config *config = output->bar->config; struct swaybar_config *config = output->bar->config;
struct box_colors box_colors; struct box_colors box_colors;
if (ws->urgent) { if (ws->urgent) {
box_colors = config->colors.urgent_workspace; box_colors = config->colors.urgent_workspace;
@ -629,66 +641,21 @@ static uint32_t render_workspace_button(struct render_context *ctx,
box_colors = config->colors.inactive_workspace; box_colors = config->colors.inactive_workspace;
} }
uint32_t height = output->height; struct box_size size = render_box(ctx, *x, box_colors,
ws->label, config->pango_markup);
cairo_t *cairo = ctx->cairo;
int text_width, text_height;
get_text_size(cairo, config->font_description, &text_width, &text_height, NULL,
1, config->pango_markup, "%s", ws->label);
int ws_vertical_padding = WS_VERTICAL_PADDING;
int ws_horizontal_padding = WS_HORIZONTAL_PADDING;
int border_width = BORDER_WIDTH;
uint32_t ideal_height = ws_vertical_padding * 2 + text_height
+ border_width * 2;
uint32_t ideal_surface_height = ideal_height;
if (!output->bar->config->height &&
output->height < ideal_surface_height) {
return ideal_surface_height;
}
uint32_t width = text_width + ws_horizontal_padding * 2 + border_width * 2;
if (width < config->workspace_min_width) {
width = config->workspace_min_width;
}
cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE);
cairo_set_source_u32(cairo, box_colors.background);
ctx->background_color = box_colors.background;
ctx->has_transparency |= (box_colors.background & 0xFF) != 0xFF;
cairo_rectangle(cairo, *x, 0, width, height);
cairo_fill(cairo);
cairo_set_source_u32(cairo, box_colors.border);
cairo_rectangle(cairo, *x, 0, width, border_width);
cairo_fill(cairo);
cairo_rectangle(cairo, *x, 0, border_width, height);
cairo_fill(cairo);
cairo_rectangle(cairo, *x + width - border_width, 0, border_width, height);
cairo_fill(cairo);
cairo_rectangle(cairo, *x, height - border_width, width, border_width);
cairo_fill(cairo);
double text_y = height / 2.0 - text_height / 2.0;
cairo_set_source_u32(cairo, box_colors.text);
cairo_move_to(cairo, *x + width / 2 - text_width / 2, (int)floor(text_y));
choose_text_aa_mode(ctx, box_colors.text);
render_text(cairo, config->font_description, 1, config->pango_markup,
"%s", ws->label);
struct swaybar_hotspot *hotspot = calloc(1, sizeof(struct swaybar_hotspot)); struct swaybar_hotspot *hotspot = calloc(1, sizeof(struct swaybar_hotspot));
hotspot->x = *x; hotspot->x = *x;
hotspot->y = 0; hotspot->y = 0;
hotspot->width = width; hotspot->width = size.width;
hotspot->height = height; hotspot->height = size.height;
hotspot->callback = workspace_hotspot_callback; hotspot->callback = workspace_hotspot_callback;
hotspot->destroy = free; hotspot->destroy = free;
hotspot->data = strdup(ws->name); hotspot->data = strdup(ws->name);
wl_list_insert(&output->hotspots, &hotspot->link); wl_list_insert(&output->hotspots, &hotspot->link);
*x += width; *x += size.width;
return output->height; return size.height;
} }
static uint32_t render_to_cairo(struct render_context *ctx) { static uint32_t render_to_cairo(struct render_context *ctx) {