swaybar: fix tray icons rendering on scaled outputs

#6504 simplified rendering code by setting scale at cairo, but not the code
for rendering tray icons, making it draws at scale of expected size.
This fixes by resetting scale before drawing icons, and restoring it
afterward.
This commit is contained in:
xdavidwu 2021-09-14 14:34:39 +08:00
parent b7b96399e1
commit 04092c7f54
No known key found for this signature in database
GPG key ID: EF1E781EE6BE877E
2 changed files with 18 additions and 7 deletions

View file

@ -494,23 +494,34 @@ uint32_t render_sni(cairo_t *cairo, struct swaybar_output *output, double *x,
} }
int padded_size = icon_size + 2*padding; int padded_size = icon_size + 2*padding;
*x -= padded_size; double padded_surface_size = (double) padded_size / output->scale;
int y = floor((height - padded_size) / 2.0); double buffer_x = (*x * output->scale) - padded_size;
int buffer_y = floor((height - padded_size) / 2.0);
// Reset scale to draw with prepared icon as source surface. Drawing with
// source surface directly with scale applied will end up blurry in HiDPI.
cairo_save(cairo);
cairo_identity_matrix(cairo);
cairo_operator_t op = cairo_get_operator(cairo); cairo_operator_t op = cairo_get_operator(cairo);
cairo_set_operator(cairo, CAIRO_OPERATOR_OVER); cairo_set_operator(cairo, CAIRO_OPERATOR_OVER);
cairo_set_source_surface(cairo, icon, *x + padding, y + padding); cairo_set_source_surface(cairo, icon, buffer_x + padding, buffer_y + padding);
cairo_rectangle(cairo, *x, y, padded_size, padded_size); cairo_rectangle(cairo, buffer_x, buffer_y, padded_size, padded_size);
cairo_fill(cairo); cairo_fill(cairo);
cairo_set_operator(cairo, op); cairo_set_operator(cairo, op);
// Restore scale
cairo_restore(cairo);
*x -= padded_surface_size;
cairo_surface_destroy(icon); cairo_surface_destroy(icon);
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 = height; hotspot->width = output->height;
hotspot->height = height; hotspot->height = output->height;
hotspot->callback = icon_hotspot_callback; hotspot->callback = icon_hotspot_callback;
hotspot->destroy = free; hotspot->destroy = free;
hotspot->data = strdup(sni->watcher_id); hotspot->data = strdup(sni->watcher_id);

View file

@ -117,7 +117,7 @@ uint32_t render_tray(cairo_t *cairo, struct swaybar_output *output, double *x) {
} // else display on all } // else display on all
if ((int) output->height*output->scale <= 2*config->tray_padding) { if ((int) output->height*output->scale <= 2*config->tray_padding) {
return 2*config->tray_padding + 1; return (2*config->tray_padding + 1) / output->scale;
} }
uint32_t max_height = 0; uint32_t max_height = 0;