vm/{avatar,tree,servers,streams,home,chat,social,studio} and the UI
settings (settings.rs: keys, UiSettings, LastVoice, ClientPlaybackMap,
the audio form) move into voelin-ui-vm with their tests (102 there now;
the 86 view-model tests of voelin-ui all survive, the old list::sync test
became the list::diff tests). They produce the plain bridge structs, with
Rgb for colours and ImageRef for pictures:
- a file picture is ImageSource::File { path, stamp }: the engine writes
a picture that arrives again into the same file, so the length and
write time are part of the reference and a changed file redraws its
row; size() reads the header once per stamp (no toolkit decoder);
- pictures held in memory (chat previews) are ImageSource::Memory;
- the line cache of the chat no longer keeps Slint models alive for
pointer equality: plain lines compare by value, so an unchanged
message makes the same line with or without the cache.
voelin-ui keeps every model as bind::rows::Rows<T>: the plain rows last
shown plus the VecModel, synced with list::diff, converting only the rows
that changed (bind/convert has the From impls). The controllers hand the
view-model rows to Rows::sync and convert forms at the bridge
(`.into()`); crate::vm is now `voelin_ui_vm as vm`, so paths stay.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
139 lines
3.6 KiB
Rust
139 lines
3.6 KiB
Rust
//! 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<str> {
|
|
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<T: Row> {
|
|
model: Rc<VecModel<T::Shown>>,
|
|
rows: RefCell<Vec<T>>,
|
|
}
|
|
|
|
impl<T: Row> Default for Rows<T> {
|
|
fn default() -> Self {
|
|
Self { model: Rc::default(), rows: RefCell::default() }
|
|
}
|
|
}
|
|
|
|
impl<T: Row> Rows<T> {
|
|
/// The model, for a window property.
|
|
pub fn model(&self) -> ModelRc<T::Shown> {
|
|
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<T> {
|
|
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<Plain> = Rows::default();
|
|
rows.sync(&old.iter().map(|&n| Plain(n)).collect::<Vec<_>>());
|
|
let changes = rows.sync(&new.iter().map(|&n| Plain(n)).collect::<Vec<_>>());
|
|
let shown: Vec<i32> = rows.model.iter().map(|s| s.0).collect();
|
|
assert_eq!(shown, new.iter().map(|n| n * 10).collect::<Vec<_>>(), "{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<Arc<str>> = Rows::default();
|
|
rows.sync(&["720p".into(), "1080p".into()]);
|
|
assert_eq!(rows.model().row_data(1).as_deref(), Some("1080p"));
|
|
}
|
|
}
|