slint
slint copied to clipboard
New data-changing method on `Model`
A new provided method should be added to the Model trait for convenience. In harmony with Slint v1.7.2's naming scheme in Model, its name would be change_row_data(). Users would pass the row index and a closure that receives the row's data, so they can adapt it and return a changed version of it, which the Model would then adopt as the row's new data, indirect triggering a call to ModelNotify::row_changed() through Model::set_row_data() behind the scenes.
EDIT: It would be desirable if change_row_data() returned an Option. This way, you could decide, based on the data, whether an update is necessary, and not unnecessarily many row changes would be communicated to Slint. However, without cloning, this would normally mean making the closure's parameter a reference to the data, and that would be in conflict with the usage where the user wants to edit the old data instance as an owned value and pass it back by returning it. So, to allow both use cases, the closure's parameter would need to be a &mut reference to the data and its return value a boolean, which communicates whether the row was in fact changed. However, this could lead to the data and the changed-states to drift apart, if the user mutated the data, but says they didn't mutate it. Theoretically, a small helper type MutTracker could be implemented (like Ref and RefMut that come out of RefCell), that implements DerefMut and sets a possibly_mutated flag in deref_mut(). If the closure passed to change_row_data() returns and the flag is set, Slint is notified about the row change. Then the closure wouldn't need a return type.
EDIT: Since Model's row_data() is very similar to slices' get(), perhaps a get_mut() analog would be in order rather than change_row_data() (although I'm not sure whether change_row_data() shouldn't be added additionally for convenience). This get_mut() analog would return a MutTracker. Slint v1.7.2's naming scheme in Model would suggest the name row_data_mut().
Neat idea ❤️
Another provided method that should be added is is_empty() like in many types in std.