[gedit/wip/spell-checking] auto-spell: check region after a timeout



commit 0fa714aa023a934baecbcef402142f401e7692bd
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Tue Jul 21 10:05:30 2015 +0200

    auto-spell: check region after a timeout

 plugins/spell/gedit-automatic-spell-checker.c |   37 +++++++++++++++++++++++-
 1 files changed, 35 insertions(+), 2 deletions(-)
---
diff --git a/plugins/spell/gedit-automatic-spell-checker.c b/plugins/spell/gedit-automatic-spell-checker.c
index 6764dd7..d4e8235 100644
--- a/plugins/spell/gedit-automatic-spell-checker.c
+++ b/plugins/spell/gedit-automatic-spell-checker.c
@@ -45,6 +45,7 @@ struct _GeditAutomaticSpellChecker
        GtkTextMark *mark_click;
 
        GtkTextRegion *scan_region;
+       guint timeout_id;
 };
 
 enum
@@ -57,6 +58,8 @@ enum
 #define AUTOMATIC_SPELL_CHECKER_KEY    "GeditAutomaticSpellCheckerID"
 #define SUGGESTION_KEY                 "GeditAutoSuggestionID"
 
+#define TIMEOUT_DURATION 200 /* in milliseconds */
+
 G_DEFINE_TYPE (GeditAutomaticSpellChecker, gedit_automatic_spell_checker, G_TYPE_OBJECT)
 
 static void
@@ -196,6 +199,28 @@ check_region (GeditAutomaticSpellChecker *spell)
        spell->scan_region = NULL;
 }
 
+static gboolean
+timeout_cb (GeditAutomaticSpellChecker *spell)
+{
+       check_region (spell);
+
+       spell->timeout_id = 0;
+       return G_SOURCE_REMOVE;
+}
+
+static void
+install_timeout (GeditAutomaticSpellChecker *spell)
+{
+       if (spell->timeout_id != 0)
+       {
+               g_source_remove (spell->timeout_id);
+       }
+
+       spell->timeout_id = g_timeout_add (TIMEOUT_DURATION,
+                                          (GSourceFunc) timeout_cb,
+                                          spell);
+}
+
 static void
 add_subregion_to_scan (GeditAutomaticSpellChecker *spell,
                       const GtkTextIter          *start,
@@ -216,8 +241,6 @@ add_subregion_to_scan (GeditAutomaticSpellChecker *spell,
        gtk_text_region_add (spell->scan_region,
                             &start_adjusted,
                             &end_adjusted);
-
-       check_region (spell);
 }
 
 static void
@@ -234,6 +257,7 @@ insert_text_after_cb (GtkTextBuffer              *buffer,
        gtk_text_iter_backward_chars (&start, g_utf8_strlen (text, length));
 
        add_subregion_to_scan (spell, &start, &end);
+       install_timeout (spell);
 }
 
 static void
@@ -243,6 +267,7 @@ delete_range_after_cb (GtkTextBuffer              *buffer,
                       GeditAutomaticSpellChecker *spell)
 {
        add_subregion_to_scan (spell, start, end);
+       install_timeout (spell);
 }
 
 static gboolean
@@ -634,6 +659,7 @@ highlight_updated_cb (GtkSourceBuffer            *buffer,
                      GeditAutomaticSpellChecker *spell)
 {
        add_subregion_to_scan (spell, start, end);
+       install_timeout (spell);
 }
 
 static void
@@ -828,6 +854,12 @@ gedit_automatic_spell_checker_dispose (GObject *object)
                spell->scan_region = NULL;
        }
 
+       if (spell->timeout_id != 0)
+       {
+               g_source_remove (spell->timeout_id);
+               spell->timeout_id = 0;
+       }
+
        G_OBJECT_CLASS (gedit_automatic_spell_checker_parent_class)->dispose (object);
 }
 
@@ -946,6 +978,7 @@ gedit_automatic_spell_checker_recheck_all (GeditAutomaticSpellChecker *spell)
        gtk_text_buffer_get_bounds (spell->buffer, &start, &end);
 
        add_subregion_to_scan (spell, &start, &end);
+       check_region (spell);
 }
 
 /* ex:set ts=8 noet: */


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