Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for Tree in Listview #142

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 32 additions & 24 deletions haxe/ui/containers/ListView.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)) {
Expand All @@ -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);
}

Expand All @@ -78,24 +78,32 @@ class ListView extends ScrollView implements IDataComponent {
selectedItem = item;
return value;
}

public var selectedItem(get, set):ItemRenderer;
private function get_selectedItem():ItemRenderer {
return _currentSelection;
}
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");
dispatch(new UIEvent(UIEvent.CHANGE));
}
return value;
}

public function resetSelection() {
if (_currentSelection != null) {
_currentSelection.removeClass(":selected", true, true);
Expand Down Expand Up @@ -159,18 +167,18 @@ class ListView extends ScrollView implements IDataComponent {
}

private var _itemRenderer:ItemRenderer;
public var itemRendererClass(get, set):Class<ItemRenderer>;
private function get_itemRendererClass():Class<ItemRenderer> {
return Type.getClass(_itemRenderer);
}
private function set_itemRendererClass(value:Class<ItemRenderer>):Class<ItemRenderer> {
_itemRenderer = Type.createInstance(value, []);
if (_ready) {
syncUI();
}
return value;
}
public var itemRendererClass(get, set):Class<ItemRenderer>;
private function get_itemRendererClass():Class<ItemRenderer> {
return Type.getClass(_itemRenderer);
}
private function set_itemRendererClass(value:Class<ItemRenderer>):Class<ItemRenderer> {
_itemRenderer = Type.createInstance(value, []);
if (_ready) {
syncUI();
}
return value;
}

private var _dataSource:DataSource<Dynamic>;
public var dataSource(get, set):DataSource<Dynamic>;
private function get_dataSource():DataSource<Dynamic> {
Expand All @@ -183,9 +191,9 @@ class ListView extends ScrollView implements IDataComponent {
private function set_dataSource(value:DataSource<Dynamic>):DataSource<Dynamic> {
_dataSource = value;
_dataSource.transformer = new NativeTypeTransformer();
if (_ready) {
syncUI();
}
if (_ready) {
syncUI();
}
_dataSource.onChange = onDataSourceChanged;
return value;
}
Expand Down Expand Up @@ -255,4 +263,4 @@ class ListView extends ScrollView implements IDataComponent {
}
}

typedef ItemRendererFunction = Dynamic->ClassFactory<ItemRenderer>;
typedef ItemRendererFunction = Dynamic->ClassFactory<ItemRenderer>;
45 changes: 45 additions & 0 deletions haxe/ui/core/TreeItemRenderer.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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;
}
}