E5D7 Notifications by daveenguyen · Pull Request #165 · pyatom/pyatom · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion atomac/AXClasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,8 @@ def _waitFor(self, timeout, notification, **kwargs):
else:
callbackKwargs = kwargs
else:
callbackArgs = (retelem, )
if retelem:
callbackArgs = (retelem, )
# Pass the kwa 10000 rgs to the default callback
callbackKwargs = kwargs

Expand Down
67 changes: 31 additions & 36 deletions atomac/_a11y.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,42 @@ def _setNotification(self, timeout=0, notificationStr=None, callbackFn=None, cal

self.observerRes = None

@objc.callbackFor(AXObserverCreate)
def _observer_callback(observer, element, notification, refcon):
cb_fn = self.callbackFn
cb_args = self.callbackArgs
cb_kwargs = self.callbackKwargs

if cb_fn is not None:
retElem = self.with_ref(element)
if retElem is None:
raise RuntimeError('Could not create new AX UI Element.')

cb_args = (retElem,) + cb_args
callbackRes = cb_fn(*cb_args, **cb_kwargs)

if callbackRes is None:
raise RuntimeError('Python callback failed.')

if callbackRes in (-1, 1):
AppHelper.stopEventLoop()

temp = self.observerRes
self.observerRes = callbackRes
else:
AppHelper.stopEventLoop()
temp = self.observerRes
self.observerRes = True

pid = self._getPid()
err, observer = AXObserverCreate(pid, observerCallback, None)
err, observer = AXObserverCreate(pid, _observer_callback, None)
if err != kAXErrorSuccess:
_setError(err, 'Could not create observer for notification')

err = AXObserverAddNotification(
observer, self.ref,
notificationStr,
self
id(self.ref)
)

if err != kAXErrorSuccess:
Expand All @@ -149,11 +176,8 @@ def _setNotification(self, timeout=0, notificationStr=None, callbackFn=None, cal
# Set the signal handlers prior to running the run loop
oldSigIntHandler = MachSignals.signal(signal.SIGINT, _sigHandler)
# If an error occurs (return value is SIG_ERR), continue as it's not fatal
AppHelper.runConsoleEventLoop(
mode=kCFRunLoopDefaultMode,
installInterrupt=False,
maxTimeout=timeout,
)
AppHelper.callLater(timeout, AppHelper.stopEventLoop)
AppHelper.runConsoleEventLoop()
MachSignals.signal(signal.SIGINT, oldSigIntHandler)
err = AXObserverRemoveNotification(observer, self.ref, notificationStr)
if err != kAXErrorSuccess:
Expand Down Expand Up @@ -350,32 +374,3 @@ def getSystemObject(cls):
raise ErrorUnsupported('Error getting a11y object')

return cls.with_ref(app_ref)


# callbacks
# Callback methods for notifications
def observerCallback(cls, element, contextData):
axObj = contextData
cb_fn = contextData.callbackFn
cb_args = contextData.callbackArgs
cb_kwargs = contextData.callbackKwargs
if cb_fn is not None:
retElem = cls.with_ref(element)
if retElem is None:
raise RuntimeError('Could not create new AX UI Element.')

cb_args = (retElem,) + cb_args
callbackRes = cb_fn(cb_args, cb_kwargs)

if callbackRes is None:
raise RuntimeError('Python callback failed.')

if callbackRes in (-1, 1):
AppHelper.stopEventLoop()

temp = axObj.observerRes
axObj.observerRes = callbackRes
else:
AppHelper.stopEventLoop()
temp = axObj.observerRes
axObj.observerRes = True
0