[gnome-calculator/gnome-3-32] Ignore keypresses while calculating result (fixes #47)



commit 2276d12dec91361370155b1c52a2fe6429a74a74
Author: Robert Roth <robert roth off gmail com>
Date:   Tue Apr 2 01:54:45 2019 +0300

    Ignore keypresses while calculating result (fixes #47)

 lib/math-equation.vala | 20 ++++++++++++++------
 src/math-display.vala  | 23 ++++++++++++++---------
 2 files changed, 28 insertions(+), 15 deletions(-)
---
diff --git a/lib/math-equation.vala b/lib/math-equation.vala
index 1932e6a2..da1da2e3 100644
--- a/lib/math-equation.vala
+++ b/lib/math-equation.vala
@@ -135,6 +135,8 @@ public class MathEquation : Gtk.SourceBuffer
     private bool in_reformat;
 
     private bool in_delete;
+    private uint in_progress_timeout;
+    private uint looking_for_answer_timeout;
 
     private bool _in_solve;
     public bool in_solve
@@ -1077,8 +1079,8 @@ public class MathEquation : Gtk.SourceBuffer
 
         new Thread<void*> ("", solve_real);
 
-        Timeout.add (50, look_for_answer);
-        Timeout.add (100, show_in_progress);
+        looking_for_answer_timeout = Timeout.add (50, look_for_answer);
+        in_progress_timeout = Timeout.add (100, show_in_progress);
     }
 
     /* Fix the offsets to consider thousand separators inserted by the gui. */
@@ -1181,6 +1183,8 @@ public class MathEquation : Gtk.SourceBuffer
     {
         var x = number;
         var factors = x.factorize ();
+        var result = new SolveData ();
+        queue.push (result);
 
         var text = "";
         var i = 0;
@@ -1192,9 +1196,7 @@ public class MathEquation : Gtk.SourceBuffer
             i++;
         }
 
-        var result = new SolveData ();
         result.text_result = text;
-        queue.push (result);
 
         return null;
     }
@@ -1217,8 +1219,8 @@ public class MathEquation : Gtk.SourceBuffer
 
         new Thread<void*> ("", factorize_real);
 
-        Timeout.add (50, look_for_answer);
-        Timeout.add (100, show_in_progress);
+        looking_for_answer_timeout = Timeout.add (50, look_for_answer);
+        in_progress_timeout = Timeout.add (100, show_in_progress);
     }
 
     public void delete_next ()
@@ -1255,6 +1257,12 @@ public class MathEquation : Gtk.SourceBuffer
         number_mode = NumberMode.NORMAL;
         set_text ("", -1);
         clear_ans (false);
+        if (in_solve) {
+            _in_solve = false;
+            queue.try_pop ();
+            Source.remove (looking_for_answer_timeout);
+            Source.remove (in_progress_timeout);
+        }
     }
 
     public void shift (int count)
diff --git a/src/math-display.vala b/src/math-display.vala
index 87528e0c..238c18e0 100644
--- a/src/math-display.vala
+++ b/src/math-display.vala
@@ -148,6 +148,20 @@ public class MathDisplay : Gtk.Viewport
 
     private bool key_press_cb (Gdk.EventKey event)
     {
+        /* Clear on escape */
+        var state = event.state & (Gdk.ModifierType.CONTROL_MASK | Gdk.ModifierType.MOD1_MASK);
+        if ((event.keyval == Gdk.Key.Escape && state == 0) ||
+            (event.keyval == Gdk.Key.Delete && state == Gdk.ModifierType.SHIFT_MASK))
+        {
+            equation.clear ();
+            status_changed_cb ();
+            return true;
+        }
+
+        /* Ignore keypresses while calculating */
+        if (equation.in_solve)
+            return true;
+
         /* Treat keypad keys as numbers even when numlock is off */
         uint new_keyval = 0;
         switch (event.keyval)
@@ -194,7 +208,6 @@ public class MathDisplay : Gtk.Viewport
             return key_press_event (new_event);
         }
 
-        var state = event.state & (Gdk.ModifierType.CONTROL_MASK | Gdk.ModifierType.MOD1_MASK);
         var c = Gdk.keyval_to_unicode (event.keyval);
 
         /* Solve on enter */
@@ -206,14 +219,6 @@ public class MathDisplay : Gtk.Viewport
             return true;
         }
 
-        /* Clear on escape */
-        if ((event.keyval == Gdk.Key.Escape && state == 0) ||
-            (event.keyval == Gdk.Key.Delete && state == Gdk.ModifierType.SHIFT_MASK))
-        {
-            equation.clear ();
-            return true;
-        }
-
         /* Numeric keypad will insert '.' or ',' depending on layout */
         if ((event.keyval == Gdk.Key.KP_Decimal) ||
             (event.keyval == Gdk.Key.KP_Separator) ||


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