[gnome-builder/wip/gtk4-port: 402/736] plugins/vim: add active property to workspace addin




commit ff9c3529d3417e1838054f270017929b60e5cf59
Author: Christian Hergert <chergert redhat com>
Date:   Tue Apr 5 09:36:29 2022 -0700

    plugins/vim: add active property to workspace addin
    
    The goal for this property is for the other components such as the
    GbpVimEditorPageAddin to key off this to enable/disable themselves without
    having to load/unload the whole plugin. Doing so keeps everything in sync
    without the extra headache of checking settings individually.

 src/plugins/vim/gbp-vim-workspace-addin.c | 101 ++++++++++++++++++++++++++++++
 src/plugins/vim/gbp-vim-workspace-addin.h |   9 +--
 2 files changed, 106 insertions(+), 4 deletions(-)
---
diff --git a/src/plugins/vim/gbp-vim-workspace-addin.c b/src/plugins/vim/gbp-vim-workspace-addin.c
index a07be39ae..0dd3dd7cb 100644
--- a/src/plugins/vim/gbp-vim-workspace-addin.c
+++ b/src/plugins/vim/gbp-vim-workspace-addin.c
@@ -33,15 +33,50 @@ struct _GbpVimWorkspaceAddin
 {
   GObject parent_instance;
 
+  GSettings *editor_settings;
+
   GtkLabel *command_bar;
   GtkLabel *command;
+
+  guint active : 1;
+};
+
+enum {
+  PROP_0,
+  PROP_ACTIVE,
+  N_PROPS
 };
 
