[tepl] Utils: add tepl_utils_binding_transform_func_smart_bool()



commit c92b03f68b75116dafb40b2ca9160d4e54df2595
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sat Apr 25 16:54:20 2020 +0200

    Utils: add tepl_utils_binding_transform_func_smart_bool()

 docs/reference/tepl-sections.txt |  1 +
 tepl/tepl-goto-line-bar.c        | 46 ++--------------------------
 tepl/tepl-utils.c                | 66 ++++++++++++++++++++++++++++++++++++++++
 tepl/tepl-utils.h                |  7 +++++
 4 files changed, 76 insertions(+), 44 deletions(-)
---
diff --git a/docs/reference/tepl-sections.txt b/docs/reference/tepl-sections.txt
index dc21387..104bcbb 100644
--- a/docs/reference/tepl-sections.txt
+++ b/docs/reference/tepl-sections.txt
@@ -491,6 +491,7 @@ tepl_utils_file_query_exists_async
 tepl_utils_file_query_exists_finish
 tepl_utils_create_close_button
 tepl_utils_show_warning_dialog
+tepl_utils_binding_transform_func_smart_bool
 </SECTION>
 
 <SECTION>
diff --git a/tepl/tepl-goto-line-bar.c b/tepl/tepl-goto-line-bar.c
index baa76c0..0dc37d1 100644
--- a/tepl/tepl-goto-line-bar.c
+++ b/tepl/tepl-goto-line-bar.c
@@ -283,48 +283,6 @@ tepl_goto_line_bar_grab_focus_to_entry (TeplGotoLineBar *bar)
        gtk_widget_grab_focus (GTK_WIDGET (bar->priv->entry));
 }
 
-static gboolean
-binding_transform_smart_bool (GBinding     *binding,
-                             const GValue *from_value,
-                             GValue       *to_value,
-                             gpointer      user_data)
-{
-       if (G_VALUE_TYPE (from_value) == G_TYPE_BOOLEAN &&
-           G_VALUE_TYPE (to_value) == G_TYPE_VARIANT)
-       {
-               gboolean bool_value;
-
-               bool_value = g_value_get_boolean (from_value);
-               g_value_set_variant (to_value, g_variant_new_boolean (bool_value));
-
-               return TRUE;
-       }
-       else if (G_VALUE_TYPE (from_value) == G_TYPE_VARIANT &&
-                G_VALUE_TYPE (to_value) == G_TYPE_BOOLEAN)
-       {
-               GVariant *variant_value;
-               gboolean bool_value;
-
-               variant_value = g_value_get_variant (from_value);
-               if (variant_value == NULL)
-               {
-                       return FALSE;
-               }
-
-               if (!g_variant_type_equal (g_variant_get_type (variant_value), G_VARIANT_TYPE_BOOLEAN))
-               {
-                       return FALSE;
-               }
-
-               bool_value = g_variant_get_boolean (variant_value);
-               g_value_set_boolean (to_value, bool_value);
-
-               return TRUE;
-       }
-
-       return FALSE;
-}
-
 void
 _tepl_goto_line_bar_bind_to_gaction_state (TeplGotoLineBar *bar,
                                           GAction         *action)
@@ -338,8 +296,8 @@ _tepl_goto_line_bar_bind_to_gaction_state (TeplGotoLineBar *bar,
                                             bar, "visible",
                                             G_BINDING_BIDIRECTIONAL |
                                             G_BINDING_SYNC_CREATE,
-                                            binding_transform_smart_bool,
-                                            binding_transform_smart_bool,
+                                            tepl_utils_binding_transform_func_smart_bool,
+                                            tepl_utils_binding_transform_func_smart_bool,
                                             NULL, NULL);
 
                bar->priv->bound_to_gaction_state = TRUE;
diff --git a/tepl/tepl-utils.c b/tepl/tepl-utils.c
index a2f06ab..1a6d84b 100644
--- a/tepl/tepl-utils.c
+++ b/tepl/tepl-utils.c
@@ -744,3 +744,69 @@ tepl_utils_show_warning_dialog (GtkWindow   *parent,
 
        gtk_widget_show (dialog);
 }
+
+/**
+ * tepl_utils_binding_transform_func_smart_bool:
+ * @binding: a #GBinding.
+ * @from_value: the #GValue containing the value to transform.
+ * @to_value: the #GValue in which to store the transformed value.
+ * @user_data: data passed to the transform function.
+ *
+ * A #GBindingTransformFunc to transform between these two #GValue types:
+ * - A #GValue of type #gboolean.
+ * - A #GValue of type #GVariant, with the #GVariant of type boolean.
+ *
+ * For convenience, this function works in both directions (hence the “smart”),
+ * it introspects the types of @from_value and @to_value.
+ *
+ * Note that if @from_value and @to_value are of the same #GValue type, this
+ * function won't work and you shouldn't use a custom #GBindingTransformFunc in
+ * the first place.
+ *
+ * Returns: %TRUE if the transformation was successful, and %FALSE otherwise.
+ * Since: 5.0
+ */
+gboolean
+tepl_utils_binding_transform_func_smart_bool (GBinding     *binding,
+                                             const GValue *from_value,
+                                             GValue       *to_value,
+                                             gpointer      user_data)
+{
+       g_return_val_if_fail (G_IS_VALUE (from_value), FALSE);
+       g_return_val_if_fail (G_IS_VALUE (to_value), FALSE);
+
+       if (G_VALUE_TYPE (from_value) == G_TYPE_BOOLEAN &&
+           G_VALUE_TYPE (to_value) == G_TYPE_VARIANT)
+       {
+               gboolean bool_value;
+
+               bool_value = g_value_get_boolean (from_value);
+               g_value_set_variant (to_value, g_variant_new_boolean (bool_value));
+
+               return TRUE;
+       }
+       else if (G_VALUE_TYPE (from_value) == G_TYPE_VARIANT &&
+                G_VALUE_TYPE (to_value) == G_TYPE_BOOLEAN)
+       {
+               GVariant *variant_value;
+               gboolean bool_value;
+
+               variant_value = g_value_get_variant (from_value);
+               if (variant_value == NULL)
+               {
+                       return FALSE;
+               }
+
+               if (!g_variant_type_equal (g_variant_get_type (variant_value), G_VARIANT_TYPE_BOOLEAN))
+               {
+                       return FALSE;
+               }
+
+               bool_value = g_variant_get_boolean (variant_value);
+               g_value_set_boolean (to_value, bool_value);
+
+               return TRUE;
+       }
+
+       return FALSE;
+}
diff --git a/tepl/tepl-utils.h b/tepl/tepl-utils.h
index 2ff26e0..e289e6b 100644
--- a/tepl/tepl-utils.h
+++ b/tepl/tepl-utils.h
@@ -82,6 +82,13 @@ void         tepl_utils_show_warning_dialog                  (GtkWindow   *parent,
                                                                 const gchar *format,
                                                                 ...) G_GNUC_PRINTF(2, 3);
 
+/* Other */
+
+gboolean       tepl_utils_binding_transform_func_smart_bool    (GBinding     *binding,
+                                                                const GValue *from_value,
+                                                                GValue       *to_value,
+                                                                gpointer      user_data);
+
 G_END_DECLS
 
 #endif /* TEPL_UTILS_H */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]