[gtk/widget-class-actions: 17/22] Do away with the query callback



commit 04ba75a87459caa173ebd20a957ade53092e702f
Author: Matthias Clasen <mclasen redhat com>
Date:   Tue Jun 18 11:27:19 2019 +0000

    Do away with the query callback
    
    We take the parameter type and store it in the
    actions array, since it is constant, and we can
    make GtkActionMuxer keep the enabled state. This
    lets us do away with the query callback.
    
    Also simplify the state query callback
    to ignore state hint and state type and return
    just the state.

 gtk/gtkactionmuxer.c        | 75 +++++++++++++++++++++++++++------------------
 gtk/gtkactionmuxerprivate.h |  9 +++---
 gtk/gtkwidget.c             | 22 ++++++-------
 gtk/gtkwidget.h             | 69 ++++++++++++-----------------------------
 4 files changed, 80 insertions(+), 95 deletions(-)
---
diff --git a/gtk/gtkactionmuxer.c b/gtk/gtkactionmuxer.c
index bcc3e3370a..8775519af5 100644
--- a/gtk/gtkactionmuxer.c
+++ b/gtk/gtkactionmuxer.c
@@ -73,6 +73,7 @@ struct _GtkActionMuxer
 
   GtkWidget *widget;
   GPtrArray *widget_actions;
