[gnome-builder/wip/gtk4-port: 531/1774] plugins/spellcheck: add gsetting for spellcheck enabled




commit 605ab031061eef88546cf985143ccaf2894fc208
Author: Christian Hergert <chergert redhat com>
Date:   Mon Apr 11 12:59:36 2022 -0700

    plugins/spellcheck: add gsetting for spellcheck enabled

 data/gsettings/meson.build                         |   1 +
 .../org.gnome.builder.spelling.gschema.xml         |   9 ++
 src/plugins/spellcheck/gbp-spell-buffer-addin.c    | 123 ++++++++++++++++++++-
 3 files changed, 127 insertions(+), 6 deletions(-)
---
diff --git a/data/gsettings/meson.build b/data/gsettings/meson.build
index 25c62eada..e8fcb81b8 100644
--- a/data/gsettings/meson.build
+++ b/data/gsettings/meson.build
@@ -35,6 +35,7 @@ schemas = [
   'org.gnome.builder.plugin.gschema.xml',
   'org.gnome.builder.project.gschema.xml',
   'org.gnome.builder.project-tree.gschema.xml',
+  'org.gnome.builder.spelling.gschema.xml',
   'org.gnome.builder.terminal.gschema.xml',
   'org.gnome.builder.workbench.gschema.xml',
 ]
diff --git a/data/gsettings/org.gnome.builder.spelling.gschema.xml 
b/data/gsettings/org.gnome.builder.spelling.gschema.xml
new file mode 100644
index 000000000..820bcae97
--- /dev/null
+++ b/data/gsettings/org.gnome.builder.spelling.gschema.xml
@@ -0,0 +1,9 @@
+<schemalist>
+  <schema id="org.gnome.builder.spelling" path="/org/gnome/builder/editor/" gettext-domain="gnome-builder">
+    <key name="check-spelling" type="b">
+      <default>true</default>
+      <summary>Check Spelling</summary>
+      <description>Automatically check spelling within documents</description>
+    </key>
+  </schema>
+</schemalist>
diff --git a/src/plugins/spellcheck/gbp-spell-buffer-addin.c b/src/plugins/spellcheck/gbp-spell-buffer-addin.c
index e52f656da..fd4f62c64 100644
--- a/src/plugins/spellcheck/gbp-spell-buffer-addin.c
+++ b/src/plugins/spellcheck/gbp-spell-buffer-addin.c
@@ -38,8 +38,29 @@ struct _GbpSpellBufferAddin
   EditorSpellChecker *checker;
   EditorTextBufferSpellAdapter *adapter;
   GPropertyAction *enabled_action;
+  guint enabled : 1;
 };
 
+enum {
+  PROP_0,
+  PROP_ENABLED,
+  N_PROPS
+};
+
+static GParamSpec *properties[N_PROPS];
+static GSettings *settings;
+
+static gboolean
+gbp_spell_buffer_addin_calculate_enabled (GbpSpellBufferAddin *self)
+{
+  g_assert (GBP_IS_SPELL_BUFFER_ADDIN (self));
+
+  return self->enabled &&
+         self->adapter != NULL &&
+         self->buffer != NULL &&
+         ide_buffer_get_state (self->buffer) == IDE_BUFFER_STATE_READY;
+}
+
 static void
 check_error (GObject      *object,
              GAsyncResult *result,
@@ -58,10 +79,11 @@ state_to_enabled (GBinding     *binding,
                   GValue       *to_value,
                   gpointer      user_data)
 {
-  if (g_value_get_enum (from_value) == IDE_BUFFER_STATE_READY)
-    g_value_set_boolean (to_value, TRUE);
-  else
-    g_value_set_boolean (to_value, FALSE);
+  GbpSpellBufferAddin *self = user_data;
+
+  g_assert (GBP_IS_SPELL_BUFFER_ADDIN (self));
+
+  g_value_set_boolean (to_value, gbp_spell_buffer_addin_calculate_enabled (self));
 
   return TRUE;
 }
@@ -114,11 +136,11 @@ gbp_spell_buffer_addin_load (IdeBufferAddin *addin,
                            self,
                            G_CONNECT_SWAPPED);
 
-  self->enabled_action = g_property_action_new ("enabled", self->adapter, "enabled");
+  self->enabled_action = g_property_action_new ("enabled", self, "enabled");
 
   g_object_bind_property_full (buffer, "state", self->adapter, "enabled",
                                G_BINDING_SYNC_CREATE,
-                               state_to_enabled, NULL, NULL, NULL);
+                               state_to_enabled, NULL, self, NULL);
 
   IDE_EXIT;
 }
