MattHicks.com

Programming on the Edge

Flex WebBrowser Component

Published by Matt Hicks under , , , , , on Wednesday, February 18, 2009
I've been working on an acceptable solution to display web pages within my Flex applications and as any of you know that have done any research on it will find, it's frustrating how lacking Flex is in support for this. The ideal solution would be for Flex to provide a fully functional component that renders web pages utilizing the native web browser. Alas, it would seem that it's far too complicated for them to leverage the browser they are already running in. Instead, their solution is to create components that support just enough HTML to be completely annoying since you can't get anything really useful to display well. Alright, so enough of that, like most of my posts, I try to mix rant and useful information, so lets get to the latter.

Okay, so the first thing that I discovered is that the only way to display web pages within my Flex content was to use IFrames. There are other possibilities, but after investigation with each they all come up with major flaws (primarily centered around lacking functionality) and IFrame support seems to be the best solution out of a bunch of bad solutions.

Yes, I'm sure if you've spent any time looking into this you'll already know that there are plenty of people that have already written IFrame support components in Flex and they work moderately well. However, there are some major pitfalls they have not resolved that I find unacceptable:
  1. Clipping - Because IFrames float above the Flash context they float above EVERYTHING. So not only do not get proper clipping of an Alert or anything else appearing above the component, you don't get proper clipping when it's inside of a container with scrollbars. This extremely limits the usefulness of the component and makes it almost useless in many sites
  2. Size and location updates. So many of these implementations of IFrame support do such an awful job of properly sizing and adjusting the frame as the Flex container is adjusted.
  3. Multiple components on the same page. In many cases I would like to have several IFrames being displayed on the page at one time and very few of the implementations have support for multiple being displayed at any time.
Yes, all of those are valid, but there are solutions available for all but #1. For a while I wondered if it was even possible to solve, but have since proven otherwise. :)

The first hurdle was figuring out a way to clip IFrame content. This was actually a lot easier than I expected it to be. I simply create a DIV, use absolute positioning, overflow to hidden, and add an iframe to it using relative positioning. From there it's just a simple matter of updating the location and size of the "visible area" for the DIV and adjusting the IFrame within it to clip the relevant areas. This does leave us without a perfect solution as we can obviously only show a single rectangular clipping area, but it's a lot better than no clipping at all.

Here's the code for the webbrowser.js file:
// WebBrowser specific code
function verifyCreated(id) {
var div = document.getElementById('div' + id);
if (div == null) {
div = document.createElement('div');
div.setAttribute('id', 'div' + id);
div.style.position = 'absolute';
div.style.visibility = 'hidden';
div.style.overflow = 'hidden';
var body = document.getElementsByTagName("body")[0];
body.appendChild(div);
}
var iframe = document.getElementById('iframe' + id);
if (iframe == null) {
iframe = document.createElement('iframe');
iframe.setAttribute('id', 'iframe' + id)
iframe.style.position = 'relative';
iframe.style.backgroundColor = 'white';
iframe.style.borderStyle = 'none';
iframe.setAttribute('frameborder', '0');
div.appendChild(iframe);
}
return iframe;
}

function updateBrowser(id, x, y, width, height, clipX, clipY, clipWidth, clipHeight) {
if ((width <= 0) || (height <= 0)) {
hideBrowser(id);
return;
}
var iframe = verifyCreated(id);
iframe.style.left = -clipX;
iframe.style.top = -clipY;
iframe.style.width = width;
iframe.style.height = height;
iframe.style.display = '';

var div = document.getElementById('div' + id);
div.style.left = x + clipX;
div.style.top = y + clipY;
div.style.width = clipWidth;
div.style.height = clipHeight;
div.style.visibility = 'visible';
div.style.display = '';
}

function loadURL(id, url) {
var iframe = verifyCreated(id);
iframe.src = url;
}

function hideBrowser(id) {
var div = document.getElementById('div' + id);
div.style.visibility = 'hidden';
div.style.display = 'none';
}
So that solves the dilema of clipping. The next hurdle, which I was quite surprised was a hurdle at all, was the of determining the visible bounds of a Flex component. You see, I wrote a WebBrowser Flex component that represents the IFrame within Flex and receives all the events for changes to size, location, visibility, etc. and then relays them to the IFrame it manages. However, Flex seems to have absolutely no support for finding the visibles bounds on a Component. So, as my solution to most things, I wrote my own:

        public static function getVisibleBounds(component:UIComponent):Rectangle {
var r:Rectangle = new Rectangle();

var step:Number = 50;

var best:Rectangle = new Rectangle();

var yOffset:Number = 0;

// Find largest bounding area
do {
nextBounds(component, yOffset, step, r);
yOffset += r.y + r.height + step;

if (r.width * r.height > best.width * best.height) {
best.x = r.x;
best.y = r.y;
best.width = r.width;
best.height = r.height;
}
} while (r.x != -1);

// Expand bounds broadly
expandBounds(component, best, step, step);

// Expand bounds narrowly
expandBounds(component, best, 1, step);

component.graphics.clear();
component.graphics.beginFill(0xffffff);
component.graphics.drawRect(best.x, best.y, best.width, best.height);
component.graphics.endFill();

return best;
}

