reset StandardListView's current-item if it is out of bounds

This commit is contained in:
Lukas Jung 2022-03-15 16:13:16 +01:00 committed by Lukas Jung
parent b510c2f7a2
commit 9d18882f9d
2 changed files with 10 additions and 8 deletions

View file

@ -377,7 +377,8 @@ export ListView := ScrollView {
export StandardListView := ListView {
property<[StandardListViewItem]> model;
property<int> current-item: -1;
property<int> current-item: min(actual-current-item, model.length - 1);
property<int> actual-current-item: -1;
for item[idx] in model : Rectangle {
l := HorizontalLayout {
padding: 8px;
@ -392,16 +393,16 @@ export StandardListView := ListView {
touch := TouchArea {
width: parent.width;
height: parent.height;
clicked => { current-item = idx; }
clicked => { actual-current-item = idx; }
}
}
FocusScope {
key-pressed(event) => {
if (event.text == Keys.UpArrow && current-item > 0) {
current-item -= 1;
actual-current-item -= 1;
return accept;
} else if (event.text == Keys.DownArrow && current-item + 1 < model.length) {
current-item += 1;
actual-current-item += 1;
return accept;
}
reject

View file

@ -69,23 +69,24 @@ export ListView := ScrollView {
export StandardListView := ListView {
property<[StandardListViewItem]> model;
property<int> current-item: -1;
property<int> current-item: min(actual-current-item, model.length - 1);
property<int> actual-current-item: -1;
for item[i] in model : NativeStandardListViewItem {
item: item;
index: i;
is-selected: current-item == i;
TouchArea {
clicked => { current-item = i; }
clicked => { actual-current-item = i; }
has-hover <=> parent.has-hover;
}
}
FocusScope {
key-pressed(event) => {
if (event.text == Keys.UpArrow && current-item > 0) {
current-item -= 1;
actual-current-item -= 1;
return accept;
} else if (event.text == Keys.DownArrow && current-item + 1 < model.length) {
current-item += 1;
actual-current-item += 1;
return accept;
}
reject