slint/internal/interpreter/value_model.rs
Olivier Goffart d55803d290 Models: fix writing data doesn't update ListView model data property
This fixes a couple of bug:
 - Bug #3740 happens because `Repeater::model_set_row_data` did not use
   the inner.offset to get the instance (So that's the `val == 106` part
   of the test)
 - But I went ahead and also tested what happenned if you changed the
   model from the "outside" using the `model[i] = `, and that was not
   implemented, hence the move of the code from
   `Repeater::model_set_row_data` to `RepeaterTracker::row_changed`,
   That does need Pin though, so Pin was added everywhere
 - C++ is not affected by bug #3740, because because the C++ listview
   don't do the "allocate only visible" optimization. But the
   "val == 1106" part of the test would fail so this patch also moces
   the update from  `model_set_row_data` to `row_changed`.
   But following that we don't set the state as Dirty in `row_changed`,
   the write_to_model test started filling  because the `row_added`
   function was missing an update of the index on every further items

The change in the interpreter prevent a borrow_mut from causing trouble.
The Value really don't need to be in a RefCell anyway

Fix #3740
2023-10-31 13:41:31 +01:00

83 lines
2.2 KiB
Rust

// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.1 OR LicenseRef-Slint-commercial
use crate::api::Value;
use i_slint_core::model::{Model, ModelTracker};
pub struct ValueModel {
value: Value,
}
impl ValueModel {
pub fn new(value: Value) -> Self {
Self { value }
}
}
impl ModelTracker for ValueModel {
fn attach_peer(&self, peer: i_slint_core::model::ModelPeer) {
if let Value::Model(ref model_ptr) = self.value {
model_ptr.model_tracker().attach_peer(peer)
}
}
fn track_row_count_changes(&self) {
if let Value::Model(ref model_ptr) = self.value {
model_ptr.model_tracker().track_row_count_changes()
}
}
fn track_row_data_changes(&self, row: usize) {
if let Value::Model(ref model_ptr) = self.value {
model_ptr.model_tracker().track_row_data_changes(row)
}
}
}
impl Model for ValueModel {
type Data = Value;
fn row_count(&self) -> usize {
match &self.value {
Value::Bool(b) => {
if *b {
1
} else {
0
}
}
Value::Number(x) => x.max(Default::default()) as usize,
Value::Void => 0,
Value::Model(model_ptr) => model_ptr.row_count(),
x => panic!("Invalid model {:?}", x),
}
}
fn row_data(&self, row: usize) -> Option<Self::Data> {
if row >= self.row_count() {
None
} else {
Some(match &self.value {
Value::Bool(_) => Value::Void,
Value::Number(_) => Value::Number(row as _),
Value::Model(model_ptr) => model_ptr.row_data(row)?,
x => panic!("Invalid model {:?}", x),
})
}
}
fn model_tracker(&self) -> &dyn ModelTracker {
self
}
fn set_row_data(&self, row: usize, data: Self::Data) {
match &self.value {
Value::Model(model_ptr) => model_ptr.set_row_data(row, data),
_ => eprintln!("Trying to change the value of a read-only integer model."),
}
}
fn as_any(&self) -> &dyn core::any::Any {
self
}
}