swaybar: fix rendering of border and background

This fixes the rendering of borders and backgrounds for blocks. This
also makes the following changes:
* both borders and padding are scaled with the output
* both lines and rectangles are rendered without an antialiasing to
avoid bleeding outside the desired area
This commit is contained in:
Brian Ashworth 2019-01-09 20:28:30 -05:00
parent 14cab78612
commit 39873bc1f0

View file

@ -90,13 +90,24 @@ static uint32_t render_status_line_text(cairo_t *cairo,
return output->height; return output->height;
} }
static void render_sharp_rectangle(cairo_t *cairo, uint32_t color,
double x, double y, double width, double height) {
cairo_save(cairo);
cairo_set_source_u32(cairo, color);
cairo_set_antialias(cairo, CAIRO_ANTIALIAS_NONE);
cairo_rectangle(cairo, x, y, width, height);
cairo_fill(cairo);
cairo_restore(cairo);
}
static void render_sharp_line(cairo_t *cairo, uint32_t color, static void render_sharp_line(cairo_t *cairo, uint32_t color,
double x, double y, double width, double height) { double x, double y, double width, double height) {
cairo_set_source_u32(cairo, color);
if (width > 1 && height > 1) { if (width > 1 && height > 1) {
cairo_rectangle(cairo, x, y, width, height); render_sharp_rectangle(cairo, color, x, y, width, height);
cairo_fill(cairo);
} else { } else {
cairo_save(cairo);
cairo_set_source_u32(cairo, color);
cairo_set_antialias(cairo, CAIRO_ANTIALIAS_NONE);
if (width == 1) { if (width == 1) {
x += 0.5; x += 0.5;
height += y; height += y;
@ -111,6 +122,7 @@ static void render_sharp_line(cairo_t *cairo, uint32_t color,
cairo_set_line_width(cairo, 1.0); cairo_set_line_width(cairo, 1.0);
cairo_line_to(cairo, width, height); cairo_line_to(cairo, width, height);
cairo_stroke(cairo); cairo_stroke(cairo);
cairo_restore(cairo);
} }
} }
@ -141,7 +153,7 @@ static uint32_t render_status_block(cairo_t *cairo,
output->scale, block->markup, "%s", block->full_text); output->scale, block->markup, "%s", block->full_text);
int margin = 3 * output->scale; int margin = 3 * output->scale;
int ws_vertical_padding = WS_VERTICAL_PADDING * 2; double ws_vertical_padding = WS_VERTICAL_PADDING * 2 * output->scale;
int width = text_width; int width = text_width;
if (width < block->min_width) { if (width < block->min_width) {
@ -157,12 +169,12 @@ static uint32_t render_status_block(cairo_t *cairo,
*x -= width; *x -= width;
if ((block->border || block->urgent) && block->border_left > 0) { if ((block->border || block->urgent) && block->border_left > 0) {
*x -= (block->border_left + margin); *x -= (block->border_left * output->scale + margin);
block_width += block->border_left + margin; block_width += block->border_left * output->scale + margin;
} }
if ((block->border || block->urgent) && block->border_right > 0) { if ((block->border || block->urgent) && block->border_right > 0) {
*x -= (block->border_right + margin); *x -= (block->border_right * output->scale + margin);
block_width += block->border_right + margin; block_width += block->border_right * output->scale + margin;
} }
int sep_width, sep_height; int sep_width, sep_height;
@ -199,48 +211,41 @@ static uint32_t render_status_block(cairo_t *cairo,
wl_list_insert(&output->hotspots, &hotspot->link); wl_list_insert(&output->hotspots, &hotspot->link);
} }
double pos = *x; double x_pos = *x;
double y_pos = WS_VERTICAL_PADDING * output->scale;
double render_height = height - ws_vertical_padding + output->scale;
uint32_t bg_color = block->urgent uint32_t bg_color = block->urgent
? config->colors.urgent_workspace.background : block->background; ? config->colors.urgent_workspace.background : block->background;
if (bg_color) { if (bg_color) {
cairo_set_source_u32(cairo, bg_color); render_sharp_rectangle(cairo, bg_color, x_pos, y_pos,
cairo_rectangle(cairo, pos - 0.5 * output->scale, block_width, render_height);
output->scale, width, height);
cairo_fill(cairo);
} }
uint32_t border_color = block->urgent uint32_t border_color = block->urgent
? config->colors.urgent_workspace.border : block->border; ? config->colors.urgent_workspace.border : block->border;
if (border_color && block->border_top > 0) { if (border_color && block->border_top > 0) {
render_sharp_line(cairo, border_color, render_sharp_line(cairo, border_color, x_pos, y_pos,
pos - 0.5 * output->scale, output->scale, block_width, block->border_top * output->scale);
text_width, block->border_top);
} }
if (border_color && block->border_bottom > 0) { if (border_color && block->border_bottom > 0) {
render_sharp_line(cairo, border_color, render_sharp_line(cairo, border_color, x_pos,
pos - 0.5 * output->scale, y_pos + render_height - block->border_bottom * output->scale,
height - output->scale - block->border_bottom, block_width, block->border_bottom * output->scale);
text_width, block->border_bottom);
} }
if (border_color != 0 && block->border_left > 0) { if (border_color && block->border_left > 0) {
render_sharp_line(cairo, border_color, render_sharp_line(cairo, border_color, x_pos, y_pos,
pos - 0.5 * output->scale, output->scale, block->border_left * output->scale, render_height);
block->border_left, height); x_pos += block->border_left * output->scale + margin;
}
if (border_color != 0 && block->border_right > 0) {
render_sharp_line(cairo, border_color,
pos - 0.5 * output->scale + text_width, output->scale,
block->border_right, height);
} }
double offset = 0; double offset = 0;
if (strncmp(block->align, "left", 5) == 0) { if (strncmp(block->align, "left", 5) == 0) {
offset = pos; offset = x_pos;
} else if (strncmp(block->align, "right", 5) == 0) { } else if (strncmp(block->align, "right", 5) == 0) {
offset = pos + width - text_width; offset = x_pos + width - text_width;
} else if (strncmp(block->align, "center", 6) == 0) { } else if (strncmp(block->align, "center", 6) == 0) {
offset = pos + (width - text_width) / 2; offset = x_pos + (width - text_width) / 2;
} }
cairo_move_to(cairo, offset, height / 2.0 - text_height / 2.0); cairo_move_to(cairo, offset, height / 2.0 - text_height / 2.0);
uint32_t color = block->color ? *block->color : config->colors.statusline; uint32_t color = block->color ? *block->color : config->colors.statusline;
@ -248,14 +253,13 @@ static uint32_t render_status_block(cairo_t *cairo,
cairo_set_source_u32(cairo, color); cairo_set_source_u32(cairo, color);
pango_printf(cairo, config->font, output->scale, pango_printf(cairo, config->font, output->scale,
block->markup, "%s", block->full_text); block->markup, "%s", block->full_text);
pos += width; x_pos += width;
if (block->border && block->border_right > 0) { if (block->border && block->border_right > 0) {
pos += margin; x_pos += margin;
render_sharp_line(cairo, block->border, render_sharp_line(cairo, border_color, x_pos, y_pos,
pos - 0.5 * output->scale, output->scale, block->border_right * output->scale, render_height);
block->border_right, height); x_pos += block->border_right * output->scale;
pos += block->border_right;
} }
if (!edge && block->separator) { if (!edge && block->separator) {
@ -265,14 +269,14 @@ static uint32_t render_status_block(cairo_t *cairo,
cairo_set_source_u32(cairo, config->colors.separator); cairo_set_source_u32(cairo, config->colors.separator);
} }
if (config->sep_symbol) { if (config->sep_symbol) {
offset = pos + (sep_block_width - sep_width) / 2; offset = x_pos + (sep_block_width - sep_width) / 2;
cairo_move_to(cairo, offset, height / 2.0 - sep_height / 2.0); cairo_move_to(cairo, offset, height / 2.0 - sep_height / 2.0);
pango_printf(cairo, config->font, output->scale, false, pango_printf(cairo, config->font, output->scale, false,
"%s", config->sep_symbol); "%s", config->sep_symbol);
} else { } else {
cairo_set_line_width(cairo, 1); cairo_set_line_width(cairo, 1);
cairo_move_to(cairo, pos + sep_block_width / 2, margin); cairo_move_to(cairo, x_pos + sep_block_width / 2, margin);
cairo_line_to(cairo, pos + sep_block_width / 2, height - margin); cairo_line_to(cairo, x_pos + sep_block_width / 2, height - margin);
cairo_stroke(cairo); cairo_stroke(cairo);
} }
} }