slint-ui / slint

Slint is a declarative GUI toolkit to build native user interfaces for Rust, C++, or JavaScript apps.
https://slint.dev
Other
16.93k stars 565 forks source link

Ability to replace inner `Vec` in `VecModel` #1045

Closed rdrpenguin04 closed 2 years ago

rdrpenguin04 commented 2 years ago

In my application, fairly often, I don't just need to remove an item from a list and add another one; I need to replace the list with an entirely separate list. I could use set_<model name> instead, but then I also have to reset every other function that had the ModelRc cloned... needless to say, this is complicated, and it seems like there should be a better way.

It seems like it wouldn't be too hard to have a function to replace the Vec in a VecModel with an entirely new Vec; is there anything that would be blocking this? If not, I could implement it probably.

Edit: It would also be useful to read the inner Vec for other purposes, like finding an element with a certain ID. Solved with Model::iter()

Originally posted by @rdrpenguin04 in https://github.com/slint-ui/slint/discussions/1043

rdrpenguin04 commented 2 years ago

I'll create a fork of the repo and try to implement this; the main thing I've been told could be problematic is notifying the model that the entire thing has been overwritten. A temporary option could be to implement the replace function in terms of removeing every existing entry and then pushing every new entry.