Skip to content

Commit

Permalink
ComboBox reset current on model changed (#5217)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Simon Hausmann <simon.hausmann@slint.dev>
  • Loading branch information
FloVanGH and tronical committed May 15, 2024
1 parent b4fc512 commit 310b01a
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 9 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/compiler/object_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions internal/compiler/passes/remove_aliases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions internal/compiler/passes/remove_unused_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
},
);
Expand Down
34 changes: 27 additions & 7 deletions internal/compiler/widgets/common/combobox-base.slint
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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 <length> scroll-delta: 2px;

forward-focus: i-focus-scope;
Expand All @@ -40,7 +63,6 @@ export component ComboBoxBase {
if (!self.enabled) {
return reject;
}

if (event.text == Key.UpArrow) {
root.move-selection-up();
return accept;
Expand All @@ -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
}
}
}
}
}
14 changes: 14 additions & 0 deletions tests/cases/widgets/combobox.slint
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,19 @@ 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;
}

/*
```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();
Expand Down Expand Up @@ -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"));
```
Expand Down

0 comments on commit 310b01a

Please sign in to comment.