[gnome-builder] spellcheck: allow dynamic enabling via begin/end calls
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-builder] spellcheck: allow dynamic enabling via begin/end calls
- Date: Wed, 19 Jul 2017 11:21:00 +0000 (UTC)
commit ab695fd5857c811f7670afd65ce50b8411338337
Author: Christian Hergert <chergert redhat com>
Date: Sun Jul 16 13:46:13 2017 -0700
spellcheck: allow dynamic enabling via begin/end calls
This allows views to dynamically enable spellcheck features on
a buffer by calling the begin/end pairs. This allows both full
time enablement of spellcheck features as well as only enabling
during spellcheck operations.
plugins/spellcheck/gbp-spell-buffer-addin.c | 106 ++++++++++++++++++++++++---
plugins/spellcheck/gbp-spell-buffer-addin.h | 4 +-
2 files changed, 99 insertions(+), 11 deletions(-)
---
diff --git a/plugins/spellcheck/gbp-spell-buffer-addin.c b/plugins/spellcheck/gbp-spell-buffer-addin.c
index cb9f3f0..516d392 100644
--- a/plugins/spellcheck/gbp-spell-buffer-addin.c
+++ b/plugins/spellcheck/gbp-spell-buffer-addin.c
@@ -30,6 +30,14 @@ struct _GbpSpellBufferAddin
/* Owned spellchecker instance */
GspellChecker *spellchecker;
+ /* To allow for dynamic enabling of the inline spellcheck, we keep
+ * track of how many views need it. We will enable the feature in
+ * the buffer if it has manually been enabled (see @enabled) or if
+ * this value is >= 1.
+ */
+ gint count;
+
+ /* Manual enabling of inline checking */
guint enabled : 1;
};
@@ -39,6 +47,14 @@ enum {
N_PROPS
};
+static gboolean
+gbp_spell_buffer_addin_get_enabled (GbpSpellBufferAddin *self)
+{
+ g_assert (GBP_IS_SPELL_BUFFER_ADDIN (self));
+
+ return self->enabled || self->count > 0;
+}
+
static void
gbp_spell_buffer_addin_apply (GbpSpellBufferAddin *self)
{
@@ -48,21 +64,28 @@ gbp_spell_buffer_addin_apply (GbpSpellBufferAddin *self)
g_assert (GBP_IS_SPELL_BUFFER_ADDIN (self));
- if (self->enabled == FALSE || self->buffer == NULL)
+ /* We might be disposed */
+ if (self->buffer == NULL)
+ return;
+
+ spell_buffer = gspell_text_buffer_get_from_gtk_text_buffer (GTK_TEXT_BUFFER (self->buffer));
+
+ if (!gbp_spell_buffer_addin_get_enabled (self))
{
+ gspell_text_buffer_set_spell_checker (spell_buffer, NULL);
g_clear_object (&self->spellchecker);
return;
}
- /* The returned GspellTextBuffer is owned by self->buffer */
- spell_buffer = gspell_text_buffer_get_from_gtk_text_buffer (GTK_TEXT_BUFFER (self->buffer));
- g_assert (GSPELL_IS_TEXT_BUFFER (spell_buffer));
+ if (self->spellchecker == NULL)
+ {
- /* Setup the spell checker for the buffer. We retrain the spellchecker
- * instance so that we can add words/modify the dictionary at runtime.
- */
- self->spellchecker = gspell_checker_new (NULL);
- gspell_text_buffer_set_spell_checker (spell_buffer, self->spellchecker);
+ /* Setup the spell checker for the buffer. We retrain the spellchecker
+ * instance so that we can add words/modify the dictionary at runtime.
+ */
+ self->spellchecker = gspell_checker_new (NULL);
+ gspell_text_buffer_set_spell_checker (spell_buffer, self->spellchecker);
+ }
IDE_EXIT;
}
@@ -126,7 +149,7 @@ gbp_spell_buffer_addin_get_property (GObject *object,
switch (prop_id)
{
case PROP_ENABLED:
- g_value_set_boolean (value, self->enabled);
+ g_value_set_boolean (value, gbp_spell_buffer_addin_get_enabled (self));
break;
default:
@@ -195,3 +218,66 @@ gbp_spell_buffer_addin_get_checker (GbpSpellBufferAddin *self)
return self->spellchecker;
}
+
+/**
+ * gbp_spell_buffer_addin_begin_checking:
+ * @self: a #GbpSpellBufferAddin
+ *
+ * Views should call this function when they begin their spellchecking
+ * process. It dynamically enables various features on the buffer that
+ * are necessary for spellchecking.
+ *
+ * When done, the consumer MUST call gbp_spell_buffer_addin_end_checking()
+ * to complete the process. If no more views are active, spellchecking
+ * may be disabled on the buffer.
+ *
+ * Since: 3.26
+ */
+void
+gbp_spell_buffer_addin_begin_checking (GbpSpellBufferAddin *self)
+{
+ gboolean before_state;
+ gboolean after_state;
+
+ g_return_if_fail (GBP_IS_SPELL_BUFFER_ADDIN (self));
+ g_return_if_fail (self->count >= 0);
+
+ before_state = gbp_spell_buffer_addin_get_enabled (self);
+ self->count++;
+ after_state = gbp_spell_buffer_addin_get_enabled (self);
+
+ if (before_state != after_state)
+ {
+ gbp_spell_buffer_addin_apply (self);
+ g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_ENABLED]);
+ }
+}
+
+/**
+ * gbp_spell_buffer_addin_end_checking:
+ * @self: a #GbpSpellBufferAddin
+ *
+ * Completes a spellcheck operation. The buffer will return to it's original
+ * state. Thay may mean inline checking is disabled.
+ *
+ * Returns:
+ */
+void
+gbp_spell_buffer_addin_end_checking (GbpSpellBufferAddin *self)
+{
+ gboolean before_state;
+ gboolean after_state;
+
+ g_return_if_fail (GBP_IS_SPELL_BUFFER_ADDIN (self));
+ g_return_if_fail (self->count >= 0);
+
+ before_state = gbp_spell_buffer_addin_get_enabled (self);
+ self->count--;
+ after_state = gbp_spell_buffer_addin_get_enabled (self);
+
+ if (before_state != after_state)
+ {
+ gbp_spell_buffer_addin_apply (self);
+ g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_ENABLED]);
+ }
+}
diff --git a/plugins/spellcheck/gbp-spell-buffer-addin.h b/plugins/spellcheck/gbp-spell-buffer-addin.h
index 7251a6e..b5ed444 100644
--- a/plugins/spellcheck/gbp-spell-buffer-addin.h
+++ b/plugins/spellcheck/gbp-spell-buffer-addin.h
@@ -27,6 +27,8 @@ G_BEGIN_DECLS
G_DECLARE_FINAL_TYPE (GbpSpellBufferAddin, gbp_spell_buffer_addin, GBP, SPELL_BUFFER_ADDIN, GObject)
-GspellChecker *gbp_spell_buffer_addin_get_checker (GbpSpellBufferAddin *self);
+GspellChecker *gbp_spell_buffer_addin_get_checker (GbpSpellBufferAddin *self);
+void gbp_spell_buffer_addin_begin_checking (GbpSpellBufferAddin *self);
+void gbp_spell_buffer_addin_end_checking (GbpSpellBufferAddin *self);
G_END_DECLS
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]