[gnome-builder] run-manager: Switch to using actual GAction instances



commit cd8e4871806cb416827fa1bcdb19f9c9b2e83640
Author: Matthew Leeds <mleeds redhat com>
Date:   Thu Apr 20 23:31:05 2017 -0500

    run-manager: Switch to using actual GAction instances
    
    IdeRunManager implements the ActionGroup interface but avoids
    instantiating any actual GAction's, instead just calling functions
    functions directly in activate_action(). Change it to use GAction
    instances as this will allow us to disable the actions when necessary
    and affect the buttons' sensitivity.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=780688

 libide/buildsystem/ide-build-manager.c |    8 +-
 libide/runner/ide-run-manager.c        |  191 ++++++++++++++++++--------------
 2 files changed, 110 insertions(+), 89 deletions(-)
---
diff --git a/libide/buildsystem/ide-build-manager.c b/libide/buildsystem/ide-build-manager.c
index 74c641a..0468075 100644
--- a/libide/buildsystem/ide-build-manager.c
+++ b/libide/buildsystem/ide-build-manager.c
@@ -778,6 +778,8 @@ ide_build_manager_action_install (GSimpleAction *action,
 static void
 ide_build_manager_init (IdeBuildManager *self)
 {
+  GAction *cancel_action;
+
   static GActionEntry actions[] = {
     { "build", ide_build_manager_action_build },
     { "cancel", ide_build_manager_action_cancel },
@@ -795,10 +797,8 @@ ide_build_manager_init (IdeBuildManager *self)
                                    G_N_ELEMENTS (actions),
                                    self);
 
-  {
-    GAction *action = g_action_map_lookup_action (G_ACTION_MAP (self->actions), "cancel");
-    g_object_bind_property (self, "busy", action, "enabled", 0);
-  }
+  cancel_action = g_action_map_lookup_action (G_ACTION_MAP (self->actions), "cancel");
+  g_object_bind_property (self, "busy", cancel_action, "enabled", 0);
 
   ide_build_manager_update_action_enabled (self);
 
diff --git a/libide/runner/ide-run-manager.c b/libide/runner/ide-run-manager.c
index 2c9d195..53f9c64 100644
--- a/libide/runner/ide-run-manager.c
+++ b/libide/runner/ide-run-manager.c
@@ -217,19 +217,6 @@ ide_run_manager_class_init (IdeRunManagerClass *klass)
                   0);
 }
 
-static void
-ide_run_manager_init (IdeRunManager *self)
-{
-  ide_run_manager_add_handler (self,
-                               "run",
-                               _("Run"),
-                               "media-playback-start-symbolic",
-                               "<Control>F5",
-                               NULL,
-                               NULL,
-                               NULL);
-}
-
 gboolean
 ide_run_manager_get_busy (IdeRunManager *self)
 {
@@ -886,48 +873,17 @@ ide_run_manager_query_action (GActionGroup        *group,
                               GVariant           **state)
 {
   IdeRunManager *self = (IdeRunManager *)group;
-  const GVariantType *real_parameter_type = NULL;
-  gboolean real_enabled = FALSE;
 
   g_assert (IDE_IS_RUN_MANAGER (self));
   g_assert (action_name != NULL);
 
-  if (g_strcmp0 (action_name, "run-with-handler") == 0)
-    {
-      real_enabled = self->busy == FALSE;
-      real_parameter_type = G_VARIANT_TYPE_STRING;
-      goto finish;
-    }
-
-  if (g_strcmp0 (action_name, "run") == 0)
-    {
-      real_enabled = self->busy == FALSE;
-      goto finish;
-    }
-
-  if (g_strcmp0 (action_name, "stop") == 0)
-    {
-      real_enabled = self->busy == TRUE;
-      goto finish;
-    }
-
-finish:
-  if (state_type)
-    *state_type = NULL;
-
-  if (state_hint)
-    *state_hint = NULL;
-
-  if (state)
-    *state = NULL;
-
-  if (enabled)
-    *enabled = real_enabled;
-
-  if (parameter_type)
-    *parameter_type = real_parameter_type;
-
-  return TRUE;
+  return g_action_group_query_action (G_ACTION_GROUP (self->actions),
+                                      action_name,
+                                      enabled,
+                                      parameter_type,
+                                      state_type,
+                                      state_hint,
+                                      state);
 }
 
 static void
@@ -955,44 +911,11 @@ ide_run_manager_activate_action (GActionGroup *group,
                                  GVariant     *parameter)
 {
   IdeRunManager *self = (IdeRunManager *)group;
-  g_autoptr(GVariant) sunk = NULL;
 
   g_assert (IDE_IS_RUN_MANAGER (self));
   g_assert (action_name != NULL);
 
-  if (parameter != NULL && g_variant_is_floating (parameter))
-    sunk = g_variant_ref_sink (parameter);
-
-  if (FALSE) {}
-  else if (g_strcmp0 (action_name, "run-with-handler") == 0)
-    {
-      const gchar *handler = NULL;
-
-      if (parameter != NULL)
-        handler = g_variant_get_string (parameter, NULL);
-
-      /* "" translates to current handler */
-      if (handler && *handler)
-        ide_run_manager_set_handler (self, handler);
-
-      ide_run_manager_run_async (self,
-                                 NULL,
-                                 NULL,
-                                 ide_run_manager_run_action_cb,
-                                 NULL);
-    }
-  else if (g_strcmp0 (action_name, "run") == 0)
-    {
-      ide_run_manager_run_async (self,
-                                 NULL,
-                                 NULL,
-                                 ide_run_manager_run_action_cb,
-                                 NULL);
-    }
-  else if (g_strcmp0 (action_name, "stop") == 0)
-    {
-      ide_run_manager_cancel (self);
-    }
+  g_action_group_activate_action (G_ACTION_GROUP (self->actions), action_name, parameter);
 }
 
 static void
@@ -1003,3 +926,101 @@ action_group_iface_init (GActionGroupInterface *iface)
   iface->query_action = ide_run_manager_query_action;
   iface->activate_action = ide_run_manager_activate_action;
 }
