ResizeEvent Looping? - apache-flex

I am having an issue with an actionscript event that I am firing every time a flex component resizes. In the event, I am altering the height of the flash object within its html wrapper via an external javascript function. This in turn causes the component to resize and the event to loop in upon itself and continually add height to the component.
Here is an example of the code causing the issue:
private function onCreationComplete(event:Event):void
{
this.mainInnerShell.addEventListener(Event.RESIZE,handleResize);
}
private function handleResize(event:Event):void {
this.mainInnerShell.removeEventListener(Event.RESIZE,handleResize);
ExternalInterface.call("changePageHeight",this.mainInnerShell.height + resizeBuffer);
this.mainInnerShell.addEventListener(Event.RESIZE,handleResize);
}
However, the event fires more than once despite my removal of the event listener. Any suggestions?

Inside the resize event handler you can use removeEventListener to stop listening to that event, make you height changes and then use addEventListener to start listening for resizes again.

Related

Letting another widget handle your events in Qt

I have a QSpinBox in a QMainWindow holding a value. Natively the spinbox, well ... spins on QWheelEvent if focused. I would like to expand the scrolling of the value in the spinbox to be applied invariantly of focus within the window.
The spinbox manages a value that dictates behavior to the whole context of the window live.
I am trying to divert the scroll event to the spinbox to be handled by the latter to implement this instead of handling a custom value somewhere and to synchronize it with the spinbox manually.
But I am failing. I have an EventFilter in the window that can intercept the event in the window invariantly of focus, but the wheelEvent method in the spinbox is protected so I cannot call it from there.
I do not want to just make a custom spinbox for this if I can help it.
Any ideas?
In the event filter, do not call the protected spinBox->wheelEvent(wheelEvent). You can instead pass the event to the spin box using static function QCoreApplication::sendEvent(spinBox, wheelEvent).
Of course, seeing your code would help to show more concrete solution. Especially you should make sure that your event filter does not intercept this re-sent event again, creating an infinite loop...

Why we use 'bubbles' in flex events

I've doubt when we create custom event in flex.
Why do we use 'type:String, bubbles:Boolean=false, cancelable:Boolean=false' these parameter in flex events.
Bubbling causes a dispatched event to continue to be dispatched up the display tree until it reaches the stage. This is useful in various scenarios.
For example: Imagine you have several buttons inside a parent DisplayObject. You could add listeners to each button, and remember to remove them afterwards, or you could just add one listener to the parent. This works because MouseEvents have bubbling enabled.
buttonParent.addEventListener(MouseEvent.CLICK,handleButtonClick);
function handleButtonClick(event:MouseEvent):void
{
trace("The button clicked was " + event.target.name);
}
The benefit of this is that you can now add and remove buttons freely, without having to worry about attaching listeners to them. The target property of the event object will be a reference to the button that was clicked, and currentTarget will be a reference to the parent.
Cancelable is a flag that sets whether or not you are permitted to stop the default action of an event by calling the preventDefault() method.

How does Flex click event work inside of containers?

I have a VBox, I assigned a handler to click, and inside the VBox I have components such as images and texts with no handler assigned for click. Would the click function be called when I click on the text and image? If not how can I make it so without assigning handlers individually, but on the container level?
Thanks
Click events "bubble" in Flex. When you click on an images, it bubbles up to its parent, then that parent's parent and so on until there are no more parents left.
If any of these have click listeners they will trigger when they are reached in the bubbling process.
Also in the event the currentTarget will refer to the object that has the listener, and the target will be what was actually clicked.
So in your case if they click the image, the event will bubble up to the container triggering the event, in your listener function the clicked image will be the event.target and the container will be the event.currentTarget.
Also in the bubbling process, it actually starts from the root parent down, this is called the capture phase, then bubbles back up. Your event will trigger when it bubbles back up unless you specify useCapturePhase = true in the event listener. This is how you can stop an event from going to its children. If you use the capture phase then call event.stopPropagation() inside the event listener then the container will receive the event but the child image will not.
It's taken an hour for an answer to this question... it probably would have been faster to just try it. :)
But yes, click events bubble up to parent containers. Adding the handler to the VBox should be fine.
I was pretty sure that containers, such as VBox do not dispatch click events; unless they are bubbled up from the children.
However, clicking on items in your container should trigger the listener on your container, as the Click event bubbles.

Event propogation whilst container is not initialised

I have a Canvas (lets call it the Drop Box) which users can drag and drop external files onto. Next to this I have a ViewStack, of which one of the layers is a Canvas with a TileList. I have successfully managed to code it so that the items dropped onto the Drop Box appear in the TileList. I simply capture the darg drop event (lets call this event A) and dispatch a new one that the TileList is listening for (lets call this event B).
However, this only works if the ViewStack selectedIndex is set to that of the Canvas with the TileList. If the Canvas with the TileList isn't selected then the event listener which is added to the TileList at CreationComplete level (event B), won't be called until after the drag drop event has dispatched (event B). This means that something is firing before something even has a chance to listen for it!
I've tried looping until the Canvas with the TileList is completely drawn, but this causes the app to hang.
I've also tried passing the event to the Canvas and storing it locally, but when I attempt to access the clipboad of the event I get an error (dead clipboard).
Effectively I want to only dispatch the event to the Canvas after it's had a chance to load, and add the event listener to the TileList.
Any ideas? :)
Maybe setting creationPolicy="all" for View Stack will help? All it's children will be created at startup.

Changing view states on application resize

I want to change the view states in my flex app when it resizes in the browser window.
I have the swf embedded at 100% x 100%. So when the user resizes the window below a certain width, I want to switch to a different state. I tried adding an event listener like this, but it only fires the event when I resize the swf outside the browser, not inside. I used:
this.addEventListener(ResizeEvent.RESIZE, SizeChanged);
I want this to work within the browser. I even tried using fixed dimensions in the embed code, instead of percentages, but that didn't help either.
You want to add the listener to the stage.
this.stage.addEventListener( Event.RESIZE, resizeHandler ); //from your Main.mxml creationComplete handler
Or you can add a listener via:
Application.application.stage.addEventListener( Event.RESIZE, resizeHandler)
Also, keep in mind that this event fires a lot as the view is resizing, so you will want to account for that.

Resources