Skip to content

Commit

Permalink
improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
cary-rowen committed Aug 25, 2023
1 parent a5fdd82 commit a45e287
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
50 changes: 28 additions & 22 deletions addon/globalPlugins/objWatcher/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
# This file is covered by the GNU General Public License.
# See the file COPYING.txt for more details.
# Copyright (C) 2023 Cary-rowen <[email protected]>

import wx
import api
import addonHandler
import ui
import gui
import addonHandler
import globalPluginHandler
import ui
import wx
from scriptHandler import script, getLastScriptRepeatCount
from . import cues

Expand All @@ -25,7 +24,7 @@ def __init__(self):
self.watchingObj = None
self.lastAttributeText = None
self.timer = wx.Timer(gui.mainFrame)
gui.mainFrame.Bind(wx.EVT_TIMER, self.onTimerEvent, self.timer)
gui.mainFrame.Bind(wx.EVT_TIMER, handler=self.onTimerEvent, source=self.timer)

@script(
description=_(
Expand All @@ -37,26 +36,32 @@ def __init__(self):
def script_startOrStopWatcher(self, gesture):
if not self.timer:
return
if getLastScriptRepeatCount() > 0:
if self.timer.IsRunning():
self.timer.Stop()
cues.Stop()
# Translators: Messages reported when watcher is stopped
ui.message(_("Stopped watcher"))
repeatCount = getLastScriptRepeatCount()
if repeatCount > 0:
self._toggleWatcher()
else:
if self.timer.IsRunning():
ui.message(self._getWatchingAttribute())
else:
self.watchingObj = api.getNavigatorObject()
self.timer.Start(WATCHER_TIMER_INTERVAL)
if self.watchingObj:
cues.Start()
# Translators: Messages reported when watcher is start.
ui.message(_("Started watcher {}").format(self._getWatchingAttribute()))
else:
cues.NoObj()
# Translators: Messages reported when No navigation object available to watch.
ui.message(_("No navigation object available to watch"))
self._toggleWatcher()

def _toggleWatcher(self):
if self.timer.IsRunning():
self.timer.Stop()
cues.Stop()
# Translators: Messages reported when watcher is stopped
ui.message(_("Stopped watcher"))
else:
self.watchingObj = api.getNavigatorObject()
self.timer.Start(WATCHER_TIMER_INTERVAL)
if self.watchingObj:
cues.Start()
# Translators: Messages reported when watcher is started.
ui.message(_("Started watcher {}").format(self._getWatchingAttribute()))
else:
cues.NoObj()
# Translators: Messages reported when no navigation object available to watch.
ui.message(_("No navigation object available to watch"))

def onTimerEvent(self, event):
if not self.watchingObj:
Expand All @@ -72,7 +77,7 @@ def _getWatchingAttribute(self):
seen_values = set()
for attr in ['name', 'value', 'description']:
if hasattr(self.watchingObj, attr):
value = getattr(self.watchingObj, attr)
value = getattr(self.watchingObj, attr, None)
if value and value not in seen_values:
non_empty_attributes.append(value)
seen_values.add(value)
Expand All @@ -83,6 +88,7 @@ def terminate(self):
super().terminate()
if self.timer:
self.timer.Stop()
self.timer.Destroy()
self.lastAttributeText = None
self.timer = None
self.watchingObj = None
Expand Down
2 changes: 1 addition & 1 deletion buildVars.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def _(arg):
# Translators: Long description to be shown for this add-on on add-on information from add-ons manager
"addon_description": _("""This NVDA add-on watches changes to attributes of navigation objects."""),
# version
"addon_version": "0.2.0",
"addon_version": "0.2.1",
# Author(s)
"addon_author": "Cary-rowen <[email protected]>",
# URL for the add-on documentation support
Expand Down

0 comments on commit a45e287

Please sign in to comment.