Add voelin-ui-vm: toolkit-free view-model types and the Slint adapter

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>
This commit is contained in:
faumarayandClaude Sonnet 5.5 committed 2026-10-20 08:53:13 +03:00
1 parent 7c6d149f78
commit 4dc8272c64
16 files changed
+3900 -6

No files matched your search

+113
View File
@@ -0,0 +1,113 @@
//! 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::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())
}
/// 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"));
}
}