2121import watchdog
2222from logHandler import log
2323import windowUtils
24- from locationHelper import RectLTRB , RectLTWH , Point
24+ from locationHelper import RectLTRB , RectLTWH
2525import textUtils
2626from typing import Union , List , Tuple
2727
@@ -300,31 +300,23 @@ def _get__storyFieldsAndRects(self) -> Tuple[
300300 List [int ]
301301 ]:
302302 # All returned coordinates are logical coordinates.
303- location = self ._location if self ._location else self .obj .location
304- if location is None or not any (location ):
305- # No location; nothing we can do.
306- return [], [], [], []
303+ if self ._location :
304+ left , top , right , bottom = self ._location
305+ else :
306+ try :
307+ left , top , width , height = self .obj .location
308+ except TypeError :
309+ # No location; nothing we can do.
310+ return [], [], [], []
311+ right = left + width
312+ bottom = top + height
307313 bindingHandle = self .obj .appModule .helperLocalBindingHandle
308314 if not bindingHandle :
309315 log .debugWarning ("AppModule does not have a binding handle" )
310316 return [], [], [], []
311- try :
312- location = location .toLogical (self .obj .windowHandle )
313- except RuntimeError :
314- log .exception ()
315- return [], [], [], []
316- text , rects = getWindowTextInRect (
317- bindingHandle ,
318- self .obj .windowHandle ,
319- location .left ,
320- location .top ,
321- location .right ,
322- location .bottom ,
323- self .minHorizontalWhitespace ,
324- self .minVerticalWhitespace ,
325- self .stripOuterWhitespace ,
326- self .includeDescendantWindows
327- )
317+ left ,top = windowUtils .physicalToLogicalPoint (self .obj .windowHandle ,left ,top )
318+ right ,bottom = windowUtils .physicalToLogicalPoint (self .obj .windowHandle ,right ,bottom )
319+ text ,rects = getWindowTextInRect (bindingHandle , self .obj .windowHandle , left , top , right , bottom , self .minHorizontalWhitespace , self .minVerticalWhitespace ,self .stripOuterWhitespace ,self .includeDescendantWindows )
328320 if not text :
329321 return [], [], [], []
330322 text = "<control>%s</control>" % text
@@ -452,29 +444,15 @@ def _normalizeFormatField(self,field):
452444
453445 def _getOffsetFromPoint (self , x , y ):
454446 # Accepts physical coordinates.
455- try :
456- x , y = windowUtils .physicalToLogicalPoint (
457- self .obj .windowHandle ,
458- x ,
459- y
460- )
461- except RuntimeError :
462- raise LookupError ("physicalToLogicalPoint failed" )
447+ x ,y = windowUtils .physicalToLogicalPoint (self .obj .windowHandle ,x ,y )
463448 for charOffset , (charLeft , charTop , charRight , charBottom ) in enumerate (self ._storyFieldsAndRects [1 ]):
464449 if charLeft <= x < charRight and charTop <= y < charBottom :
465450 return charOffset
466451 raise LookupError
467452
468453 def _getClosestOffsetFromPoint (self ,x ,y ):
469454 # Accepts physical coordinates.
470- try :
471- x , y = windowUtils .physicalToLogicalPoint (
472- self .obj .windowHandle ,
473- x ,
474- y
475- )
476- except RuntimeError :
477- raise LookupError ("physicalToLogicalPoint failed" )
455+ x ,y = windowUtils .physicalToLogicalPoint (self .obj .windowHandle ,x ,y )
478456 #Enumerate the character rectangles
479457 a = enumerate (self ._storyFieldsAndRects [1 ])
480458 #Convert calculate center points for all the rectangles
@@ -492,14 +470,7 @@ def _getBoundingRectFromOffset(self, offset):
492470 rects = self ._storyFieldsAndRects [1 ]
493471 if not rects or offset >= len (rects ):
494472 raise LookupError
495- rect = rects [offset ].toLTWH ()
496- try :
497- rect = rect .toPhysical (self .obj .windowHandle )
498- except RuntimeError :
499- raise LookupError (
500- f"Couldn't convert character rectangle at offset { offset } to physical coordinates"
501- )
502- return rect
473+ return rects [offset ].toPhysical (self .obj .windowHandle ).toLTWH ()
503474
504475 def _getNVDAObjectFromOffset (self ,offset ):
505476 try :
@@ -591,15 +562,7 @@ def _get_boundingRects(self):
591562 for lineEndOffset in lineEndOffsets :
592563 startOffset = endOffset
593564 endOffset = lineEndOffset
594- lineRect = RectLTWH .fromCollection (* self ._storyFieldsAndRects [1 ][startOffset :endOffset ])
595- try :
596- lineRect = lineRect .toPhysical (self .obj .windowHandle )
597- except RuntimeError :
598- raise LookupError (
599- f"Couldn't convert line rectangle at offsets { startOffset } to { endOffset } "
600- "to physical coordinates"
601- )
602- rects .append (lineRect )
565+ rects .append (RectLTWH .fromCollection (* self ._storyFieldsAndRects [1 ][startOffset :endOffset ]).toPhysical (self .obj .windowHandle ))
603566 return rects
604567
605568 def _getFirstVisibleOffset (self ):
@@ -643,12 +606,7 @@ def _findCaretOffsetFromLocation(
643606 def _getCaretOffset (self ):
644607 caretRect = getCaretRect (self .obj )
645608 objLocation = self .obj .location
646- try :
647- objRect = objLocation .toLTRB ().toLogical (self .obj .windowHandle )
648- except RuntimeError :
649- raise RuntimeError (
650- "Couldn't convert object location to logical coordinates when getting caret offset"
651- )
609+ objRect = objLocation .toLTRB ().toLogical (self .obj .windowHandle )
652610 caretRect = caretRect .intersection (objRect )
653611 if not any (caretRect ):
654612 raise RuntimeError ("The caret rectangle does not overlap with the window" )
@@ -673,14 +631,11 @@ def _setCaretOffset(self,offset):
673631 if offset >= len (rects ):
674632 raise RuntimeError ("offset %d out of range" )
675633 rect = rects [offset ]
676- # Place the cursor at the left coordinate of the character, vertically centered.
677- point = Point (rect .left , rect .center .y )
678- try :
679- point = point .toPhysical (self .obj .windowHandle )
680- except RuntimeError :
681- raise RuntimeError ("Conversion to physical coordinates failed when setting caret offset" )
682- oldX , oldY = winUser .getCursorPos ()
683- winUser .setCursorPos (* point )
634+ x = rect .left
635+ y = rect .center .y
636+ x ,y = windowUtils .logicalToPhysicalPoint (self .obj .windowHandle ,x ,y )
637+ oldX ,oldY = winUser .getCursorPos ()
638+ winUser .setCursorPos (x ,y )
684639 mouseHandler .executeMouseEvent (winUser .MOUSEEVENTF_LEFTDOWN ,0 ,0 )
685640 mouseHandler .executeMouseEvent (winUser .MOUSEEVENTF_LEFTUP ,0 ,0 )
686641 winUser .setCursorPos (oldX ,oldY )
0 commit comments