[gnome-calculator] Implemented navigation through results using keyboard (fixes #129)



commit 8417af309b59f8e5e575197fafad247c955337b5
Author: Robert Roth <robert roth off gmail com>
Date:   Sat Feb 1 21:13:51 2020 +0200

    Implemented navigation through results using keyboard (fixes #129)

 src/math-display.vala | 17 +++++++++++++++++
 src/math-history.vala | 17 ++++++++++++++++-
 src/math-shortcuts.ui | 14 ++++++++++++++
 3 files changed, 47 insertions(+), 1 deletion(-)
---
diff --git a/src/math-display.vala b/src/math-display.vala
index e420029d..b08d53bf 100644
--- a/src/math-display.vala
+++ b/src/math-display.vala
@@ -161,6 +161,23 @@ public class MathDisplay : Gtk.Viewport
             Gtk.SourceCompletion completion = source_view.get_completion ();
             completion.hide ();
             return true;
+        } else if (state == Gdk.ModifierType.MOD1_MASK && (event.keyval == Gdk.Key.Left || event.keyval == 
Gdk.Key.Right))
+        {
+            switch (event.keyval)
+            {
+            case Gdk.Key.Left:
+                history.current -= 1;
+                break;
+            case Gdk.Key.Right:
+                history.current += 1;
+                break;
+            }
+            HistoryEntry? entry = history.get_entry_at (history.current);
+            if (entry != null) {
+                equation.clear();
+                insert_text (entry.answer_label.get_text ());
+            }
+            return true;
         }
 
         /* Ignore keypresses while calculating */
diff --git a/src/math-history.vala b/src/math-history.vala
index 53ab5eec..f30d53af 100644
--- a/src/math-history.vala
+++ b/src/math-history.vala
@@ -20,11 +20,22 @@ public class HistoryView : Gtk.ScrolledWindow
     [GtkChild]
     Gtk.ListBox listbox;
 
+    private int _rows = 0;
+    private int _current = 0;
+    public int rows {get {return _rows;} }
+    public int current {get {return _current;} set {_current = value.clamp(0, _rows); } }
     public signal void answer_clicked   (string ans);
     public signal void equation_clicked (string equation);
     public signal void row_added       ();
 
 
+    public HistoryEntry? get_entry_at(int index)
+    {
+        if ( index >= 0 && index < rows)
+            return (HistoryEntry?) listbox.get_row_at_index (index);
+        return null;
+    }
+
     [GtkCallback]
     public void scroll_bottom ()
     {
@@ -61,11 +72,15 @@ public class HistoryView : Gtk.ScrolledWindow
 
         last_answer = answer_nine_digits;
         last_equation = equation;
+        _rows++;
+        current = rows - 1;
         row_added ();
     }
 
     public void clear ()
     {
+        _rows = 0;
+        _current = 0;
         listbox.foreach ((child) => { listbox.remove(child); });
     }
 }
@@ -76,7 +91,7 @@ public class HistoryEntry : Gtk.ListBoxRow
     [GtkChild]
     Gtk.Label equation_label;
     [GtkChild]
-    Gtk.Label answer_label;
+    public Gtk.Label answer_label;
 
     public signal void answer_clicked (string ans);
     public signal void equation_clicked (string equation);
diff --git a/src/math-shortcuts.ui b/src/math-shortcuts.ui
index df91ac11..abf4ee1d 100644
--- a/src/math-shortcuts.ui
+++ b/src/math-shortcuts.ui
@@ -219,6 +219,20 @@
                 <property name="title" translatable="yes" context="shortcut window">Redo</property>
               </object>
             </child>
+            <child>
+              <object class="GtkShortcutsShortcut">
+                <property name="visible">1</property>
+                <property name="accelerator">&lt;alt&gt;Left</property>
+                <property name="title" translatable="yes" context="shortcut window">Previous 
result</property>
+              </object>
+            </child>
+            <child>
+              <object class="GtkShortcutsShortcut">
+                <property name="visible">1</property>
+                <property name="accelerator">&lt;alt&gt;Right</property>
+                <property name="title" translatable="yes" context="shortcut window">Next result</property>
+              </object>
+            </child>
             <child>
               <object class="GtkShortcutsShortcut">
                 <property name="visible">1</property>


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