Execute Script Every Time a Popup is Opened

Hi all,

I am using a popup window that allows the user to enter a value into a Numeric Entry Field and submit. Because this is the only input field in the view I thought it would be useful to automatically set the focus to it when the popup opens.

However, it seems that the onStartup script only executes the first time the popup is opened in a session. After some reading on the startup event, it seems this is because the popup view is already loaded into the session after the first time.

My questions is: Would it be possible to have this script execute every time the popup window is opened? Because the user would likely be using this multiple times in one session.

Component I am trying to set the focus on:

onStartup event script on the root container:

self.getChild('AmountContainer').getChild('AmountInput').focus()

UPDATE: it seems to be working not only on the first time the popup is opened, however it is not consistently working properly and I can't seem to get an idea of in what case the script is firing and when it isn't.

You could add a custom prop on your view, with the expression now(0).
Then put your code in the value change script of this prop.

I don't like this kind of workaround, but I can't think of something else right now.

1 Like

I've definitely done that before, no judgement here :joy:

If all else fails, that may be the way...

Wait, actually... You may encounter the very same issue. If the script runs before it can effectively put the focus on the input field...
Try putting that script on the input field's onStartup event. I feel like that may be the best place to put it.

edit:
I just tried putting self.focus() in the field's onStartup event, and it seems to work.

I moved it to the onStartup event of the input field while I was troubleshooting, and I did not see any real difference. Sometimes it'd work fine, others nothing.

My solution to this problem has been making the script sleep for a hundredth of a second before running. It will now focus 100% of the time with no discernable wait.

I won't mark this as the solution because it stinks, but it's the best I've got right now.

Setting focus onStartup is inherently flawed because it might execute before the input you're trying to focus is rendered. They really need to add an "onLoad" event handler, something I've requested and you can vote for:

My solution has been to add a delay:

from threading import Timer

def focus(self):
    self.focus()

Timer(1, focus, [self]).start()
1 Like

Thanks for the info. I wasn't really sure why that was happening, but that makes sense.

Upvoted your feature request, hopefully the Ignition gods are behind us :crossed_fingers:

This issue is particularly frustrating for Ryans...

2 Likes

This thread gave me an idea. I use a generic popup ConfirmWithInput which has an input parameter of presetValue. This is bidirectional bound to my text input in the popup. If there is not a preset value, I want the text field to be in focus.

The onStartUp event was hit or miss as previously mentioned, so I added a change script on the text input which should get fired when the binding is executed.

if not currentValue.value:
   self.focus()

So far it works every time.

1 Like