[orca] Add debugging output and flag some todos



commit 27dbaf1ec05b5f81a869341e11cb04ce51a5f36d
Author: Joanmarie Diggs <jdiggs igalia com>
Date:   Tue Apr 28 16:42:58 2020 -0400

    Add debugging output and flag some todos

 src/orca/braille.py         | 144 ++++++++++++++++++++++++++------------------
 src/orca/flat_review.py     |   6 ++
 src/orca/scripts/default.py |  84 +++++++++++++++++++-------
 3 files changed, 151 insertions(+), 83 deletions(-)
---
diff --git a/src/orca/braille.py b/src/orca/braille.py
index 9db6636ca..a9012d0a8 100644
--- a/src/orca/braille.py
+++ b/src/orca/braille.py
@@ -1051,10 +1051,7 @@ def setFocus(region, panToFocus=True, getLinkMask=True):
 
     viewport[0] = max(0, offset)
 
-def refresh(panToCursor=True,
-            targetCursorCell=0,
-            getLinkMask=True,
-            stopFlash=True):
+def refresh(panToCursor=True, targetCursorCell=0, getLinkMask=True, stopFlash=True):
     """Repaints the Braille on the physical display.  This clips the entire
     logical structure by the viewport and also sets the cursor to the
     appropriate location.  [[[TODO: WDW - I'm not sure how BrlTTY handles
@@ -1062,9 +1059,7 @@ def refresh(panToCursor=True,
     drawing one line right now.]]]
 
     Arguments:
-
-    - panToCursor: if True, will adjust the viewport so the cursor is
-      showing.
+    - panToCursor: if True, will adjust the viewport so the cursor is showing.
     - targetCursorCell: Only effective if panToCursor is True.
       0 means automatically place the cursor somewhere on the display so
       as to minimize movement but show as much of the line as possible.
@@ -1078,39 +1073,26 @@ def refresh(panToCursor=True,
     - stopFlash: if True, kill any flashed message that may be showing.
     """
 
+    # TODO - JD: Split this work out into smaller methods.
+
     global endIsShowing
     global beginningIsShowing
     global cursorCell
     global _monitor
     global _lastTextInfo
 
-    # Check out what we were displaying the last time - it might be
-    # the same text object we are displaying now.
-    #
-    (lastTextObj, lastCaretOffset, lastLineOffset, lastCursorCell) = \
-        _lastTextInfo
-    if _regionWithFocus and isinstance(_regionWithFocus, Text):
-        currentTextObj = _regionWithFocus.accessible
-        currentCaretOffset = _regionWithFocus.caretOffset
-        currentLineOffset = _regionWithFocus.lineOffset
-    else:
-        currentTextObj = None
-        currentCaretOffset = 0
-        currentLineOffset = 0
+    msg = "BRAILLE: Refresh. Pan: %s target: %i" % (panToCursor, targetCursorCell)
+    debug.println(debug.LEVEL_INFO, msg, True)
 
     if stopFlash:
         killFlash(restoreSaved=False)
 
+    # TODO - JD: This should be taken care of in orca.py.
     if not _settingsManager.getSetting('enableBraille') \
        and not _settingsManager.getSetting('enableBrailleMonitor'):
         if _brlAPIRunning:
-            try:
-                _brlAPI.writeText("", 0)
-            except:
-                debug.println(debug.LEVEL_WARNING,
-                              "BrlTTY seems to have disappeared:")
-                debug.printException(debug.LEVEL_WARNING)
-            # In this case we always shut it down
+            msg = "BRAILLE: FIXME - Braille disabled, but not properly shut down."
+            debug.println(debug.LEVEL_INFO, msg, True)
             shutdown()
         _lastTextInfo = (None, 0, 0, 0)
         return
