Ellipsize titles that are too large in the bar

When titles were very large they would overlap the status line.
Ellipsize the title to get it to fit.
This commit is contained in:
Pedro Côrte-Real 2019-08-17 20:59:07 +01:00
parent 8a019a83c9
commit fc19355327
3 changed files with 38 additions and 8 deletions

View file

@ -136,3 +136,33 @@ void pango_printf(cairo_t *cairo, const char *font,
g_object_unref(layout);
free(buf);
}
void pango_printf_ellipsized(cairo_t *cairo, const char *font,
double scale, bool markup, int max_width, const char *fmt, ...) {
va_list args;
va_start(args, fmt);
// Add one since vsnprintf excludes null terminator.
int length = vsnprintf(NULL, 0, fmt, args) + 1;
va_end(args);
char *buf = malloc(length);
if (buf == NULL) {
sway_log(SWAY_ERROR, "Failed to allocate memory");
return;
}
va_start(args, fmt);
vsnprintf(buf, length, fmt, args);
va_end(args);
PangoLayout *layout = get_pango_layout(cairo, font, buf, scale, markup);
pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END);
pango_layout_set_width(layout, pango_units_from_double(max_width));
cairo_font_options_t *fo = cairo_font_options_create();
cairo_get_font_options(cairo, fo);
pango_cairo_context_set_font_options(pango_layout_get_context(layout), fo);
cairo_font_options_destroy(fo);
pango_cairo_update_layout(cairo, layout);
pango_cairo_show_layout(cairo, layout);
g_object_unref(layout);
free(buf);
}