[gnome-builder] run-manager: Disable run button when can-build is FALSE



commit 6228da7e947b500fc4445932972a9a22a7c041e9
Author: Matthew Leeds <mleeds redhat com>
Date:   Thu Apr 20 19:11:54 2017 -0500

    run-manager: Disable run button when can-build is FALSE
    
    We're already disabling the build buttons when the build isn't possible
    (e.g. due to a missing runtime) so it makes sense to disable the Run
    button too (which does an install before running). Since the actions'
    enabled state also depends on whether the IdeRunManager is busy or not,
    we have to use a callback rather than a simple property binding. And
    since the context isn't available in the init() we have to get it in the
    async init instead.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=780688

 libide/ide-context.c            |   22 +++++++++
 libide/runner/ide-run-manager.c |   95 +++++++++++++++++++++++++++++++++-----
 2 files changed, 104 insertions(+), 13 deletions(-)
---
diff --git a/libide/ide-context.c b/libide/ide-context.c
index 0d170e1..77a65ca 100644
--- a/libide/ide-context.c
+++ b/libide/ide-context.c
@@ -1503,6 +1503,27 @@ ide_context_init_build_manager (gpointer             source_object,
 }
 
 static void
+ide_context_init_run_manager (gpointer             source_object,
+                              GCancellable        *cancellable,
+                              GAsyncReadyCallback  callback,
+                              gpointer             user_data)
+{
+  g_autoptr(GError) error = NULL;
+  g_autoptr(GTask) task = NULL;
+  IdeContext *self = source_object;
+
+  g_assert (IDE_IS_CONTEXT (self));
+  g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+  task = g_task_new (self, cancellable, callback, user_data);
+
+  if (!g_initable_init (G_INITABLE (self->run_manager), cancellable, &error))
+    g_task_return_error (task, g_steal_pointer (&error));
+  else
+    g_task_return_boolean (task, TRUE);
+}
+
+static void
 ide_context_init_loaded (gpointer             source_object,
                          GCancellable        *cancellable,
                          GAsyncReadyCallback  callback,
@@ -1670,6 +1691,7 @@ ide_context_init_async (GAsyncInitable      *initable,
                         ide_context_init_runtimes,
                         ide_context_init_configuration_manager,
                         ide_context_init_build_manager,
+                        ide_context_init_run_manager,
                         ide_context_init_diagnostics_manager,
                         ide_context_init_loaded,
                         NULL);
diff --git a/libide/runner/ide-run-manager.c b/libide/runner/ide-run-manager.c
index 53f9c64..ace1c1f 100644
--- a/libide/runner/ide-run-manager.c
+++ b/libide/runner/ide-run-manager.c
@@ -47,9 +47,11 @@ struct _IdeRunManager
   guint                    busy : 1;
 };
 
+static void initable_iface_init             (GInitableIface        *iface);
 static void action_group_iface_init (GActionGroupInterface *iface);
 
 G_DEFINE_TYPE_EXTENDED (IdeRunManager, ide_run_manager, IDE_TYPE_OBJECT, 0,
+                        G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init)
                         G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP, action_group_iface_init))
 
 enum {
@@ -101,6 +103,82 @@ ide_run_manager_finalize (GObject *object)
 }
 
 static void
