ComboBox reset current value if current-index < 0

This commit is contained in:
Florian Blasius 2023-10-10 07:47:05 +02:00
parent b88a3caacd
commit f378a6825c
4 changed files with 60 additions and 28 deletions

View file

@ -13,7 +13,7 @@ export component ComboBox {
in property <bool> enabled <=> i-focus-scope.enabled;
out property <bool> has-focus <=> i-focus-scope.has-focus;
in-out property <int> current-index: 0;
in-out property <string> current-value: root.model[root.current-index];
in-out property <string> current-value: root.get-current-value();
min-width: max(160px, i-layout.min-width);
min-height: max(22px, i-layout.min-height);
@ -168,10 +168,13 @@ export component ComboBox {
function select(index: int) {
root.current-index = index;
root.current-value = root.model[root.current-index];
root.selected(root.current-value);
}
function get-current-value() -> string {
root.current-index >= 0 && root.current-index <= root.model.length ? root.model[root.current-index] : ""
}
function move-selection-up() {
root.select(Math.max(root.current-index - 1, 0));
}

View file

@ -11,7 +11,7 @@ export component ComboBox {
in property <bool> enabled <=> i-focus-scope.enabled;
out property <bool> has-focus <=> i-focus-scope.has-focus;
in-out property <int> current-index: 0;
in-out property <string> current-value: root.model[root.current-index];
in-out property <string> current-value: root.get-current-value();
min-width: max(160px, i-layout.min-height);
min-height: max(32px, i-layout.min-height);
@ -105,10 +105,13 @@ export component ComboBox {
function select(index: int) {
root.current-index = index;
root.current-value = root.model[root.current-index];
root.selected(root.current-value);
}
function get-current-value() -> string {
root.current-index >= 0 && root.current-index <= root.model.length ? root.model[root.current-index] : ""
}
function move-selection-up() {
root.select(Math.max(root.current-index - 1, 0));
}

View file

@ -12,7 +12,8 @@ export component ComboBox {
in property <[string]> model;
out property <bool> has-focus <=> i-focus-scope.has-focus;
in-out property <int> current-index : 0;
in-out property <string> current-value: root.model[root.current-index];
in-out property <string> current-value: root.current-index >= 0 && root.current-index <= root.model.length ?
root.model[root.current-index] : "";
horizontal-stretch: 1;
vertical-stretch: 0;
@ -25,13 +26,14 @@ export component ComboBox {
i-focus-scope := FocusScope {
key-pressed(event) => {
if (event.text == Key.UpArrow) {
root.current-index = Math.max(root.current-index - 1, 0);
root.current-value = root.model[root.current-index];
root.move-selection-up();
return accept;
} else if (event.text == Key.DownArrow) {
root.current-index = Math.min(root.current-index + 1, root.model.length - 1);
root.current-value = root.model[root.current-index];
root.move-selection-down();
return accept;
} else if (event.text == Key.Return) {
i-popup.show();
}
return reject;
}
@ -92,21 +94,34 @@ export component ComboBox {
}
VerticalLayout {
for value[idx] in root.model: ListItem {
for value[index] in root.model: ListItem {
text: value;
selected: idx == root.current-index;
selected: index == root.current-index;
clicked => {
if (root.enabled) {
root.current-index = idx;
root.current-value = value;
root.selected(root.current-value);
}
root.select(index);
}
}
}
}
function select(index: int) {
root.current-index = index;
root.selected(root.current-value);
}
function move-selection-up() {
root.select(Math.max(root.current-index - 1, 0));
}
function move-selection-down() {
root.select(Math.min(root.current-index + 1, root.model.length - 1));
}
function get-current-value() -> string {
root.current-index >= 0 && root.current-index <= root.model.length ? root.model[root.current-index] : ""
}
states [
disabled when !root.enabled : {
i-background.border-color: Palette.on-surface;

View file

@ -234,7 +234,7 @@ export component StandardListView inherits StandardListViewBase {
export component ComboBox inherits NativeComboBox {
in property <[string]> model;
in-out property <int> current-index : 0;
current-value: root.model[root.current-index];
current-value: root.get-current-value();
out property has-focus <=> fs.has-focus;
enabled: true;
callback selected(string);
@ -253,18 +253,14 @@ export component ComboBox inherits NativeComboBox {
width: root.width;
VerticalLayout {
spacing: 0px;
for value[i] in root.model: NativeStandardListViewItem {
for value[index] in root.model: NativeStandardListViewItem {
item: { text: value };
is-selected: root.current-index == i;
is-selected: root.current-index == index;
has-hover: ta.has-hover;
combobox: true;
ta := TouchArea {
clicked => {
if (root.enabled) {
root.current-index = i;
root.current-value = value;
root.selected(root.current-value);
}
root.select(index);
//is-open = false;
}
}
@ -275,12 +271,10 @@ export component ComboBox inherits NativeComboBox {
fs := FocusScope {
key-pressed(event) => {
if (event.text == Key.UpArrow) {
root.current-index = Math.max(root.current-index - 1, 0);
root.current-value = root.model[root.current-index];
root.move-selection-up();
return accept;
} else if (event.text == Key.DownArrow) {
root.current-index = Math.min(root.current-index + 1, root.model.length - 1);
root.current-value = root.model[root.current-index];
root.move-selection-down();
return accept;
// PopupWindow can not get hidden again at this time, so do not allow to pop that up.
// } else if (event.text == Key.Return) {
@ -298,6 +292,23 @@ export component ComboBox inherits NativeComboBox {
}
}
}
function select(index: int) {
root.current-index = index;
root.selected(root.current-value);
}
function move-selection-up() {
root.select(Math.max(root.current-index - 1, 0));
}
function move-selection-down() {
root.select(Math.min(root.current-index + 1, root.model.length - 1));
}
function get-current-value() -> string {
root.current-index >= 0 && root.current-index <= root.model.length ? root.model[root.current-index] : ""
}
}
export component TabWidgetImpl inherits NativeTabWidget { }