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:
1 parent
7c6d149f78
commit
4dc8272c64
16 files changed
+3900
-6
No files matched your search
@@ -31,6 +31,7 @@ use voelin_model::{Capabilities, ChannelId, ChatMessage, ChatTarget, GroupInfo,
|
||||
use voelin_platform::crash;
|
||||
use voelin_store::{Bookmark, MemorySecrets, Secrets, Store};
|
||||
|
||||
use crate::bind::rows::Rows;
|
||||
use crate::hotkey::GlobalPtt;
|
||||
use crate::settings::{
|
||||
CLIENT_PLAYBACK, ClientPlaybackMap, DeviceChoices, UI, UiSettings, appearance_keys,
|
||||
@@ -495,7 +496,7 @@ pub(crate) struct Models {
|
||||
pub topics: Rc<VecModel<TopicItem>>,
|
||||
pub topic_messages: Rc<VecModel<ChatLine>>,
|
||||
/// The quality choices of the watched stream.
|
||||
pub qualities: Rc<VecModel<slint::SharedString>>,
|
||||
pub qualities: Rows<Arc<str>>,
|
||||
/// Shown when there is no chat.
|
||||
pub no_chat: Rc<VecModel<ChatLine>>,
|
||||
/// Home, friends, messages, the bell, the search, events.
|
||||
@@ -523,7 +524,7 @@ impl Models {
|
||||
pins: Rc::default(),
|
||||
topics: Rc::default(),
|
||||
topic_messages: Rc::default(),
|
||||
qualities: Rc::default(),
|
||||
qualities: Rows::default(),
|
||||
no_chat: Rc::default(),
|
||||
social: crate::social::SocialModels::new(bridge),
|
||||
layers: Rc::default(),
|
||||
@@ -550,7 +551,7 @@ impl Models {
|
||||
bridge.set_pins(ModelRc::from(self.pins.clone()));
|
||||
bridge.set_topics(ModelRc::from(self.topics.clone()));
|
||||
bridge.set_topic_messages(ModelRc::from(self.topic_messages.clone()));
|
||||
bridge.set_viewer_qualities(ModelRc::from(self.qualities.clone()));
|
||||
bridge.set_viewer_qualities(self.qualities.model());
|
||||
bridge.set_messages(ModelRc::from(self.no_chat.clone()));
|
||||
bridge.set_layers(ModelRc::from(self.layers.clone()));
|
||||
bridge.set_audio_sources(ModelRc::from(self.audio_sources.clone()));
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
//! Plain view-model values (`voelin-ui-vm`) to Slint values, and back where
|
||||
//! that can be done. Temporary, like the Slint front end itself: the rows
|
||||
//! (`items`) are converted when [`super::rows::Rows`] applies a change, so
|
||||
//! only changed rows pay for it.
|
||||
|
||||
use std::rc::Rc;
|
||||
use std::sync::Arc;
|
||||
|
||||
use slint::{Color, Image, ModelRc, Rgba8Pixel, SharedPixelBuffer, SharedString, VecModel};
|
||||
use voelin_ui_vm::{ImageRef, ImageSource, Rgb};
|
||||
|
||||
mod items;
|
||||
|
||||
/// An opaque colour.
|
||||
pub(crate) fn color(c: Rgb) -> Color {
|
||||
Color::from_rgb_u8(c.r(), c.g(), c.b())
|
||||
}
|
||||
|
||||
/// A colour that may be absent: transparent is the text's own colour.
|
||||
pub(crate) fn ink(c: Option<Rgb>) -> Color {
|
||||
c.map(color).unwrap_or_default()
|
||||
}
|
||||
|
||||
/// The opaque colour of a Slint colour (its transparency is dropped).
|
||||
pub(crate) fn unrgb(c: Color) -> Rgb {
|
||||
Rgb::new(c.red(), c.green(), c.blue())
|
||||
}
|
||||
|
||||
/// The colour of a Slint colour, none for a transparent one.
|
||||
pub(crate) fn unink(c: Color) -> Option<Rgb> {
|
||||
(c.alpha() > 0).then(|| unrgb(c))
|
||||
}
|
||||
|
||||
/// A picture as Slint shows it: files and downloaded pictures through the
|
||||
/// decoded-picture cache (`images.rs`), pixels as they are.
|
||||
pub(crate) fn image(picture: &ImageRef) -> Image {
|
||||
match &**picture {
|
||||
ImageSource::None => Image::default(),
|
||||
ImageSource::File(path) => crate::images::file(path),
|
||||
ImageSource::Rgba { w, h, bytes } => {
|
||||
Image::from_rgba8(SharedPixelBuffer::<Rgba8Pixel>::clone_from_slice(bytes, *w, *h))
|
||||
}
|
||||
ImageSource::Svg(data) => Image::load_from_svg_data(data).unwrap_or_default(),
|
||||
ImageSource::Memory { key, bytes } => crate::images::picture(key, bytes),
|
||||
}
|
||||
}
|
||||
|
||||
/// A model of pictures.
|
||||
pub(crate) fn images(pictures: &[ImageRef]) -> ModelRc<Image> {
|
||||
ModelRc::from(Rc::new(VecModel::from(pictures.iter().map(image).collect::<Vec<_>>())))
|
||||
}
|
||||
|
||||
/// A model of strings.
|
||||
pub(crate) fn strings(texts: &[Arc<str>]) -> ModelRc<SharedString> {
|
||||
ModelRc::from(Rc::new(VecModel::from(
|
||||
texts.iter().map(|t| SharedString::from(&**t)).collect::<Vec<_>>(),
|
||||
)))
|
||||
}
|
||||
|
||||
/// The strings of a model.
|
||||
pub(crate) fn unstrings(model: &ModelRc<SharedString>) -> Vec<Arc<str>> {
|
||||
slint::Model::iter(model).map(|t| Arc::from(t.as_str())).collect()
|
||||
}
|
||||
|
||||
/// A model of the Slint rows the plain rows convert to.
|
||||
pub(crate) fn model<V, S: for<'a> From<&'a V> + Clone + 'static>(rows: &[V]) -> ModelRc<S> {
|
||||
ModelRc::from(Rc::new(VecModel::from(rows.iter().map(S::from).collect::<Vec<_>>())))
|
||||
}
|
||||
|
||||
/// The plain rows of a model.
|
||||
pub(crate) fn rows<S: Clone + 'static, V: From<S>>(model: &ModelRc<S>) -> Vec<V> {
|
||||
slint::Model::iter(model).map(V::from).collect()
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -2,8 +2,10 @@
|
||||
//! area; each has a `wire` that the window calls once.
|
||||
|
||||
mod chat;
|
||||
pub(crate) mod convert;
|
||||
mod emoji;
|
||||
mod pages;
|
||||
pub(crate) mod rows;
|
||||
mod servers;
|
||||
mod settings;
|
||||
mod social;
|
||||
|
||||
@@ -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"));
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
//! viewer. The engine runs the stream sessions; `video.rs` captures,
|
||||
//! encodes and decodes.
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use slint::{ComponentHandle, Model};
|
||||
@@ -111,11 +112,11 @@ impl Watch {
|
||||
}
|
||||
|
||||
/// "Auto" and one entry per layer the streamer offers.
|
||||
fn qualities(&self) -> Vec<slint::SharedString> {
|
||||
fn qualities(&self) -> Vec<Arc<str>> {
|
||||
if self.layers.len() < 2 {
|
||||
return Vec::new();
|
||||
}
|
||||
let mut names = vec![slint::SharedString::from("Auto")];
|
||||
let mut names: Vec<Arc<str>> = vec!["Auto".into()];
|
||||
names.extend(self.layers.iter().map(|l| layer_name(l).into()));
|
||||
names
|
||||
}
|
||||
@@ -472,7 +473,7 @@ impl App {
|
||||
.layer
|
||||
.and_then(|id| watch.layers.iter().position(|l| l.id == id))
|
||||
.map_or(0, |i| i as i32 + 1);
|
||||
crate::vm::list::sync(&self.models.qualities, &qualities);
|
||||
self.models.qualities.sync(&qualities);
|
||||
self.viewer_each(|bridge| {
|
||||
bridge.set_viewer_popped(popped);
|
||||
bridge.set_viewer_title(watch.title.as_str().into());
|
||||
|
||||
Reference in new issue
Block a user