Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/haxeui/haxeui-core
Browse files Browse the repository at this point in the history
  • Loading branch information
ianharrigan committed Aug 30, 2024
2 parents 2c302f6 + 932b07f commit a1b0508
Showing 1 changed file with 34 additions and 8 deletions.
42 changes: 34 additions & 8 deletions haxe/ui/containers/Box.hx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Box extends Component implements IDataComponent {
**/
@:clonable @:behaviour(DefaultBehaviour) public var icon:Variant;
@:clonable @:behaviour(DataSourceBehaviour) public var dataSource:DataSource<Dynamic>;
@:clonable @:behaviour(DefaultBehaviour, true) public var cacheItemRenderers:Bool;

@:noCompletion private var _layoutName:String;
@:clonable public var layoutName(get, set):String;
Expand Down Expand Up @@ -76,11 +77,11 @@ class Box extends Component implements IDataComponent {
_defaultLayoutClass = DefaultLayout;
}
}

@:noCompletion private var _direction:String = null;
private override function applyStyle(style:Style) {
super.applyStyle(style);

if (style.direction != null && style.direction != _direction) {
_direction = style.direction;
this.layout = LayoutFactory.createFromName(_direction);
Expand All @@ -99,6 +100,7 @@ class Box extends Component implements IDataComponent {
//***********************************************************************************************************
private class Builder extends CompositeBuilder {
private var _box:Box;
private var _cachedItemRenderers:Array<ItemRenderer>;

public function new(box:Box) {
super(box);
Expand Down Expand Up @@ -143,36 +145,60 @@ private class Builder extends CompositeBuilder {
_box.itemRenderer.handleVisibility(false);
}

var childRenderers = _component.findComponents(ItemRenderer, 1);

for (i in 0...dataSource.size) {
var item = dataSource.get(i);
var renderer = findRenderer(item);
var renderer = findRenderer(item, childRenderers);
if (renderer == null && _box.cacheItemRenderers && _cachedItemRenderers != null) {
renderer = findRenderer(item, _cachedItemRenderers);
if (renderer != null){
_cachedItemRenderers.remove(item);
_box.addComponent(renderer);
}
}
if (renderer == null) {
renderer = itemRenderer.cloneComponent();
_box.addComponent(renderer);
}

renderer.itemIndex = i;
_box.setComponentIndex(renderer, i);
renderer.data = item;
}

for (child in _component.findComponents(ItemRenderer)) {
for (child in childRenderers) {
if (child == _box.itemRenderer) {
continue;
}
if (dataSource.indexOf(child.data) == -1) {
_box.removeComponent(child);
_box.removeComponent(child, !_box.cacheItemRenderers);
if (_box.cacheItemRenderers) {
if (_cachedItemRenderers == null){
_cachedItemRenderers = [];
}
_cachedItemRenderers.push(child);
}
}
}
}

private function findRenderer(data:Dynamic):ItemRenderer {
for (child in _component.findComponents(ItemRenderer)) {
private function findRenderer(data:Dynamic, renderers:Array<ItemRenderer>):ItemRenderer {
for (child in renderers) {
if (child.data == data) {
return child;
}
}
return null;
}

public override function destroy() {
if (_cachedItemRenderers != null) {
for (itemRenderer in _cachedItemRenderers) {
itemRenderer.disposeComponent();
}
}
super.destroy();
}
}

//***********************************************************************************************************
Expand Down

0 comments on commit a1b0508

Please sign in to comment.