[orca] Web: Make onCaretMoved more efficent



commit 86723c6b5855ff7a5793068035f12d99e7df4897
Author: Joanmarie Diggs <jdiggs igalia com>
Date:   Mon Jun 7 18:08:32 2021 +0200

    Web: Make onCaretMoved more efficent
    
    We need to do a number of checks to determine if and how every caret-moved
    event should be presented. Many of these checks were causing us to seek
    the document frame, and often doing so from the top down which can be
    less performant than searching the ancestry. Therefore, get the top-level
    document for the event source and include it as an argument where possible.

 src/orca/script_utilities.py             | 12 ++++--------
 src/orca/scripts/web/script.py           | 27 ++++++++++++++-------------
 src/orca/scripts/web/script_utilities.py | 11 ++++++-----
 3 files changed, 24 insertions(+), 26 deletions(-)
---
diff --git a/src/orca/script_utilities.py b/src/orca/script_utilities.py
index 851cad7c0..2450cec48 100644
--- a/src/orca/script_utilities.py
+++ b/src/orca/script_utilities.py
@@ -1237,15 +1237,11 @@ class Utilities:
     def activeDocument(self, window=None):
         return self.getTopLevelDocumentForObject(orca_state.locusOfFocus)
 
-    def getTopLevelDocumentForObject(self, obj):
-        document = self.getDocumentForObject(obj)
-        while document:
-            ancestor = pyatspi.findAncestor(document, self.isDocument)
-            if not ancestor or ancestor == document:
-                break
-            document = ancestor
+    def isTopLevelDocument(self, obj):
+        return self.isDocument(obj) and not pyatspi.findAncestor(obj, self.isDocument)
 
-        return document
+    def getTopLevelDocumentForObject(self, obj):
+        return pyatspi.findAncestor(obj, self.isTopLevelDocument)
 
     def getDocumentForObject(self, obj):
         if not obj:
diff --git a/src/orca/scripts/web/script.py b/src/orca/scripts/web/script.py
index 1f6a6e98f..62f948458 100644
--- a/src/orca/scripts/web/script.py
+++ b/src/orca/scripts/web/script.py
@@ -1547,22 +1547,23 @@ class Script(default.Script):
             debug.println(debug.LEVEL_INFO, msg, True)
             return True
 
-        if self.utilities.eventIsBrowserUINoise(event):
-            msg = "WEB: Ignoring event believed to be browser UI noise"
-            debug.println(debug.LEVEL_INFO, msg, True)
-            return True
+        document = self.utilities.getTopLevelDocumentForObject(event.source)
+        if not document:
+            if self.utilities.eventIsBrowserUINoise(event):
+                msg = "WEB: Ignoring event believed to be browser UI noise"
+                debug.println(debug.LEVEL_INFO, msg, True)
+                return True
 
-        if self.utilities.eventIsBrowserUIAutocompleteNoise(event):
-            msg = "WEB: Ignoring event believed to be browser UI autocomplete noise"
-            debug.println(debug.LEVEL_INFO, msg, True)
-            return True
+            if self.utilities.eventIsBrowserUIAutocompleteNoise(event):
+                msg = "WEB: Ignoring event believed to be browser UI autocomplete noise"
+                debug.println(debug.LEVEL_INFO, msg, True)
+                return True
 
-        if not self.utilities.inDocumentContent(event.source):
             msg = "WEB: Event source is not in document content"
             debug.println(debug.LEVEL_INFO, msg, True)
             return False
 
-        obj, offset = self.utilities.getCaretContext(getZombieReplicant=False, searchIfNeeded=False)
+        obj, offset = self.utilities.getCaretContext(document, False, False)
         msg = "WEB: Context: %s, %i (focus: %s)" % (obj, offset, orca_state.locusOfFocus)
         debug.println(debug.LEVEL_INFO, msg, True)
 
@@ -1641,7 +1642,7 @@ class Script(default.Script):
             self._saveLastCursorPosition(event.source, event.detail1)
             return True
 
-        if self.utilities.eventIsAutocompleteNoise(event):
+        if self.utilities.eventIsAutocompleteNoise(event, document):
             msg = "WEB: Event ignored: Autocomplete noise"
             debug.println(debug.LEVEL_INFO, msg, True)
             return True
@@ -1678,12 +1679,12 @@ class Script(default.Script):
             notify = force = handled = True
 
         elif self.utilities.lastInputEventWasCaretNav():
-            msg = "WEB: Caret moved to due native caret navigation."
+            msg = "WEB: Caret moved due to native caret navigation."
             debug.println(debug.LEVEL_INFO, msg, True)
 
         msg = "WEB: Setting context and focus to: %s, %i" % (obj, offset)
         debug.println(debug.LEVEL_INFO, msg, True)
-        self.utilities.setCaretContext(obj, offset)
+        self.utilities.setCaretContext(obj, offset, document)
         orca.setLocusOfFocus(event, obj, notify, force)
         return handled
 
diff --git a/src/orca/scripts/web/script_utilities.py b/src/orca/scripts/web/script_utilities.py
index d159ff373..be5ad6aae 100644
--- a/src/orca/scripts/web/script_utilities.py
+++ b/src/orca/scripts/web/script_utilities.py
@@ -4302,8 +4302,9 @@ class Utilities(script_utilities.Utilities):
 
         return False
 
-    def eventIsAutocompleteNoise(self, event):
-        if not self.inDocumentContent(event.source):
+    def eventIsAutocompleteNoise(self, event, documentFrame=None):
+        inContent = documentFrame or self.inDocumentContent(event.source)
+        if not inContent:
             return False
 
         isListBoxItem = lambda x: x and x.parent and x.parent.getRole() == pyatspi.ROLE_LIST_BOX
@@ -4312,7 +4313,7 @@ class Utilities(script_utilities.Utilities):
 
         if event.source.getState().contains(pyatspi.STATE_EDITABLE) \
            and event.type.startswith("object:text-"):
-            obj, offset = self.getCaretContext()
+            obj, offset = self.getCaretContext(documentFrame)
             if isListBoxItem(obj) or isMenuItem(obj):
                 return True
 
@@ -4731,7 +4732,7 @@ class Utilities(script_utilities.Utilities):
         return obj, offset
 
     def getCaretContext(self, documentFrame=None, getZombieReplicant=False, searchIfNeeded=True):
-        msg = "WEB: Getting caret context"
+        msg = "WEB: Getting caret context for %s" % documentFrame
         debug.println(debug.LEVEL_INFO, msg, True)
 
         if not documentFrame or self.isZombie(documentFrame):
@@ -4749,7 +4750,7 @@ class Utilities(script_utilities.Utilities):
             return obj, offset
 
         context = self._caretContexts.get(hash(documentFrame.parent))
-        if not context or documentFrame != self.getTopLevelDocumentForObject(context[0]):
+        if not context or not self.isTopLevelDocument(documentFrame):
             if not searchIfNeeded:
                 return None, -1
             obj, offset = self.searchForCaretContext(documentFrame)


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