Skip to content

Commit

Permalink
Refactor autoSpeak function to listen for Virtual Buffer updates inst…
Browse files Browse the repository at this point in the history
…ead of polling in a separate thread
  • Loading branch information
mltony committed Nov 29, 2024
1 parent f2c1979 commit f4d4cf9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 35 deletions.
5 changes: 1 addition & 4 deletions addon/globalPlugins/browserNav/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,7 @@ def bnSetFocusObject(obj):
def bnVirtualBufferHandleUpdate(self):
result = originalVirtualBufferHandleUpdate(self)
watchURL()
quickJump.onVirtualBufferUpdate(self)
return result

originalSpeakTextInfo = None
Expand Down Expand Up @@ -744,8 +745,6 @@ def __init__(self, *args, **kwargs):
quickJump.originalReportLiveRegion = NVDAHelper.nvdaControllerInternal_reportLiveRegion
NVDAHelper.nvdaControllerInternal_reportLiveRegion = quickJump.newReportLiveRegion
NVDAHelper._setDllFuncPointer(NVDAHelper.localLib,"_nvdaControllerInternal_reportLiveRegion", quickJump.newReportLiveRegion)
self.thread = threading.Thread(name="BrowserNav browser monitor thread", target = quickJump.browseMonitorThreadFunc, args =())
self.thread.start()
while len(gc.callbacks) > 0:
del gc.callbacks[0]
garbageHandler.terminate = lambda: None
Expand Down Expand Up @@ -776,8 +775,6 @@ def terminate(self):
browseMode.BrowseModeDocumentTreeInterceptor.event_treeInterceptor_gainFocus = quickJump.original_event_treeInterceptor_gainFocus
NVDAHelper.nvdaControllerInternal_reportLiveRegion = quickJump.originalReportLiveRegion
NVDAHelper._setDllFuncPointer(NVDAHelper.localLib,"_nvdaControllerInternal_reportLiveRegion", quickJump.originalReportLiveRegion)
quickJump.browseMonitorThreadShutdownRequested = True
self.thread.join()

api.setFocusObject = originalSetFocusObject
virtualBuffers.VirtualBuffer._handleUpdate = originalVirtualBufferHandleUpdate
Expand Down
45 changes: 14 additions & 31 deletions addon/globalPlugins/browserNav/quickJump.py
Original file line number Diff line number Diff line change
Expand Up @@ -950,38 +950,21 @@ def __init__(self, lines):
self.lines = lines
self.enabled = True

browseMonitorThreadShutdownRequested = False
AutoSpeakCache = weakref.WeakKeyDictionary()
def browseMonitorThreadFunc():
while not browseMonitorThreadShutdownRequested:
try:
time.sleep(0.5)
focus = api.getFocusObject()
try:
browse = focus.treeInterceptor
if browse is None:
continue
except AttributeError:
continue
url = getUrl(browse, onlyFromCache=True)
if url is None:
continue
bookmarks = getAutoSpeakBookmarksForUrl(url, globalConfig)
try:
cacheEntry = AutoSpeakCache[browse]
except KeyError:
cacheEntry = {}
AutoSpeakCache[browse] = cacheEntry
processAutoSpeakEntry(browse, bookmarks, cacheEntry)
try:
#processWholePageDiff(browse, url)
pass
except OSError:
pass
except Exception as e:
#mylog(e)
#core.callLater(50, ui.message, _("Warning: BrowserNav browse monitor thread crashed: %s") % str(e))
log.error("Exception in BrowserNav monitor thread", e)
def onVirtualBufferUpdate(browse):
url = getUrl(browse, onlyFromCache=True)
if url is None:
return
bookmarks = getAutoSpeakBookmarksForUrl(url, globalConfig)
try:
cacheEntry = AutoSpeakCache[browse]
except KeyError:
cacheEntry = {}
AutoSpeakCache[browse] = cacheEntry
try:
processAutoSpeakEntry(browse, bookmarks, cacheEntry)
except Exception as e:
log.exception("Exception during virtual buffer update processing in BrowserNav QuickJump", e)

def processAutoSpeakEntry(browse, bookmarks, cacheEntry):
if len(bookmarks) == 0:
Expand Down

0 comments on commit f4d4cf9

Please sign in to comment.