Skip to content

Commit

Permalink
Merge pull request #556 from Shallowmallow/TableScrolling
Browse files Browse the repository at this point in the history
Table scrolling
  • Loading branch information
ianharrigan authored Dec 23, 2023
2 parents fc6b5a7 + 1a06bd6 commit e765a3e
Showing 1 changed file with 81 additions and 10 deletions.
91 changes: 81 additions & 10 deletions haxe/ui/containers/TableView.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package haxe.ui.containers;

import haxe.ui.actions.ActionType;
import haxe.ui.behaviours.Behaviour;
import haxe.ui.behaviours.DataBehaviour;
import haxe.ui.behaviours.DefaultBehaviour;
Expand All @@ -17,6 +18,7 @@ import haxe.ui.core.ItemRenderer;
import haxe.ui.data.ArrayDataSource;
import haxe.ui.data.DataSource;
import haxe.ui.data.transformation.NativeTypeTransformer;
import haxe.ui.events.ActionEvent;
import haxe.ui.events.ItemEvent;
import haxe.ui.events.MouseEvent;
import haxe.ui.events.ScrollEvent;
Expand Down Expand Up @@ -200,6 +202,7 @@ private class Events extends ScrollViewEvents {
}

private function onRendererMouseDown(e:MouseEvent) {
_tableview.focus = true;
switch (_tableview.selectionMode) {
case SelectionMode.MULTIPLE_LONG_PRESS:
if (_tableview.selectedIndices.length == 0) {
Expand Down Expand Up @@ -342,6 +345,42 @@ private class Events extends ScrollViewEvents {
private function selectRange(fromIndex:Int, toIndex:Int) {
_tableview.selectedIndices = [for (i in fromIndex...toIndex + 1) i];
}

private override function onActionStart(event:ActionEvent) {
switch (event.action) {
case ActionType.DOWN:
if (_tableview.selectedIndex < 0) {
_tableview.selectedIndex = 0;
} else {
var n:Int = _tableview.selectedIndex;
n++;
if (n > _tableview.dataSource.size - 1) {
n = 0;
}
_tableview.selectedIndex = n;
}
event.repeater = true;
case ActionType.UP:
if (_tableview.selectedIndex < 0) {
_tableview.selectedIndex = _tableview.dataSource.size - 1;
} else {
var n:Int = _tableview.selectedIndex;
n--;
if (n < 0) {
n = _tableview.selectedIndex = _tableview.dataSource.size - 1;
}
_tableview.selectedIndex = n;
}
event.repeater = true;
case ActionType.LEFT:
_scrollview.hscrollPos -= 10;
event.repeater = true;
case ActionType.RIGHT:
_scrollview.hscrollPos += 10;
event.repeater = true;
case _:
}
}
}

//***********************************************************************************************************
Expand Down Expand Up @@ -527,6 +566,8 @@ private class Builder extends ScrollViewBuilder {
i++;
}



/* NOT SURE WHAT THIS IS, OR WHY ITS HERE, IT SEEMS LIKE TEST CODE THAT
* HAS BEEN LEFT IN, COMMENTING FOR NOW, BUT LOOK TO REMOVE LATER IF
* NO USE HAS BEEN FOUND
Expand Down Expand Up @@ -587,6 +628,41 @@ private class Builder extends ScrollViewBuilder {
return true;
});
}

private function ensureVisible(itemToEnsure:ItemRenderer) {
if (itemToEnsure != null && _tableview.virtual == false) {
var vscroll:VerticalScroll = _tableview.findComponent(VerticalScroll);
if (vscroll != null) {
var vpos:Float = vscroll.pos;
var contents:Component = _tableview.findComponent("tableview-contents", "css");
if (itemToEnsure.top + itemToEnsure.height > vpos + contents.componentClipRect.height) {
vscroll.pos = ((itemToEnsure.top + itemToEnsure.height) - contents.componentClipRect.height);
} else if (itemToEnsure.top < vpos) {
vscroll.pos = itemToEnsure.top;
}
}
}
}


@:access(haxe.ui.layouts.VerticalVirtualLayout)
private function ensureVirtualItemVisible(index:Int) {
var vscroll:VerticalScroll = _tableview.findComponent(VerticalScroll);
if (vscroll != null) {
var layout = cast(_tableview.layout, VerticalVirtualLayout);
var itemHeight = layout.itemHeight;
var itemTop = index * itemHeight;
var vpos:Float = vscroll.pos;
var contents:Component = _tableview.findComponent("tableview-contents", "css");
if (itemTop + itemHeight > vpos + contents.componentClipRect.height) {
vscroll.pos = ((itemTop + itemHeight) - contents.componentClipRect.height);
} else if (itemTop < vpos) {
vscroll.pos = itemTop;
}
}
}


}

//***********************************************************************************************************
Expand Down Expand Up @@ -767,17 +843,12 @@ private class SelectedIndicesBehaviour extends DataBehaviour {
}
}

if (itemToEnsure != null && tableView.virtual == false) { // TODO: virtual scroll into view
var vscroll:VerticalScroll = tableView.findComponent(VerticalScroll);
if (vscroll != null) {
var vpos:Float = vscroll.pos;
var contents:Component = tableView.findComponent("tableview-contents", "css");
if (itemToEnsure.top + itemToEnsure.height > vpos + contents.componentClipRect.height) {
vscroll.pos = ((itemToEnsure.top + itemToEnsure.height) - contents.componentClipRect.height);
} else if (itemToEnsure.top < vpos) {
vscroll.pos = itemToEnsure.top;
}
if (tableView.virtual) {
for (i in selectedIndices) {
@:privateAccess builder.ensureVirtualItemVisible(i);
}
} else {
@:privateAccess builder.ensureVisible(itemToEnsure);
}

if (tableView.selectedIndex != -1 && tableView.selectedIndices.length != 0) {
Expand Down

0 comments on commit e765a3e

Please sign in to comment.