aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Hugo Osvaldo Barrera <hugo@barrera.io>2021-08-22 22:01:10 +0200
committerLibravatar Simon Ser <contact@emersion.fr>2021-08-27 10:12:57 +0200
commit9acb015755c444068aebf3bd0a9b3969a1360a9d (patch)
tree7b1a0f8ee87b22df7f79fc7f88e5a7a919ab4f0e
parentUse fixed titlebar heights (diff)
downloadsway-9acb015755c444068aebf3bd0a9b3969a1360a9d.tar.gz
sway-9acb015755c444068aebf3bd0a9b3969a1360a9d.tar.zst
sway-9acb015755c444068aebf3bd0a9b3969a1360a9d.zip
Deduplicate code for rendering titlebar texts
The title itself and marks were being rendered by two very-similar yet different functions, and any changes made to one had to be reflected on the other. This mostly prevents such oversights from happening, and keeps makes sure we keep both consistent.
-rw-r--r--sway/tree/container.c80
1 files changed, 23 insertions, 57 deletions
diff --git a/sway/tree/container.c b/sway/tree/container.c
index 00c40218..41d43b43 100644
--- a/sway/tree/container.c
+++ b/sway/tree/container.c
@@ -493,20 +493,9 @@ struct sway_output *container_get_effective_output(struct sway_container *con) {
493 return con->outputs->items[con->outputs->length - 1]; 493 return con->outputs->items[con->outputs->length - 1];
494} 494}
495 495
496static void update_title_texture(struct sway_container *con, 496static void render_titlebar_text_texture(struct sway_output *output,
497 struct wlr_texture **texture, struct border_colors *class) { 497 struct sway_container *con, struct wlr_texture **texture,
498 struct sway_output *output = container_get_effective_output(con); 498 struct border_colors *class, bool pango_markup, char *text) {
499 if (!output) {
500 return;
501 }
502 if (*texture) {
503 wlr_texture_destroy(*texture);
504 *texture = NULL;
505 }
506 if (!con->formatted_title) {
507 return;
508 }
509
510 double scale = output->wlr_output->scale; 499 double scale = output->wlr_output->scale;
511 int width = 0; 500 int width = 0;
512 int height = config->font_height * scale; 501 int height = config->font_height * scale;
@@ -555,8 +544,7 @@ static void update_title_texture(struct sway_container *con,
555 class->text[2], class->text[3]); 544 class->text[2], class->text[3]);
556 cairo_move_to(cairo, 0, config->font_baseline * scale - baseline); 545 cairo_move_to(cairo, 0, config->font_baseline * scale - baseline);
557 546
558 pango_printf(cairo, config->font, scale, config->pango_markup, 547 pango_printf(cairo, config->font, scale, pango_markup, "%s", text);
559 "%s", con->formatted_title);
560 548
561 cairo_surface_flush(surface); 549 cairo_surface_flush(surface);
562 unsigned char *data = cairo_image_surface_get_data(surface); 550 unsigned char *data = cairo_image_surface_get_data(surface);
@@ -570,6 +558,24 @@ static void update_title_texture(struct sway_container *con,
570 cairo_destroy(cairo); 558 cairo_destroy(cairo);
571} 559}
572 560
561static void update_title_texture(struct sway_container *con,
562 struct wlr_texture **texture, struct border_colors *class) {
563 struct sway_output *output = container_get_effective_output(con);
564 if (!output) {
565 return;
566 }
567 if (*texture) {
568 wlr_texture_destroy(*texture);
569 *texture = NULL;
570 }
571 if (!con->formatted_title) {
572 return;
573 }
574
575 render_titlebar_text_texture(output, con, texture, class,
576 config->pango_markup, con->formatted_title);
577}
578
573void container_update_title_textures(struct sway_container *container) { 579void container_update_title_textures(struct sway_container *container) {
574 update_title_texture(container, &container->title_focused, 580 update_title_texture(container, &container->title_focused,
575 &config->border_colors.focused); 581 &config->border_colors.focused);
@@ -1615,48 +1621,8 @@ static void update_marks_texture(struct sway_container *con,
1615 } 1621 }
1616 free(part); 1622 free(part);
1617 1623
1618 double scale = output->wlr_output->scale; 1624 render_titlebar_text_texture(output, con, texture, class, false, buffer);
1619 int width = 0;
1620 int height = config->font_height * scale;
1621 int baseline;
1622
1623 cairo_t *c = cairo_create(NULL);
1624 get_text_size(c, config->font, &width, NULL, &baseline, scale, false,
1625 "%s", buffer);
1626 cairo_destroy(c);
1627
1628 if (width == 0 || height == 0) {
1629 return;
1630 }
1631
1632 if (height > config->font_height) {
1633 height = config->font_height;
1634 }
1635
1636 cairo_surface_t *surface = cairo_image_surface_create(
1637 CAIRO_FORMAT_ARGB32, width, height);
1638 cairo_t *cairo = cairo_create(surface);
1639 cairo_set_source_rgba(cairo, class->background[0], class->background[1],
1640 class->background[2], class->background[3]);
1641 cairo_paint(cairo);
1642 PangoContext *pango = pango_cairo_create_context(cairo);
1643 cairo_set_antialias(cairo, CAIRO_ANTIALIAS_BEST);
1644 cairo_set_source_rgba(cairo, class->text[0], class->text[1],
1645 class->text[2], class->text[3]);
1646 cairo_move_to(cairo, 0, config->font_baseline * scale - baseline);
1647
1648 pango_printf(cairo, config->font, scale, false, "%s", buffer);
1649 1625
1650 cairo_surface_flush(surface);
1651 unsigned char *data = cairo_image_surface_get_data(surface);
1652 int stride = cairo_image_surface_get_stride(surface);
1653 struct wlr_renderer *renderer = wlr_backend_get_renderer(
1654 output->wlr_output->backend);
1655 *texture = wlr_texture_from_pixels(
1656 renderer, DRM_FORMAT_ARGB8888, stride, width, height, data);
1657 cairo_surface_destroy(surface);
1658 g_object_unref(pango);
1659 cairo_destroy(cairo);
1660 free(buffer); 1626 free(buffer);
1661} 1627}
1662 1628