Files
VoelinClient/crates/voelin-ui/src/host.rs
T
faumarayandClaude Sonnet 5.5 a77ccc0415 voelin-ui-vm: the server sequences both front ends ran twice are one
The GPUI shell repeated the Slint crate's connect, observe, gateway lookup,
save and remove of a server, its default nickname, its FallbackSecrets and its
settings opening and key registration. They now live once and both front ends
call them:

- voelin_ui_vm::servers::sequence: connect_voice(_to), observe,
  discover_gateway, gateway_found, select_server, save_bookmark,
  save_and_connect, delete_bookmark, bookmark_form and default_nickname, as
  transitions over the state, the Cx and a Backing (the store, the secrets,
  the identity a connection uses). The commands, the status line and what to
  keep go out through the Update as for every other transition; a new field,
  Update::discover, asks the front end to look a gateway up (the thread hop
  stays with each front end: Later in GPUI, later() in Slint). stream_peer is
  the one input a front end gives (Slint's codecs; GPUI passes none).
- voelin_ui_vm::settings::open_settings and register_keys; a front end adds
  its own key (ui.window, studio.ui) through a function it passes, so Slint's
  settings page still lists what it listed.
- voelin_store::FallbackSecrets (feature keyring) and Store::identity_for.
- voelin-ui: the methods of servers/bind.rs, open_settings and default_secrets
  call them, so their call sites are unchanged; host.rs gets transition_with
  and the discover arm of perform. voelin-gpui: AppState's methods and the
  Backend call them. What cannot move is named in docs/ui.md.

voelin-ui-vm now depends on tracing (the sequences log as the copies did): one
line of Cargo.lock, THIRD_PARTY_NOTICES.md unchanged (scripts/notices.sh
--check clean), cargo deny check clean. The sequences have ten tests over a
fake front end (commands, status, store, secrets, gateways), open_settings and
Store::identity_for one each; the GPUI and Slint suites pass unchanged.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-27 20:05:46 +03:00

372 lines
10 KiB
Rust

//! The Slint side of the transitions of `voelin-ui-vm`: what they ask of
//! the front end while they run ([`SlintHost`]) and doing what their
//! [`Update`] says ([`App::perform`]). The state changes are in the view
//! model crate; this is where the engine, the store and the window are
//! touched for them.
use slint::ComponentHandle;
use tracing::{debug, warn};
use voelin_core::Event;
use voelin_core::settings::{AUDIO, CACHE_FETCH_IMAGES, Settings};
use voelin_store::{ChatRead, ChatTarget as StoredTarget, Store};
use crate::app::{App, AppState, Bridge, MainWindow, Nav, Page};
use crate::settings::{CLIENT_PLAYBACK, UI_IMAGE_PREVIEW_KB};
use crate::social::Social;
use crate::studio::StudioState;
use crate::vm::host::{Cx, Host, PasswordPrompt, Persist, Refresh, Update};
use crate::vm::servers::sequence::Backing;
/// What the transitions ask while they run: the window, the store and the
/// settings, borrowed from the app besides its state.
pub(crate) struct SlintHost<'a> {
pub demo_ui: bool,
pub watching: bool,
/// The private chat the messages page has open: session and peer.
pub dm: Option<(i64, String)>,
pub ui: &'a slint::Weak<MainWindow>,
pub store: &'a Store,
pub prefs: &'a Settings,
pub studio: &'a StudioState,
}
impl Host for SlintHost<'_> {
fn demo_ui(&self) -> bool {
self.demo_ui
}
fn chat_shown(&self) -> bool {
self.ui.upgrade().is_some_and(|ui| ui.global::<Nav>().get_chat_shown())
}
fn watching(&self) -> bool {
self.watching
}
fn autowatch(&self) -> bool {
std::env::var("VOELIN_AUTOWATCH").is_ok_and(|v| v == "1")
}
fn studio_streams_to(&self, session: i64) -> bool {
self.studio.streams_to(session)
}
fn preview_limit(&self) -> u64 {
u64::from(self.prefs.get(&UI_IMAGE_PREVIEW_KB)) * 1024
}
fn fetch_images(&self) -> bool {
self.prefs.get(&CACHE_FETCH_IMAGES)
}
fn dm_shown(&self, session: i64, uid: &str) -> bool {
self.dm.as_ref().is_some_and(|(s, u)| *s == session && u == uid)
&& self.ui.upgrade().is_some_and(|ui| ui.global::<Nav>().get_page() == Page::Messages)
}
fn chat_reads(&self, server_uid: &str) -> Result<Vec<(StoredTarget, ChatRead)>, String> {
self.store.chat_reads(server_uid).map_err(|e| e.to_string())
}
fn set_chat_read(
&self,
server_uid: &str,
target: &StoredTarget,
read: &ChatRead,
) -> Result<(), String> {
self.store.set_chat_read(server_uid, target, read).map_err(|e| e.to_string())
}
}
impl App {
/// Run `f` with the state and the host, and return what it returns.
pub(crate) fn with_host<R>(&mut self, f: impl FnOnce(&mut AppState, &SlintHost<'_>) -> R) -> R {
self.with_social(|state, _, host| f(state, host))
}
/// Run `f` with the state, the social state and the host.
pub(crate) fn with_social<R>(
&mut self,
f: impl FnOnce(&mut AppState, &mut Social, &SlintHost<'_>) -> R,
) -> R {
self.with_parts(|state, social, host, _| f(state, social, host))
}
/// Run `f` with the state, the social state, the host and the
/// [`Backing`] of the server sequences (the store, the secrets and the
/// identity, borrowed from the app besides its state).
fn with_parts<R>(
&mut self,
f: impl FnOnce(&mut AppState, &mut Social, &SlintHost<'_>, &Backing<'_>) -> R,
) -> R {
let host = SlintHost {
demo_ui: self.demo_ui,
watching: self.state.watch.is_some(),
dm: self.social.dm.open.as_ref().and_then(|open| {
open.session.filter(|_| open.mail.is_none()).map(|s| (s, open.uid.clone()))
}),
ui: &self.ui,
store: &self.store,
prefs: &self.prefs,
studio: &self.studio,
};
let identity = |named| {
voelin_core::ts::identity::to_blob(&self.store.identity_for(named, &self.identity))
};
let backing = Backing { store: &self.store, secrets: &*self.secrets, identity: &identity };
f(&mut self.state, &mut self.social, &host, &backing)
}
/// An engine event: what it does to the state is in `voelin-ui-vm`
/// ([`crate::vm::events::handle`]), then the update is carried out.
pub(crate) fn handle_event(&mut self, event: Event) {
let update = self.with_social(|state, social, host| {
crate::vm::events::handle(state, social, host, event)
});
self.perform(update);
}
/// Run a transition of the view model on the state, and carry out what
/// it says.
pub(crate) fn transition<R>(&mut self, f: impl FnOnce(&mut AppState, &mut Cx<'_>) -> R) -> R {
let (result, update) = self.with_host(|state, host| {
let mut cx = Cx::new(host);
let result = f(state, &mut cx);
(result, cx.update)
});
self.perform(update);
result
}
/// Like [`App::transition`], for a sequence that also reads and writes the
/// store and the secrets and picks the identity
/// (`voelin_ui_vm::servers::sequence`).
pub(crate) fn transition_with<R>(
&mut self,
f: impl FnOnce(&mut AppState, &mut Cx<'_>, &Backing<'_>) -> R,
) -> R {
let (result, update) = self.with_parts(|state, _, host, backing| {
let mut cx = Cx::new(host);
let result = f(state, &mut cx, backing);
(result, cx.update)
});
self.perform(update);
result
}
/// Like [`App::transition`], for a transition that changes the social
/// state too.
pub(crate) fn social_transition<R>(
&mut self,
f: impl FnOnce(&mut AppState, &mut Social, &mut Cx<'_>) -> R,
) -> R {
let (result, update) = self.with_social(|state, social, host| {
let mut cx = Cx::new(host);
let result = f(state, social, &mut cx);
(result, cx.update)
});
self.perform(update);
result
}
/// Do what a transition asked for: tell the engine, keep what is to be
/// kept, draw what changed, and hand the rest back to the code that
/// still owns it.
pub(crate) fn perform(&mut self, update: Update) {
let Update {
refresh,
status,
commands,
persist,
reloaded,
discover,
input_level,
password,
notices,
warnings,
chats,
watch,
stream,
autoshare,
first_client,
rest,
} = update;
for text in warnings {
warn!("{text}");
}
// What the engine wrote again is decoded again, when it is drawn.
for path in &reloaded {
crate::images::reloaded(path);
}
for command in commands {
self.engine.send(command);
}
// The gateways to look up, on the engine's runtime.
for (id, address) in discover {
self.look_up_gateway(id, address);
}
// The capture and the decoder.
if let Some(sink) = stream.attach
&& let Some(share) = &self.share
{
share.capture.attach(sink);
}
if stream.drop_share {
self.share = None;
}
if stream.drop_decoder {
self.decoder = None;
}
if stream.leave_fullscreen {
self.set_fullscreen(false);
}
if stream.mixer {
self.mixer_refresh();
}
if let Some(text) = status {
self.set_status(text);
}
for what in persist {
self.keep(what);
}
if let Some((level_db, sending)) = input_level
&& let Some(ui) = self.ui.upgrade()
{
let bridge = ui.global::<Bridge>();
bridge.set_input_level(level_db);
bridge.set_input_sending(sending);
}
if let Some(PasswordPrompt { name, wrong }) = password
&& let Some(ui) = self.ui.upgrade()
{
let nav = ui.global::<Nav>();
nav.set_channel_password_name(name.into());
nav.set_channel_password_error(wrong);
nav.set_channel_password_open(true);
}
for notice in notices {
self.notify(
notice.kind,
notice.title,
notice.body,
notice.target,
notice.name,
notice.uid,
);
}
// What the front end keeps itself comes before the drawing: what
// it changes is on screen then.
if let Some(event) = rest {
self.event_rest(event);
}
self.refresh_parts(refresh);
for (id, target) in chats {
self.studio_chat_changed(id, &target);
}
if let Some(stream) = watch {
self.watch_stream(stream);
}
if autoshare {
self.autoshare();
}
if first_client && self.open_client_pending {
self.open_first_client();
}
}
/// Keep what a transition changed.
fn keep(&mut self, what: Persist) {
match what {
Persist::Bookmark { id, what } => {
let Some(bookmark) = self.state.bookmarks.iter().find(|b| b.id == id) else {
return;
};
match self.store.update_bookmark(bookmark) {
Ok(()) => debug!(server = %bookmark.address, "{what}"),
Err(e) => warn!(%e, "could not keep the bookmark: {what}"),
}
}
Persist::Settings => self.store_settings(),
Persist::Audio => {
if let Err(e) = self.prefs.set(&AUDIO, self.state.audio.clone()) {
warn!(%e, "could not store audio settings");
}
}
Persist::Playback => {
if let Err(e) = self.prefs.set(&CLIENT_PLAYBACK, self.state.playback.clone()) {
warn!(%e, "could not store client volumes");
}
}
}
}
/// Draw the parts of the window a transition changed.
pub(crate) fn refresh_parts(&mut self, parts: Refresh) {
if parts.servers {
self.refresh_servers();
}
if parts.toolbar {
self.refresh_toolbar();
}
if parts.tree {
self.refresh_tree();
}
if parts.chat {
self.refresh_chat();
}
if parts.streams {
self.refresh_streams();
}
if parts.viewer {
self.refresh_viewer();
}
if parts.member_card {
self.refresh_member_card();
}
if parts.people {
self.refresh_people();
}
if parts.conversations {
self.ask_inboxes();
self.refresh_chats();
}
if parts.home {
self.refresh_home();
}
if parts.dm {
self.refresh_dm();
}
if parts.events {
self.refresh_events();
}
if parts.gateways {
self.refresh_gateways();
}
}
/// The events whose state the view model does not hold: a setting that
/// changed, the studio's stream and the requests of the messages page.
/// [`App::perform`] runs this before it draws, so that what these change
/// is what is drawn.
fn event_rest(&mut self, event: Event) {
match event {
Event::SettingChanged { key } => self.setting_changed(&key),
// The Stream Studio's stream (studio/).
Event::StreamState { session, state } if self.studio_streams_to(session as i64) => {
self.studio_stream_state(state);
}
Event::StreamViewers { session, viewers } if self.studio_streams_to(session as i64) => {
self.studio_viewers(viewers);
}
// The messages page keeps the offline message requests
// (messages.rs); the text of a message is the view model's
// (`social::logic::event`).
Event::OfflineMessages { result: Err(e), .. } => debug!("offline messages: {e}"),
Event::RequestDone { session, request, result } => {
self.request_done(session as i64, request, result);
}
_ => {}
}
}
}