#!/usr/bin/python3

import pyatspi

def findLabels(root):
    hasRole = lambda x: x and x.getRole() == pyatspi.ROLE_LABEL
    return pyatspi.findAllDescendants(root, hasRole)

def listener(e):
    if e.host_application.name != "gtk3-demo":
        return

    if e.source.getRole() in [pyatspi.ROLE_TEXT, pyatspi.ROLE_FRAME]:
        return

    print(e.source, e.type)

def onWindowActivated(e):
    if e.host_application.name != "gtk3-demo":
        return

    print("======================")
    # Comment this next line to stop the event spewage
    findLabels(e.source)

pyatspi.Registry.registerEventListener(listener, "object:state-changed:checked")
pyatspi.Registry.registerEventListener(listener, "object:text-changed")
pyatspi.Registry.registerEventListener(listener, "object:property-change:accessible-name")
pyatspi.Registry.registerEventListener(onWindowActivated, "window:activate")
pyatspi.Registry.start()
