widget: Refactor gtk_widget_class_set_template()

Don't call gbytes_get_data/get_size so often and exit early if the given
bytes is already precompiled.
This commit is contained in:
Timm Bäder 2021-01-03 08:45:30 +01:00
parent c91b5eaa41
commit 699adb0b10

View File

@ -10852,35 +10852,36 @@ out:
* in the widgets instance initializer.
*/
void
gtk_widget_class_set_template (GtkWidgetClass *widget_class,
GBytes *template_bytes)
gtk_widget_class_set_template (GtkWidgetClass *widget_class,
GBytes *template_bytes)
{
GError *error = NULL;
GBytes *data = NULL;
gconstpointer bytes_data;
gsize bytes_size;
g_return_if_fail (GTK_IS_WIDGET_CLASS (widget_class));
g_return_if_fail (widget_class->priv->template == NULL);
g_return_if_fail (template_bytes != NULL);
widget_class->priv->template = g_slice_new0 (GtkWidgetTemplate);
bytes_data = g_bytes_get_data (template_bytes, &bytes_size);
if (!_gtk_buildable_parser_is_precompiled (g_bytes_get_data (template_bytes, NULL), g_bytes_get_size (template_bytes)))
if (_gtk_buildable_parser_is_precompiled (bytes_data, bytes_size))
{
GError *error = NULL;
data = _gtk_buildable_parser_precompile (g_bytes_get_data (template_bytes, NULL),
g_bytes_get_size (template_bytes),
&error);
if (data == NULL)
{
g_warning ("Failed to precompile template for class %s: %s", G_OBJECT_CLASS_NAME (widget_class), error->message);
g_error_free (error);
}
widget_class->priv->template->data = g_bytes_ref (template_bytes);
return;
}
if (data)
widget_class->priv->template->data = data;
else
widget_class->priv->template->data = g_bytes_ref (template_bytes);
data = _gtk_buildable_parser_precompile (bytes_data, bytes_size, &error);
if (data == NULL)
{
g_warning ("Failed to precompile template for class %s: %s", G_OBJECT_CLASS_NAME (widget_class), error->message);
g_error_free (error);
return;
}
widget_class->priv->template->data = data;
}
/**