+
+static void
+ide_run_manager_action_run (GSimpleAction *action,
+                            GVariant      *parameter,
+                            gpointer       user_data)
+{
+  IdeRunManager *self = user_data;
+
+  IDE_ENTRY;
+
+  g_assert (G_IS_SIMPLE_ACTION (action));
+  g_assert (IDE_IS_RUN_MANAGER (self));
+
+  ide_run_manager_run_async (self,
+                             NULL,
+                             NULL,
+                             ide_run_manager_run_action_cb,
+                             NULL);
+
+  IDE_EXIT;
+}
+
+static void
+ide_run_manager_action_run_with_handler (GSimpleAction *action,
+                                         GVariant      *parameter,
+                                         gpointer       user_data)
+{
+  IdeRunManager *self = user_data;
+  const gchar *handler = NULL;
+  g_autoptr(GVariant) sunk = NULL;
+
+  IDE_ENTRY;
+
+  g_assert (G_IS_SIMPLE_ACTION (action));
+  g_assert (IDE_IS_RUN_MANAGER (self));
+
+  if (parameter != NULL)
+  {
+    handler = g_variant_get_string (parameter, NULL);
+    if (g_variant_is_floating (parameter))
+      sunk = g_variant_ref_sink (parameter);
+  }
+
+  /* translates to current handler */
+  if (handler && *handler)
+    ide_run_manager_set_handler (self, handler);
+
+  ide_run_manager_run_async (self,
+                             NULL,
+                             NULL,
+                             ide_run_manager_run_action_cb,
+                             NULL);
+
+  IDE_EXIT;
+}
+
+static void
+ide_run_manager_action_stop (GSimpleAction *action,
+                             GVariant      *parameter,
+                             gpointer       user_data)
+{
+  IdeRunManager *self = user_data;
+
+  IDE_ENTRY;
+
+  g_assert (G_IS_SIMPLE_ACTION (action));
+  g_assert (IDE_IS_RUN_MANAGER (self));
+
+  ide_run_manager_cancel (self);
+
+  IDE_EXIT;
+}
+
+static void
+ide_run_manager_init (IdeRunManager *self)
+{
+  static GActionEntry actions[] = {
+    { "run", ide_run_manager_action_run },
+    { "run-with-handler", ide_run_manager_action_run_with_handler, "s" },
+    { "stop", ide_run_manager_action_stop }
+  };
+
+  self->actions = g_simple_action_group_new ();
+
+  g_action_map_add_action_entries (G_ACTION_MAP (self->actions),
+                                   actions,
+                                   G_N_ELEMENTS (actions),
+                                   self);
+
+  ide_run_manager_add_handler (self,
+                               "run",
+                               _("Run"),
+                               "media-playback-start-symbolic",
+                               "<Control>F5",
+                               NULL,
+                               NULL,
+                               NULL);
+}


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