menu: render submenu arrows

This commit is contained in:
Johan Malm 2022-08-02 22:00:24 +01:00 committed by Consolatis
parent 6a750d465e
commit 429df42a8f
6 changed files with 70 additions and 34 deletions

View file

@ -1,4 +1,5 @@
// SPDX-License-Identifier: GPL-2.0-only
#include <assert.h>
#include <cairo.h>
#include <drm_fourcc.h>
#include <pango/pangocairo.h>
@ -12,7 +13,10 @@
static PangoRectangle
font_extents(struct font *font, const char *string)
{
PangoRectangle rect;
PangoRectangle rect = { 0 };
if (!string) {
return rect;
}
cairo_surface_t *surface;
cairo_t *c;
PangoLayout *layout;
@ -58,20 +62,37 @@ font_width(struct font *font, const char *string)
void
font_buffer_create(struct lab_data_buffer **buffer, int max_width,
const char *text, struct font *font, float *color, double scale)
const char *text, struct font *font, float *color, const char *arrow,
double scale)
{
/* Allow a minimum of one pixel each for text and arrow */
if (max_width < 2) {
max_width = 2;
}
if (!text || !*text) {
return;
}
PangoRectangle rect = font_extents(font, text);
if (max_width && rect.width > max_width) {
rect.width = max_width;
PangoRectangle text_extents = font_extents(font, text);
PangoRectangle arrow_extents = font_extents(font, arrow);
if (arrow) {
if (arrow_extents.width >= max_width - 1) {
/* It would be weird to get here, but just in case */
arrow_extents.width = max_width - 1;
text_extents.width = 1;
} else {
text_extents.width = max_width - arrow_extents.width;
}
} else if (text_extents.width > max_width) {
text_extents.width = max_width;
}
*buffer = buffer_create_cairo(rect.width, rect.height, scale, true);
*buffer = buffer_create_cairo(text_extents.width + arrow_extents.width,
text_extents.height, scale, true);
if (!*buffer) {
wlr_log(WLR_ERROR, "Failed to create font buffer of size %dx%d",
rect.width, rect.height);
wlr_log(WLR_ERROR, "Failed to create font buffer");
return;
}
@ -82,7 +103,7 @@ font_buffer_create(struct lab_data_buffer **buffer, int max_width,
cairo_move_to(cairo, 0, 0);
PangoLayout *layout = pango_cairo_create_layout(cairo);
pango_layout_set_width(layout, rect.width * PANGO_SCALE);
pango_layout_set_width(layout, text_extents.width * PANGO_SCALE);
pango_layout_set_text(layout, text, -1);
pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END);
@ -92,8 +113,15 @@ font_buffer_create(struct lab_data_buffer **buffer, int max_width,
pango_layout_set_font_description(layout, desc);
pango_font_description_free(desc);
pango_cairo_update_layout(cairo, layout);
pango_cairo_show_layout(cairo, layout);
if (arrow) {
cairo_move_to(cairo, text_extents.width, 0);
pango_layout_set_width(layout, arrow_extents.width * PANGO_SCALE);
pango_layout_set_text(layout, arrow, -1);
pango_cairo_show_layout(cairo, layout);
}
g_object_unref(layout);
cairo_surface_flush(surf);