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);

View file

@ -19,7 +19,7 @@ _create_buffer(struct scaled_scene_buffer *scaled_buffer, double scale)
/* Buffer gets free'd automatically along the backing wlr_buffer */
font_buffer_create(&buffer, self->max_width, self->text,
&self->font, self->color, scale);
&self->font, self->color, self->arrow, scale);
self->width = buffer ? buffer->unscaled_width : 0;
self->height = buffer ? buffer->unscaled_height : 0;
@ -36,6 +36,7 @@ _destroy(struct scaled_scene_buffer *scaled_buffer)
if (self->font.name) {
zfree(self->font.name);
}
zfree(self->arrow);
zfree(scaled_buffer->data);
}
@ -68,8 +69,9 @@ scaled_font_buffer_create(struct wlr_scene_tree *parent)
}
void
scaled_font_buffer_update(struct scaled_font_buffer *self,
const char *text, int max_width, struct font *font, float *color)
scaled_font_buffer_update(struct scaled_font_buffer *self, const char *text,
int max_width, struct font *font, float *color,
const char *arrow)
{
assert(self);
assert(text);
@ -83,6 +85,7 @@ scaled_font_buffer_update(struct scaled_font_buffer *self,
if (self->font.name) {
zfree(self->font.name);
}
zfree(self->arrow);
/* Update internal state */
self->text = strdup(text);
@ -92,6 +95,7 @@ scaled_font_buffer_update(struct scaled_font_buffer *self,
}
self->font.size = font->size;
memcpy(self->color, color, sizeof(self->color));
self->arrow = arrow ? strdup(arrow) : NULL;
/* Invalidate cache and force a new render */
scaled_scene_buffer_invalidate_cache(self->scaled_buffer);