From 5df2e0584bb6b0610e2654f3a7d5432be7c6b31c Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Fri, 21 Jul 2017 00:40:43 +0200 Subject: Improved keyboard navigation --- framework/qml/TreeView.qml | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) (limited to 'framework/qml/TreeView.qml') diff --git a/framework/qml/TreeView.qml b/framework/qml/TreeView.qml index bf2abddd..c0be01f3 100644 --- a/framework/qml/TreeView.qml +++ b/framework/qml/TreeView.qml @@ -81,16 +81,22 @@ Flickable { function selectNext() { - var nextIndex = model.sibling(selection.currentIndex.row + 1, 0, selection.currentIndex) - if (nextIndex.valid) { - treeView.selection.setCurrentIndex(nextIndex, ItemSelectionModel.ClearAndSelect) + //If we have already expanded children go to them instead + var childIndex = model.index(0, 0, selection.currentIndex) + if (childIndex.valid && treeView.isExpanded(selection.currentIndex)) { + treeView.selection.setCurrentIndex(childIndex, ItemSelectionModel.ClearAndSelect) } else { - var childIndex = model.index(0, 0, selection.currentIndex) - if (childIndex.valid) { - if (!treeView.isExpanded(selection.currentIndex)) { - treeView.expand(selection.currentIndex) + //Otherwise just advance to the next index, if we can + var nextIndex = model.sibling(selection.currentIndex.row + 1, 0, selection.currentIndex) + if (nextIndex.valid) { + treeView.selection.setCurrentIndex(nextIndex, ItemSelectionModel.ClearAndSelect) + } else { + //Try to go to the next of the parent instead TODO do this recursively + var parentIndex = model.parent(selection.currentIndex) + if (parentIndex.valid) { + var parentNext = model.sibling(parentIndex.row + 1, 0, parentIndex) + treeView.selection.setCurrentIndex(parentNext, ItemSelectionModel.ClearAndSelect) } - treeView.selection.setCurrentIndex(childIndex, ItemSelectionModel.ClearAndSelect) } } } @@ -100,6 +106,7 @@ Flickable { var previousIndex = model.sibling(selection.currentIndex.row - 1, 0, selection.currentIndex) if (previousIndex.valid) { treeView.selection.setCurrentIndex(previousIndex, ItemSelectionModel.ClearAndSelect) + //TODO if the previous index is expanded, go to the last visible child instead (recursively) } else { var parentIndex = model.parent(selection.currentIndex) if (parentIndex.valid) { @@ -125,6 +132,8 @@ Flickable { Keys.onUpPressed: selectPrevious() Keys.onReturnPressed: treeView.activated(selection.currentIndex) + Keys.onRightPressed: treeView.expand(selection.currentIndex) + Keys.onLeftPressed: treeView.collapse(selection.currentIndex) //Forward the signal because on a desktopsystem activated is only triggerd by double clicks onClicked: treeView.activated(index) -- cgit v1.2.3