How can I force Flex to load a particular element first? - apache-flex

I have a read-only accessor whose value depends on the width of an image. The width of this image determines all sorts of positional values defined in flex. It turns out that, when I load, the accessor returns 0 for a while, and then, about when the thing starts to accept input, it returns its proper value.
Is there any way that I can force Flex to load the image first, thereby specifying its width?

why don't u load the image using Loader class, and loader class has load complete event, and then on load complete of image, do whtever u want,
var loader:Loader = nee Loader();
loader.load(new URLRequest('tree.png');
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,productLoadingComplete);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,productLoadingError);
private function productLoadingComplete(event:Event):void
{
}

Related

getComputedStyle returns a CSSStyleDeclaration but all properties are empty on access

I have a simple div that uses a css class which in turn has a color and background-color property set.
var div = document.createElement("div");
div.classList.add("my-css-class");
container.appendChild(div);
//This prints out a CSSStyleDeclaration object. This is a large object to which I see `color` and `backgroundColor`
console.log(getComputedStyle(div));
//this gives me an empty string
console.log(getComputedStyle(div).color);
//this gives me an empty string
console.log(getComputedStyle(div).getPropertyValue("color"));
Why am I unable to access the properties of the CSSStyleDeclaration? I know it is there from my first log. I can see color: "rgb(82, 194, 145)"
Ultimately the problem with getComputedStyle is that it appears that the element in question must be a part of the DOMTree in order for any style to be available.
Notice in my case, I was adding the div to the parent container, however, the parent container at the point of instantiation is not yet added to the DOMTree.
My failure to use JSON.stringify() to print the object meant that values were appearing later but at the time of access, were blank.
So basically, I changed my container.appendChild to document.body.appendChild temporarily to compute the style I need straight away, and then remove it and destroy it leaving me just the colors I needed.

when is the proper time to call UIObject.getOffsetWidth?

i want get the uiobject's offsetwidth in gwt. but this value depends:
where the uiobject attached or not?
where relative css loaded or not?
uiobject's size may be modified in onload callback.
...
so, things get complicate, especially when you want get this value of FlowPanel,Label,InlineLabel,etc.. which size is dynamically calculated.
my question: when or how to get offsetwidth properly?
You can get the correct size only after the page is completely rendered. Otherwise you will get zero values. Typically, there are two use cases:
(A) Tell the browser to render your view. Then use Scheduler to make sure the browser has finished rendering, before you check for sizes:
Scheduler.get().scheduleDeferred(new ScheduledCommand() {
#Override
public void execute() {
int width = myPanel.getOffsetWidth();
// do something with this width
]
]
(B) Attach a resize handler to a Window. If a user changes the browser's window size, all width and heights may change. You don't need to use a Scheduler in the ResizeHandler - a browser will complete re-rendering the page before calling the ResizeHandler.

Capture "screenshot" of Flex Display Object created with ActionScript but not added to stage

Is it possible to create a display object in AS and take a screenshot of it as ByteArray or whatever without adding it to stage?
You can do this with any DisplayObject using BitmapData http://blog.728media.com/tag/bitmapdata/ . Note that in Flex, components that have not been added to the stage may not lay out properly until you give them a width and height and call validateNow() on them.

Generate a flex image from a hidden component

I'm trying to put an image, generated from some text, in a RichEditableText. Since it's a styled text, I thought about putting it another RichEditableText, style it, then print it to a Bitmap to use as source for InlineGraphicsElement.
I use the following code to do that
var txt:RichEditableText = new RichEditableText();
txt.text = name;
// Appliy given styles to the text flow of input rich editable text
createApplyNamedStyle(name, styles).call(null, txt.textFlow);
var bitmapData:BitmapData = new BitmapData(txt.width, txt.height);
bitmapData.draw(txt);
var bitmap:Bitmap = new Bitmap(bitmapData);
Unfortunatly, when called, it displays an error stack
ArgumentError: Error #2015: BitmapData non valide.
at flash.display::BitmapData()
at RichTextEditor/getTagImage()[E:\FlexWorkspace\Test\src\RichTextEditor.mxml:74]
at RichTextEditor/insertTag()[E:\FlexWorkspace\Test\src\RichTextEditor.mxml:154]
I suspect it is due to the fact that my RichEditableText, not being in visible component, is not laid out.
How can I ensure it is properly laid out ?
And am i doing the right thing to transform my text into an image ?
You're right; since the text is not on the display list, it is never validated and hence has 0 height and width.
A typical workaround is to add the item to the display list and then remove it immediatley. A little more discussion in this SO question.
You should trace txt.width and txt.height. They must be at least greater or equal to one. It does not matter if a DisplayObject is visible or not.

Why are width & height of loaded SWFLoader zero?

I have a class which extends SWFLoader, I use it like a normal SWFLoader:
var loader:MySWFLoader = new MySWFLoader();
loader.load("myFile.SWF");
myScene.addChild(loader);
The loading works OK, except that it remains 0 because the width & height never change from 0. I had to override the width/height get properties to make it work:
class MySWFLoader extends SWFLoader
{
public override function get width():Number{ return contentWidth; }
public override function get height():Number{ return contentHeight; }
}
As well as being a big hack this isn't quite correct; especially since the SWF has multiple frames and the frames are of different width/height.
Why aren't width/height working in the first place? Shouldn't they be automatically set during loading? Is it possible the issue lies with the SWF itself somehow?
Update: Changing scaleContent to true or false makes no difference.
I'm not sure if this applies for SWF loading, but whenever I'm loading content, i cannot access width and height before the whole thing is loaded.
So make an event listener that listens when the loading is completed, and then read the height/width.
Also take a look at the loaderInfo class in AS3
I'm not sure what part of the cycle you're trying to grab the height and width (and is it of the loader object or the loaded content) but you can can access the height and width of the loaded object using SWFLoader.loaderInfo.height and SWFLoader.loaderInfo.width.
By default the SWFLoader scales the content to the size of the loader, so you have to set the size of the loader. If you want the loader to scale to the size of the content then you have to set the scaleContent property to false.
SWFLoader is a UIComponent.
It needs to be run through the UIComponent framework to set its dimensions correctly
In other words, it is waiting for calls to commitProperties(), updateDisplayList(), etc.
If you drop it in your application (so that it is part of the layout framework) it should be fine.

Resources