Skip to content

Commit

Permalink
Python: Add missing write-back support for Models
Browse files Browse the repository at this point in the history
Ooops :-)
  • Loading branch information
tronical committed Mar 7, 2024
1 parent cc83784 commit c1e8d7c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
17 changes: 17 additions & 0 deletions api/python/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,23 @@ impl i_slint_core::model::Model for PyModelShared {
})
}

fn set_row_data(&self, row: usize, data: Self::Data) {
Python::with_gil(|py| {
let obj = self.self_ref.borrow();
let Some(obj) = obj.as_ref() else {
eprintln!("Python: Model implementation is lacking self object (in set_row_data)");
return;
};

if let Err(err) = obj.call_method1(py, "set_row_data", (row, PyValue::from(data))) {
eprintln!(
"Python: Model implementation of set_row_data() threw an exception: {}",
err
);
};
});
}

fn model_tracker(&self) -> &dyn i_slint_core::model::ModelTracker {
&self.notify
}
Expand Down
32 changes: 32 additions & 0 deletions api/python/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,35 @@ def test_rust_model_sequence():
assert len(model) == 5
assert list(model) == [1, 2, 3, 4, 5]
assert model[2] == 3


def test_model_writeback():
compiler = native.ComponentCompiler()

compdef = compiler.build_from_source("""
export component App {
width: 300px;
height: 300px;
in-out property<[int]> model;
callback write-to-model(int, int);
write-to-model(index, value) => {
self.model[index] = value
}
}
""", "")
assert compdef != None

instance = compdef.create()
assert instance != None

model = models.ListModel([100, 0])

instance.set_property(
"model", model)

instance.invoke("write-to-model", 1, 42)
assert list(instance.get_property("model")) == [100, 42]
instance.invoke("write-to-model", 0, 25)
assert list(instance.get_property("model")) == [25, 42]

0 comments on commit c1e8d7c

Please sign in to comment.