Friday, November 6, 2009

Mojo Rising: AutoFocusing on Dialogs

While I have every confidence the documented controller.setInitialFocusedElement() method will work during scene pushes, the story is somewhat different if you only showDialog(). I'm currently adding the final touches to my first complete Palm Pre application, in preparation for a postmortem blog post on developing with Mojo.
One of those final touches was automatically focusing on the text input widget when the dialog was shown. This simple thing took me over 2 hours to figure out and required reading through the actual framework code provided by Palm.
For whatever reason some methods of the scene controller aren't fired on showDialog() like they are on pushScene(). Might track down more on this later.
For now, to get Mojo to automatically focus on an element when a dialog is shown, do this:

1. When creating your view, add a tabindex="0" to the div which will become the widget. Or use the Mojo.View.makeFocusable() method, which currently does the same thing.
2. In the Assistant object you passed to the showDialog() method as the "assistant" property, add the following line of code to the activate() method:
Mojo.View.advanceFocus(this.controller.sceneElement);

Alternatively you could just call:
this.controller.advanceFocus();
However, the advanceFocus() method of the scene controller object is annotated as private so using it is likely not the best practice. There also might be a better way to get the sceneElement, but at the time of this posting I am far too tired to look it up.

More Mojo Rising coming soon, folks.

--PXA

No comments: