The sessions and their chat tabs, the downloads, pins and linked pictures, and the data the screens share (the servers, the settings, the dialogs' targets, the share and watch bookkeeping) no longer live in the Slint crate. `SessionView<L>` and `Tab<L>` are generic over the list type of a tab's chat lines: the Slint app plugs in `Rows<ChatLine>` (`app::ChatRows`), a GPUI view can use a plain `Vec`. `App` keeps one `AppState` in `state` and the Slint models; every `self.sessions`, `self.current`, `self.bookmarks`, ... becomes `self.state.<field>`. server_icon and server_icon_for stay with the session: `ImageRef` is toolkit-free now, so they need no adapter. Tests for the tab and session logic (private chat focus, tab removal, read markers, the divider) are new, since that logic could not be tested without the Slint crate before. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
192 lines
6.4 KiB
Rust
192 lines
6.4 KiB
Rust
//! The members panel: the card over a member row and what it can do
|
|
//! (private chat, poke, contacts, per-client volume). The rows themselves
|
|
//! are built in `vm::tree::members`.
|
|
|
|
use std::sync::Arc;
|
|
|
|
use slint::ComponentHandle;
|
|
use voelin_core::{Command, Contact, Relation};
|
|
|
|
use crate::app::{App, Bridge, Nav};
|
|
use crate::settings::ClientPlayback;
|
|
use crate::vm;
|
|
use crate::vm::bridge::MemberCard;
|
|
|
|
impl App {
|
|
/// `VOELIN_OPEN=member`: the card of the first person that is not us.
|
|
pub(crate) fn open_first_member(&mut self) {
|
|
let Some(view) = self.view() else { return };
|
|
let own = view.state.own_client;
|
|
let mut others: Vec<u16> = view
|
|
.presence
|
|
.members(view.state.own_channel.unwrap_or_default())
|
|
.filter(|c| Some(c.id) != own)
|
|
.map(|c| c.id)
|
|
.collect();
|
|
others.sort_unstable();
|
|
if let Some(id) = others.first().copied() {
|
|
self.open_member(i32::from(id));
|
|
}
|
|
}
|
|
|
|
/// Open the card of a client (`-1` closes it).
|
|
pub(crate) fn open_member(&mut self, client: i32) {
|
|
if let Some(view) = self.view_mut() {
|
|
view.member_card = u16::try_from(client).ok();
|
|
}
|
|
self.refresh_member_card();
|
|
}
|
|
|
|
pub(crate) fn refresh_member_card(&self) {
|
|
let Some(ui) = self.ui.upgrade() else { return };
|
|
let bridge = ui.global::<Bridge>();
|
|
let card = self
|
|
.view()
|
|
.and_then(|view| {
|
|
let id = view.member_card?;
|
|
let client = view.presence.clients.get(&id)?;
|
|
let uid = client.uid.clone();
|
|
let contact = uid.as_ref().and_then(|u| self.state.contacts.get(u));
|
|
let playback = uid
|
|
.as_ref()
|
|
.and_then(|u| self.state.playback.get(u))
|
|
.copied()
|
|
.unwrap_or_default();
|
|
let groups: Vec<Arc<str>> = client
|
|
.server_groups
|
|
.iter()
|
|
.filter_map(|g| view.server_groups.iter().find(|i| i.id == *g))
|
|
.map(|g| g.name.clone().into())
|
|
.collect();
|
|
let badges = vm::tree::badge_items(&client.badges, &view.pictures);
|
|
Some(MemberCard {
|
|
open: true,
|
|
id: i32::from(id),
|
|
name: client.nickname.clone().into(),
|
|
initials: vm::avatar::initials(&client.nickname).into(),
|
|
tint: vm::avatar::tint(&client.nickname),
|
|
avatar: vm::avatar::image(view.avatar(id)),
|
|
status: vm::tree::status_of(client, view.talking.contains(&id)).into(),
|
|
description: client.description.clone().unwrap_or_default().into(),
|
|
groups,
|
|
badges,
|
|
country: client.country.clone().unwrap_or_default().into(),
|
|
talk_power: client.talk_power,
|
|
friend: contact.is_some_and(|c| c.relation == Relation::Friend),
|
|
blocked: contact.is_some_and(|c| c.relation == Relation::Blocked),
|
|
volume: playback.volume * 100.0,
|
|
muted: playback.muted,
|
|
known: uid.is_some(),
|
|
streaming: client.streaming == Some(true),
|
|
})
|
|
})
|
|
.unwrap_or_default();
|
|
bridge.set_member_card(card.into());
|
|
}
|
|
|
|
/// The card's buttons: "message", "poke", "friend", "blocked", "mute".
|
|
pub(crate) fn member_action(&mut self, action: &str) {
|
|
let Some(id) = self.state.current else { return };
|
|
let Some(view) = self.state.sessions.get(&id) else { return };
|
|
let Some(client) = view.member_card.and_then(|c| view.presence.clients.get(&c)) else {
|
|
return;
|
|
};
|
|
let (client_id, nickname, uid) = (client.id, client.nickname.clone(), client.uid.clone());
|
|
match action {
|
|
// Private chats are on the messages page.
|
|
"message" => {
|
|
if let Some(uid) = uid {
|
|
self.open_member(-1);
|
|
self.open_dm_with(Some(id), &uid);
|
|
}
|
|
}
|
|
"watch" => {
|
|
self.open_member(-1);
|
|
self.watch_client(client_id);
|
|
}
|
|
"poke" => self.ask_poke(id, client_id),
|
|
"friend" | "blocked" => {
|
|
let Some(uid) = uid else { return };
|
|
let wanted = if action == "friend" { Relation::Friend } else { Relation::Blocked };
|
|
let current = self.state.contacts.get(&uid).map(|c| c.relation);
|
|
let mut contact = self
|
|
.state
|
|
.contacts
|
|
.get(&uid)
|
|
.cloned()
|
|
.unwrap_or_else(|| Contact::new(uid.clone()));
|
|
contact.nickname = nickname;
|
|
contact.relation = if current == Some(wanted) { Relation::Neutral } else { wanted };
|
|
self.state.contacts.insert(uid, contact.clone());
|
|
self.engine.send(Command::SetContact { contact: Box::new(contact) });
|
|
self.refresh_member_card();
|
|
}
|
|
"mute" => {
|
|
let Some(uid) = uid else { return };
|
|
let mut playback = self.state.playback.get(&uid).copied().unwrap_or_default();
|
|
playback.muted = !playback.muted;
|
|
self.set_playback(&uid, client_id, playback);
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
|
|
/// Open the poke dialog for a client (the member card, a contact).
|
|
pub(crate) fn ask_poke(&mut self, session: i64, client: u16) {
|
|
let Some(view) = self.state.sessions.get(&session) else { return };
|
|
let name = view.nickname(client);
|
|
self.state.poke_target = Some((session, client, name.clone()));
|
|
let Some(ui) = self.ui.upgrade() else { return };
|
|
let nav = ui.global::<Nav>();
|
|
nav.set_poke_name(name.into());
|
|
nav.set_poke_open(true);
|
|
}
|
|
|
|
/// The poke dialog's Send, with its message (may be empty).
|
|
pub(crate) fn send_poke(&mut self, text: &str) {
|
|
let Some((session, client, name)) = self.state.poke_target.take() else { return };
|
|
if !self.demo_ui {
|
|
self.engine.send(Command::Poke {
|
|
session: session as u64,
|
|
client,
|
|
message: vm::social::poke_message(text),
|
|
});
|
|
}
|
|
self.set_status(format!("Poked {name}"));
|
|
}
|
|
|
|
/// The card's volume slider, in percent.
|
|
pub(crate) fn member_volume(&mut self, percent: f32) {
|
|
let Some(view) = self.view() else { return };
|
|
let Some(client) = view.member_card.and_then(|c| view.presence.clients.get(&c)) else {
|
|
return;
|
|
};
|
|
let (client_id, Some(uid)) = (client.id, client.uid.clone()) else { return };
|
|
let mut playback = self.state.playback.get(&uid).copied().unwrap_or_default();
|
|
playback.volume = (percent / 100.0).clamp(0.0, 4.0);
|
|
self.set_playback(&uid, client_id, playback);
|
|
}
|
|
|
|
/// Store one client's playback and apply it to the session.
|
|
fn set_playback(&mut self, uid: &str, client: u16, playback: ClientPlayback) {
|
|
if playback.is_default() {
|
|
self.state.playback.remove(uid);
|
|
} else {
|
|
self.state.playback.insert(uid.to_owned(), playback);
|
|
}
|
|
if let Err(e) =
|
|
self.prefs.set(&crate::settings::CLIENT_PLAYBACK, self.state.playback.clone())
|
|
{
|
|
tracing::warn!(%e, "could not store client volumes");
|
|
}
|
|
self.command(|session| Command::SetClientMuted { session, client, muted: playback.muted });
|
|
self.command(|session| Command::SetClientVolume {
|
|
session,
|
|
client,
|
|
volume: playback.volume,
|
|
});
|
|
self.refresh_tree();
|
|
self.refresh_member_card();
|
|
}
|
|
}
|