# Getting better speed from adjusting rows in a DockableToolBar

I've been using FlexLib for its dockable toolbar feature. When you shrink the window, it will try to wrap rows if the stage width is less than the row width. Unfortunately, it won't reverse the process when you expand the window. To get around that, I wrote the following method to add to the class (the caller is responsible for looping through all current toolbar rows and calling this method on them).

It does the job, but it's not quite as fast as I'd like. I'm hoping for suggestions to improve both performance and code style, if possible.

/**
*  @private
*  Moves ToolBars to the previous row if there is sufficient space in that row to display
*  another ToolBar properly.
*/
private function unwrapRow(row:HBox, rowIndex:int, toolbar:VBox):void
{
if (rowIndex == 0) {
return;
}
var prevRow:HBox = HBox(toolbar.getChildAt(rowIndex-1));
var prevRowW:int = 0;
var totalW:int = 0;

//First, get the width of the previous row
for (var j:int = 0; j < prevRow.numChildren; j++)
{
var ch:DockableToolBar = DockableToolBar(prevRow.getChildAt(j));
prevRowW += ch.measuredWidth;
}
//Second, cycle through the children of the "row of interest", moving them
//up to the previous row if there's room.
for (var k:int = 0; k < row.numChildren; k++)
{
var child:DockableToolBar = DockableToolBar(row.getChildAt(k));
if ((prevRowW + child.measuredWidth) >= toolbar.width) {
break;
}
//Move child.
child.parent.removeChild(child);
prevRowW += child.measuredWidth;
--k;
}
if (row.numChildren == 0) {
toolbar.removeChild(row);
}
}


You can see the rest of this class here (that listing doesn't include my code; I plan to submit this as a patch once it's in good enough shape).

-

I have one little suggestion with regard to style. It is much better to invert your conditions to have the simpler code run first when it involves yielding control. It also helps you avoid excessive indentation.

var child:DockableToolBar = DockableToolBar(row.getChildAt(k));

//Skip the rest of the row if we can't move the first child up.
if (prevRowW + child.measuredWidth >= toolbar.width) {
break;
}

//Move child.
child.parent.removeChild(child);