From 6c65faa90b345a38e5c39f342698f69d4ed06ca1 Mon Sep 17 00:00:00 2001 From: elnabo Date: Wed, 7 Jun 2017 21:45:41 +0200 Subject: [PATCH 1/2] Adding support for Tree Listview --- haxe/ui/containers/ListView.hx | 56 ++++++++++++++++++-------------- haxe/ui/core/TreeItemRenderer.hx | 41 +++++++++++++++++++++++ 2 files changed, 73 insertions(+), 24 deletions(-) create mode 100644 haxe/ui/core/TreeItemRenderer.hx diff --git a/haxe/ui/containers/ListView.hx b/haxe/ui/containers/ListView.hx index a30bc563f..4d4c7df10 100644 --- a/haxe/ui/containers/ListView.hx +++ b/haxe/ui/containers/ListView.hx @@ -6,6 +6,7 @@ import haxe.ui.core.Component; import haxe.ui.core.IDataComponent; import haxe.ui.core.InteractiveComponent; import haxe.ui.core.ItemRenderer; +import haxe.ui.core.TreeItemRenderer; import haxe.ui.core.MouseEvent; import haxe.ui.core.UIEvent; import haxe.ui.data.ArrayDataSource; @@ -26,11 +27,11 @@ class ListView extends ScrollView implements IDataComponent { _contents.percentWidth = 100; _contents.addClass("listview-contents"); } - + private override function onReady() { syncUI(); } - + public override function addComponent(child:Component):Component { var r = null; if (Std.is(child, ItemRenderer) && (_itemRenderer == null && _itemRendererFunction == null)) { @@ -52,17 +53,16 @@ class ListView extends ScrollView implements IDataComponent { private var _currentSelection:ItemRenderer; private function onItemClick(event:MouseEvent) { - if (event.target == _currentSelection) { + if (event.target == _currentSelection || Std.is(event.target, TreeItemRenderer)) { return; } - var arr = event.target.findComponentsUnderPoint(event.screenX, event.screenY); for (a in arr) { if (Std.is(a, InteractiveComponent)) { return; } } - + selectedItem = cast(event.target, ItemRenderer); } @@ -78,7 +78,7 @@ class ListView extends ScrollView implements IDataComponent { selectedItem = item; return value; } - + public var selectedItem(get, set):ItemRenderer; private function get_selectedItem():ItemRenderer { return _currentSelection; @@ -86,8 +86,16 @@ class ListView extends ScrollView implements IDataComponent { private function set_selectedItem(value:ItemRenderer):ItemRenderer { if (_currentSelection != null) { _currentSelection.removeClass(":selected"); + if (Std.is(_currentSelection, TreeItemRenderer)) { + var tree:TreeItemRenderer = cast _currentSelection; + tree.list.selectedItem = null; + } } - + if (value != null && parentComponent != null && Std.is(parentComponent.parentComponent, TreeItemRenderer)) { + var list:ListView = cast parentComponent.parentComponent.parentComponent.parentComponent; + list.selectedItem = null; + list.selectedItem = cast parentComponent.parentComponent; + } _currentSelection = value; if (_currentSelection != null) { _currentSelection.addClass(":selected"); @@ -95,7 +103,7 @@ class ListView extends ScrollView implements IDataComponent { } return value; } - + public function resetSelection() { if (_currentSelection != null) { _currentSelection.removeClass(":selected", true, true); @@ -159,18 +167,18 @@ class ListView extends ScrollView implements IDataComponent { } private var _itemRenderer:ItemRenderer; - public var itemRendererClass(get, set):Class; - private function get_itemRendererClass():Class { - return Type.getClass(_itemRenderer); - } - private function set_itemRendererClass(value:Class):Class { - _itemRenderer = Type.createInstance(value, []); - if (_ready) { - syncUI(); - } - return value; - } - + public var itemRendererClass(get, set):Class; + private function get_itemRendererClass():Class { + return Type.getClass(_itemRenderer); + } + private function set_itemRendererClass(value:Class):Class { + _itemRenderer = Type.createInstance(value, []); + if (_ready) { + syncUI(); + } + return value; + } + private var _dataSource:DataSource; public var dataSource(get, set):DataSource; private function get_dataSource():DataSource { @@ -183,9 +191,9 @@ class ListView extends ScrollView implements IDataComponent { private function set_dataSource(value:DataSource):DataSource { _dataSource = value; _dataSource.transformer = new NativeTypeTransformer(); - if (_ready) { - syncUI(); - } + if (_ready) { + syncUI(); + } _dataSource.onChange = onDataSourceChanged; return value; } @@ -255,4 +263,4 @@ class ListView extends ScrollView implements IDataComponent { } } -typedef ItemRendererFunction = Dynamic->ClassFactory; \ No newline at end of file +typedef ItemRendererFunction = Dynamic->ClassFactory; diff --git a/haxe/ui/core/TreeItemRenderer.hx b/haxe/ui/core/TreeItemRenderer.hx new file mode 100644 index 000000000..6c2655a87 --- /dev/null +++ b/haxe/ui/core/TreeItemRenderer.hx @@ -0,0 +1,41 @@ +package haxe.ui.core; + +import haxe.ui.components.Label; +import haxe.ui.containers.ListView; +import haxe.ui.containers.VBox; +import haxe.ui.data.DataSource; + +class TreeItemRenderer extends ItemRenderer { + public var button:Label = new Label(); + public var list:ListView = new ListView(); + public function new() { + super(); + addClass("itemrenderer"); // TODO: shouldnt have to do this + this.percentWidth = 100; + + var vbox:VBox = new VBox(); + vbox.percentWidth = 100; + button.percentWidth = 100; + button.onClick = function (e:UIEvent) { + if (list.hidden) { list.show(); } + else { list.hide(); } + } + + list.id = "value"; + list.percentWidth = 90; + vbox.addComponent(button); + vbox.addComponent(list); + + addComponent(vbox); + } + + private override function get_data():Dynamic { + return _data; + } + private override function set_data(value:Dynamic):Dynamic { + _data = value; + button.text = value.label; + list.dataSource = value.data; + return value; + } +} From 4c2b2ae8762529301a3e4fd5a4e7c0f2556b9a2e Mon Sep 17 00:00:00 2001 From: elnabo Date: Wed, 7 Jun 2017 22:23:16 +0200 Subject: [PATCH 2/2] code climate --- haxe/ui/core/TreeItemRenderer.hx | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/haxe/ui/core/TreeItemRenderer.hx b/haxe/ui/core/TreeItemRenderer.hx index 6c2655a87..98a64b535 100644 --- a/haxe/ui/core/TreeItemRenderer.hx +++ b/haxe/ui/core/TreeItemRenderer.hx @@ -17,8 +17,12 @@ class TreeItemRenderer extends ItemRenderer { vbox.percentWidth = 100; button.percentWidth = 100; button.onClick = function (e:UIEvent) { - if (list.hidden) { list.show(); } - else { list.hide(); } + if (list.hidden) { + list.show(); + } + else { + list.hide(); + } } list.id = "value";