Signed-off-by: Faumaray <faumaray@gmail.com>
This commit is contained in:
faumaray committed 2026-10-04 19:15:42 +03:00
1 parent b2549f867e
commit 3f177a77aa
29 files changed
+457 -196

No files matched your search

+18 -6
View File
@@ -215,6 +215,8 @@ pub(crate) struct SessionView {
/// The server's icon id as its voice connection last told it
/// ([`Event::ServerDetails`]); kept while disconnected, for the rail.
pub server_icon: u32,
/// Bookmark address that supplied this session's icon.
pub icon_address: Option<String>,
/// The server groups in display order ([`Event::Groups`]).
pub server_groups: Vec<GroupInfo>,
/// The channel groups ([`Event::Groups`]).
@@ -250,6 +252,7 @@ impl Default for SessionView {
icons: HashMap::new(),
pictures: HashMap::new(),
server_icon: 0,
icon_address: None,
server_groups: Vec::new(),
channel_groups: Vec::new(),
gateway_caps: Vec::new(),
@@ -318,6 +321,16 @@ impl SessionView {
self.icons.get(&self.server_icon).filter(|_| self.server_icon != 0),
)
}
/// Icons stay associated with the address that supplied them, including
/// while a reconnect to an edited bookmark is pending or has failed.
pub fn server_icon_for(&self, address: &str) -> slint::Image {
if self.icon_address.as_deref() == Some(address) {
self.server_icon()
} else {
slint::Image::default()
}
}
}
/// The models the Bridge shows, created once and updated row by row
@@ -942,12 +955,10 @@ impl App {
self.refresh_tree();
}
}
Event::ServerDetails { session, details } => {
let view = self.sessions.entry(session as i64).or_default();
if view.server_icon != details.icon {
view.server_icon = details.icon;
self.refresh_servers();
}
Event::ServerDetails { session, address, details } => {
self.remember_server_icon(session as i64, &address, details.icon);
self.refresh_servers();
self.refresh_toolbar();
}
// Pictures can arrive after the rows were built: draw them again.
Event::AvatarReady { session, client_uid, path, .. } => {
@@ -967,6 +978,7 @@ impl App {
self.refresh_servers();
if self.current == Some(session as i64) {
self.refresh_tree();
self.refresh_toolbar();
}
}
Event::PictureReady { session, url, path } => {
+4 -2
View File
@@ -369,6 +369,7 @@ fn demo_ui(app: &mut App) {
gateway_url: None,
query: None,
client_version: None,
cached_server_icon: None,
};
app.bookmarks = vec![
Bookmark {
@@ -385,7 +386,7 @@ fn demo_ui(app: &mut App) {
p.server.welcome_message =
"Welcome to [b]Nightfall Guild[/b]! Raids on Tuesdays and Fridays, all levels welcome."
.into();
// The host banner, a server icon, banners above three channels and an icon
// The host banner, a server icon, backgrounds on three channels and an icon
// on another; the pictures arrive below (`demo_pictures`).
p.server.banner_gfx_url = DEMO_HOST_BANNER.into();
p.server.banner_mode = BannerMode::KeepAspect;
@@ -407,7 +408,7 @@ fn demo_ui(app: &mut App) {
for c in [
channel(1, 0, 0, "Lobby"),
chill,
channel(3, 0, 2, "Gaming"),
channel(3, 0, 2, "[cspacer0]Gaming"),
raid,
music,
channel(6, 0, 5, "AFK"),
@@ -1024,6 +1025,7 @@ fn demo_pictures(app: &mut App) {
};
let mut events = vec![Event::ServerDetails {
session,
address: app.bookmarks.iter().find(|b| b.id == DEMO).unwrap().address.clone(),
details: Arc::new(voelin_model::ServerDetails {
icon: DEMO_SERVER_ICON,
..Default::default()
+95 -2
View File
@@ -7,7 +7,7 @@ use voelin_core::identity::LaunchImport;
use voelin_core::{Command, ObserveState, Source, VoiceOptions, VoiceState};
use voelin_store::{Bookmark, QueryConfig, QueryTransport};
use crate::app::{App, BookmarkForm, Bridge, later};
use crate::app::{App, BookmarkForm, Bridge, SessionView, later};
use crate::vm;
/// The name of the identity the app creates when it has none.
@@ -44,6 +44,9 @@ fn bookmark_from_form(
let mut bookmark = old.cloned().unwrap_or_default();
bookmark.id = form.id as i64;
bookmark.address = form.address.trim().to_string();
if old.is_some_and(|old| old.address != bookmark.address) {
bookmark.cached_server_icon = None;
}
bookmark.name = match form.name.trim() {
"" => bookmark.address.clone(),
name => name.to_string(),
@@ -84,6 +87,24 @@ fn bookmark_from_form(
bookmark
}
/// Apply only details originating from the bookmark's current address.
/// Returns whether its persisted metadata changed.
fn apply_server_icon(
bookmark: &mut Bookmark,
view: &mut SessionView,
address: &str,
icon: u32,
) -> bool {
if bookmark.address != address {
return false;
}
view.icon_address = Some(address.to_owned());
view.server_icon = icon;
let before = bookmark.cached_server_icon.clone();
bookmark.remember_server_icon(icon);
bookmark.cached_server_icon != before
}
impl App {
pub(crate) fn connect_voice(&mut self) {
let channel =
@@ -305,6 +326,33 @@ impl App {
self.refresh_all();
}
/// Keep only metadata supplied by the bookmark's current server.
pub(crate) fn remember_server_icon(&mut self, id: i64, address: &str, icon: u32) {
let Some(bookmark) = self.bookmarks.iter_mut().find(|b| b.id == id) else {
return;
};
let changed =
apply_server_icon(bookmark, self.sessions.entry(id).or_default(), address, icon);
if changed && !self.demo_ui {
if let Err(error) = self.store.update_bookmark(bookmark) {
warn!(%error, "could not keep the server icon");
}
}
}
pub(crate) fn server_list_icon(&self, bookmark: &Bookmark) -> slint::Image {
let current = self
.sessions
.get(&bookmark.id)
.map(|view| view.server_icon_for(&bookmark.address))
.unwrap_or_default();
if current.size().width > 0 {
current
} else {
vm::servers::cached_icon(bookmark, &self.engine.cache())
}
}
/// The rail: servers with their state, unread count and streams.
pub(crate) fn refresh_servers(&self) {
let Some(ui) = self.ui.upgrade() else { return };
@@ -322,7 +370,7 @@ impl App {
.map(|v| vm::tree::stats(&v.presence))
.unwrap_or_default();
let flavor = view.map(|v| v.extra.flavor.clone()).unwrap_or_default();
let icon = view.map(|v| v.server_icon()).unwrap_or_default();
let icon = self.server_list_icon(b);
vm::servers::item(b, &state, unread, live, detail, flavor, icon)
})
.collect();
@@ -338,6 +386,7 @@ impl App {
let bookmark = self.current.and_then(|id| self.bookmark(id));
let view = self.view();
let state = view.map(|v| v.state.clone()).unwrap_or_default();
bridge.set_server_icon(bookmark.map(|b| self.server_list_icon(b)).unwrap_or_default());
bridge.set_server_title(
bookmark.map_or("No server selected".into(), |b| b.name.clone()).into(),
);
@@ -498,6 +547,45 @@ mod tests {
use super::*;
#[test]
fn edited_server_rejects_queued_icons_and_failed_reconnect_keeps_them_hidden() {
let path = std::env::temp_dir()
.join(format!("voelin-server-icon-provenance-{}.svg", std::process::id()));
std::fs::write(&path, r#"<svg xmlns="http://www.w3.org/2000/svg" width="4" height="4"><rect width="4" height="4" fill="red"/></svg>"#).unwrap();
let mut bookmark = Bookmark { address: "a.test".into(), ..Default::default() };
let mut view = SessionView::default();
view.icons.insert(1234, path.clone());
assert!(apply_server_icon(&mut bookmark, &mut view, "a.test", 1234));
assert_eq!(view.server_icon_for(&bookmark.address).size().width, 4);
bookmark = bookmark_from_form(
Some(&bookmark),
&BookmarkForm { address: "b.test".into(), ..Default::default() },
|| "Me".into(),
);
// Connecting does not retag received imagery. A failed B connection
// must not make A's image appear under B's name.
for state in [VoiceState::Connecting, VoiceState::Disconnected] {
view.state.voice = state;
assert_eq!(view.server_icon_for(&bookmark.address).size().width, 0);
}
assert!(!apply_server_icon(&mut bookmark, &mut view, "a.test", 4321));
assert_eq!(view.server_icon, 1234);
assert_eq!(view.icon_address.as_deref(), Some("a.test"));
assert!(bookmark.server_icon_id().is_none());
// Equal icon IDs on the next server still need new provenance.
assert!(apply_server_icon(&mut bookmark, &mut view, "b.test", 1234));
assert_eq!(bookmark.server_icon_id(), Some(1234));
assert_eq!(view.server_icon_for(&bookmark.address).size().width, 4);
assert!(!apply_server_icon(&mut bookmark, &mut view, "a.test", 0));
assert_eq!(bookmark.server_icon_id(), Some(1234));
assert!(apply_server_icon(&mut bookmark, &mut view, "b.test", 0));
assert!(bookmark.server_icon_id().is_none());
assert_eq!(view.server_icon_for(&bookmark.address).size().width, 0);
std::fs::remove_file(path).unwrap();
}
#[test]
fn the_address_alone_makes_a_server() {
let form = BookmarkForm {
@@ -527,6 +615,10 @@ mod tests {
gateway_url: Some("ws://gw.example.test:7788/v1".into()),
query: Some(QueryConfig { server_port: Some(9988), ..Default::default() }),
client_version: Some("linux".into()),
cached_server_icon: Some(voelin_store::CachedServerIcon {
address: "old.example.test".into(),
id: 1234,
}),
};
let form = BookmarkForm {
id: 7,
@@ -543,6 +635,7 @@ mod tests {
let b = bookmark_from_form(Some(&old), &form, || unreachable!("a nickname was given"));
assert_eq!((b.id, b.name.as_str(), b.nickname.as_str()), (7, "Mine", "Other"));
assert_eq!(b.address, "new.example.test:9988");
assert!(b.cached_server_icon.is_none());
assert_eq!((b.identity, b.default_channel), (Some(2), Some("Lobby/Sub".into())));
assert_eq!(b.client_version.as_deref(), Some("linux"));
assert_eq!(b.gateway_url, None);
+1 -5
View File
@@ -958,11 +958,7 @@ impl App {
subtitle: b.address.clone().into(),
initials: vm::avatar::initials(&b.name).into(),
tint: vm::avatar::tint(&b.name),
avatar: self
.sessions
.get(&b.id)
.map(|v| v.server_icon())
.unwrap_or_default(),
avatar: self.server_list_icon(b),
..Default::default()
},
Found::Server(b.id),
+40
View File
@@ -6,6 +6,12 @@ use voelin_store::Bookmark;
use crate::app::ServerItem;
use crate::vm::avatar;
/// Restore a known server icon before any session exists. A missing/evicted
/// cache file falls back to initials; the bookmark stores no filesystem path.
pub fn cached_icon(bookmark: &Bookmark, cache: &voelin_core::Cache) -> slint::Image {
avatar::image(bookmark.server_icon_id().and_then(|id| cache.icon(id)).as_ref())
}
/// "offline", "connecting", "observing" or "connected".
pub fn status(state: &SessionState) -> &'static str {
match (state.voice, state.observe) {
@@ -92,6 +98,40 @@ pub fn invite_link(address: &str, path: &[&str]) -> String {
mod tests {
use super::*;
#[test]
fn server_icon_is_visible_before_connection_and_missing_files_fall_back() {
let dir =
std::env::temp_dir().join(format!("voelin-server-icon-model-{}", std::process::id()));
std::fs::create_dir_all(dir.join("icons")).unwrap();
let path = dir.join("icons/1234");
let mut bytes = Vec::new();
let mut encoder = png::Encoder::new(&mut bytes, 4, 4);
encoder.set_color(png::ColorType::Rgba);
encoder.set_depth(png::BitDepth::Eight);
encoder.write_header().unwrap().write_image_data(&[200; 64]).unwrap();
std::fs::write(&path, bytes).unwrap();
let cache = voelin_core::Cache::new(&dir);
let mut bookmark = Bookmark { address: "example.test".into(), ..Default::default() };
bookmark.remember_server_icon(1234);
let server = item(
&bookmark,
&SessionState::default(),
0,
false,
String::new(),
String::new(),
cached_icon(&bookmark, &cache),
);
assert_eq!(server.status.as_str(), "offline");
assert_eq!(server.icon.size().width, 4);
bookmark.address = "different.test".into();
assert_eq!(cached_icon(&bookmark, &cache).size().width, 0);
bookmark.address = "example.test".into();
std::fs::remove_file(path).unwrap();
assert_eq!(cached_icon(&bookmark, &cache).size().width, 0);
std::fs::remove_dir_all(dir).unwrap();
}
#[test]
fn invite_links() {
assert_eq!(
+48 -16
View File
@@ -102,28 +102,43 @@ fn talking(input: &TreeInput, client: &ClientInfo) -> bool {
input.talking.contains(&client.id) || client.talking == Some(true)
}
/// Centered spacer prefixes are presentation metadata, not part of the title.
/// Leave other names untouched rather than guessing at the full spacer syntax.
fn channel_label(name: &str) -> (&str, bool) {
if let Some((suffix, label)) = name.strip_prefix("[cspacer").and_then(|s| s.split_once(']')) {
if suffix.bytes().all(|b| b.is_ascii_digit()) {
return (label, true);
}
}
(name, false)
}
/// The rows of the tree, channels with their clients below.
pub fn rows(input: &TreeInput) -> Vec<TreeItem> {
let p = input.presence;
let rows: Vec<TreeItem> = tree_rows(p, &|cid| input.collapsed.contains(&cid))
.into_iter()
.map(|row| match row {
TreeRow::Channel { depth, channel } => TreeItem {
is_channel: true,
depth: depth as i32,
id: channel.id as i32,
name: channel.name.clone().into(),
own: input.own_channel == Some(channel.id),
locked: channel.has_password,
collapsed: input.collapsed.contains(&channel.id),
local_volume: 100,
members: p.members(channel.id).count() as i32,
max_members: channel.max_clients.filter(|m| *m >= 0).unwrap_or(-1),
icon: input.icon(channel.icon).unwrap_or_default(),
banner: banner(input.pictures, channel.banner_gfx_url.as_deref()),
banner_mode: banner_mode(channel.banner_mode),
..Default::default()
},
TreeRow::Channel { depth, channel } => {
let (name, centered_spacer) = channel_label(&channel.name);
TreeItem {
centered_spacer,
is_channel: true,
depth: depth as i32,
id: channel.id as i32,
name: name.into(),
own: input.own_channel == Some(channel.id),
locked: channel.has_password,
collapsed: input.collapsed.contains(&channel.id),
local_volume: 100,
members: p.members(channel.id).count() as i32,
max_members: channel.max_clients.filter(|m| *m >= 0).unwrap_or(-1),
icon: input.icon(channel.icon).unwrap_or_default(),
banner: banner(input.pictures, channel.banner_gfx_url.as_deref()),
banner_mode: banner_mode(channel.banner_mode),
..Default::default()
}
}
TreeRow::Client { depth, client } => {
let playback = client
.uid
@@ -480,6 +495,23 @@ mod tests {
std::fs::remove_dir_all(dir).unwrap();
}
#[test]
fn centered_spacer_titles_keep_tree_identity_and_search() {
let mut p = presence();
p.channels.get_mut(&2).unwrap().name = "[cspacer12]Гамесы".into();
let (t, c, pb) = (HashSet::new(), HashSet::from([2]), ClientPlaybackMap::new());
let e = Extras::default();
let rows = rows(&input(&p, &t, &c, &pb, "Гамесы", &e));
assert_eq!(rows.len(), 1);
assert_eq!(rows[0].name, "Гамесы");
assert!(rows[0].centered_spacer && rows[0].collapsed);
assert_eq!(rows[0].id, 2);
assert_eq!(channel_label("[cspacer]Games"), ("Games", true));
for name in ["[cspacerx]Games", "[cspacer2Games", "Games", "[spacer0]Games"] {
assert_eq!(channel_label(name), (name, false));
}
}
#[test]
fn tree_rows_and_counts() {
let p = presence();