[gnome-builder] gui: add API for shortcuts to IdeCommand



commit afd3da7dc6961ae88cf1abfa9e924b4c069b6bc4
Author: Christian Hergert <chergert redhat com>
Date:   Wed Aug 7 13:32:31 2019 -0700

    gui: add API for shortcuts to IdeCommand
    
    This allows command implmentations to register shortcuts when the provider
    is loaded or unloaded. Currently, those need to be attached to the
    workspace.
    
    The load/unload functions will be called in response to workspace
    creation and destruction.

 src/libide/gui/ide-command-manager.c  | 102 ++++++++++++++++++++++++++++++++++
 src/libide/gui/ide-command-provider.c |  22 ++++++++
 src/libide/gui/ide-command-provider.h |  10 ++++
 src/libide/gui/ide-command.h          |   1 -
 src/libide/gui/ide-gui-private.h      |   4 ++
 src/libide/gui/ide-workbench.c        |  10 ++++
 6 files changed, 148 insertions(+), 1 deletion(-)
---
diff --git a/src/libide/gui/ide-command-manager.c b/src/libide/gui/ide-command-manager.c
index 225d376ef..ad717404f 100644
--- a/src/libide/gui/ide-command-manager.c
+++ b/src/libide/gui/ide-command-manager.c
@@ -67,6 +67,18 @@ query_free (Query *q)
   g_slice_free (Query, q);
 }
 
