mirror of
https://github.com/swaywm/sway.git
synced 2026-04-30 06:46:24 -04:00
Align titles to baseline
This does the following: * Adds a baseline argument to get_text_size (the baseline is the distance from the top of the texture to the baseline). * Stores the baseline in the container when calculating the title height. * Takes the baseline into account when calculating the config's max font height. * When rendering, pads the textures according to the baseline so they line up.
This commit is contained in:
parent
9226aad58c
commit
9215ca0f01
11 changed files with 49 additions and 32 deletions
|
|
@ -807,22 +807,31 @@ int workspace_output_cmp_workspace(const void *a, const void *b) {
|
|||
return lenient_strcmp(wsa->workspace, wsb->workspace);
|
||||
}
|
||||
|
||||
static void find_font_height_iterator(struct sway_container *container,
|
||||
void *data) {
|
||||
static void find_font_height_iterator(struct sway_container *con, void *data) {
|
||||
size_t amount_below_baseline = con->title_height - con->title_baseline;
|
||||
size_t extended_height = config->font_baseline + amount_below_baseline;
|
||||
if (extended_height > config->font_height) {
|
||||
config->font_height = extended_height;
|
||||
}
|
||||
}
|
||||
|
||||
static void find_baseline_iterator(struct sway_container *con, void *data) {
|
||||
bool *recalculate = data;
|
||||
if (*recalculate) {
|
||||
container_calculate_title_height(container);
|
||||
container_calculate_title_height(con);
|
||||
}
|
||||
if (container->title_height > config->font_height) {
|
||||
config->font_height = container->title_height;
|
||||
if (con->title_baseline > config->font_baseline) {
|
||||
config->font_baseline = con->title_baseline;
|
||||
}
|
||||
}
|
||||
|
||||
void config_update_font_height(bool recalculate) {
|
||||
size_t prev_max_height = config->font_height;
|
||||
config->font_height = 0;
|
||||
config->font_baseline = 0;
|
||||
|
||||
root_for_each_container(find_font_height_iterator, &recalculate);
|
||||
root_for_each_container(find_baseline_iterator, &recalculate);
|
||||
root_for_each_container(find_font_height_iterator, NULL);
|
||||
|
||||
if (config->font_height != prev_max_height) {
|
||||
arrange_root();
|
||||
|
|
|
|||
|
|
@ -84,7 +84,7 @@ static int draw_node(cairo_t *cairo, struct sway_node *node,
|
|||
struct sway_node *focus, int x, int y) {
|
||||
int text_width, text_height;
|
||||
char *buffer = get_string(node);
|
||||
get_text_size(cairo, "monospace", &text_width, &text_height,
|
||||
get_text_size(cairo, "monospace", &text_width, &text_height, NULL,
|
||||
1, false, buffer);
|
||||
cairo_save(cairo);
|
||||
cairo_rectangle(cairo, x + 2, y, text_width - 2, text_height);
|
||||
|
|
|
|||
|
|
@ -466,12 +466,12 @@ static void render_titlebar(struct sway_output *output,
|
|||
&texture_box.width, &texture_box.height);
|
||||
title_ob_width = texture_box.width;
|
||||
|
||||
// The title texture might be shorter than the config->font_height, in
|
||||
// which case we need to pad it as evenly as possible above and below.
|
||||
int ob_padding_total = config->font_height * output_scale -
|
||||
texture_box.height;
|
||||
int ob_padding_above = floor(ob_padding_total / 2);
|
||||
int ob_padding_below = ceil(ob_padding_total / 2);
|
||||
// The title texture might be shorter than the config->font_height,
|
||||
// in which case we need to pad it above and below.
|
||||
int ob_padding_above = (config->font_baseline - con->title_baseline)
|
||||
* output_scale;
|
||||
int ob_padding_below = (config->font_height - con->title_height)
|
||||
* output_scale - ob_padding_above;
|
||||
|
||||
// Render texture
|
||||
texture_box.x = (x - output_x + TITLEBAR_H_PADDING) * output_scale;
|
||||
|
|
|
|||
|
|
@ -455,8 +455,8 @@ static void update_title_texture(struct sway_container *con,
|
|||
int height = con->title_height * scale;
|
||||
|
||||
cairo_t *c = cairo_create(NULL);
|
||||
get_text_size(c, config->font, &width, NULL, scale, config->pango_markup,
|
||||
"%s", con->formatted_title);
|
||||
get_text_size(c, config->font, &width, NULL, NULL, scale,
|
||||
config->pango_markup, "%s", con->formatted_title);
|
||||
cairo_destroy(c);
|
||||
|
||||
cairo_surface_t *surface = cairo_image_surface_create(
|
||||
|
|
@ -505,10 +505,12 @@ void container_calculate_title_height(struct sway_container *container) {
|
|||
}
|
||||
cairo_t *cairo = cairo_create(NULL);
|
||||
int height;
|
||||
get_text_size(cairo, config->font, NULL, &height, 1, config->pango_markup,
|
||||
"%s", container->formatted_title);
|
||||
int baseline;
|
||||
get_text_size(cairo, config->font, NULL, &height, &baseline, 1,
|
||||
config->pango_markup, "%s", container->formatted_title);
|
||||
cairo_destroy(cairo);
|
||||
container->title_height = height;
|
||||
container->title_baseline = baseline;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -935,7 +935,8 @@ static void update_marks_texture(struct sway_view *view,
|
|||
int height = view->container->title_height * scale;
|
||||
|
||||
cairo_t *c = cairo_create(NULL);
|
||||
get_text_size(c, config->font, &width, NULL, scale, false, "%s", buffer);
|
||||
get_text_size(c, config->font, &width, NULL, NULL, scale, false,
|
||||
"%s", buffer);
|
||||
cairo_destroy(c);
|
||||
|
||||
cairo_surface_t *surface = cairo_image_surface_create(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue