aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/sway/config.h9
-rw-r--r--include/sway/tree/container.h9
-rw-r--r--sway/commands/font.c6
-rw-r--r--sway/config.c30
-rw-r--r--sway/tree/container.c53
5 files changed, 91 insertions, 16 deletions
diff --git a/include/sway/config.h b/include/sway/config.h
index 02ae3b63..345f26a0 100644
--- a/include/sway/config.h
+++ b/include/sway/config.h
@@ -304,7 +304,7 @@ struct sway_config {
304 enum sway_container_layout default_orientation; 304 enum sway_container_layout default_orientation;
305 enum sway_container_layout default_layout; 305 enum sway_container_layout default_layout;
306 char *font; 306 char *font;
307 int font_height; 307 size_t font_height;
308 308
309 // Flags 309 // Flags
310 bool focus_follows_mouse; 310 bool focus_follows_mouse;
@@ -461,7 +461,12 @@ struct bar_config *default_bar_config(void);
461 461
462void free_bar_config(struct bar_config *bar); 462void free_bar_config(struct bar_config *bar);
463 463
464int get_font_text_height(char *font); 464/**
465 * Updates the value of config->font_height based on the max title height
466 * reported by each container. If recalculate is true, the containers will
467 * recalculate their heights before reporting.
468 */
469void config_find_font_height(bool recalculate);
465 470
466/* Global config singleton. */ 471/* Global config singleton. */
467extern struct sway_config *config; 472extern struct sway_config *config;
diff --git a/include/sway/tree/container.h b/include/sway/tree/container.h
index b508f310..b07af72c 100644
--- a/include/sway/tree/container.h
+++ b/include/sway/tree/container.h
@@ -89,6 +89,7 @@ struct sway_container {
89 struct wlr_texture *title_focused_inactive; 89 struct wlr_texture *title_focused_inactive;
90 struct wlr_texture *title_unfocused; 90 struct wlr_texture *title_unfocused;
91 struct wlr_texture *title_urgent; 91 struct wlr_texture *title_urgent;
92 size_t title_height;
92 93
93 struct { 94 struct {
94 struct wl_signal destroy; 95 struct wl_signal destroy;
@@ -198,4 +199,12 @@ struct sway_container *container_flatten(struct sway_container *container);
198 199
199void container_update_title_textures(struct sway_container *container); 200void container_update_title_textures(struct sway_container *container);
200 201
202/**
203 * Calculate the container's title_height property.
204 */
205void container_calculate_title_height(struct sway_container *container);
206
207void container_update_title(struct sway_container *container,
208 const char *new_title);
209
201#endif 210#endif
diff --git a/sway/commands/font.c b/sway/commands/font.c
index 96127055..38ad8880 100644
--- a/sway/commands/font.c
+++ b/sway/commands/font.c
@@ -2,6 +2,7 @@
2#include <string.h> 2#include <string.h>
3#include "sway/commands.h" 3#include "sway/commands.h"
4#include "sway/config.h" 4#include "sway/config.h"
5#include "sway/tree/arrange.h"
5#include "log.h" 6#include "log.h"
6#include "stringop.h" 7#include "stringop.h"
7 8
@@ -13,6 +14,9 @@ struct cmd_results *cmd_font(int argc, char **argv) {
13 char *font = join_args(argv, argc); 14 char *font = join_args(argv, argc);
14 free(config->font); 15 free(config->font);
15 config->font = strdup(font); 16 config->font = strdup(font);
16 config->font_height = get_font_text_height(font); 17 config_find_font_height(true);
18 if (!config->reading) {
19 arrange_root();
20 }
17 return cmd_results_new(CMD_SUCCESS, NULL, NULL); 21 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
18} 22}
diff --git a/sway/config.c b/sway/config.c
index 60b62bbc..0ad9c3a2 100644
--- a/sway/config.c
+++ b/sway/config.c
@@ -132,17 +132,6 @@ static void destroy_removed_seats(struct sway_config *old_config,
132 } 132 }
133} 133}
134 134
135int get_font_text_height(char *font) {
136 cairo_t *cairo = cairo_create(NULL);
137 int text_height;
138 get_text_size(cairo, font, NULL, &text_height, 1, false,
139 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
140 "abcdefghijklmnopqrstuvwxyz"
141 "!@#$%^&*([{|");
142 cairo_destroy(cairo);
143 return text_height;
144}
145
146static void set_color(float dest[static 4], uint32_t color) { 135static void set_color(float dest[static 4], uint32_t color) {
147 dest[0] = ((color >> 16) & 0xff) / 255.0; 136 dest[0] = ((color >> 16) & 0xff) / 255.0;
148 dest[1] = ((color >> 8) & 0xff) / 255.0; 137 dest[1] = ((color >> 8) & 0xff) / 255.0;
@@ -182,7 +171,7 @@ static void config_defaults(struct sway_config *config) {
182 config->default_layout = L_NONE; 171 config->default_layout = L_NONE;
183 config->default_orientation = L_NONE; 172 config->default_orientation = L_NONE;
184 if (!(config->font = strdup("monospace 10"))) goto cleanup; 173 if (!(config->font = strdup("monospace 10"))) goto cleanup;
185 config->font_height = get_font_text_height(config->font); 174 config->font_height = 0;
186 175
187 // floating view 176 // floating view
188 config->floating_maximum_width = 0; 177 config->floating_maximum_width = 0;
@@ -740,3 +729,20 @@ int workspace_output_cmp_workspace(const void *a, const void *b) {
740 const struct workspace_output *wsa = a, *wsb = b; 729 const struct workspace_output *wsa = a, *wsb = b;
741 return lenient_strcmp(wsa->workspace, wsb->workspace); 730 return lenient_strcmp(wsa->workspace, wsb->workspace);
742} 731}
732
733static void find_font_height_iterator(struct sway_container *container,
734 void *data) {
735 bool *recalculate = data;
736 if (*recalculate) {
737 container_calculate_title_height(container);
738 }
739 if (container->title_height > config->font_height) {
740 config->font_height = container->title_height;
741 }
742}
743
744void config_find_font_height(bool recalculate) {
745 config->font_height = 0;
746 container_for_each_descendant_dfs(&root_container,
747 find_font_height_iterator, &recalculate);
748}
diff --git a/sway/tree/container.c b/sway/tree/container.c
index b33985af..d19f13ae 100644
--- a/sway/tree/container.c
+++ b/sway/tree/container.c
@@ -348,7 +348,7 @@ struct sway_container *container_view_create(struct sway_container *sibling,
348 swayc, title, sibling, sibling ? sibling->type : 0, sibling->name); 348 swayc, title, sibling, sibling ? sibling->type : 0, sibling->name);
349 // Setup values 349 // Setup values
350 swayc->sway_view = sway_view; 350 swayc->sway_view = sway_view;
351 swayc->name = title ? strdup(title) : NULL; 351 container_update_title(swayc, title);
352 swayc->width = 0; 352 swayc->width = 0;
353 swayc->height = 0; 353 swayc->height = 0;
354 354
@@ -611,3 +611,54 @@ void container_update_title_textures(struct sway_container *container) {
611 update_title_texture(container, &container->title_urgent, 611 update_title_texture(container, &container->title_urgent,
612 &config->border_colors.urgent); 612 &config->border_colors.urgent);
613} 613}
614
615void container_calculate_title_height(struct sway_container *container) {
616 if (!container->name) {
617 container->title_height = 0;
618 return;
619 }
620 cairo_t *cairo = cairo_create(NULL);
621 int height;
622 get_text_size(cairo, config->font, NULL, &height, 1, false,
623 "%s", container->name);
624 cairo_destroy(cairo);
625 container->title_height = height;
626}
627
628static void container_notify_child_title_changed(
629 struct sway_container *container) {
630 if (!container || container->type != C_CONTAINER) {
631 return;
632 }
633 if (container->layout != L_TABBED && container->layout != L_STACKED) {
634 return;
635 }
636 if (container->name) {
637 free(container->name);
638 }
639 // TODO: iterate children and concatenate their titles
640 container->name = strdup("");
641 container_calculate_title_height(container);
642 container_update_title_textures(container);
643 container_notify_child_title_changed(container->parent);
644}
645
646void container_update_title(struct sway_container *container,
647 const char *new_title) {
648 if (container->name && strcmp(container->name, new_title) == 0) {
649 return;
650 }
651 if (container->name) {
652 free(container->name);
653 }
654 container->name = strdup(new_title);
655 container_calculate_title_height(container);
656 container_update_title_textures(container);
657 container_notify_child_title_changed(container->parent);
658
659 size_t prev_max_height = config->font_height;
660 config_find_font_height(false);
661 if (config->font_height != prev_max_height) {
662 arrange_root();
663 }
664}