Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor BrailleHandler.setDisplayByName #14524

Merged
merged 15 commits into from Feb 13, 2023
59 changes: 32 additions & 27 deletions source/braille.py
Expand Up @@ -18,7 +18,7 @@
Set,
Tuple,
Union,
Type
Type,
)
from locale import strxfrm

Expand Down Expand Up @@ -349,9 +349,10 @@
#: braille displays should be automatically detected and used.
#: @type: str
AUTO_DISPLAY_NAME = AUTOMATIC_PORT[0]
#: The name of the noBraille display driver
#: @type: str
NO_BRAILLE_DISPLAY_NAME = "noBraille"

NO_BRAILLE_DISPLAY_NAME: str = "noBraille"
"""The name of the noBraille display driver."""

#: A port name which indicates that USB should be used.
#: @type: tuple
# Translators: String representing the USB port selection for braille displays.
Expand Down Expand Up @@ -1835,8 +1836,10 @@ def getFocusRegions(
if isinstance(obj, CursorManager):
region2 = (ReviewTextInfoRegion if review else CursorManagerRegion)(obj)
elif (
isinstance(obj, DocumentTreeInterceptor) or (
isinstance(obj, NVDAObject) and NVDAObjectHasUsefulText(obj)
isinstance(obj, DocumentTreeInterceptor)
or (
isinstance(obj, NVDAObject)
and NVDAObjectHasUsefulText(obj)
)
):
region2 = (ReviewTextInfoRegion if review else TextInfoRegion)(obj)
Expand Down Expand Up @@ -1971,7 +1974,7 @@ def setDisplayByName(
name: str,
isFallback: bool = False,
detected: typing.Optional[bdDetect.DeviceMatch] = None,
):
) -> bool:
if name == AUTO_DISPLAY_NAME:
# Calling _enableDetection will set the display to noBraille until a display is detected.
# Note that L{isFallback} is ignored in these cases.
Expand Down Expand Up @@ -2014,16 +2017,16 @@ def _switchDisplay(
newDisplayClass: Type["BrailleDisplayDriver"],
**kwargs
) -> "BrailleDisplayDriver":
sameDisplayReinit = newDisplayClass == oldDisplay.__class__
if sameDisplayReinit:
sameDisplayReInit = newDisplayClass == oldDisplay.__class__
if sameDisplayReInit:
# This is the same driver as was already set, so just re-initialize it.
log.debug(f"Reinitializing {newDisplayClass.name!r} braille display")
oldDisplay.terminate()
newDisplay = oldDisplay
else:
newDisplay = newDisplayClass.__new__(newDisplayClass)
extensionPoints.callWithSupportedKwargs(newDisplay.__init__, **kwargs)
if not sameDisplayReinit:
if not sameDisplayReInit:
if oldDisplay:
log.debug(f"Switching braille display from {oldDisplay.name!r} to {newDisplay.name!r}")
try:
Expand Down Expand Up @@ -2375,25 +2378,15 @@ def initialDisplay(self):
log.debugWarning("Error in initial display", exc_info=True)

def handlePostConfigProfileSwitch(self):
displayName = config.conf["braille"]["display"]
try:
port = config.conf["braille"][displayName]["port"]
except KeyError:
port = None
coveredByAutoDetect = (
displayName == AUTO_DISPLAY_NAME
and bdDetect.driverSupportsAutoDetection(self.display.name)
)
display = config.conf["braille"]["display"]
# Do not choose a new display if:
if not (
# The display in the new profile is equal to the last requested display name
# and it has no explicit port defined
displayName == self._lastRequestedDisplayName and port is None
# or the new profile uses auto detection,
# and the currently active display is supported by auto detection.
or coveredByAutoDetect
display == self._lastRequestedDisplayName
# or the new profile uses auto detection, which supports detection of the currently active display.
or (display == AUTO_DISPLAY_NAME and bdDetect.driverSupportsAutoDetection(self.display.name))
):
self.setDisplayByName(displayName)
self.setDisplayByName(display)
self._tether = config.conf["braille"]["tetherTo"]

def handleDisplayUnavailable(self):
Expand All @@ -2410,12 +2403,24 @@ def handleDisplayUnavailable(self):
)
self.setDisplayByName(newDisplay, isFallback=True)

def _enableDetection(self, usb=True, bluetooth=True, limitToDevices=None):
def _enableDetection(
self,
usb: bool = True,
bluetooth: bool = True,
limitToDevices: Optional[List[str]] = None
):
"""Enables automatic detection of braille displays.
When auto detection is already active, this will force a rescan for devices.
This should also be executed when auto detection should be resumed due to loss of display connectivity.
In that case, it is triggered by L{setDisplayByname}.
@param usb: Whether to scan for USB devices
@param Bluetooth: WWhether to scan for Bluetooth devices.
seanbudd marked this conversation as resolved.
Show resolved Hide resolved
@param limitToDevices: An optional list of driver names a scan should be limited to.
This is used when a Bluetooth device is detected, in order to switch to USB
when an USB device for the same driver is found.
C{None} if no driver filtering should occur.
"""
self.setDisplayByName("noBraille", isFallback=True)
self.setDisplayByName(NO_BRAILLE_DISPLAY_NAME, isFallback=True)
if self._detector:
self._detector.rescan(usb=usb, bluetooth=bluetooth, limitToDevices=limitToDevices)
return
Expand Down