Monday, December 11, 2006

Bug with V2 Alert component?

A collegue was telling me that he was having troubles with displaying alerts using the V2 Alert component. The alert message was getting truncated at some point or other. I set out to investigate the problem, and saw that this only happens when the alert displays an icon in it. A simple alert (with no icon) didn't seem to exhibit this problem.
Try this out:"Hello world, this is a test implementation", "Alert", Alert.OK, this, alertHandler, "alertIcon", Alert.OK);

"alertIcon" is the linkage ID of a 25 * 35 px movieclip in library.
Do a test movie, and the alert displays "Hello world, this is a test implement". As you see, the last word is partially truncated.
After a few minutes of fiddling around with the mx.controls.alertClasses.AlertForm class, I figured out that editing the following statement(lin no 213) in getSize method helps
width = Math.min(extent.width + 4 + 8, 2 * bWidth);
width = Math.max(extent.width + 4 + 8, 2 * bWidth);
Please note that when you do this change, And when your alert message is really small ( like "hello world") you'll still see that the alert box is a lil too wider. But in a practical sense, most messages that you alert users are atleast 3 -4 words long, so this wouldn't be as much as a problem as losing out some data!

I googled for this issue a bit, and since I didn't find much info on that, thought I'd post it up here.


Anonymous said...

I love the way you're always digging into the original classes and fixing the problem right where they are. =)

Now let's work on that invalidate() bug in scrollPane. haha... =P

Anonymous said...

the issue can be reduced to the problem with getTextExtent.

(TextFormat.)getTextExtent doesn't seem to give accurate measurement for font metrics.

See: Keith's post on this here.

Arul said...

Thanks for the comments Lionell. We'll do tat when we are free the next time ;)

Hey Anonymous,
Thanks for the comments. I am aware of the getTextExtent issue. And also the getTextExtent2 added to the TextField prototype from mx.core.ext.UIObjectExtensions as well. Infact, the AlertForm class for the Alert component does use the getTextExtent2 method in there.

Keep digging ;)

rctay said...

thanks for the tip, didn't know that.

Sol said...

I notice the truncation problem too, but my alerts have no icons. I find the problem comes in when I have spaces in my long text. Without spaces everything fits just fine but with spaces it doesn't. I'm curious to try your fix, but am not sure the best way to go about changing the source. Do you have directions on how best to do this an incorporate the new components into your project? I tried it, but it was very frustrating because I didn't have a consistent way to activate my changes.

Thanks in advance for your help,

Arul said...


If you were worried about changing the source u could as well extend the existing Alert component class, and create ur class - which would have the fix.

I had to edit the Alert component, because we came across this issue kinda very late in into the project, and the Alert component was already being used in multiple places on the project. Fixing it in the source gave a global fix for the problem.