From f378a6825cf1abc7709e10f9d124ec7925c3d745 Mon Sep 17 00:00:00 2001 From: Florian Blasius Date: Tue, 10 Oct 2023 07:47:05 +0200 Subject: [PATCH] ComboBox reset current value if current-index < 0 --- .../widgets/cupertino-base/combobox.slint | 7 +++- .../widgets/fluent-base/combobox.slint | 7 +++- .../widgets/material-base/combobox.slint | 39 +++++++++++++------ .../compiler/widgets/qt/std-widgets.slint | 35 +++++++++++------ 4 files changed, 60 insertions(+), 28 deletions(-) diff --git a/internal/compiler/widgets/cupertino-base/combobox.slint b/internal/compiler/widgets/cupertino-base/combobox.slint index fd9735a242..9d20ec4f31 100644 --- a/internal/compiler/widgets/cupertino-base/combobox.slint +++ b/internal/compiler/widgets/cupertino-base/combobox.slint @@ -13,7 +13,7 @@ export component ComboBox { in property enabled <=> i-focus-scope.enabled; out property has-focus <=> i-focus-scope.has-focus; in-out property current-index: 0; - in-out property current-value: root.model[root.current-index]; + in-out property 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)); } diff --git a/internal/compiler/widgets/fluent-base/combobox.slint b/internal/compiler/widgets/fluent-base/combobox.slint index ab0fea8ba9..81f699b155 100644 --- a/internal/compiler/widgets/fluent-base/combobox.slint +++ b/internal/compiler/widgets/fluent-base/combobox.slint @@ -11,7 +11,7 @@ export component ComboBox { in property enabled <=> i-focus-scope.enabled; out property has-focus <=> i-focus-scope.has-focus; in-out property current-index: 0; - in-out property current-value: root.model[root.current-index]; + in-out property 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)); } diff --git a/internal/compiler/widgets/material-base/combobox.slint b/internal/compiler/widgets/material-base/combobox.slint index f1b040a414..24ebc52f01 100644 --- a/internal/compiler/widgets/material-base/combobox.slint +++ b/internal/compiler/widgets/material-base/combobox.slint @@ -12,7 +12,8 @@ export component ComboBox { in property <[string]> model; out property has-focus <=> i-focus-scope.has-focus; in-out property current-index : 0; - in-out property current-value: root.model[root.current-index]; + in-out property 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; diff --git a/internal/compiler/widgets/qt/std-widgets.slint b/internal/compiler/widgets/qt/std-widgets.slint index ccc1d4275d..1c4f3679cc 100644 --- a/internal/compiler/widgets/qt/std-widgets.slint +++ b/internal/compiler/widgets/qt/std-widgets.slint @@ -234,7 +234,7 @@ export component StandardListView inherits StandardListViewBase { export component ComboBox inherits NativeComboBox { in property <[string]> model; in-out property 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 { }