New crate voelin-ui-vm with no UI toolkit in its tree: ImageRef (Arc<ImageSource>: File, Rgba, Svg, Memory, None, with size()), Rgb for colours, list::diff(old, new) -> Changes (updates, removals and insertions by index) and the bridge structs as plain Clone + PartialEq values (the 51 of bridge.slint without the account's two, and the 10 of studio-bridge.slint). Strings are Arc<str>, lists Vec. voelin-ui keeps the temporary seam: From impls between every bridge struct and its Slint twin (bind/convert), and bind/rows.rs, which applies Changes to a VecModel and converts only the rows that changed. The viewer's quality list is the first model on it. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
139 lines
4.4 KiB
Rust
139 lines
4.4 KiB
Rust
//! Incremental list updates: what changed between two versions of a list,
|
|
//! as row operations. A front end that keeps a list model (Slint's
|
|
//! `VecModel`) applies them row by row, so a client that starts talking
|
|
//! redraws one row of the tree; one that rebuilds from the list (GPUI) just
|
|
//! re-renders and has no use for them.
|
|
|
|
/// One row operation of a [`Changes`].
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub enum Change {
|
|
/// Row `i` holds another value: `new[i]`.
|
|
Update(usize),
|
|
/// Row `i` is gone.
|
|
Remove(usize),
|
|
/// `new[i]` is a new row, at `i`.
|
|
Insert(usize),
|
|
}
|
|
|
|
/// Row operations that turn the old list into the new one, in the order to
|
|
/// apply them: updates, then removals (each at the index it has when its
|
|
/// turn comes), then insertions (in ascending order).
|
|
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
|
pub struct Changes(pub Vec<Change>);
|
|
|
|
impl Changes {
|
|
/// Nothing changed.
|
|
pub fn is_empty(&self) -> bool {
|
|
self.0.is_empty()
|
|
}
|
|
|
|
/// How many rows are updated in place.
|
|
pub fn updated(&self) -> usize {
|
|
self.0.iter().filter(|c| matches!(c, Change::Update(_))).count()
|
|
}
|
|
|
|
/// How many rows are removed.
|
|
pub fn removed(&self) -> usize {
|
|
self.0.iter().filter(|c| matches!(c, Change::Remove(_))).count()
|
|
}
|
|
|
|
/// How many rows are inserted.
|
|
pub fn inserted(&self) -> usize {
|
|
self.0.iter().filter(|c| matches!(c, Change::Insert(_))).count()
|
|
}
|
|
|
|
/// Apply the changes to `rows` (the old list), which then equals `new`.
|
|
/// Also the reference for front ends: their models do the same
|
|
/// operations in the same order.
|
|
pub fn apply<T: Clone>(&self, rows: &mut Vec<T>, new: &[T]) {
|
|
for change in &self.0 {
|
|
match *change {
|
|
Change::Update(i) => rows[i] = new[i].clone(),
|
|
Change::Remove(i) => {
|
|
rows.remove(i);
|
|
}
|
|
Change::Insert(i) => rows.insert(i, new[i].clone()),
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// What changes `old` into `new` with few row operations: the rows both
|
|
/// share at the start and the end stay, rows in between are changed in
|
|
/// place where they differ, and the rest inserted or removed.
|
|
pub fn diff<T: PartialEq>(old: &[T], new: &[T]) -> Changes {
|
|
let min = old.len().min(new.len());
|
|
let prefix = old.iter().zip(new).take_while(|(a, b)| a == b).count();
|
|
let suffix = old[prefix..]
|
|
.iter()
|
|
.rev()
|
|
.zip(new[prefix..].iter().rev())
|
|
.take(min - prefix)
|
|
.take_while(|(a, b)| a == b)
|
|
.count();
|
|
let old_mid = old.len() - prefix - suffix;
|
|
let new_mid = new.len() - prefix - suffix;
|
|
let common = old_mid.min(new_mid);
|
|
let mut changes = Vec::new();
|
|
for i in prefix..prefix + common {
|
|
if old[i] != new[i] {
|
|
changes.push(Change::Update(i));
|
|
}
|
|
}
|
|
let at = prefix + common;
|
|
changes.extend((new_mid..old_mid).map(|_| Change::Remove(at)));
|
|
changes.extend((at..at + new_mid.saturating_sub(old_mid)).map(Change::Insert));
|
|
Changes(changes)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
/// The changes from `old` to `new`; applying them gives `new`.
|
|
fn run(old: &[i32], new: &[i32]) -> Changes {
|
|
let changes = diff(old, new);
|
|
let mut rows = old.to_vec();
|
|
changes.apply(&mut rows, new);
|
|
assert_eq!(rows, new, "{old:?} -> {new:?}");
|
|
changes
|
|
}
|
|
|
|
fn counts(changes: &Changes) -> (usize, usize, usize) {
|
|
(changes.updated(), changes.inserted(), changes.removed())
|
|
}
|
|
|
|
#[test]
|
|
fn minimal_changes() {
|
|
assert!(run(&[1, 2, 3], &[1, 2, 3]).is_empty());
|
|
assert_eq!(run(&[1, 2, 3], &[1, 9, 3]), Changes(vec![Change::Update(1)]));
|
|
assert_eq!(run(&[1, 2, 3], &[1, 2, 7, 3]), Changes(vec![Change::Insert(2)]));
|
|
assert_eq!(run(&[1, 2, 3], &[1, 3]), Changes(vec![Change::Remove(1)]));
|
|
assert_eq!(counts(&run(&[], &[1, 2])), (0, 2, 0));
|
|
assert_eq!(counts(&run(&[1, 2], &[])), (0, 0, 2));
|
|
assert_eq!(run(&[1, 2, 3, 4], &[5, 6]).updated(), 2);
|
|
run(&[1, 1, 1], &[1, 1]);
|
|
run(&[1, 2, 1], &[1, 1, 2, 1]);
|
|
run(&[4, 5, 6], &[1, 2, 3, 4, 5, 6, 7]);
|
|
}
|
|
|
|
/// Whatever the lists, applying the changes gives the new list, and
|
|
/// rows that stay at the ends are not touched.
|
|
#[test]
|
|
fn every_small_pair() {
|
|
let lists: Vec<Vec<i32>> = (0..4usize)
|
|
.flat_map(|n| (0..3i32.pow(n as u32)).map(move |k| (n, k)))
|
|
.map(|(n, k)| (0..n as u32).map(|i| (k / 3i32.pow(i)) % 3).collect())
|
|
.collect();
|
|
for old in &lists {
|
|
for new in &lists {
|
|
let changes = run(old, new);
|
|
let kept = old.iter().zip(new).take_while(|(a, b)| a == b).count();
|
|
assert!(changes.0.iter().all(|c| match *c {
|
|
Change::Update(i) | Change::Remove(i) | Change::Insert(i) => i >= kept,
|
|
}));
|
|
}
|
|
}
|
|
}
|
|
}
|