//! A Slint list model kept in step with plain rows: the view-model code //! produces the new list, [`list::diff`](voelin_ui_vm::list::diff) finds //! the row operations, and only those rows are converted and handed to the //! `VecModel` (views redraw only the rows that changed: a client that //! starts talking is one `set_row_data`). use std::cell::{Ref, RefCell}; use std::rc::Rc; use std::sync::Arc; use slint::{Model, ModelRc, SharedString, VecModel}; use voelin_ui_vm::list::{Change, Changes, diff}; /// A plain row that a Slint model shows as another type. Implemented for /// the bridge structs (`convert/items.rs`). pub(crate) trait Row: Clone + PartialEq { /// The Slint type of the row. type Shown: Clone + 'static; /// The row as the window shows it. fn show(&self) -> Self::Shown; } impl Row for Arc { type Shown = SharedString; fn show(&self) -> SharedString { SharedString::from(&**self) } } /// The rows of a list as the view model made them (`T`), and the model the /// window shows. pub(crate) struct Rows { model: Rc>, rows: RefCell>, } impl Default for Rows { fn default() -> Self { Self { model: Rc::default(), rows: RefCell::default() } } } impl Rows { /// The model, for a window property. pub fn model(&self) -> ModelRc { ModelRc::from(self.model.clone()) } /// The rows now. pub fn rows(&self) -> Ref<'_, [T]> { Ref::map(self.rows.borrow(), Vec::as_slice) } /// Row `i` now. pub fn get(&self, i: usize) -> Option { self.rows.borrow().get(i).cloned() } /// Change row `i` to `row` if it differs. pub fn set(&self, i: usize, row: T) { let mut rows = self.rows.borrow_mut(); if rows.get(i).is_some_and(|old| *old != row) { self.model.set_row_data(i, row.show()); rows[i] = row; } } /// Append a row. pub fn push(&self, row: T) { self.model.push(row.show()); self.rows.borrow_mut().push(row); } /// Make the list `new`, with the fewest row operations. pub fn sync(&self, new: &[T]) -> Changes { let mut rows = self.rows.borrow_mut(); let changes = diff(&rows, new); for change in &changes.0 { match *change { Change::Update(i) => self.model.set_row_data(i, new[i].show()), Change::Remove(i) => { self.model.remove(i); } Change::Insert(i) => self.model.insert(i, new[i].show()), } } changes.apply(&mut rows, new); changes } } #[cfg(test)] mod tests { use super::*; #[derive(Clone, Debug, PartialEq)] struct Plain(i32); #[derive(Clone, Debug, PartialEq)] struct Shown(i32); impl Row for Plain { type Shown = Shown; fn show(&self) -> Shown { Shown(self.0 * 10) } } fn run(old: &[i32], new: &[i32]) -> Changes { let rows: Rows = Rows::default(); rows.sync(&old.iter().map(|&n| Plain(n)).collect::>()); let changes = rows.sync(&new.iter().map(|&n| Plain(n)).collect::>()); let shown: Vec = rows.model.iter().map(|s| s.0).collect(); assert_eq!(shown, new.iter().map(|n| n * 10).collect::>(), "{old:?} -> {new:?}"); assert_eq!(rows.rows.borrow().len(), new.len()); changes } #[test] fn the_model_follows_the_rows() { assert!(run(&[1, 2, 3], &[1, 2, 3]).is_empty()); assert_eq!(run(&[1, 2, 3], &[1, 9, 3]).updated(), 1); assert_eq!(run(&[1, 2, 3], &[1, 2, 7, 3]).inserted(), 1); assert_eq!(run(&[1, 2, 3], &[1, 3]).removed(), 1); run(&[], &[1, 2]); run(&[1, 2], &[]); run(&[1, 2, 1], &[1, 1, 2, 1]); run(&[4, 5, 6], &[1, 2, 3, 4, 5, 6, 7]); } #[test] fn strings_are_rows() { let rows: Rows> = Rows::default(); rows.sync(&["720p".into(), "1080p".into()]); assert_eq!(rows.model().row_data(1).as_deref(), Some("1080p")); } }