Title: Ignition Perspective 8.3 – self.focus() works from button events but not property change or startup scripts
I'm using Ignition 8.3 (Perspective) and trying to automatically set focus to a Text Field when an Embedded View loads.
The Text Field is the first input in a four-digit authentication screen. Ideally, when the operator navigates to this screen, the cursor should automatically be placed in the first field so they can immediately begin entering their code.
I'm using the documented focus() method:
self.focus()
I've tested several approaches with the following results:
Button onActionPerformed: Works correctly.
Button onMouseEnter: Works correctly.
Text Field onStartup: Does not focus the field.
Property-change script: Executes, but does not focus the field.
Message handler: Executes, but does not focus the field.
I also have a reset function that clears all four inputs and calls focus() on the first field. When invoked from a button, both the reset and focus work. When invoked from a property-change script, the inputs reset correctly, but focus is never applied.
I've confirmed the component path is correct, and the scripts are executing using print statements.
This initially appeared to be an Embedded View loading/timing issue, but I've also been unable to get automatic focus working when testing the authentication view independently.
Has anyone successfully used self.focus() from a property-change script or onStartup event in Ignition Perspective 8.3?
Is this a known limitation related to the event context or browser rendering, or is there a recommended way to automatically focus a Text Field after a view loads without requiring user interaction?
Unfortunately there is no onLoad event yet that can be used to trigger such things once every part of a view is finished rendering, which is often necessary for things like .focus() to work. There is a request for it that one can vote for:
In lieu of that, I have often found the easiest way to ensure focus happens when I want it to is to use time.sleep():
import time
def focusMe():
time.sleep(0.5)
self.focus()
system.util.invokeAsynchronous(focusMe)
Always use system.util.invokeAsynchronous() when using .sleep(), because it blocks the thread that it runs on. It's a hack that uses an estimate of how much time will be required for the view to finish loading, and is not ideal, so if you can figure out another event that would invoke it at the right time, then do that.
I saw the topic you referenced while I was troubleshooting. I tried to implement focusing on a parent but wasnt sure if I was implementing correctly and didnt make progress. My text field is in flex containers root>Main>Code and has name digit_1. Tried setting focus on each in turn to get down to my text field but got errors about None types. That's when I created my topic here.
That makes sense, except for the part where it doesn't even trigger from a property change event or message handler. The page can be fully loaded. I'm triggering it from a property change or message handler. It doesn't work, but when I'm triggering it from a button, it does work. I probably misunderstanding, but that test shouldn't be affected by loading state, correct? This test fails in the designer when I manually change the custom property that has the change script or if I click a button and have it change the custom property with the change script or trigger a message handler on the input that just does self.focus().
That may be because the button exists within the same container as the input you're trying to focus, which would mean that it is setting focus first to something that the input itself is able to steal from. I would try this sequence:
Use system.perspective.sendMessage() to send a message that the Main container captures with a script that invokes self.focus() and then sends another message that the Code container captures and similarly focuses itself and then invokes a final message that the input can capture. I don't think starting the chain with a message that root would have to capture is necessary, but try that if it still doesn't work. You could probably also just focus the outermost container first, followed by the input itself...
The reset button I have that focuses on the text input is in a different flex container from Code. So the text field is in root>Main>Code and the reset button is root>Main>Reset. However, the reset button calls a method resetInputs on the root which calls the focus on digit_1 using
I tried the cascade calls of self.focus() inside message handlers and it didnt work with either the top level on Main or root. Example of focusRoot message handler that was on root:
I saw all the print statements so I'm confident my handlers are firing, but focus on the text field still didn't occur.
If I call the resetInputs method on root from my change script, it will clear the inputs, but not focus. Calling it from the reset button will clear inputs and focus.