+static GParamSpec *properties[N_PROPS];
+
+static void
+on_keybindings_changed_cb (GbpVimWorkspaceAddin *self,
+                           const char           *key,
+                           GSettings            *editor_settings)
+{
+  g_autofree char *keybindings = NULL;
+  gboolean active;
+
+  g_assert (GBP_IS_VIM_WORKSPACE_ADDIN (self));
+  g_assert (g_strcmp0 (key, "keybindings") == 0);
+  g_assert (G_IS_SETTINGS (editor_settings));
+
+  keybindings = g_settings_get_string (editor_settings, "keybindings");
+  active = g_strcmp0 (keybindings, "vim") == 0;
+
+  if (active != self->active)
+    {
+      self->active = active;
+      g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_ACTIVE]);
+    }
+}
+
 static void
 gbp_vim_workspace_addin_load (IdeWorkspaceAddin *addin,
                               IdeWorkspace      *workspace)
 {
   GbpVimWorkspaceAddin *self = (GbpVimWorkspaceAddin *)addin;
+  g_autofree char *keybindings = NULL;
   PanelStatusbar *statusbar;
   PangoAttrList *attrs;
 
@@ -50,6 +85,20 @@ gbp_vim_workspace_addin_load (IdeWorkspaceAddin *addin,
   g_assert (IDE_IS_WORKSPACE_ADDIN (self));
   g_assert (IDE_IS_WORKSPACE (workspace));
 
+  self->editor_settings = g_settings_new ("org.gnome.builder.editor");
+  keybindings = g_settings_get_string (self->editor_settings, "keybindings");
+  g_signal_connect_object (self->editor_settings,
+                           "changed::keybindings",
+                           G_CALLBACK (on_keybindings_changed_cb),
+                           self,
+                           G_CONNECT_SWAPPED);
+
+  self->active = g_strcmp0 (keybindings, "vim") == 0;
+
+  if (!self->active)
+    g_debug ("Vim plugin loaded but inactive as keybindings are currently \"%s\"",
+             keybindings);
+
   attrs = pango_attr_list_new ();
   pango_attr_list_insert (attrs, pango_attr_family_new ("Monospace"));
 
@@ -57,6 +106,7 @@ gbp_vim_workspace_addin_load (IdeWorkspaceAddin *addin,
                                     "attributes", attrs,
                                     "hexpand", TRUE,
                                     "selectable", TRUE,
+                                    "visible", self->active,
                                     "xalign", .0f,
                                     NULL);
   self->command = g_object_new (GTK_TYPE_LABEL,
@@ -95,6 +145,11 @@ gbp_vim_workspace_addin_unload (IdeWorkspaceAddin *addin,
 
   self->command_bar = NULL;
   self->command = NULL;
+  self->active = FALSE;
+
+  g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_ACTIVE]);
+
+  g_clear_object (&self->editor_settings);
 
   IDE_EXIT;
 }
@@ -109,9 +164,47 @@ workspace_addin_iface_init (IdeWorkspaceAddinInterface *iface)
 G_DEFINE_FINAL_TYPE_WITH_CODE (GbpVimWorkspaceAddin, gbp_vim_workspace_addin, G_TYPE_OBJECT,
                                G_IMPLEMENT_INTERFACE (IDE_TYPE_WORKSPACE_ADDIN, workspace_addin_iface_init))
 
+static void
+gbp_vim_workspace_addin_get_property (GObject    *object,
+                                      guint       prop_id,
+                                      GValue     *value,
+                                      GParamSpec *pspec)
+{
+  GbpVimWorkspaceAddin *self = GBP_VIM_WORKSPACE_ADDIN (object);
+
+  switch (prop_id)
+    {
+    case PROP_ACTIVE:
+      g_value_set_boolean (value, self->active);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
 static void
 gbp_vim_workspace_addin_class_init (GbpVimWorkspaceAddinClass *klass)
 {
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->get_property = gbp_vim_workspace_addin_get_property;
+
+  /**
+   * GbpVimWorkspaceAddin:active:
+   *
+   * The "active" property is bound by other vim plugin hooks so that they
+   * may all enable/disable together based on the current keybindings setting
+   * in org.gnome.builder.editor.
+   */
+  properties [PROP_ACTIVE] =
+    g_param_spec_boolean ("active",
+                          "Active",
+                          "If vim is the active keybindings for the application",
+                          FALSE,
+                          (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
+
+  g_object_class_install_properties (object_class, N_PROPS, properties);
 }
 
 static void
@@ -138,3 +231,11 @@ gbp_vim_workspace_addin_set_command_bar (GbpVimWorkspaceAddin *self,
   if (self->command_bar != NULL)
     gtk_label_set_label (self->command_bar, command_bar);
 }
+
+gboolean
+gbp_vim_workspace_addin_get_active (GbpVimWorkspaceAddin *self)
+{
+  g_return_val_if_fail (GBP_IS_VIM_WORKSPACE_ADDIN (self), FALSE);
+
+  return self->active;
+}
diff --git a/src/plugins/vim/gbp-vim-workspace-addin.h b/src/plugins/vim/gbp-vim-workspace-addin.h
index d4a8bd700..c03ecf0ed 100644
--- a/src/plugins/vim/gbp-vim-workspace-addin.h
+++ b/src/plugins/vim/gbp-vim-workspace-addin.h
@@ -28,9 +28,10 @@ G_BEGIN_DECLS
 
 G_DECLARE_FINAL_TYPE (GbpVimWorkspaceAddin, gbp_vim_workspace_addin, GBP, VIM_WORKSPACE_ADDIN, GObject)
 
-void gbp_vim_workspace_addin_set_command     (GbpVimWorkspaceAddin *self,
-                                              const char           *command);
-void gbp_vim_workspace_addin_set_command_bar (GbpVimWorkspaceAddin *self,
-                                              const char           *command_bar);
+gboolean gbp_vim_workspace_addin_get_active      (GbpVimWorkspaceAddin *self);
+void     gbp_vim_workspace_addin_set_command     (GbpVimWorkspaceAddin *self,
+                                                  const char           *command);
+void     gbp_vim_workspace_addin_set_command_bar (GbpVimWorkspaceAddin *self,
+                                                  const char           *command_bar);
 
 G_END_DECLS


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