[orca] Rewrite setLocusOfFocus and default script's onCaretMoved



commit f844e9e609d58c82fd740ea6b224e1cd3126e04c
Author: Joanmarie Diggs <jdiggs igalia com>
Date:   Tue Nov 17 19:04:37 2015 -0500

    Rewrite setLocusOfFocus and default script's onCaretMoved
    
    * Be less strict about updating locusOfFocus so we don't ignore
      valid events
    * Eliminate some non-performant logic

 src/orca/orca.py            |   87 +++++++++++++++++-------------------------
 src/orca/scripts/default.py |   51 +++++++++----------------
 2 files changed, 54 insertions(+), 84 deletions(-)
---
diff --git a/src/orca/orca.py b/src/orca/orca.py
index 62acbb7..dfe031c 100644
--- a/src/orca/orca.py
+++ b/src/orca/orca.py
@@ -139,66 +139,49 @@ def setLocusOfFocus(event, obj, notifyScript=True, force=False):
     """
 
     if not force and obj == orca_state.locusOfFocus:
+        msg = "INFO: Setting locusOfFocus to existing locusOfFocus"
+        debug.println(debug.LEVEL_INFO, msg)
         return
 
-    # If this event is not for the currently active script, then just return.
-    #
-    if event and event.source and \
-       event.host_application and orca_state.activeScript:
-        currentApp = orca_state.activeScript.app
-        try:
-            appList = [event.host_application, event.source.getApplication()]
-        except (LookupError, RuntimeError):
-            appList = []
-            debug.println(debug.LEVEL_SEVERE,
-                           "orca.setLocusOfFocus() application Error")
-        if not currentApp in appList:
-            return
+    if event and (orca_state.activeScript and not orca_state.activeScript.app):
+        script = _scriptManager.getScript(event.host_application, event.source)
+        _scriptManager.setActiveScript(script, "Setting locusOfFocus")
 
-    oldLocusOfFocus = orca_state.locusOfFocus
+    oldFocus = orca_state.locusOfFocus
     try:
-        # Just to see if we have a valid object.
-        oldLocusOfFocus.getRole()
+        oldFocus.getRole()
     except:
-        # Either it's None or it's an invalid remote object.
-        oldLocusOfFocus = None
+        msg = "INFO: Old locusOfFocus is null or defunct"
+        debug.println(debug.LEVEL_INFO, msg)
+        oldFocus = None
+
+    if not obj:
+        msg = "INFO: New locusOfFocus is null (being cleared)"
+        debug.println(debug.LEVEL_INFO, msg)
+        orca_state.locusOfFocus = None
+        return
 
-    orca_state.locusOfFocus = obj
     try:
-        app = orca_state.locusOfFocus.getApplication()
+        app = obj.getApplication()
     except:
+        msg = "ERROR: Exception getting application for %s" % obj
+        debug.println(debug.LEVEL_INFO, msg)
         orca_state.locusOfFocus = None
-        if event:
-            debug.println(debug.LEVEL_FINE,
-                          "LOCUS OF FOCUS: None event='%s'" % event.type)
-        else:
-            debug.println(debug.LEVEL_FINE,
-                          "LOCUS OF FOCUS: None event=None")
-    else:
-        try:
-            appname = "'" + app.name + "'"
-        except:
-            appname = "None"
-        try:
-            name = orca_state.locusOfFocus.name
-            rolename = orca_state.locusOfFocus.getRoleName()
-        except:
-            name = "Error"
-            rolename = "Error"
-        debug.println(debug.LEVEL_FINE,
-                      "LOCUS OF FOCUS: app=%s name='%s' role='%s'" \
-                      % (appname, name, rolename))
-
-        if event:
-            debug.println(debug.LEVEL_FINE,
-                          "                event='%s'" % event.type)
-        else:
-            debug.println(debug.LEVEL_FINE,
-                          "                event=None")
+        return
+
+    msg = "INFO: Changing locusOfFocus from %s to %s" % (oldFocus, obj)
+    debug.println(debug.LEVEL_INFO, msg)
+    orca_state.locusOfFocus = obj
+
+    if not notifyScript:
+        return
+
+    if not orca_state.activeScript:
+        msg = "INFO: Cannot notify active script because there isn't one"
+        debug.println(debug.LEVEL_INFO, msg)
+        return
 
-    if notifyScript and orca_state.activeScript:
-        orca_state.activeScript.locusOfFocusChanged(
-            event, oldLocusOfFocus, orca_state.locusOfFocus)
+    orca_state.activeScript.locusOfFocusChanged(event, oldFocus, orca_state.locusOfFocus)
 
 ########################################################################
 #                                                                      #
diff --git a/src/orca/scripts/default.py b/src/orca/scripts/default.py
index 7da859b..38068f5 100644
--- a/src/orca/scripts/default.py
+++ b/src/orca/scripts/default.py
@@ -2134,52 +2134,39 @@ class Script(script.Script):
         pass
 
     def onCaretMoved(self, event):
-        """Called whenever the caret moves.
-
-        Arguments:
-        - event: the Event
-        """
-
-        if not orca_state.locusOfFocus:
-            return
+        """Callback for object:text-caret-moved accessibility events."""
 
         obj, offset = self.pointOfReference.get("lastCursorPosition", (None, -1))
-        if offset == event.detail1 \
-           and self.utilities.isSameObject(obj, event.source):
+        if offset == event.detail1 and obj == event.source:
+            msg = "DEFAULT: Event is for last saved cursor position"
+            debug.println(debug.LEVEL_INFO, msg)
             return
 
-        # Should the event source be the locusOfFocus?
-        #
-        try:
-            role = orca_state.locusOfFocus.getRole()
-        except (LookupError, RuntimeError):
-            role = None
-        if role in [pyatspi.ROLE_FRAME, pyatspi.ROLE_DIALOG]:
-            frameApp = orca_state.locusOfFocus.getApplication()
-            eventApp = event.source.getApplication()
-            if frameApp == eventApp \
-               and event.source.getState().contains(pyatspi.STATE_FOCUSED):
-                orca.setLocusOfFocus(event, event.source, False)
-
-        # Ignore caret movements from non-focused objects, unless the
-        # currently focused object is the parent of the object which
-        # has the caret.
-        #
-        if (event.source != orca_state.locusOfFocus) \
-            and (event.source.parent != orca_state.locusOfFocus):
+        if event.source != orca_state.locusOfFocus \
+           and event.source.getState().contains(pyatspi.STATE_FOCUSED):
+            msg = "DEFAULT: Updating locusOfFocus from %s to %s" % \
+                  (orca_state.locusOfFocus, event.source)
+            debug.println(debug.LEVEL_INFO, msg)
+            orca.setLocusOfFocus(event, event.source, False)
+
+        if event.source != orca_state.locusOfFocus:
+            msg = "DEFAULT: Event source (%s) is not locusOfFocus (%s)" \
+                  % (event.source, orca_state.locusOfFocus)
+            debug.println(debug.LEVEL_INFO, msg)
             return
 
-        # We always automatically go back to focus tracking mode when
-        # the caret moves in the focused object.
-        #
         if self.flatReviewContext:
             self.toggleFlatReviewMode()
 
         text = event.source.queryText()
         self._saveLastCursorPosition(event.source, text.caretOffset)
         if text.getNSelections():
+            msg = "DEFAULT: Event source has text selections"
+            debug.println(debug.LEVEL_INFO, msg)
             return
 
+        msg = "DEFAULT: Presenting text at new caret position"
+        debug.println(debug.LEVEL_INFO, msg)
         self._presentTextAtNewCaretPosition(event)
 
     def onDocumentReload(self, event):


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