[orca] Cache the string in addition to the offsets for selected text



commit 10c3b486e2e05d37334f5f2f3cb4945dfd2ca53c
Author: Joanmarie Diggs <jdiggs igalia com>
Date:   Mon Nov 30 16:30:54 2015 -0500

    Cache the string in addition to the offsets for selected text

 src/orca/script_utilities.py            |   33 +++++++++++++++++++++++++++++++
 src/orca/scripts/apps/evince/script.py  |    3 +-
 src/orca/scripts/apps/soffice/script.py |    3 +-
 src/orca/scripts/default.py             |   21 ++++---------------
 4 files changed, 40 insertions(+), 20 deletions(-)
---
diff --git a/src/orca/script_utilities.py b/src/orca/script_utilities.py
index fb0b2a5..9747ecd 100644
--- a/src/orca/script_utilities.py
+++ b/src/orca/script_utilities.py
@@ -3389,3 +3389,36 @@ class Utilities:
         position = siblings.index(obj)
         setSize = len(siblings)
         return position, setSize
+
+    def getCachedTextSelection(self, obj):
+        textSelections = self._script.pointOfReference.get('textSelections', {})
+        start, end, string = textSelections.get(hash(obj), (0, 0, ''))
+        msg = "INFO: Cached selection for %s is '%s' (%i, %i)" % (obj, string, start, end)
+        debug.println(debug.LEVEL_INFO, msg, True)
+        return start, end, string
+
+    def updateCachedTextSelection(self, obj):
+        try:
+            text = obj.queryText()
+        except NotImplementedError:
+            msg = "ERROR: %s doesn't implement AtspiText" % obj
+            debug.println(debug.LEVEL_INFO, msg, True)
+            text = None
+        except:
+            msg = "ERROR: Exception querying text interface for %s" % obj
+            debug.println(debug.LEVEL_INFO, msg, True)
+            text = None
+
+        # TODO: JD - this doesn't yet handle the case of multiple non-contiguous
+        # selections in a single accessible object.
+        textSelections = self._script.pointOfReference.get('textSelections', {})
+        if text:
+            start, end = text.getSelection(0)
+            string = text.getText(start, end)
+        else:
+            start, end, string = 0, 0, ''
+
+        msg = "INFO: New selection for %s is '%s' (%i, %i)" % (obj, string, start, end)
+        debug.println(debug.LEVEL_INFO, msg, True)
+        textSelections[hash(obj)] = start, end, string
+        self._script.pointOfReference['textSelections'] = textSelections
diff --git a/src/orca/scripts/apps/evince/script.py b/src/orca/scripts/apps/evince/script.py
index c140948..2b03d4c 100644
--- a/src/orca/scripts/apps/evince/script.py
+++ b/src/orca/scripts/apps/evince/script.py
@@ -129,8 +129,7 @@ class Script(gtk.Script):
         # location no longer exists.
 
         obj = event.source
-        textSelections = self.pointOfReference.get('textSelections', {})
-        oldStart, oldEnd = textSelections.get(hash(obj), (0, 0))
+        oldStart, oldEnd, oldString = self.utilities.getCachedTextSelection(obj)
 
         crossedPages = False
         keyString, mods = self.utilities.lastKeyAndModifiers()
diff --git a/src/orca/scripts/apps/soffice/script.py b/src/orca/scripts/apps/soffice/script.py
index b1afa8a..cc6a75f 100644
--- a/src/orca/scripts/apps/soffice/script.py
+++ b/src/orca/scripts/apps/soffice/script.py
@@ -774,8 +774,7 @@ class Script(default.Script):
 
         if role == pyatspi.ROLE_PARAGRAPH:
             obj, offset = self.pointOfReference.get("lastCursorPosition", (None, -1))
-            textSelections = self.pointOfReference.get('textSelections', {})
-            start, end = textSelections.get(hash(obj), (0, 0))
+            start, end, string = self.utilities.getCachedTextSelection(obj)
             if start != end:
                 return
 
diff --git a/src/orca/scripts/default.py b/src/orca/scripts/default.py
index 9a1f3fb..57278a3 100644
--- a/src/orca/scripts/default.py
+++ b/src/orca/scripts/default.py
@@ -765,9 +765,7 @@ class Script(script.Script):
             pass
         else:
             self._saveLastCursorPosition(obj, max(0, text.caretOffset))
-            textSelections = self.pointOfReference.get('textSelections', {})
-            textSelections[hash(obj)] = text.getSelection(0)
-            self.pointOfReference['textSelections'] = textSelections
+        self.utilities.updateCachedTextSelection(obj)
 
         # We want to save the current row and column of a newly focused
         # or selected table cell so that on subsequent cell focus/selection
@@ -2765,23 +2763,14 @@ class Script(script.Script):
         # to text selection will get eliminated once the new text-selection API
         # is added to ATK and implemented by the toolkits. (BGO 638378)
 
-        textSelections = self.pointOfReference.get('textSelections', {})
-        oldStart, oldEnd = textSelections.get(hash(obj), (0, 0))
-
-        # TODO: JD - this doesn't yet handle the case of multiple non-contiguous
-        # selections in a single accessible object.
-
-        text = obj.queryText()
-        newStart, newEnd = text.getSelection(0)
-        textSelections[hash(obj)] = newStart, newEnd
-        self.pointOfReference['textSelections'] = textSelections
+        oldStart, oldEnd, oldString = self.utilities.getCachedTextSelection(obj)
+        self.utilities.updateCachedTextSelection(obj)
+        newStart, newEnd, newString = self.utilities.getCachedTextSelection(obj)
 
         if self.pointOfReference.get('lastAutoComplete') == hash(obj):
             return
 
-        nSelections = text.getNSelections()
-        handled = self._speakTextSelectionState(nSelections)
-        if handled:
+        if self._speakTextSelectionState(max(1, len(newString))):
             return
 
         changes = []


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