private static function expandBounds(component:UIComponent, r:Rectangle, step:int, stepJump:int):void {
// Look up
while (validateHorizontal(component, r.x, r.y - step, r.width, stepJump)) {
r.y -= step;
r.height += step;
}

// Look down
while (validateHorizontal(component, r.x, r.y + r.height + step, r.width, stepJump)) {
r.height += step;
}

// Look left
while (validateVertical(component, r.x - step, r.y, r.height, stepJump)) {
r.x -= step;
r.width += step;
}

// Look right
while (validateVertical(component, r.x + r.width + step, r.y, r.height, stepJump)) {
r.width += step;
}
}

private static function validateHorizontal(component:UIComponent, x:int, y:int, width:int, step:int):Boolean {
for (var i:int = x; i <= width; i += step) {
if (!isUnderPoint(component, i, y)) {
return false;
}
}
if (isUnderPoint(component, x, y)) {
if (isUnderPoint(component, x + width, y)) {
return true;
}
}
return false;
}

private static function validateVertical(component:UIComponent, x:int, y:int, height:int, step:int):Boolean {
for (var i:int = y; i <= height; i += step) {
if (!isUnderPoint(component, x, i)) {
return false;
}
}
if (isUnderPoint(component, x, y)) {
if (isUnderPoint(component, x, y + height)) {
return true;
}
}
return false;
}

private static function nextBounds(component:UIComponent, yOffset:Number, step:int, r:Rectangle):Rectangle {
r.x = -1;
r.y = -1;
r.width = -1;
r.height = 0;

var p:Point = findFirstVisible(component, yOffset, step);
if (p != null) {
r.x = p.x;
r.y = p.y;
for (var y:int = p.y + step; y <= component.height; y += step) {
var currentWidth:Number = 0;
for (var x:int = p.x + step; x <= component.width; x += step) {
if (isUnderPoint(component, x, y)) {
currentWidth += step;
}
}
if (r.width == -1) {
r.width = currentWidth;
} else if (r.width > currentWidth) {
return r;
}
r.height += step;
}
}

return r;
}

private static function findFirstVisible(component:UIComponent, yOffset:Number, step:int):Point {
for (var y:int = yOffset; y <= component.height; y += step) {
for (var x:int = 0; x <= component.width; x += step) {
if (isUnderPoint(component, x, y)) {
return new Point(x, y);
}
}
}
return null;
}

private static var underPoint:Point = new Point();
public static function isUnderPoint(component:UIComponent, localX:Number, localY:Number):Boolean {
underPoint.x = localX + 1;
underPoint.y = localY;
underPoint = component.localToGlobal(underPoint);
var a:Array = Application.application.stage.getObjectsUnderPoint(underPoint);
for (var i:int = a.length - 1; i >= 0; i--) {
var c:UIComponent = getComponent(a[i]);
if (c != null) {
if (c.mouseEnabled) {
return c == component;
}
}
}
return false;
}

public static function getComponent(obj:DisplayObject):UIComponent {
while (obj != null) {
if (obj is UIComponent) {
return UIComponent(obj);
}
obj = obj.parent;
}
return null;
}


Yes, that's an awful lot of code, but it does work, and it works quite well. I find the largest bounding visible rectangular area for the Component and return a Rectangle representing it. I work in large steps (50 pixels at a time) to increase the performance and then make minor adjustments at the end to correct for the margin of precision loss.

The only thing still necessary to post is the WebBrowser Flex component:

    import flash.display.DisplayObjectContainer;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.external.ExternalInterface;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.utils.Timer;

import mx.containers.Canvas;
import mx.core.Application;
import mx.core.UIComponent;
import mx.events.FlexEvent;
import mx.events.MoveEvent;
import mx.events.ResizeEvent;

