Skip to content

Commit

Permalink
Speech History feature: simplify code & NVDA 2023.1 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
aaclause committed Apr 16, 2023
1 parent ae564b3 commit 7bf3b3e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 77 deletions.
22 changes: 8 additions & 14 deletions addon/globalPlugins/brailleExtender/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
from . import patches
from . import rolelabels
from . import settings
from . import speechhistorymode
from . import tabledictionaries
from . import undefinedchars
from . import updatecheck
Expand Down Expand Up @@ -1197,17 +1196,15 @@ def script_toggleRoutingCursorsEditFields(self, gesture):
script_toggleRoutingCursorsEditFields.__doc__ = _("Toggle routing cursors behavior in edit fields")

def script_toggleSpeechHistoryMode(self, gesture):
msg = ""
if config.conf["brailleExtender"]["speechHistoryMode"]["enabled"]:
speechhistorymode.disable()
tether = "auto"
if not config.conf["braille"]["autoTether"]:
tether = braille.handler.getTether()
msg = [e[1] for e in braille.handler.tetherValues if e[0] == tether][0]
msg = _("Speech History Mode disabled (%s)") % msg
else:
speechhistorymode.enable()
newState = not config.conf["brailleExtender"]["speechHistoryMode"]["enabled"]
config.conf["brailleExtender"]["speechHistoryMode"]["enabled"] = newState
msg = _("Speech History Mode disabled")
if newState:
msg = _("Speech History Mode enabled")
else:
braille.handler.initialDisplay()
braille.handler.buffer.update()
braille.handler.update()
speech.speakMessage(msg)
script_toggleSpeechHistoryMode.__doc__ = _("Toggle Speech History Mode")

Expand All @@ -1230,9 +1227,6 @@ def script_toggleSpeechHistoryMode(self, gesture):
__gestures["kb:nvda+alt+y"] = "addDictionaryEntry"

def terminate(self):
if braille.handler.getTether() == "speech":
speechhistorymode.disable()
config.conf["brailleExtender"]["speechHistoryMode"]["enabled"] = True
braille.TextInfoRegion._addTextWithFields = self.backup__addTextWithFields
braille.TextInfoRegion.update = self.backup__update
braille.TextInfoRegion._getTypeformFromFormatField = self.backup__getTypeformFromFormatField
Expand Down
2 changes: 2 additions & 0 deletions addon/globalPlugins/brailleExtender/patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,8 @@ def _displayWithCursor(self):
origGetTether = braille.BrailleHandler.getTether

def getTetherWithRoleTerminal(self):
if config.conf["brailleExtender"]["speechHistoryMode"]["enabled"]:
return speechhistorymode.TETHER_SPEECH
role = None
obj = api.getNavigatorObject()
if obj:
Expand Down
76 changes: 13 additions & 63 deletions addon/globalPlugins/brailleExtender/speechhistorymode.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# coding: utf-8
# speechhistorymode.py
# Part of BrailleExtender addon for NVDA
# Copyright 2021 Emil Hesmyr, André-Abush Clause, released under GPL.
# Copyright 2021-2023 Emil Hesmyr, André-Abush Clause, released under GPL.
import braille
import config
import speech
Expand Down Expand Up @@ -56,40 +56,7 @@ def onSave(self):
orig_speak= speech.speak
else:
orig_speak = speech.speech.speak
speechInList = False
for i in braille.handler.tetherValues:
if TETHER_SPEECH in i:
speechInList = True
if not speechInList:
# the speech option can not be positioned such that the user goes directly
# from it to "automaticly" when using the NVDA + ctrl + t command. If it
# is, the braille display does not show the focus propperly until the user
# either switches to another tether option or moves the focus.
for i in range(len(braille.handler.tetherValues)):
if braille.handler.tetherValues[i][0] == braille.handler.TETHER_AUTO:
braille.handler.tetherValues.insert(
i + 1, (TETHER_SPEECH,
# Translators: The label for a braille setting indicating that braille should be tethered to the speech history.
_("to speech history")))

def enable():
if not config.conf["brailleExtender"]["speechHistoryMode"]["enabled"]:
tetherTo = braille.handler.getTether()
config.conf["brailleExtender"]["speechHistoryMode"]["backup_tetherTo"] = tetherTo if tetherTo != TETHER_SPEECH else braille.handler.TETHER_FOCUS
config.conf["brailleExtender"]["speechHistoryMode"]["backup_autoTether"] = config.conf["braille"]["autoTether"]
config.conf["braille"]["autoTether"] = False
braille.handler.setTether(TETHER_SPEECH)