+static void
+ide_command_manager_load_shortcuts_cb (GtkWidget *workspace,
+                                       gpointer   user_data)
+{
+  IdeCommandProvider *provider = user_data;
+
+  g_assert (IDE_IS_WORKSPACE (workspace));
+  g_assert (IDE_IS_COMMAND_PROVIDER (provider));
+
+  ide_command_provider_load_shortcuts (provider, IDE_WORKSPACE (workspace));
+}
+
 static void
 ide_command_manager_provider_added_cb (IdeExtensionSetAdapter *set,
                                        PeasPluginInfo         *plugin_info,
@@ -75,6 +87,8 @@ ide_command_manager_provider_added_cb (IdeExtensionSetAdapter *set,
 {
   IdeCommandProvider *provider = (IdeCommandProvider *)exten;
   IdeCommandManager *self = user_data;
+  g_autoptr(IdeContext) context = NULL;
+  IdeWorkbench *workbench;
 
   g_assert (IDE_IS_MAIN_THREAD ());
   g_assert (IDE_IS_EXTENSION_SET_ADAPTER (set));
@@ -84,6 +98,24 @@ ide_command_manager_provider_added_cb (IdeExtensionSetAdapter *set,
 
   g_debug ("Adding command provider %s", G_OBJECT_TYPE_NAME (exten));
 
+  context = ide_object_ref_context (IDE_OBJECT (self));
+  workbench = _ide_workbench_from_context (context);
+
+  ide_workbench_foreach_workspace (workbench,
+                                   ide_command_manager_load_shortcuts_cb,
+                                   provider);
+}
+
+static void
+ide_command_manager_unload_shortcuts_cb (GtkWidget *workspace,
+                                         gpointer   user_data)
+{
+  IdeCommandProvider *provider = user_data;
+
+  g_assert (IDE_IS_WORKSPACE (workspace));
+  g_assert (IDE_IS_COMMAND_PROVIDER (provider));
+
+  ide_command_provider_unload_shortcuts (provider, IDE_WORKSPACE (workspace));
 }
 
 static void
@@ -94,6 +126,8 @@ ide_command_manager_provider_removed_cb (IdeExtensionSetAdapter *set,
 {
   IdeCommandProvider *provider = (IdeCommandProvider *)exten;
   IdeCommandManager *self = user_data;
+  g_autoptr(IdeContext) context = NULL;
+  IdeWorkbench *workbench;
 
   g_assert (IDE_IS_MAIN_THREAD ());
   g_assert (IDE_IS_EXTENSION_SET_ADAPTER (set));
@@ -103,6 +137,12 @@ ide_command_manager_provider_removed_cb (IdeExtensionSetAdapter *set,
 
   g_debug ("Removing command provider %s", G_OBJECT_TYPE_NAME (exten));
 
+  context = ide_object_ref_context (IDE_OBJECT (self));
+  workbench = _ide_workbench_from_context (context);
+
+  ide_workbench_foreach_workspace (workbench,
+                                   ide_command_manager_unload_shortcuts_cb,
+                                   provider);
 }
 
 static void
@@ -317,6 +357,68 @@ ide_command_manager_query_finish (IdeCommandManager  *self,
   return IDE_PTR_ARRAY_STEAL_FULL (&ret);
 }
 
+static void
+ide_command_manager_init_shortcuts_cb (IdeExtensionSetAdapter *set,
+                                       PeasPluginInfo         *plugin_info,
+                                       PeasExtension          *exten,
+                                       gpointer                user_data)
+{
+  IdeCommandProvider *provider = (IdeCommandProvider *)exten;
+  IdeWorkspace *workspace = user_data;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (IDE_IS_EXTENSION_SET_ADAPTER (set));
+  g_assert (plugin_info != NULL);
+  g_assert (IDE_IS_COMMAND_PROVIDER (provider));
+  g_assert (IDE_IS_WORKSPACE (workspace));
+
+  ide_command_provider_load_shortcuts (provider, workspace);
+}
+
+void
+_ide_command_manager_init_shortcuts (IdeCommandManager *self,
+                                     IdeWorkspace      *workspace)
+{
+  g_return_if_fail (IDE_IS_COMMAND_MANAGER (self));
+  g_return_if_fail (IDE_IS_WORKSPACE (workspace));
+  g_return_if_fail (self->adapter != NULL);
+
+  ide_extension_set_adapter_foreach (self->adapter,
+                                     ide_command_manager_init_shortcuts_cb,
+                                     workspace);
+}
+
+static void
+ide_command_manager_unload_shortcuts_foreach_cb (IdeExtensionSetAdapter *set,
+                                                 PeasPluginInfo         *plugin_info,
+                                                 PeasExtension          *exten,
+                                                 gpointer                user_data)
+{
+  IdeCommandProvider *provider = (IdeCommandProvider *)exten;
+  IdeWorkspace *workspace = user_data;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (IDE_IS_EXTENSION_SET_ADAPTER (set));
+  g_assert (plugin_info != NULL);
+  g_assert (IDE_IS_COMMAND_PROVIDER (provider));
+  g_assert (IDE_IS_WORKSPACE (workspace));
+
+  ide_command_provider_unload_shortcuts (provider, workspace);
+}
+
+void
+_ide_command_manager_unload_shortcuts (IdeCommandManager *self,
+                                       IdeWorkspace      *workspace)
+{
+  g_return_if_fail (IDE_IS_COMMAND_MANAGER (self));
+  g_return_if_fail (IDE_IS_WORKSPACE (workspace));
+  g_return_if_fail (self->adapter != NULL);
+
+  ide_extension_set_adapter_foreach (self->adapter,
+                                     ide_command_manager_unload_shortcuts_foreach_cb,
+                                     workspace);
+}
+
 static void
 ide_command_manager_get_command_by_id_cb (IdeExtensionSetAdapter *set,
                                           PeasPluginInfo         *plugin_info,
diff --git a/src/libide/gui/ide-command-provider.c b/src/libide/gui/ide-command-provider.c
index 45c625fb0..cf1c3d297 100644
--- a/src/libide/gui/ide-command-provider.c
+++ b/src/libide/gui/ide-command-provider.c
@@ -102,6 +102,28 @@ ide_command_provider_query_finish (IdeCommandProvider  *self,
   return IDE_COMMAND_PROVIDER_GET_IFACE (self)->query_finish (self, result, error);
 }
 
+void
+ide_command_provider_load_shortcuts (IdeCommandProvider *self,
+                                     IdeWorkspace       *workspace)
+{
+  g_return_if_fail (IDE_IS_COMMAND_PROVIDER (self));
+  g_return_if_fail (IDE_IS_WORKSPACE (workspace));
+
+  if (IDE_COMMAND_PROVIDER_GET_IFACE (self)->load_shortcuts)
+    IDE_COMMAND_PROVIDER_GET_IFACE (self)->load_shortcuts (self, workspace);
+}
+
+void
+ide_command_provider_unload_shortcuts (IdeCommandProvider *self,
+                                       IdeWorkspace       *workspace)
+{
+  g_return_if_fail (IDE_IS_COMMAND_PROVIDER (self));
+  g_return_if_fail (IDE_IS_WORKSPACE (workspace));
+
+  if (IDE_COMMAND_PROVIDER_GET_IFACE (self)->unload_shortcuts)
+    IDE_COMMAND_PROVIDER_GET_IFACE (self)->unload_shortcuts (self, workspace);
+}
+
 /**
  * ide_command_provider_get_command_by_id:
  * @self: a #IdeCommandProvider
diff --git a/src/libide/gui/ide-command-provider.h b/src/libide/gui/ide-command-provider.h
index 2fe0d9b6d..a3d3a56e9 100644
--- a/src/libide/gui/ide-command-provider.h
+++ b/src/libide/gui/ide-command-provider.h
@@ -52,8 +52,18 @@ struct _IdeCommandProviderInterface
   IdeCommand *(*get_command_by_id) (IdeCommandProvider   *self,
                                     IdeWorkspace         *workspace,
                                     const gchar          *command_id);
+  void        (*load_shortcuts)    (IdeCommandProvider   *self,
+                                    IdeWorkspace         *workspace);
+  void        (*unload_shortcuts)  (IdeCommandProvider   *self,
+                                    IdeWorkspace         *workspace);
 };
 
+IDE_AVAILABLE_IN_3_34
+void        ide_command_provider_load_shortcuts    (IdeCommandProvider   *self,
+                                                    IdeWorkspace         *workspace);
+IDE_AVAILABLE_IN_3_34
+void        ide_command_provider_unload_shortcuts  (IdeCommandProvider   *self,
+                                                    IdeWorkspace         *workspace);
 IDE_AVAILABLE_IN_3_32
 void        ide_command_provider_query_async       (IdeCommandProvider   *self,
                                                     IdeWorkspace         *workspace,
diff --git a/src/libide/gui/ide-command.h b/src/libide/gui/ide-command.h
index 0f1057a8e..432916eb5 100644
--- a/src/libide/gui/ide-command.h
+++ b/src/libide/gui/ide-command.h
@@ -68,5 +68,4 @@ gboolean  ide_command_run_finish   (IdeCommand           *self,
                                     GAsyncResult         *result,
                                     GError              **error);
 
-
 G_END_DECLS
diff --git a/src/libide/gui/ide-gui-private.h b/src/libide/gui/ide-gui-private.h
index b7c3f90ec..82c1f28a4 100644
--- a/src/libide/gui/ide-gui-private.h
+++ b/src/libide/gui/ide-gui-private.h
@@ -44,6 +44,10 @@
 
 G_BEGIN_DECLS
 
+void      _ide_command_manager_init_shortcuts   (IdeCommandManager   *self,
+                                                 IdeWorkspace        *workspace);
+void      _ide_command_manager_unload_shortcuts (IdeCommandManager   *self,
+                                                 IdeWorkspace        *workspace);
 void      _ide_command_manager_execute          (IdeCommandManager   *self,
                                                  IdeWorkspace        *workspace,
                                                  const gchar         *command);
diff --git a/src/libide/gui/ide-workbench.c b/src/libide/gui/ide-workbench.c
index 4c5642813..99d219ac0 100644
--- a/src/libide/gui/ide-workbench.c
+++ b/src/libide/gui/ide-workbench.c
@@ -769,6 +769,7 @@ ide_workbench_add_workspace (IdeWorkbench *self,
                              IdeWorkspace *workspace)
 {
   g_autoptr(GPtrArray) addins = NULL;
+  IdeCommandManager *command_manager;
   GList *mru_link;
 
   g_return_if_fail (IDE_IS_MAIN_THREAD ());
@@ -842,6 +843,10 @@ ide_workbench_add_workspace (IdeWorkbench *self,
       formatted = g_strdup_printf (_("Builder — %s"), title);
       gtk_window_set_title (GTK_WINDOW (workspace), formatted);
     }
+
+  /* Load shortcuts for commands */
+  command_manager = ide_command_manager_from_context (self->context);
+  _ide_command_manager_init_shortcuts (command_manager, workspace);
 }
 
 /**
@@ -858,6 +863,7 @@ ide_workbench_remove_workspace (IdeWorkbench *self,
                                 IdeWorkspace *workspace)
 {
   g_autoptr(GPtrArray) addins = NULL;
+  IdeCommandManager *command_manager;
   GList *list;
   GList *mru_link;
   guint count = 0;
@@ -873,6 +879,10 @@ ide_workbench_remove_workspace (IdeWorkbench *self,
                                         G_CALLBACK (ide_workbench_workspace_has_toplevel_focus_cb),
                                         self);
 
+  /* Remove any shortcuts that were registered by command providers */
+  command_manager = ide_command_manager_from_context (self->context);
+  _ide_command_manager_unload_shortcuts (command_manager, workspace);
+
   /* Notify all the addins about losing the workspace. */
   if ((addins = ide_workbench_collect_addins (self)))
     {


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