+  gboolean *widget_actions_enabled;
 };
 
 G_DEFINE_TYPE_WITH_CODE (GtkActionMuxer, gtk_action_muxer, G_TYPE_OBJECT,
@@ -209,6 +210,19 @@ gtk_action_muxer_action_enabled_changed (GtkActionMuxer *muxer,
   Action *action;
   GSList *node;
 
+  if (muxer->widget_actions)
+    {
+      int i;
+      for (i = 0; i < muxer->widget_actions->len; i++)
+        {
+          GtkWidgetAction *a = g_ptr_array_index (muxer->widget_actions, i);
+          if (strcmp (a->name, action_name) == 0)
+            {
+              muxer->widget_actions_enabled[i] = enabled;
+              break;
+            }
+        }
+    }
   action = g_hash_table_lookup (muxer->observed_actions, action_name);
   for (node = action ? action->watchers : NULL; node; node = node->next)
     gtk_action_observer_action_enabled_changed (node->data, GTK_ACTION_OBSERVABLE (muxer), action_name, 
enabled);
@@ -428,39 +442,32 @@ gtk_action_muxer_query_action (GActionGroup        *action_group,
           GtkWidgetAction *action = g_ptr_array_index (muxer->widget_actions, i);
           if (strcmp (action->name, action_name) == 0)
             {
-              if (action->query)
+              if (enabled)
+                *enabled = muxer->widget_actions_enabled[i];
+              if (parameter_type)
+                *parameter_type = action->parameter_type;
+
+              if (state_hint)
+                *state_hint = NULL;
+              if (state_type)
+                *state_type = NULL;
+              if (state)
+                *state = NULL;
+
+              if (action->get_state)
                 {
-                  action->query (muxer->widget,
-                                 action->name,
-                                 enabled,
-                                 parameter_type);
-                }
-              else
-                {
-                  if (enabled)
-                    *enabled = TRUE;
-                  if (parameter_type)
-                    *parameter_type = NULL;
-                }
+                  GVariant *s;
+
+                  s = g_variant_ref_sink (action->get_state (muxer->widget, action->name));
 
-              if (action->query_state)
-                {
-                  action->query_state (muxer->widget,
-                                       action->name,
-                                       state_type,
-                                       state_hint,
-                                       state);
-                }
-              else
-                {
                   if (state_type)
-                    *state_type = NULL;
-                  if (state_hint)
-                    *state_hint = NULL;
+                    *state_type = g_variant_get_type (s);
                   if (state)
-                    *state = NULL;
-                }
+                    *state = g_variant_ref (s);
 
+                  g_variant_unref (s);
+                }
+         
               return TRUE;
             }
        }
@@ -531,8 +538,8 @@ gtk_action_muxer_change_action_state (GActionGroup *action_group,
           GtkWidgetAction *action = g_ptr_array_index (muxer->widget_actions, i);
           if (strcmp (action->name, action_name) == 0)
             {
-              if (action->change_state)
-                action->change_state (muxer->widget, action->name, state);
+              if (action->set_state)
+                action->set_state (muxer->widget, action->name, state);
 
               return;
             }
@@ -727,6 +734,14 @@ gtk_action_muxer_set_property (GObject      *object,
 
     case PROP_WIDGET_ACTIONS:
       muxer->widget_actions = g_value_get_boxed (value);
+      if (muxer->widget_actions)
+        {
+          int i;
+
+          muxer->widget_actions_enabled = g_new (gboolean, muxer->widget_actions->len);
+          for (i = 0; i < muxer->widget_actions->len; i++)
+            muxer->widget_actions_enabled[i] = TRUE;
+        }
       break;
 
     default:
diff --git a/gtk/gtkactionmuxerprivate.h b/gtk/gtkactionmuxerprivate.h
index 33b96452d1..325c1f817a 100644
--- a/gtk/gtkactionmuxerprivate.h
+++ b/gtk/gtkactionmuxerprivate.h
@@ -34,10 +34,11 @@ G_BEGIN_DECLS
 typedef struct {
   char *name;
 
-  GtkWidgetActionActivateFunc    activate;
-  GtkWidgetActionQueryFunc       query;
-  GtkWidgetActionChangeStateFunc change_state;
-  GtkWidgetActionQueryStateFunc  query_state;
+  GVariantType *parameter_type;
+
+  GtkWidgetActionActivateFunc activate;
+  GtkWidgetActionSetStateFunc set_state;
+  GtkWidgetActionGetStateFunc get_state;
 } GtkWidgetAction;
 
 typedef struct _GtkActionMuxer                              GtkActionMuxer;
diff --git a/gtk/gtkwidget.c b/gtk/gtkwidget.c
index 14073f1df9..bda4278d14 100644
--- a/gtk/gtkwidget.c
+++ b/gtk/gtkwidget.c
@@ -13448,8 +13448,7 @@ gtk_widget_should_layout (GtkWidget *widget)
  * @widget_class: a #GtkWidgetClass
  * @action_name: a prefixed action name, such as "clipboard.paste"
  * @activate: callback to use when the action is activated
- * @query: (allow-none): callback to use when the action properties
-       are queried, or %NULL for always-enabled, parameterless actions
+ * @parameter_type: (allow-none): the parameter type, or %NULL
  *
  * This should be called at class initialization time to specify
  * actions to be added for all instances of this class.
@@ -13462,10 +13461,10 @@ void
 gtk_widget_class_install_action (GtkWidgetClass              *widget_class,
                                  const char                  *action_name,
                                  GtkWidgetActionActivateFunc  activate,
-                                 GtkWidgetActionQueryFunc     query)
+                                 const char                  *parameter_type)
 {
   gtk_widget_class_install_stateful_action (widget_class, action_name,
-                                            activate, query,
+                                            activate, parameter_type,
                                             NULL, NULL);
 }
 
@@ -13474,10 +13473,9 @@ gtk_widget_class_install_action (GtkWidgetClass              *widget_class,
  * @widget_class: a #GtkWidgetClass
  * @action_name: a prefixed action name, such as "clipboard.paste"
  * @activate: callback to use when the action is activated
+ * @parameter_type: (allow-none): the parameter type, or %NULL
  * @query: (allow-none): callback to use when the action properties
        are queried, or %NULL for always-enabled stateless actions
- * @change: (allow-none): callback to use when the action state is
- *     changed, or %NULL for stateless actions
  * @query_state: (allow-none): callback to use when the action state
        is queried, or %NULL for stateless actions
  *
@@ -13491,9 +13489,9 @@ void
 gtk_widget_class_install_stateful_action (GtkWidgetClass                 *widget_class,
                                           const char                     *action_name,
                                           GtkWidgetActionActivateFunc     activate,
-                                          GtkWidgetActionQueryFunc        query,
-                                          GtkWidgetActionChangeStateFunc  change_state,
-                                          GtkWidgetActionQueryStateFunc   query_state)
+                                          const char *parameter_type,
+                                          GtkWidgetActionSetStateFunc  set_state,
+                                          GtkWidgetActionGetStateFunc  get_state)
 {
   GtkWidgetClassPrivate *priv = widget_class->priv;
   GtkWidgetAction *action;
@@ -13521,9 +13519,9 @@ gtk_widget_class_install_stateful_action (GtkWidgetClass                 *widget
   action = g_new0 (GtkWidgetAction, 1);
   action->name = g_strdup (action_name);
   action->activate = activate;
-  action->query = query;
-  action->change_state = change_state;
-  action->query_state = query_state;
+  action->parameter_type = parameter_type ? g_variant_type_new (parameter_type) : NULL;
+  action->set_state = set_state;
+  action->get_state = get_state;
 
   GTK_NOTE(ACTIONS,
            g_message ("%sClass: Adding %s action\n",
diff --git a/gtk/gtkwidget.h b/gtk/gtkwidget.h
index a9e064c178..726ddbcdd4 100644
--- a/gtk/gtkwidget.h
+++ b/gtk/gtkwidget.h
@@ -1026,7 +1026,7 @@ gboolean                gtk_widget_should_layout        (GtkWidget   *widget);
 /**
  * GtkWidgetActionActivateFunc:
  * @widget: the widget to which the action belongs
- * @action_name: the (unprefixed) action name
+ * @action_name: the action name
  * @parameter: parameter for activation
  *
  * The type of the callback functions used for activating
@@ -1039,31 +1039,9 @@ typedef void (* GtkWidgetActionActivateFunc) (GtkWidget  *widget,
                                               GVariant   *parameter);
 
 /**
- * GtkWidgetActionQueryFunc:
+ * GtkWidgetActionGetStateFunc:
  * @widget: the widget to which the action belongs
- * @action_name: the (unprefixed) action name
- * @enabled: (out) (optional): return location for the enabled state
- * @parameter_type: (out) (optional): return location for the parameter type
- *
- * The type of the callback functions used to query
- * the enabledness and parameter type of actions installed with
- * gtk_widget_class_install_action().
- *
- * See the #GAction documentation for more details about the
- * meaning of these properties.
- */
-typedef void (* GtkWidgetActionQueryFunc) (GtkWidget           *widget,
-                                           const char          *action_name,
-                                           gboolean            *enabled,
-                                           const GVariantType **parameter_type);
-
-/**
- * GtkWidgetActionQueryStateFunc:
- * @widget: the widget to which the action belongs
- * @action_name: the (unprefixed) action name
- * @state_type: (out) (optional): return location for the state type
- * @state_hint: (out) (optional): return location for the state hint
- * @state: (out) (optional): return location for the state
+ * @action_name: the action name
  *
  * The type of the callback functions used to query the state
  * of stateful actions installed with gtk_widget_class_install_action().
@@ -1071,16 +1049,13 @@ typedef void (* GtkWidgetActionQueryFunc) (GtkWidget           *widget,
  * See the #GAction documentation for more details about the
  * meaning of these properties.
  */
-typedef void (* GtkWidgetActionQueryStateFunc) (GtkWidget           *widget,
-                                                const char          *action_name,
-                                                const GVariantType **state_type,
-                                                GVariant           **state_hint,
-                                                GVariant           **state);
+typedef GVariant * (* GtkWidgetActionGetStateFunc) (GtkWidget  *widget,
+                                                    const char *action_name);
 
 /**
- * GtkWidgetActionChangeStateFunc:
+ * GtkWidgetActionSetStateFunc:
  * @widget: the widget to which the action belongs
- * @action_name: the (unprefixed) action name
+ * @action_name: the action name
  * @state: the new state
  *
  * The type of the callback functions used to change the
@@ -1088,30 +1063,26 @@ typedef void (* GtkWidgetActionQueryStateFunc) (GtkWidget           *widget,
  *
  * The @state must match the @state_type of the action.
  *
- * Note that you can change the enabledness and state
- * of widget actions by other means, as long as you
- * emit the required #GActionGroup notification signals,
- * which can be done with GtkWidget convenience API.
  * This callback is used when the action state is
  * changed via the #GActionGroup API.
  */
-typedef void (*GtkWidgetActionChangeStateFunc) (GtkWidget  *widget,
-                                                const char *action_name,
-                                                GVariant   *state);
+typedef void (*GtkWidgetActionSetStateFunc) (GtkWidget  *widget,
+                                             const char *action_name,
+                                             GVariant   *state);
 
 GDK_AVAILABLE_IN_ALL
-void                    gtk_widget_class_install_action (GtkWidgetClass                *widget_class,
-                                                         const char                    *action_name,
-                                                         GtkWidgetActionActivateFunc    activate,
-                                                         GtkWidgetActionQueryFunc       query);
+void                    gtk_widget_class_install_action (GtkWidgetClass              *widget_class,
+                                                         const char                  *action_name,
+                                                         GtkWidgetActionActivateFunc  activate,
+                                                         const char                  *parameter_type);
 
 GDK_AVAILABLE_IN_ALL
-void                    gtk_widget_class_install_stateful_action (GtkWidgetClass                
*widget_class,
-                                                                  const char                    *action_name,
-                                                                  GtkWidgetActionActivateFunc    activate,
-                                                                  GtkWidgetActionQueryFunc       query,
-                                                                  GtkWidgetActionChangeStateFunc 
change_state,
-                                                                  GtkWidgetActionQueryStateFunc  
query_state);
+void                    gtk_widget_class_install_stateful_action (GtkWidgetClass              *widget_class,
+                                                                  const char                  *action_name,
+                                                                  GtkWidgetActionActivateFunc  activate,
+                                                                  const char                  
*parameter_type,
+                                                                  GtkWidgetActionSetStateFunc  set_state,
+                                                                  GtkWidgetActionGetStateFunc  get_state);
 
 GDK_AVAILABLE_IN_ALL
 void                    gtk_widget_notify_class_action_enabled (GtkWidget  *widget,


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