From dfa1dcc6218af13eb3cf328b6ac6ccb86f1a961c Mon Sep 17 00:00:00 2001 From: Hans de Goede Date: Wed, 6 Feb 2019 11:48:55 +0100 Subject: [PATCH] ply-label: Make sure get_width_of_control / get_height_of_control return correct values Users of ply_label may want to know the height / width of the text before calling ply_label_show, so that they can e.g. vertically align it. This commit adds a size_needs_update bool to the label plugin and uses this to check if executing size_control is necessary before returning the width / height and also modifies the ply-label code to load the plugin from its get_width / get_height methods. As an added advantage this will also skip unnecessary size_control calls when calling ply_label_show on an already visible label. Signed-off-by: Hans de Goede --- src/libply-splash-graphics/ply-label.c | 6 ++-- src/plugins/controls/label/plugin.c | 47 +++++++++++++++----------- 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/src/libply-splash-graphics/ply-label.c b/src/libply-splash-graphics/ply-label.c index cfc8098..42d9dfb 100644 --- a/src/libply-splash-graphics/ply-label.c +++ b/src/libply-splash-graphics/ply-label.c @@ -299,7 +299,8 @@ long ply_label_get_width (ply_label_t *label) { if (label->plugin_interface == NULL) - return 0; + if (!ply_label_load_plugin (label)) + return 0; return label->plugin_interface->get_width_of_control (label->control); } @@ -308,7 +309,8 @@ long ply_label_get_height (ply_label_t *label) { if (label->plugin_interface == NULL) - return 0; + if (!ply_label_load_plugin (label)) + return 0; return label->plugin_interface->get_height_of_control (label->control); } diff --git a/src/plugins/controls/label/plugin.c b/src/plugins/controls/label/plugin.c index acba52b..b552981 100644 --- a/src/plugins/controls/label/plugin.c +++ b/src/plugins/controls/label/plugin.c @@ -66,6 +66,7 @@ struct _ply_label_plugin_control float alpha; uint32_t is_hidden : 1; + uint32_t needs_size_update : 1; }; ply_label_plugin_interface_t *ply_label_plugin_get_interface (void); @@ -93,18 +94,6 @@ destroy_control (ply_label_plugin_control_t *label) free (label); } -static long -get_width_of_control (ply_label_plugin_control_t *label) -{ - return label->area.width; -} - -static long -get_height_of_control (ply_label_plugin_control_t *label) -{ - return label->area.height; -} - static cairo_t * get_cairo_context_for_pixel_buffer (ply_label_plugin_control_t *label, ply_pixel_buffer_t *pixel_buffer) @@ -175,15 +164,20 @@ init_pango_text_layout (cairo_t *cairo_context, } static void -size_control (ply_label_plugin_control_t *label) +size_control (ply_label_plugin_control_t *label, bool force) { cairo_t *cairo_context; PangoLayout *pango_layout; int text_width; int text_height; - if (label->is_hidden) + if (force && !label->needs_size_update) + return; /* Size already is up to date */ + + if (!force && label->is_hidden) { + label->needs_size_update = true; return; + } cairo_context = get_cairo_context_for_sizing (label); @@ -195,6 +189,7 @@ size_control (ply_label_plugin_control_t *label) g_object_unref (pango_layout); cairo_destroy (cairo_context); + label->needs_size_update = false; } static void @@ -261,7 +256,7 @@ set_alignment_for_control (ply_label_plugin_control_t *label, if (label->alignment != pango_alignment) { dirty_area = label->area; label->alignment = pango_alignment; - size_control (label); + size_control (label, false); if (!label->is_hidden && label->display != NULL) ply_pixel_display_draw_area (label->display, dirty_area.x, dirty_area.y, @@ -278,7 +273,7 @@ set_width_for_control (ply_label_plugin_control_t *label, if (label->width != width) { dirty_area = label->area; label->width = width; - size_control (label); + size_control (label, false); if (!label->is_hidden && label->display != NULL) ply_pixel_display_draw_area (label->display, dirty_area.x, dirty_area.y, @@ -296,7 +291,7 @@ set_text_for_control (ply_label_plugin_control_t *label, dirty_area = label->area; free (label->text); label->text = strdup (text); - size_control (label); + size_control (label, false); if (!label->is_hidden && label->display != NULL) ply_pixel_display_draw_area (label->display, dirty_area.x, dirty_area.y, @@ -317,7 +312,7 @@ set_font_for_control (ply_label_plugin_control_t *label, label->fontdesc = strdup (fontdesc); else label->fontdesc = NULL; - size_control (label); + size_control (label, false); if (!label->is_hidden && label->display != NULL) ply_pixel_display_draw_area (label->display, dirty_area.x, dirty_area.y, @@ -358,7 +353,7 @@ show_control (ply_label_plugin_control_t *label, label->is_hidden = false; - size_control (label); + size_control (label, true); if (!label->is_hidden && label->display != NULL) ply_pixel_display_draw_area (label->display, @@ -389,6 +384,20 @@ is_control_hidden (ply_label_plugin_control_t *label) return label->is_hidden; } +static long +get_width_of_control (ply_label_plugin_control_t *label) +{ + size_control (label, true); + return label->area.width; +} + +static long +get_height_of_control (ply_label_plugin_control_t *label) +{ + size_control (label, true); + return label->area.height; +} + ply_label_plugin_interface_t * ply_label_plugin_get_interface (void) {