I needed a way to execute code and be notified when a window changes.
First I created a client tag change script for a value change on [System]Client/User/CurrentWindow as described here.
Here is the tag change script:
newWindowName = newValue.value
shared.nav.onWindowChanged(newWindowName)
Then I created two client tags to keep track of the previous and current windows:
[Client]YourProject/navigationContext/PreviousWindowName
[Client]YourProject/navigationContext/CurrentWindowName
Then I created a script in the script library shared.nav:
[code]_onWindowChangeCallbacks = {}
def registerOnWindowChangeCallback(name,func):
if _onWindowChangeCallbacks.has_key(name):
shared.developer.logDebug(“onWindowChange callback NOT registered: %s” % name)
else:
_onWindowChangeCallbacks[name] = func
shared.developer.logDebug(“onWindowChange callback registered: %s” % name)
shared.developer.logDebug(“onWindowChange callback count: %s” % len(_onWindowChangeCallbacks))
def unregisterOnWindowChangeCallback(name):
if _onWindowChangeCallbacks.has_key(name):
shared.developer.logDebug(“onWindowChange callback unregistered: %s” % name)
del _onWindowChangeCallbacks[name]
shared.developer.logDebug(“onWindowChange callback count: %s” % len(_onWindowChangeCallbacks))
def _executeOnWindowChangeCallbacks():
unregisters = []
for key,value in _onWindowChangeCallbacks.iteritems():
result = value(key,getPreviousWindowName(),getCurrentWindowName())
if(result == True):
unregisters.append(key)
for r in unregisters:
shared.developer.logDebug("onWindowChange callback self-unregister request: %s" % r)
unregisterOnWindowChangeCallback(r)
def getPreviousWindowName():
return system.tag.read("[Client]YourProject/navigationContext/PreviousWindowName").value
def setPreviousWindowName(name):
system.tag.write("[Client]YourProject/navigationContext/PreviousWindowName", name)
def getCurrentWindowName():
return system.tag.read("[Client]YourProject/navigationContext/CurrentWindowName").value
def setCurrentWindowName(name):
system.tag.write("[Client]YourProject/navigationContext/CurrentWindowName", name)
def onWindowChanged(newWindowName):
if(newWindowName):
previousWindowName = getCurrentWindowName()
setPreviousWindowName(previousWindowName)
setCurrentWindowName(newWindowName)
shared.developer.logDebug(“onWindowChange:: ‘%s’ => ‘%s’.” % (previousWindowName, newWindowName))
_executeOnWindowChangeCallbacks()
return True
else:
return False[/code]
The above script now allows you to register functions for invocation when the current window changes:
Register Function in visionWindowOpened:
shared.nav.registerOnWindowChangeCallback("myCallback",event.source.newMethod)
Unregister Function in visionWindowClosed:
shared.nav.unregisterOnWindowChangeCallback("myCallback")
And then define the “newMethod” as a custom method on the window:
def newMethod(self,a,b,c)
print "callback:::: " + a + " : " + b + " => " + c
return True
NOTE: returning False would prevent the callback from unregistering itself until unregisterOnWindowChangeCallback is invoked.
Sample output:
onWindowChange callback registered: myCallback
onWindowChange callback count: 1
onWindowChange:: 'authentication/Kronos' => 'navigation/Landing'.
callback:::: myCallback : authentication/Kronos => navigation/Landing
onWindowChange callback self-unregister request: myCallback
onWindowChange callback unregistered: myCallback
onWindowChange callback count: 0