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

Listview with multiple selections #53

Open
wants to merge 1 commit 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
78 changes: 78 additions & 0 deletions haxe/ui/backend/hxwidgets/behaviours/DataComponentSelectedItems.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package haxe.ui.backend.hxwidgets.behaviours;

import haxe.ui.data.DataSource;
import haxe.ui.behaviours.DataBehaviour;
import haxe.ui.components.DropDown;
import haxe.ui.containers.ListView;
import haxe.ui.core.IDataComponent;
import haxe.ui.util.Variant;

class DataComponentSelectedItems extends DataBehaviour {

public override function get():Variant {
if ((_component is IDataComponent) == false) {
return false;
}

var dataComponent:IDataComponent = cast(_component, IDataComponent);
var ds = dataComponent.dataSource;
var selectedItems:Array<Dynamic> = [];

if ((_component is ListView)) {
var listview = cast(_component, ListView);
for (i in listview.selectedIndices) {
selectedItems.push(ds.get(i));
}
}

return selectedItems;
}

public override function set(value:Variant):Void {
_value = value;
var dataComponent:IDataComponent = cast(_component, IDataComponent);
var selectedIndices = [];

var ds = dataComponent.dataSource;
var values:Array<Dynamic> = value;
for (item in values) {
var selectedIndex = findIndexOfItem(value, ds);
selectedIndices.push(selectedIndex);
}

if ((_component is ListView)) {
var listview = cast(_component, ListView).selectedIndices = selectedIndices;
}
}

private function findIndexOfItem(value:Dynamic, ds:DataSource<Dynamic>) {
var n = -1;

var text = valueToString(value);
if (text == null) {
return -1;
}

for (i in 0...ds.size) {
if (text == valueToString(ds.get(i))) {
n = i;
break;
}
}

return n;
}

private function valueToString(value:Dynamic):String {
var text = null;
if (Type.typeof(value) == TObject) {
text = value.text;
if (text == null) {
text = value.value;
}
} else {
text = Std.string(value);
}
return text;
}
}
35 changes: 35 additions & 0 deletions haxe/ui/backend/hxwidgets/behaviours/ListViewSelectedIndices.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package haxe.ui.backend.hxwidgets.behaviours;

import haxe.ui.behaviours.DataBehaviour;
import haxe.ui.util.Variant;
import hx.widgets.ListView;

class ListViewSelectedIndices extends DataBehaviour {

private override function validateData() {
if (_component.window == null) {
return;
}

if (!_value.isArray) {
return;
}

var view:ListView = cast(_component.window, ListView);
view.selectedIndexes = _value;

for (i in (_value:Array<Int>)) {
view.ensureVisible(i);
}
}

public override function get():Variant {
if (_component.window == null) {
return -1;
}

var view:ListView = cast(_component.window, ListView);
return view.selectedIndexes;

}
}
21 changes: 21 additions & 0 deletions haxe/ui/backend/hxwidgets/creators/ListViewCreator.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package haxe.ui.backend.hxwidgets.creators;
import haxe.ui.containers.ListView;
import haxe.ui.constants.SelectionMode;
import hx.widgets.Defs;
import hx.widgets.styles.ListCtrlStyle;

class ListViewCreator extends Creator {
private var _listView:ListView;

public function new(listView:ListView) {
super(listView);
_listView = listView;
}

public override function createConstructorParams(params:Array<Dynamic>):Array<Dynamic> {
var style = 0;
if (_listView.selectionMode == SelectionMode.ONE_ITEM) style |= ListCtrlStyle.SINGLE_SEL;
params.push(style);
return params;
}
}
2 changes: 1 addition & 1 deletion haxe/ui/backend/hxwidgets/custom/SimpleListView.hx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import hx.widgets.styles.ListCtrlStyle;

class SimpleListView extends ListView {
public function new(parent:Window, style:Int = 0, id:Int = -1) {
super(parent, ListCtrlStyle.REPORT | ListCtrlStyle.NO_HEADER | ListCtrlStyle.SINGLE_SEL | style, id);
super(parent, ListCtrlStyle.REPORT | ListCtrlStyle.NO_HEADER | style, id);
appendColumn("");
bind(EventType.SIZE, onResized);
}
Expand Down
4 changes: 3 additions & 1 deletion haxe/ui/backend/native.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?xml version="1.0" encoding="utf-8" ?>
<native>
<component id="haxe.ui.containers.ListView" class="haxe.ui.backend.hxwidgets.custom.SimpleListView" allowChildren="false">
<component id="haxe.ui.containers.ListView" class="haxe.ui.backend.hxwidgets.custom.SimpleListView" creator="haxe.ui.backend.hxwidgets.creators.ListViewCreator" allowChildren="false">
<behaviour id="dataSource" class="haxe.ui.backend.hxwidgets.behaviours.ListViewDataSource" />
<behaviour id="selectedIndex" class="haxe.ui.backend.hxwidgets.behaviours.ListViewSelectedIndex" />
<behaviour id="selectedIndices" class="haxe.ui.backend.hxwidgets.behaviours.ListViewSelectedIndices" />
<behaviour id="contentLayoutName" class="haxe.ui.behaviours.DefaultBehaviour" />
<behaviour id="selectedItem" class="haxe.ui.backend.hxwidgets.behaviours.DataComponentSelectedItem" />
<behaviour id="selectedItems" class="haxe.ui.backend.hxwidgets.behaviours.DataComponentSelectedItems" />
<behaviour id="tooltip" class="haxe.ui.backend.hxwidgets.behaviours.ToolTipBehaviour" />
<size class="haxe.ui.backend.hxwidgets.size.WindowSize" includePadding="false" />
<event id="change" native="EventType.LIST_ITEM_SELECTED" />
Expand Down