+ide_run_manager_update_action_enabled (IdeRunManager *self)
+{
+  IdeBuildManager *build_manager;
+  IdeContext *context;
+  gboolean can_build;
+  GAction *run_action;
+  GAction *run_with_action;
+
+  g_assert (IDE_IS_RUN_MANAGER (self));
+
+  context = ide_object_get_context (IDE_OBJECT (self));
+  build_manager = ide_context_get_build_manager (context);
+  can_build = ide_build_manager_get_can_build (build_manager);
+
+  run_action = g_action_map_lookup_action (G_ACTION_MAP (self->actions), "run");
+  run_with_action = g_action_map_lookup_action (G_ACTION_MAP (self->actions), "run-with-handler");
+
+  g_simple_action_set_enabled (G_SIMPLE_ACTION (run_action), self->busy == FALSE && can_build == TRUE);
+  g_simple_action_set_enabled (G_SIMPLE_ACTION (run_with_action), self->busy == FALSE && can_build == TRUE);
+
+  g_action_group_action_enabled_changed (G_ACTION_GROUP (self), "run", self->busy == FALSE && can_build == 
TRUE);
+  g_action_group_action_enabled_changed (G_ACTION_GROUP (self), "run-with-handler", self->busy == FALSE && 
can_build == TRUE);
+  g_action_group_action_enabled_changed (G_ACTION_GROUP (self), "stop", self->busy == TRUE);
+}
+
+static void
+ide_run_manager_notify_can_build (IdeRunManager   *self,
+                                  GParamSpec      *pspec,
+                                  IdeBuildManager *build_manager)
+{
+  IDE_ENTRY;
+
+  g_assert (IDE_IS_RUN_MANAGER (self));
+  g_assert (G_IS_PARAM_SPEC (pspec));
+  g_assert (IDE_IS_BUILD_MANAGER (build_manager));
+
+  ide_run_manager_update_action_enabled (self);
+
+  IDE_EXIT;
+}
+
+static gboolean
+initable_init (GInitable     *initable,
+               GCancellable  *cancellable,
+               GError       **error)
+{
+  IdeRunManager *self = (IdeRunManager *)initable;
+  IdeBuildManager *build_manager;
+  IdeContext *context;
+
+  IDE_ENTRY;
+
+  g_assert (IDE_IS_RUN_MANAGER (self));
+  g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+  context = ide_object_get_context (IDE_OBJECT (self));
+  build_manager = ide_context_get_build_manager (context);
+
+  g_signal_connect_object (build_manager,
+                           "notify::can-build",
+                           G_CALLBACK (ide_run_manager_notify_can_build),
+                           self,
+                           G_CONNECT_SWAPPED);
+
+  ide_run_manager_update_action_enabled (self);
+
+  IDE_RETURN (TRUE);
+}
+
+static void
+initable_iface_init (GInitableIface *iface)
+{
+  iface->init = initable_init;
+}
+
+static void
 ide_run_manager_get_property (GObject    *object,
                               guint       prop_id,
                               GValue     *value,
@@ -422,17 +500,6 @@ ide_run_manager_install_cb (GObject      *object,
 }
 
 static void
-ide_run_manager_notify_busy (IdeRunManager *self)
-{
-  g_assert (IDE_IS_RUN_MANAGER (self));
-
-  g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_BUSY]);
-  g_action_group_action_enabled_changed (G_ACTION_GROUP (self), "run", self->busy == FALSE);
-  g_action_group_action_enabled_changed (G_ACTION_GROUP (self), "run-with-handler", self->busy == FALSE);
-  g_action_group_action_enabled_changed (G_ACTION_GROUP (self), "stop", self->busy == TRUE);
-}
-
-static void
 ide_run_manager_task_completed (IdeRunManager *self,
                                 GParamSpec    *pspec,
                                 GTask         *task)
@@ -444,8 +511,9 @@ ide_run_manager_task_completed (IdeRunManager *self,
   g_assert (G_IS_TASK (task));
 
   self->busy = FALSE;
+  g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_BUSY]);
 
-  ide_run_manager_notify_busy (self);
+  ide_run_manager_update_action_enabled (self);
 
   IDE_EXIT;
 }
@@ -471,6 +539,7 @@ ide_run_manager_do_install_before_run (IdeRunManager *self,
    */
 
   self->busy = TRUE;
+  g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_BUSY]);
 
   g_signal_connect_object (task,
                            "notify::completed",
@@ -484,7 +553,7 @@ ide_run_manager_do_install_before_run (IdeRunManager *self,
                                    ide_run_manager_install_cb,
                                    g_object_ref (task));
 
-  ide_run_manager_notify_busy (self);
+  ide_run_manager_update_action_enabled (self);
 
   IDE_EXIT;
 }


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