public class WebBrowser extends Canvas {
private static var ID_GENERATOR:int = 0;

[Inspectable(defaultValue=null)]
private var pageUrl:String = null;
private var initted:Boolean = false;

private var changed:Boolean;
private var lastChanged:Number;
private var hidden:Boolean;

private var lastX:Number = -1;
private var lastY:Number = -1;
private var lastW:Number = -1;
private var lastH:Number = -1;

public function WebBrowser() {
super();
id = String(++ID_GENERATOR);
this.addEventListener(FlexEvent.CREATION_COMPLETE, onFlex, false, 0, true);

var timer:Timer = new Timer(15);
timer.addEventListener(TimerEvent.TIMER, update);
timer.start();

setStyle('borderThickness', 0);
clipContent = false;

changed = false;
hidden = true;

lastChanged = -1;
}

private function onEvent(event:Event):void {
if (event.target is UIComponent) {
if (event.target.owns(this)) {
if (event.type == 'remove') {
// Hide it
visible = false;
}
validateWindow();
}
}
}

private function onFlex(event:FlexEvent):void {
validateWindow();
if (pageUrl != null) ExternalInterface.call('loadURL', id, pageUrl);
systemManager.addEventListener(FlexEvent.HIDE, onEvent, true, 0, true);
systemManager.addEventListener(FlexEvent.SHOW, onEvent, true, 0, true);
systemManager.addEventListener(MoveEvent.MOVE, onEvent, true, 0, true);
systemManager.addEventListener(ResizeEvent.RESIZE, onEvent, true, 0, true);
systemManager.addEventListener(FlexEvent.REMOVE, onEvent, true, 0, true);
systemManager.addEventListener(FlexEvent.ADD, onEvent, true, 0, true);
initted = true;
}

private function onHide(event:FlexEvent):void {
ExternalInterface.call('hideBrowser', id);
}

private function isVisible():Boolean {
var obj:DisplayObjectContainer = this;
while (obj != Application.application) {
if (obj == null) {
// Removed from container
return false;
}
if (!obj.visible) {
return false;
}
obj = obj.parent;
}
return true;
}

public function validateWindow():void {
if (!isVisible()) {
onHide(null);
return;
}
changed = true;
}

private function update(evt:TimerEvent):void {
var time:Number = new Date().time;

var p:Point = new Point(0, 0);
p = localToGlobal(p);
if ((p.x == 0) && (p.y == 0)) {
return;
}
if ((lastX != p.x) || (lastY != p.y) || (lastW != width) || (lastH != height)) {
changed = true;
} else if (isVisible() == hidden) {
changed = true;
} else if ((lastChanged != -1) && (time - lastChanged > 500)) {
changed = true;
}

if (changed) {
changed = false;
lastChanged = time;

if (isVisible()) {
hidden = false;
lastX = p.x;
lastY = p.y;
lastW = width;
lastH = height;

var rect:Rectangle = FlexUtilities.getVisibleBounds(this);
ExternalInterface.call('updateBrowser', id, p.x, p.y, width, height, rect.x, rect.y, rect.width, rect.height);
} else {
hidden = true;
onHide(null);
}
}
}

public function set source(_pageUrl:String):void {
pageUrl = _pageUrl;
if (initted) {
ExternalInterface.call('loadURL', id, pageUrl);
}
}

[Bindable(event="changeUrl")]
public function get source():String {
return pageUrl;
}
}
Well, that does it. I hope you found this useful and proof that IFrame support can actually be done moderately well in Flex. The biggest problem I still have is that it can still be moderately slow, but that's an underlying flaw of the IFrame displaying in Flex and not something I can do anything about unfortunately.

10 comments:

Karl P said... @ April 12, 2009 at 10:25 PM

Hey, this is great. Does this only run in AIR or can it run in flex. If it can run in flex, would you mind posting this code in a small application - I'm very new to flex and am having a hard time getting the code to work. Thanks for the efforts.

ansolas said... @ May 3, 2009 at 5:14 AM
This comment has been removed by the author.
Matt Hicks said... @ May 3, 2009 at 8:22 AM

The code to make this work is pretty simple. You just need to import the package and then use it like: <browser:WebBrowser source="http://www.google.com" width="300" height="300"/>

The "browser" prefix represents the imported package namespace.

Juan said... @ May 4, 2009 at 12:12 PM

Hey Matt,
this is amazing, just what I am looking for, i get to compile it but I still don't get a result on my app, is it too much to ask if you can zip your src folder for us?
To see to correct implementation =)

Baz said... @ September 15, 2009 at 8:02 AM

Well done.

noureddine.belmabrouk said... @ October 21, 2009 at 11:52 AM

Good work,

Thank you for this great job, but I was wondering if you can share the source code. That will be appreciated.

Thanks for all

Matt Hicks said... @ October 21, 2009 at 12:26 PM

That's 99% of what that this post was is source code! :o

noureddine.belmabrouk said... @ October 22, 2009 at 5:26 AM

Hi,

Thank you Matt.
I've copied the source from the blog but it didn't work. Nothing is shown.

Could you help, please?

Thanks

Matt Hicks said... @ October 22, 2009 at 7:15 AM

Sorry, code is offered as-is and really requires a basic understanding of how it works to use properly. I would suggest going through the code and trying to understand it better and that will likely lead you to the solution.

manju100 said... @ March 31, 2010 at 6:36 AM

i tried all the way to execute this code, but seem it wont work.

even now where the sample demo provided which makes me feel this is not working code

Post a Comment