@@ -144,6 +166,8 @@ gbp_spell_buffer_addin_unload (IdeBufferAddin *addin,
 
   self->buffer = NULL;
 
+  self->enabled = FALSE;
+
   IDE_EXIT;
 }
 
@@ -157,14 +181,101 @@ buffer_addin_iface_init (IdeBufferAddinInterface *iface)
 G_DEFINE_FINAL_TYPE_WITH_CODE (GbpSpellBufferAddin, gbp_spell_buffer_addin, G_TYPE_OBJECT,
                                G_IMPLEMENT_INTERFACE (IDE_TYPE_BUFFER_ADDIN, buffer_addin_iface_init))
 
+static void
+gbp_spell_buffer_addin_set_enabled (GbpSpellBufferAddin *self,
+                                    gboolean             enabled)
+{
+  g_assert (GBP_IS_SPELL_BUFFER_ADDIN (self));
+
+  enabled = !!enabled;
+
+  if (enabled != self->enabled)
+    {
+      self->enabled = enabled;
+      if (self->adapter != NULL)
+        editor_text_buffer_spell_adapter_set_enabled (self->adapter,
+                                                      gbp_spell_buffer_addin_calculate_enabled (self));
+      g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_ENABLED]);
+    }
+}
+
+static gboolean
+gbp_spell_buffer_addin_get_enabled (GbpSpellBufferAddin *self)
+{
+  g_assert (GBP_IS_SPELL_BUFFER_ADDIN (self));
+
+  return self->enabled;
+}
+
+static void
+gbp_spell_buffer_addin_get_property (GObject    *object,
+                                     guint       prop_id,
+                                     GValue     *value,
+                                     GParamSpec *pspec)
+{
+  GbpSpellBufferAddin *self = GBP_SPELL_BUFFER_ADDIN (object);
+
+  switch (prop_id)
+    {
+    case PROP_ENABLED:
+      g_value_set_boolean (value, gbp_spell_buffer_addin_get_enabled (self));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+gbp_spell_buffer_addin_set_property (GObject      *object,
+                                     guint         prop_id,
+                                     const GValue *value,
+                                     GParamSpec   *pspec)
+{
+  GbpSpellBufferAddin *self = GBP_SPELL_BUFFER_ADDIN (object);
+
+  switch (prop_id)
+    {
+    case PROP_ENABLED:
+      gbp_spell_buffer_addin_set_enabled (self, g_value_get_boolean (value));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
 static void
 gbp_spell_buffer_addin_class_init (GbpSpellBufferAddinClass *klass)
 {
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->get_property = gbp_spell_buffer_addin_get_property;
+  object_class->set_property = gbp_spell_buffer_addin_set_property;
+
+  properties [PROP_ENABLED] =
+    g_param_spec_boolean ("enabled",
+                          "Enabled",
+                          "If spellcheck is enabled for the buffer",
+                          FALSE,
+                          (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+  g_object_class_install_properties (object_class, N_PROPS, properties);
 }
 
 static void
 gbp_spell_buffer_addin_init (GbpSpellBufferAddin *self)
 {
+  if (settings == NULL)
+    {
+      settings = g_settings_new ("org.gnome.builder.spelling");
+      g_debug ("Spellcheck settings loaded with initial value of %s",
+               g_settings_get_boolean (settings, "check-spelling") ? "true" : "false");
+    }
+
+  g_settings_bind (settings, "check-spelling",
+                   self, "enabled",
+                   G_SETTINGS_BIND_GET);
 }
 
 void


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