@@ -1122,57 +1104,85 @@ def refresh(panToCursor=True,
             try:
                 _brlAPI.writeText("", 0)
             except:
-                debug.println(debug.LEVEL_WARNING,
-                              "BrlTTY seems to have disappeared:")
-                debug.printException(debug.LEVEL_WARNING)
+                msg = "BRAILLE: BrlTTY seems to have disappeared."
+                debug.println(debug.LEVEL_WARNING, msg, True)
                 shutdown()
-            # In this case we just leave the display with empty text
         _lastTextInfo = (None, 0, 0, 0)
         return
 
-    # Now determine the location of the cursor.  First, we'll figure
-    # out the 1-based offset for where we want the cursor to be.  If
-    # the target cell is less than zero, it means an offset from the
-    # right hand side of the display.
-    #
+
+    lastTextObj, lastCaretOffset, lastLineOffset, lastCursorCell = _lastTextInfo
+    msg = "BRAILLE: Last text obj: %s (Caret: %i, Line: %i, Cell: %i)" % _lastTextInfo
+    debug.println(debug.LEVEL_INFO, msg, True)
+
+    if _regionWithFocus and isinstance(_regionWithFocus, Text):
+        currentTextObj = _regionWithFocus.accessible
+        currentCaretOffset = _regionWithFocus.caretOffset
+        currentLineOffset = _regionWithFocus.lineOffset
+    else:
+        currentTextObj = None
+        currentCaretOffset = 0
+        currentLineOffset = 0
+
+    onSameLine = currentTextObj and currentTextObj == lastTextObj \
+        and currentLineOffset == lastLineOffset
+
+    msg = "BRAILLE: Current text obj: %s (Caret: %i, Line: %i). On same line: %s" % \
+        (currentTextObj, currentCaretOffset, currentLineOffset, bool(onSameLine))
+    debug.println(debug.LEVEL_INFO, msg, True)
+
     if targetCursorCell < 0:
         targetCursorCell = _displaySize[0] + targetCursorCell + 1
+        msg = "BRAILLE: Adjusted targetCursorCell to: %i" % targetCursorCell
+        debug.println(debug.LEVEL_INFO, msg, True)
 
     # If there is no target cursor cell, then try to set one.  We
     # currently only do this for text objects, and we do so by looking
     # at the last position of the caret offset and cursor cell.  The
     # primary goal here is to keep the cursor movement on the display
     # somewhat predictable.
-    #
-    if (targetCursorCell == 0) \
-       and currentTextObj and (currentTextObj == lastTextObj) \
-       and (currentLineOffset == lastLineOffset):
+
+    if targetCursorCell == 0 and onSameLine:
         if lastCursorCell == 0:
-            # The lastCursorCell will be 0 if the user has panned
-            # the display on a long line and the caret of the text
-            # object is no longer in view.  We'll pass here and
-            # let the panning code figure out what to do.
-            #
-            pass
+            msg = "BRAILLE: Not adjusting targetCursorCell. User panned caret out of view."
+            debug.println(debug.LEVEL_INFO, msg, True)
         elif lastCaretOffset == currentCaretOffset:
             targetCursorCell = lastCursorCell
+            msg = "BRAILLE: Setting targetCursorCell to previous value. Caret hasn't moved."
+            debug.println(debug.LEVEL_INFO, msg, True)
         elif lastCaretOffset < currentCaretOffset:
-            targetCursorCell = min(_displaySize[0],
-                                   lastCursorCell \
-                                   + (currentCaretOffset - lastCaretOffset))
+            newLocation = lastCursorCell + (currentCaretOffset - lastCaretOffset)
+            if newLocation <= _displaySize[0]:
+                msg = "BRAILLE: Setting targetCursorCell based on offset: %i" % newLocation
+                debug.println(debug.LEVEL_INFO, msg, True)
+                targetCursorCell = newLocation
+            else:
+                msg = "BRAILLE: Setting targetCursorCell to end of display."
+                debug.println(debug.LEVEL_INFO, msg, True)
+                targetCursorCell = _displaySize[0]
         elif lastCaretOffset > currentCaretOffset:
-            targetCursorCell = max(1,
-                                   lastCursorCell \
-                                   - (lastCaretOffset - currentCaretOffset))
+            newLocation = lastCursorCell - (lastCaretOffset - currentCaretOffset)
+            if newLocation >= 1:
+                msg = "BRAILLE: Setting targetCursorCell based on offset: %i" % newLocation
+                debug.println(debug.LEVEL_INFO, msg, True)
+                targetCursorCell = newLocation
+            else:
+                msg = "BRAILLE: Setting targetCursorCell to start of display."
+                debug.println(debug.LEVEL_INFO, msg, True)
+                targetCursorCell = 1
+
+    # Now, we figure out the 0-based offset for where the cursor actually is in the string.
 
-    # Now, we figure out the 0-based offset for where the cursor
-    # actually is in the string.
-    #
     line = _lines[viewport[1]]
     [string, focusOffset, attributeMask, ranges] = line.getLineInfo(getLinkMask)
+    msg = "BRAILLE: Line %i: '%s' focusOffset: %i %s" % (viewport[1], string, focusOffset, ranges)
+    debug.println(debug.LEVEL_INFO, msg, True)
+
     cursorOffset = -1
     if focusOffset >= 0:
         cursorOffset = focusOffset + _regionWithFocus.cursorOffset
+        msg = "BRAILLE: Cursor offset in line string is: %i" % cursorOffset
+        debug.println(debug.LEVEL_INFO, msg, True)
 
     # Now, if desired, we'll automatically pan the viewport to show
     # the cursor.  If there's no targetCursorCell, then we favor the
@@ -1181,18 +1191,29 @@ def refresh(panToCursor=True,
     #
     if panToCursor and (cursorOffset >= 0):
         if len(string) <= _displaySize[0] and cursorOffset < _displaySize[0]:
-            pass
+            msg = "BRAILLE: Not adjusting offset %i. Cursor offset fits on display." % viewport[0]
+            debug.println(debug.LEVEL_INFO, msg, True)
         elif targetCursorCell:
             viewport[0] = max(0, cursorOffset - targetCursorCell + 1)
+            msg = "BRAILLE: Adjusting offset to %i based on targetCursorCell" % viewport[0]
+            debug.println(debug.LEVEL_INFO, msg, True)
         elif cursorOffset < viewport[0]:
             viewport[0] = max(0, cursorOffset)
+            msg = "BRAILLE: Adjusting offset to %i (cursor on left)" % viewport[0]
+            debug.println(debug.LEVEL_INFO, msg, True)
         elif cursorOffset >= (viewport[0] + _displaySize[0]):
             viewport[0] = max(0, cursorOffset - _displaySize[0] + 1)
+            msg = "BRAILLE: Adjusting offset to %i (cursor beyond display end)" % viewport[0]
+            debug.println(debug.LEVEL_INFO, msg, True)
         else:
             rangeForOffset = _getRangeForOffset(cursorOffset)
             viewport[0] = max(0, rangeForOffset[0])
+            msg = "BRAILLE: Adjusting offset to %i (unhandled condition)" % viewport[0]
+            debug.println(debug.LEVEL_INFO, msg, True)
             if cursorOffset >= (viewport[0] + _displaySize[0]):
                 viewport[0] = max(0, cursorOffset - _displaySize[0] + 1)
+                msg = "BRAILLE: Readjusting offset to %i (cursor beyond display end)" % viewport[0]
+                debug.println(debug.LEVEL_INFO, msg, True)
 
     startPos, endPos = _adjustForWordWrap(targetCursorCell)
     viewport[0] = startPos
@@ -1467,6 +1488,8 @@ def panLeft(panAmount=0):
         panAmount = max(0, min(oldStart - newStart, _displaySize[0]))
 
     viewport[0] = max(0, viewport[0] - panAmount)
+    msg = "BRAILLE: Panning left. Amount: %i (from %i to %i)" % (panAmount, oldX, viewport[0])
+    debug.println(debug.LEVEL_INFO, msg, True)
     return oldX != viewport[0]
 
 def panRight(panAmount=0):
@@ -1493,21 +1516,22 @@ def panRight(panAmount=0):
         if newX < len(string):
             viewport[0] = newX
 
+    msg = "BRAILLE: Panning right. Amount: %i (from %i to %i)" % (panAmount, oldX, viewport[0])
+    debug.println(debug.LEVEL_INFO, msg, True)
     return oldX != viewport[0]
 
 def panToOffset(offset):
     """Automatically pan left or right to make sure the current offset is
     showing."""
 
+    msg = "BRAILLE: Panning to offset %i. Current offset: %i." % (offset, viewport[0])
+    debug.println(debug.LEVEL_INFO, msg, True)
+
     while offset < viewport[0]:
-        debug.println(debug.LEVEL_FINEST,
-                      "braille.panToOffset (left) %d" % offset)
         if not panLeft():
             break
 
     while offset >= (viewport[0] + _displaySize[0]):
-        debug.println(debug.LEVEL_FINEST,
-                      "braille.panToOffset (right) %d" % offset)
         if not panRight():
             break
 
diff --git a/src/orca/flat_review.py b/src/orca/flat_review.py
index 84fa330eb..7b0176888 100644
--- a/src/orca/flat_review.py
+++ b/src/orca/flat_review.py
@@ -89,6 +89,9 @@ class Word:
         self.height = height
         self.chars = []
 
+    def __str__(self):
+        return "WORD: '%s' %s" % (self.string.replace("\n", "\\n"), self.zone.accessible)
+
     def __getattribute__(self, attr):
         if attr != "chars":
             return super().__getattribute__(attr)
@@ -146,6 +149,9 @@ class Zone:
         self.role = role or accessible.getRole()
         self._words = []
 
+    def __str__(self):
+        return "ZONE: '%s' %s" % (self._string.replace("\n", "\\n"), self.accessible)
+
     def __getattribute__(self, attr):
         """To ensure we update the content."""
 
diff --git a/src/orca/scripts/default.py b/src/orca/scripts/default.py
index 031c595cf..6af1cd34b 100644
--- a/src/orca/scripts/default.py
+++ b/src/orca/scripts/default.py
@@ -1060,10 +1060,16 @@ class Script(script.Script):
             #
             self._setFlatReviewContextToBeginningOfBrailleDisplay()
 
+            # TODO - JD: Why is this here?
             [charString, x, y, width, height] = \
                 self.flatReviewContext.getCurrent(flat_review.Context.CHAR)
 
+            # TODO - JD: Why are we resetting the target cursor cell here?
+            oldCell = self.targetCursorCell
             self.targetCursorCell = 1
+            msg = "DEFAULT: pan left: targetCursorCell %i -> %i" % (oldCell, self.targetCursorCell)
+            debug.println(debug.LEVEL_INFO, msg, True)
+
             self.updateBrailleReview(self.targetCursorCell)
         elif self.isBrailleBeginningShowing() and orca_state.locusOfFocus \
              and self.utilities.isTextArea(orca_state.locusOfFocus):
@@ -1133,10 +1139,16 @@ class Script(script.Script):
             #
             self._setFlatReviewContextToBeginningOfBrailleDisplay()
 
+            # TODO - JD: Why is this here?
             [charString, x, y, width, height] = \
                 self.flatReviewContext.getCurrent(flat_review.Context.CHAR)
 
+            # TODO - JD: Why are we resetting the target cursor cell here?
+            oldCell = self.targetCursorCell
             self.targetCursorCell = 1
+            msg = "DEFAULT: pan right: targetCursorCell %i -> %i" % (oldCell, self.targetCursorCell)
+            debug.println(debug.LEVEL_INFO, msg, True)
+
             self.updateBrailleReview(self.targetCursorCell)
         elif self.isBrailleEndShowing() and orca_state.locusOfFocus \
              and self.utilities.isTextArea(orca_state.locusOfFocus):
@@ -3487,29 +3499,55 @@ class Script(script.Script):
 
         context = self.getFlatReviewContext()
         [regions, regionWithFocus] = context.getCurrentBrailleRegions()
-        for region in regions:
-            if ((region.brailleOffset + len(region.string)) \
-                   > braille.viewport[0]) \
-                and (isinstance(region, braille.ReviewText) \
-                     or isinstance(region, braille.ReviewComponent)):
-                position = max(region.brailleOffset, braille.viewport[0])
-                offset = position - region.brailleOffset
-                self.targetCursorCell = region.brailleOffset \
-                                        - braille.viewport[0]
-                [word, charOffset] = region.zone.getWordAtOffset(offset)
-                if word:
-                    self.flatReviewContext.setCurrent(
-                        word.zone.line.index,
-                        word.zone.index,
-                        word.index,
-                        charOffset)
-                else:
-                    self.flatReviewContext.setCurrent(
-                        region.zone.line.index,
-                        region.zone.index,
-                        0, # word index
-                        0) # character index
-                break
+
+        # The first character on the flat review line has to be in object with text.
+        isTextOrComponent = lambda x: isinstance(x, (braille.ReviewText, braille.ReviewComponent))
+        regions = list(filter(isTextOrComponent, regions))
+
+        # TODO - JD: The current code was stopping on the first region which met the
+        # following condition. Is that definitely the right thing to do? Assume so for now.
+        # Also: Should the default script be accessing things like the viewport directly??
+        isMatch = lambda x: x.brailleOffset + len(x.string) > braille.viewport[0]
+        regions = list(filter(isMatch, regions))
+
+        if not regions:
+            msg = "DEFAULT: Could not find review region to move to start of display"
+            debug.println(debug.LEVEL_INFO, msg, True)
+            return
+
+        msg = "DEFAULT: Candidates for start of display:\n%s" % "\n".join(map(str, regions))
+        debug.println(debug.LEVEL_INFO, msg, True)
+
+        # TODO - JD: Again, for now we're preserving the original behavior of choosing the first.
+        region = regions[0]
+        position = max(region.brailleOffset, braille.viewport[0])
+        offset = position - region.brailleOffset
+        msg = "DEFAULT: Offset for region: %i" % offset
+        debug.println(debug.LEVEL_INFO, msg, True)
+
+        # TODO - JD: Why are we doing this here? The only caller undoes it.
+        oldCell = self.targetCursorCell
+        self.targetCursorCell = region.brailleOffset - braille.viewport[0]
+        msg = "DEFAULT: Updating targetCursorCell %i -> %i" % (oldCell, self.targetCursorCell)
+        debug.println(debug.LEVEL_INFO, msg, True)
+
+        [word, charOffset] = region.zone.getWordAtOffset(offset)
+        if word:
+            msg = "DEFAULT: Setting start of display to %s, %i" % (str(word), charOffset)
+            debug.println(debug.LEVEL_INFO, msg, True)
+            self.flatReviewContext.setCurrent(
+                word.zone.line.index,
+                word.zone.index,
+                word.index,
+                charOffset)
+        else:
+            msg = "DEFAULT: Setting start of display to %s" % region.zone
+            debug.println(debug.LEVEL_INFO, msg, True)
+            self.flatReviewContext.setCurrent(
+                region.zone.line.index,
+                region.zone.index,
+                0, # word index
+                0) # character index
 
     def find(self, query=None):
         """Searches for the specified query.  If no query is specified,


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