def disable():
backup_tetherTo = config.conf["brailleExtender"]["speechHistoryMode"]["backup_tetherTo"]
backup_autoTether = config.conf["brailleExtender"]["speechHistoryMode"]["backup_autoTether"]
if backup_tetherTo == TETHER_SPEECH:
backup_tetherTo = braille.handler.TETHER_FOCUS
backup_autoTether = True
config.conf["braille"]["autoTether"] = backup_autoTether
braille.handler.setTether(backup_tetherTo, backup_autoTether)
braille.handler.initialDisplay()
braille.handler.update()


def showSpeech(index, allowReadEntry=False):
try:
Expand All @@ -100,6 +67,7 @@ def showSpeech(index, allowReadEntry=False):
text = f"#%.{size_limit}d:{text}" % (index+1)
region = braille.TextRegion(text)
region.update()
region.obj = None
braille.handler._doNewObject([region])
if allowReadEntry and config.conf["brailleExtender"]["speechHistoryMode"]["speakEntries"]:
speech.cancelSpeech()
Expand Down Expand Up @@ -138,13 +106,12 @@ def speak(
speech.speech.speak = speak
if hasattr(speech, "speak"):
speech.speak = speak
oldScrollBack = braille.BrailleBuffer.scrollBack


def scrollBack(self):
windowRawText = braille.handler.mainBuffer.windowRawText
windowEndPos = braille.handler.buffer.windowEndPos
oldScrollBack(self)
orig_ScrollBack(self)
if braille.handler.buffer == braille.handler.mainBuffer and braille.handler.getTether(
) == TETHER_SPEECH and braille.handler.buffer.windowRawText == windowRawText and braille.handler.buffer.windowEndPos == windowEndPos:
global index
Expand All @@ -153,14 +120,10 @@ def scrollBack(self):
showSpeech(index, allowReadEntry=True)


braille.BrailleBuffer.scrollBack = scrollBack
oldScrollForward = braille.BrailleBuffer.scrollForward


def scrollForward(self):
windowRawText = braille.handler.mainBuffer.windowRawText
windowEndPos = braille.handler.buffer.windowEndPos
oldScrollForward(self)
orig_ScrollForward(self)
if braille.handler.buffer == braille.handler.mainBuffer and braille.handler.getTether(
) == TETHER_SPEECH and braille.handler.buffer.windowRawText == windowRawText and braille.handler.buffer.windowEndPos == windowEndPos:
global index
Expand All @@ -169,13 +132,9 @@ def scrollForward(self):
showSpeech(index, allowReadEntry=True)


braille.BrailleBuffer.scrollForward = scrollForward
oldBrailleMessage = braille.handler.message


def newBrailleMessage(*args, **kwargs):
def newBrailleMessage(self, *args, **kwargs):
if braille.handler.getTether() != TETHER_SPEECH:
oldBrailleMessage(*args, **kwargs)
orig_BrailleMessage(self, *args, **kwargs)


def showSpeechFromRoutingIndex(routingNumber):
Expand All @@ -198,18 +157,9 @@ def showSpeechFromRoutingIndex(routingNumber):
showSpeech(index, allowReadEntry=True)


orig_setTether = braille.BrailleHandler.setTether

def setTether(*args, **kwargs):
orig_setTether(*args, **kwargs)
config.conf["brailleExtender"]["speechHistoryMode"]["enabled"] = braille.handler.getTether() == TETHER_SPEECH

braille.BrailleHandler.setTether = setTether
braille.handler.message = newBrailleMessage

# Translators: Reports which position braille is tethered to
# (braille can be tethered automatically or to either focus or review position or speech history).
globalCommands.GlobalCommands.script_braille_toggleTether.__doc__ = _("Toggle tethering of braille between the focus, the review position and the speech history")

if config.conf["brailleExtender"]["speechHistoryMode"]["enabled"]:
enable()
orig_ScrollBack = braille.BrailleBuffer.scrollBack
braille.BrailleBuffer.scrollBack = scrollBack
orig_BrailleMessage = braille.BrailleHandler.message
braille.BrailleHandler.message = newBrailleMessage
orig_ScrollForward = braille.BrailleBuffer.scrollForward
braille.BrailleBuffer.scrollForward = scrollForward

0 comments on commit 7bf3b3e

Please sign in to comment.