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. * in the widgets instance initializer.
*/ */
void void
gtk_widget_class_set_template (GtkWidgetClass *widget_class, gtk_widget_class_set_template (GtkWidgetClass *widget_class,
GBytes *template_bytes) GBytes *template_bytes)
{ {
GError *error = NULL;
GBytes *data = NULL; GBytes *data = NULL;
gconstpointer bytes_data;
gsize bytes_size;
g_return_if_fail (GTK_IS_WIDGET_CLASS (widget_class)); g_return_if_fail (GTK_IS_WIDGET_CLASS (widget_class));
g_return_if_fail (widget_class->priv->template == NULL); g_return_if_fail (widget_class->priv->template == NULL);
g_return_if_fail (template_bytes != NULL); g_return_if_fail (template_bytes != NULL);
widget_class->priv->template = g_slice_new0 (GtkWidgetTemplate); 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; widget_class->priv->template->data = g_bytes_ref (template_bytes);
return;
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);
}
} }
if (data) data = _gtk_buildable_parser_precompile (bytes_data, bytes_size, &error);
widget_class->priv->template->data = data; if (data == NULL)
else {
widget_class->priv->template->data = g_bytes_ref (template_bytes); 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;
} }
/** /**