Thursday, September 30, 2010

Flex annoyances...Can I grabz the initialz focuz?

I've been playing a bit with ActionScript and the like lately. ActionScript and few other languages never really caught my interest. Sure, I can read and I could manage, but why? ;-).

In fact, I've been avoiding Perl, ActionScript and few others professionally for years:
Q:"So you know Java, Quartz, JMX, Unix/Linux, etc. what about Perl?"
A:"Oh my, to be honest, I'm afraid of it...".
Sometimes, you just have to suck it up and do the damn thing anyway.

It's surprising to me that Flex is used a lot for "intensive" tasks, that would/should require threads to poll data at specific intervals. What else would you use if you need a decent toolkit with advanced drawing capabilities on the browser? A beloved Java Applet? Silverlight? HTML5? Javascript?? ActiveX???

Flex has some interesting things despite being single threaded(not the flash engine itself) : function pointers, easy binding, easy drag and drop, etc. However, some simple to more complex things can become annoying, while with more code, but still simple code, the same thing would be trivial with a Desktop UI toolkit(that you're familiar with).
  • Can I be sure that a return inside a switch statement will just return, at once?
  • Can I get the initial focus on a component once the flash movie is loaded, without keyboard interaction from my part? without random errors once in a while? without upgrading to bleeding edge?
  • Can I get a simple even bare layout manager interface without plugging stuff directly inside some updateDisplayList method? Or without managing myself couple of  "drawing delegates"?
  • etc...

It looks like Flex has couple of issues with its FocusManager. All I wanted was setting the focus on a component at startup with a blinking cursor... Trivial right?

I was looking for an answer on Google as many seem to have that issue. I found a suggestion that seem to work :
ExternalInterfaceIfAvailable.call(SomeJavascript.focusFlashComponent).
callLater(mycomponent.grabTheFocus)

Lots of people seem to be ok with the fact that the Javascript that they write might not work on most browsers. Well, good enough if only a defined set of browsers will be supported by the application.
But hey man, this is 2010, most web applications are past the time where a notice would be displayed "Only supported in Internet Explorer". Unless this is the web interface of your online banking accounts, you switch to an alternative immediately."Sorry man, I do not use Windows, so how could I use I.E??".

I just grabbed the latest version of prototypejs and I'm confident that the code compatible with most modern browsers(and even maybe text based browsers that support a little subset of Javascript). Why not write it myself? Well I don't do tons of Javascript, and looking at browsers specs and compatibility with Javascript versions might not be worth the time. From prototypejs to JQuery and the like, people already did the dirty work.

Let's say that the Javascript code is working, now you're able to set the initial focus on a component once the flash movie is loaded. In my case, the component that need the focus is added/removed at runtime.

When going back to the initial screen, Flex 3.4 throws randomly errors in the FocusManager. It looks like that FocusManager bug got fixed(defaultButton issue), but my solution was to deactivate/activate the FocusManager myself :
  • Initial screen
onCreationComplete -> javascript call to set the focus -> Specific flex component requests then the focus
  • Leaving the initial screen(Avoid some null errors on the defaultButton if you use that)
component.focusmanager.deactivate()
  • Going back to the initial screen(Avoid some null errors on the defaultButton if you use that)
mycodeToGrabTheFocus
component.focusmanager.activate()