aboutsummaryrefslogtreecommitdiffstats
path: root/sway/tree/container.c
diff options
context:
space:
mode:
Diffstat (limited to 'sway/tree/container.c')
-rw-r--r--sway/tree/container.c123
1 files changed, 122 insertions, 1 deletions
diff --git a/sway/tree/container.c b/sway/tree/container.c
index 6ac59547..90ca9b2c 100644
--- a/sway/tree/container.c
+++ b/sway/tree/container.c
@@ -7,6 +7,8 @@
7#include <wayland-server.h> 7#include <wayland-server.h>
8#include <wlr/types/wlr_output_layout.h> 8#include <wlr/types/wlr_output_layout.h>
9#include <wlr/types/wlr_wl_shell.h> 9#include <wlr/types/wlr_wl_shell.h>
10#include "cairo.h"
11#include "pango.h"
10#include "sway/config.h" 12#include "sway/config.h"
11#include "sway/input/input-manager.h" 13#include "sway/input/input-manager.h"
12#include "sway/input/seat.h" 14#include "sway/input/seat.h"
@@ -120,6 +122,13 @@ static void _container_destroy(struct sway_container *cont) {
120 if (cont->name) { 122 if (cont->name) {
121 free(cont->name); 123 free(cont->name);
122 } 124 }
125 if (cont->title_focused) {
126 // If one is set then all of these are set
127 wlr_texture_destroy(cont->title_focused);
128 wlr_texture_destroy(cont->title_focused_inactive);
129 wlr_texture_destroy(cont->title_unfocused);
130 wlr_texture_destroy(cont->title_urgent);
131 }
123 list_free(cont->children); 132 list_free(cont->children);
124 cont->children = NULL; 133 cont->children = NULL;
125 free(cont); 134 free(cont);
@@ -339,7 +348,7 @@ struct sway_container *container_view_create(struct sway_container *sibling,
339 swayc, title, sibling, sibling ? sibling->type : 0, sibling->name); 348 swayc, title, sibling, sibling ? sibling->type : 0, sibling->name);
340 // Setup values 349 // Setup values
341 swayc->sway_view = sway_view; 350 swayc->sway_view = sway_view;
342 swayc->name = title ? strdup(title) : NULL; 351 container_update_title(swayc, title ? title : "");
343 swayc->width = 0; 352 swayc->width = 0;
344 swayc->height = 0; 353 swayc->height = 0;
345 354
@@ -546,3 +555,115 @@ void container_damage_whole(struct sway_container *con) {
546 } 555 }
547 output_damage_whole_container(output->sway_output, con); 556 output_damage_whole_container(output->sway_output, con);
548} 557}
558
559static void update_title_texture(struct sway_container *con,
560 struct wlr_texture **texture, struct border_colors *class) {
561 if (!sway_assert(con->type == C_CONTAINER || con->type == C_VIEW,
562 "Unexpected type %s", container_type_to_str(con->type))) {
563 return;
564 }
565 if (!con->width) {
566 return;
567 }
568 struct sway_container *output = container_parent(con, C_OUTPUT);
569 if (!output) {
570 return;
571 }
572 if (*texture) {
573 wlr_texture_destroy(*texture);
574 }
575 if (!con->name) {
576 return;
577 }
578
579 int width = con->width * output->sway_output->wlr_output->scale;
580 int height = config->font_height * output->sway_output->wlr_output->scale;
581
582 cairo_surface_t *surface = cairo_image_surface_create(
583 CAIRO_FORMAT_ARGB32, width, height);
584 cairo_t *cairo = cairo_create(surface);
585 cairo_set_source_rgba(cairo, class->background[0], class->background[1],
586 class->background[2], class->background[3]);
587 cairo_paint(cairo);
588 PangoContext *pango = pango_cairo_create_context(cairo);
589 cairo_set_antialias(cairo, CAIRO_ANTIALIAS_BEST);
590 cairo_set_source_rgba(cairo, class->text[0], class->text[1],
591 class->text[2], class->text[3]);
592 cairo_move_to(cairo, 0, 0);
593
594 pango_printf(cairo, config->font, output->sway_output->wlr_output->scale,
595 false, "%s", con->name);
596
597 cairo_surface_flush(surface);
598 unsigned char *data = cairo_image_surface_get_data(surface);
599 int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
600 struct wlr_renderer *renderer = wlr_backend_get_renderer(
601 output->sway_output->wlr_output->backend);
602 *texture = wlr_texture_from_pixels(
603 renderer, WL_SHM_FORMAT_ARGB8888, stride, width, height, data);
604 cairo_surface_destroy(surface);
605 g_object_unref(pango);
606 cairo_destroy(cairo);
607}
608
609void container_update_title_textures(struct sway_container *container) {
610 update_title_texture(container, &container->title_focused,
611 &config->border_colors.focused);
612 update_title_texture(container, &container->title_focused_inactive,
613 &config->border_colors.focused_inactive);
614 update_title_texture(container, &container->title_unfocused,
615 &config->border_colors.unfocused);
616 update_title_texture(container, &container->title_urgent,
617 &config->border_colors.urgent);
618}
619
620void container_calculate_title_height(struct sway_container *container) {
621 if (!container->name) {
622 container->title_height = 0;
623 return;
624 }
625 cairo_t *cairo = cairo_create(NULL);
626 int height;
627 get_text_size(cairo, config->font, NULL, &height, 1, false,
628 "%s", container->name);
629 cairo_destroy(cairo);
630 container->title_height = height;
631}
632
633static void container_notify_child_title_changed(
634 struct sway_container *container) {
635 if (!container || container->type != C_CONTAINER) {
636 return;
637 }
638 if (container->layout != L_TABBED && container->layout != L_STACKED) {
639 return;
640 }
641 if (container->name) {
642 free(container->name);
643 }
644 // TODO: iterate children and concatenate their titles
645 container->name = strdup("");
646 container_calculate_title_height(container);
647 container_update_title_textures(container);
648 container_notify_child_title_changed(container->parent);
649}
650
651void container_update_title(struct sway_container *container,
652 const char *new_title) {
653 if (container->name && strcmp(container->name, new_title) == 0) {
654 return;
655 }
656 if (container->name) {
657 free(container->name);
658 }
659 container->name = strdup(new_title);
660 container_calculate_title_height(container);
661 container_update_title_textures(container);
662 container_notify_child_title_changed(container->parent);
663
664 size_t prev_max_height = config->font_height;
665 config_find_font_height(false);
666 if (config->font_height != prev_max_height) {
667 arrange_root();
668 }
669}