diff --git a/CHANGELOG.md b/CHANGELOG.md index 7359bf963b8..d722bf5af6a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,9 @@ All notable changes to this project are documented in this file. ### Widgets - Added `placeholder-text` property to `TextEdit`. - + - Fixed updating model of ComboBox does not change current-value + - Fixed set current-index of ComboBox to -1 does not reset current-value + ## [1.6.0] - 2024-05-13 ## General diff --git a/internal/compiler/object_tree.rs b/internal/compiler/object_tree.rs index 4b1b20dea55..417ee716061 100644 --- a/internal/compiler/object_tree.rs +++ b/internal/compiler/object_tree.rs @@ -1297,7 +1297,7 @@ impl Element { } for ch in node.PropertyChangedCallback() { - if !diag.enable_experimental { + if !diag.enable_experimental && !tr.expose_internal_types { diag.push_error( "Change callbacks are experimental and not yet implemented in this version of Slint".into(), &ch, diff --git a/internal/compiler/passes/remove_aliases.rs b/internal/compiler/passes/remove_aliases.rs index 3bed4a34b79..5e5f2b823a8 100644 --- a/internal/compiler/passes/remove_aliases.rs +++ b/internal/compiler/passes/remove_aliases.rs @@ -160,6 +160,14 @@ pub fn remove_aliases(doc: &Document, diag: &mut BuildDiagnostics) { } }; + // Adjust the change callbacks + { + let mut elem = elem.borrow_mut(); + if let Some(old_change_callback) = elem.change_callbacks.remove(remove.name()) { + elem.change_callbacks.insert(to.name().to_owned(), old_change_callback); + } + } + // Remove the declaration { let mut elem = elem.borrow_mut(); diff --git a/internal/compiler/passes/remove_unused_properties.rs b/internal/compiler/passes/remove_unused_properties.rs index eb9bc53554f..30e4ce109a3 100644 --- a/internal/compiler/passes/remove_unused_properties.rs +++ b/internal/compiler/passes/remove_unused_properties.rs @@ -31,6 +31,7 @@ pub fn remove_unused_properties(component: &Component) { elem.property_declarations.remove(x); elem.property_analysis.borrow_mut().remove(x); elem.bindings.remove(x); + elem.change_callbacks.remove(x); } }, ); diff --git a/internal/compiler/widgets/common/combobox-base.slint b/internal/compiler/widgets/common/combobox-base.slint index 405d97dc63c..6c87803ba07 100644 --- a/internal/compiler/widgets/common/combobox-base.slint +++ b/internal/compiler/widgets/common/combobox-base.slint @@ -14,12 +14,15 @@ export component ComboBoxBase { callback show-popup(); public function select(index: int) { - if (!root.enabled) { + if !root.enabled { return; } - root.current-index = index; - root.current-value = root.model[root.current-index]; + + if root.current-value != root.model[root.current-index] { + root.update-current-value(); + } + root.selected(root.current-value); } @@ -31,6 +34,26 @@ export component ComboBoxBase { root.select(Math.min(root.current-index + 1, root.model.length - 1)); } + function reset-current() { + root.current-index = 0; + } + + function update-current-value() { + if root.current-index < 0 || root.current-index >= root.model.length { + root.current-value = ""; + return; + } + root.current-value = root.model[root.current-index]; + } + + changed model => { + root.reset-current(); + } + + changed current-index => { + root.update-current-value(); + } + private property scroll-delta: 2px; forward-focus: i-focus-scope; @@ -40,7 +63,6 @@ export component ComboBoxBase { if (!self.enabled) { return reject; } - if (event.text == Key.UpArrow) { root.move-selection-up(); return accept; @@ -66,14 +88,12 @@ export component ComboBoxBase { root.move-selection-down(); return accept; } - if (event.delta-y > root.scroll-delta) { root.move-selection-up(); return accept; } - reject } } } -} \ No newline at end of file +} diff --git a/tests/cases/widgets/combobox.slint b/tests/cases/widgets/combobox.slint index 30974acd575..ade0f68dbe3 100644 --- a/tests/cases/widgets/combobox.slint +++ b/tests/cases/widgets/combobox.slint @@ -23,6 +23,7 @@ export component TestCase inherits Window { in-out property current-index <=> box.current-index; in-out property current-value <=> box.current-value; + in-out property model <=> box.model; out property has-focus <=> box.has-focus; } @@ -30,8 +31,11 @@ export component TestCase inherits Window { ```rust +use std::rc::Rc; use slint::platform::Key; use slint::SharedString; +use slint::VecModel; +use i_slint_backend_testing::mock_elapsed_time; let instance = TestCase::new().unwrap(); @@ -86,6 +90,16 @@ assert_eq!(instance.get_current_index(), 1); assert_eq!(instance.get_output(), "selected(Bbb,1)\nselected(Ccc,2)\nselected(Bbb,1)\n"); instance.set_output(Default::default()); +// Set current-index to -1 +instance.set_current_index(-1); +mock_elapsed_time(500); +assert_eq!(instance.get_current_value(), &SharedString::from("")); + +// Replace model +instance.set_model(Rc::new(VecModel::from_slice(&[SharedString::from("A"), SharedString::from("B")])).into()); +mock_elapsed_time(500); +assert_eq!(instance.get_current_index(), 0); +assert_eq!(instance.get_current_value(), &SharedString::from("A")); ```