[gnome-builder] widget: add gb_widget_activate_action()



commit 5a2d715a2e81fa12795dba99d97c105355588efd
Author: Christian Hergert <christian hergert me>
Date:   Sun Jan 18 17:28:15 2015 -0800

    widget: add gb_widget_activate_action()
    
    This is a helper to activate an action based on what the action sees as
    available actions, working it's way up to the toplevel widget.
    
    If "win" is specified, and no other groups overrode "win", then the
    toplevel will be used.
    
    If "app" is specified, and no other groups override "app", then the
    default GApplication will be used.

 src/util/gb-widget.c |   47 +++++++++++++++++++++++++++++++++++++++++++++++
 src/util/gb-widget.h |    4 ++++
 2 files changed, 51 insertions(+), 0 deletions(-)
---
diff --git a/src/util/gb-widget.c b/src/util/gb-widget.c
index b6d2793..df304d9 100644
--- a/src/util/gb-widget.c
+++ b/src/util/gb-widget.c
@@ -209,3 +209,50 @@ gb_widget_fade_show (GtkWidget *widget)
                               NULL);
     }
 }
+
+void
+gb_widget_activate_action (GtkWidget   *widget,
+                           const gchar *prefix,
+                           const gchar *action_name,
+                           GVariant    *parameter)
+{
+  GApplication *app;
+  GtkWidget *toplevel;
+  GActionGroup *group = NULL;
+
+  g_return_if_fail (GTK_IS_WIDGET (widget));
+  g_return_if_fail (prefix);
+  g_return_if_fail (action_name);
+
+  app = g_application_get_default ();
+  toplevel = gtk_widget_get_toplevel (widget);
+
+  while ((group == NULL) && (widget != NULL))
+    {
+      group = gtk_widget_get_action_group (widget, prefix);
+      widget = gtk_widget_get_parent (widget);
+    }
+
+  if (!group && g_str_equal (prefix, "win") && G_IS_ACTION_GROUP (toplevel))
+    group = G_ACTION_GROUP (toplevel);
+
+  if (!group && g_str_equal (prefix, "app") && G_IS_ACTION_GROUP (app))
+    group = G_ACTION_GROUP (app);
+
+  if (group)
+    {
+      if (g_action_group_has_action (group, action_name))
+        {
+          g_action_group_activate_action (group, action_name, parameter);
+          return;
+        }
+    }
+
+  if (g_variant_is_floating (parameter))
+    {
+      parameter = g_variant_ref_sink (parameter);
+      g_variant_unref (parameter);
+    }
+
+  g_warning ("Failed to resolve action %s.%s", prefix, action_name);
+}
diff --git a/src/util/gb-widget.h b/src/util/gb-widget.h
index cfda6c3..5bca324 100644
--- a/src/util/gb-widget.h
+++ b/src/util/gb-widget.h
@@ -42,6 +42,10 @@ cairo_surface_t *gb_widget_snapshot        (GtkWidget    *widget,
 GbWorkbench     *gb_widget_get_workbench   (GtkWidget    *widget);
 void             gb_widget_fade_hide       (GtkWidget    *widget);
 void             gb_widget_fade_show       (GtkWidget    *widget);
+void             gb_widget_activate_action (GtkWidget    *widget,
+                                            const gchar  *prefix,
+                                            const gchar  *action_name,
+                                            GVariant     *parameter);
 
 G_END_DECLS
 


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