patternMinor
Getting better speed from adjusting rows in a DockableToolBar
Viewed 0 times
rowsadjustinggettingbetterdockabletoolbarfromspeed
Problem
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.
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).
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 = toolbar.width) {
break;
}
//Move child.
child.parent.removeChild(child);
prevRow.addChild(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).
Solution
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);
prevRow.addChild(child);
prevRowW += child.measuredWidth;
--k;Code Snippets
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);
prevRow.addChild(child);
prevRowW += child.measuredWidth;
--k;Context
StackExchange Code Review Q#1132, answer score: 5
Revisions (0)
No revisions yet.