feat(client): redesign native UI, themes, settings and avatars

Separate conversation drafts and voice view state, add native settings and restricted CSS themes, implement validated avatar cropping, and refresh dependencies with documented desktop compatibility backports.
This commit is contained in:
Loki 2026-10-24 14:23:48 +02:00
parent 44d284a568
commit 8e5ab56611
Signed by: boba
GPG key ID: 253067914055423B
276 changed files with 53546 additions and 3399 deletions

View file

@ -1,38 +1,82 @@
use std::rc::Rc;
use slint::{SharedString, VecModel};
use slint::{Model, ModelRc, SharedString, VecModel};
use crate::ui::state::{ConnState, UiState};
type MainWindow = crate::ui::slint::MainWindow;
type BookmarkItem = crate::ui::slint::BookmarkItem;
type ChannelItem = crate::ui::slint::ChannelItem;
type DmItem = crate::ui::slint::DmItem;
type GuildItem = crate::ui::slint::GuildItem;
type MessageItem = crate::ui::slint::MessageItem;
type MemberItem = crate::ui::slint::MemberItem;
type FriendItem = crate::ui::slint::FriendItem;
type VoiceMemberItem = crate::ui::slint::VoiceMemberItem;
fn replace_model<T: Clone + PartialEq + 'static>(
current: ModelRc<T>,
items: Vec<T>,
set: impl FnOnce(ModelRc<T>),
) {
if !current.iter().eq(items.iter().cloned()) {
set(Rc::new(VecModel::from(items)).into());
}
}
fn fmt_ts(ts: i64) -> String {
let secs = (ts / 1000).abs() % 86400;
format!("{:02}:{:02}", secs / 3600, (secs % 3600) / 60)
}
fn set_messages(window: &MainWindow, msgs: &[crate::ui::state::ChatMessage], own_user_id: &str) {
fn set_messages(
window: &MainWindow,
state: &UiState,
msgs: &[crate::ui::state::ChatMessage],
own_user_id: &str,
) {
let items: Vec<MessageItem> = msgs
.iter()
.map(|m| MessageItem {
.enumerate()
.map(|(index, m)| MessageItem {
avatar_text: state
.nick_for(&m.sender_id)
.chars()
.take(2)
.collect::<String>()
.into(),
show_author: index == 0
|| msgs[index - 1].sender_id != m.sender_id
|| m.timestamp.saturating_sub(msgs[index - 1].timestamp) > 300_000,
message_id: SharedString::from(m.message_id.as_str()),
sender_id: SharedString::from(m.sender_id.as_str()),
sender_name: SharedString::from(m.sender_id.as_str()),
sender_name: SharedString::from(state.nick_for(&m.sender_id)),
content: SharedString::from(m.content.as_str()),
timestamp: SharedString::from(fmt_ts(m.timestamp)),
is_own: m.sender_id == own_user_id,
edited: m.edited,
reply_to: SharedString::from(m.reply_to.as_deref().unwrap_or("")),
reply_to: SharedString::from(
m.reply_to
.as_ref()
.map(|id| {
msgs.iter()
.find(|other| &other.message_id == id)
.map(|other| {
format!(
"{}: {}",
state.nick_for(&other.sender_id),
other.content.chars().take(90).collect::<String>()
)
})
.unwrap_or_else(|| "Original message unavailable".into())
})
.unwrap_or_default(),
),
})
.collect();
window.set_messages(Rc::new(VecModel::from(items)).into());
replace_model(window.get_messages(), items, |model| {
window.set_messages(model)
});
}
fn set_members_state(window: &MainWindow, state: &UiState, ch_id: &str) {
@ -53,13 +97,16 @@ fn set_members_state(window: &MainWindow, state: &UiState, ch_id: &str) {
.map(|p| p.status.as_str())
.unwrap_or("offline");
MemberItem {
avatar_text: nick.chars().take(2).collect::<String>().into(),
user_id: SharedString::from(user_id),
nickname: SharedString::from(nick.as_str()),
status: SharedString::from(status),
}
})
.collect();
window.set_members(Rc::new(VecModel::from(items)).into());
replace_model(window.get_members(), items, |model| {
window.set_members(model)
});
}
}
@ -79,17 +126,15 @@ fn sync_sidebar(window: &MainWindow, state: &UiState, own_nickname: &str) {
kind: SharedString::from(c.kind.as_str()),
})
.collect();
window.set_channels(Rc::new(VecModel::from(channels)).into());
replace_model(window.get_channels(), channels, |model| {
window.set_channels(model)
});
let guilds: Vec<GuildItem> = state
.guilds
.iter()
.map(|g| {
let short = if g.name.len() >= 2 {
g.name[..2].to_string()
} else {
g.name.clone()
};
let short: String = g.name.chars().take(2).collect();
GuildItem {
guild_id: SharedString::from(g.guild_id.as_str()),
name: SharedString::from(g.name.as_str()),
@ -98,18 +143,27 @@ fn sync_sidebar(window: &MainWindow, state: &UiState, own_nickname: &str) {
}
})
.collect();
window.set_guilds(Rc::new(VecModel::from(guilds)).into());
replace_model(window.get_guilds(), guilds, |model| {
window.set_guilds(model)
});
let dms: Vec<SharedString> = state
let dms: Vec<DmItem> = state
.dm_conversations
.iter()
.map(|c| SharedString::from(c.other_nickname.as_str()))
.map(|c| DmItem {
dm_id: c.dm_id.as_str().into(),
name: c.other_nickname.as_str().into(),
unread: c.unread_count.min(i32::MAX as u32) as i32,
})
.collect();
window.set_dm_conversations(Rc::new(VecModel::from(dms)).into());
replace_model(window.get_dm_conversations(), dms, |model| {
window.set_dm_conversations(model)
});
window.set_username(SharedString::from(own_nickname));
window.set_user_status(SharedString::from("online"));
window.set_show_dms(guild_id.is_none() && !state.dm_conversations.is_empty());
window.set_show_dms(state.browsing_dms);
window.set_active_dm_id(state.active_dm_id.as_deref().unwrap_or("").into());
window.set_replying_to_message(SharedString::from(
state.replying_to_message.as_deref().unwrap_or(""),
));
@ -128,31 +182,55 @@ fn sync_sidebar(window: &MainWindow, state: &UiState, own_nickname: &str) {
}
fn sync_chat(window: &MainWindow, state: &UiState, own_user_id: &str) {
if let Some(ch_id) = &state.active_channel {
if let Some(ch) = state.channels.iter().find(|c| &c.id == ch_id) {
window.set_active_channel_name(SharedString::from(ch.name.as_str()));
if ch.kind == "voice" {
window.set_show_chat(false);
window.set_show_voice(true);
window.set_show_friends(false);
} else {
window.set_show_chat(true);
window.set_show_voice(false);
window.set_show_friends(false);
if let Some(msgs) = state.messages.get(ch_id) {
set_messages(window, msgs, own_user_id);
} else {
window.set_messages(Rc::new(VecModel::from(vec![])).into());
}
set_members_state(window, state, ch_id);
}
}
} else {
window.set_show_chat(false);
window.set_show_voice(false);
window.set_show_friends(true);
let dm = state
.active_dm_id
.as_ref()
.and_then(|id| state.dm_conversations.iter().find(|c| &c.dm_id == id));
let channel = state
.active_channel
.as_ref()
.and_then(|id| state.channels.iter().find(|c| &c.id == id));
window.set_is_dm(dm.is_some());
// Encryption is not advertised until the transport has a verified nonce/message-ID contract.
window.set_e2ee_enabled(false);
window.set_dm_partner_name(dm.map_or("", |c| c.other_nickname.as_str()).into());
window.set_show_voice(state.showing_voice);
window.set_show_chat(!state.showing_voice && (dm.is_some() || channel.is_some()));
window.set_show_friends(!state.showing_voice && dm.is_none() && channel.is_none());
if let Some(dm) = dm {
window.set_active_channel_name(dm.other_nickname.as_str().into());
set_messages(
window,
state,
state
.dm_messages
.get(&dm.dm_id)
.map(Vec::as_slice)
.unwrap_or_default(),
own_user_id,
);
replace_model(window.get_members(), Vec::<MemberItem>::new(), |m| {
window.set_members(m)
});
} else if let Some(channel) = channel {
window.set_active_channel_name(channel.name.as_str().into());
set_messages(
window,
state,
state
.messages
.get(&channel.id)
.map(Vec::as_slice)
.unwrap_or_default(),
own_user_id,
);
set_members_state(window, state, &channel.id);
}
// User edits are immediately written back through input-edited. Only a real
// state change (navigation/send/edit) should replace the editor contents.
if window.get_input_text().as_str() != state.chat_input {
window.set_input_text(state.chat_input.as_str().into());
}
window.set_input_text(SharedString::from(state.chat_input.as_str()));
}
fn sync_friends(window: &MainWindow, state: &UiState) {
@ -171,7 +249,9 @@ fn sync_friends(window: &MainWindow, state: &UiState) {
),
})
.collect();
window.set_friends(Rc::new(VecModel::from(friends)).into());
replace_model(window.get_friends(), friends, |model| {
window.set_friends(model)
});
let pending: Vec<FriendItem> = state
.pending_friend_requests
@ -182,14 +262,18 @@ fn sync_friends(window: &MainWindow, state: &UiState) {
status: SharedString::from("pending"),
})
.collect();
window.set_pending_requests(Rc::new(VecModel::from(pending)).into());
replace_model(window.get_pending_requests(), pending, |model| {
window.set_pending_requests(model)
});
let blocked: Vec<SharedString> = state
.blocked_users
.iter()
.map(|u| SharedString::from(u.as_str()))
.collect();
window.set_blocked_users(Rc::new(VecModel::from(blocked)).into());
replace_model(window.get_blocked_users(), blocked, |model| {
window.set_blocked_users(model)
});
window.set_friends_active_tab(SharedString::from(state.friends_tab.as_str()));
}
@ -213,13 +297,16 @@ pub fn sync_voice(window: &MainWindow, state: &UiState) {
.map(|(id, _)| id.clone())
.unwrap_or_else(|| nick.clone());
VoiceMemberItem {
avatar_text: nick.chars().take(2).collect::<String>().into(),
user_id: SharedString::from(user_id.clone()),
nickname: SharedString::from(nick.as_str()),
speaking: state.last_remote_speaker_id == user_id,
speaking: state.remote_speaking && state.last_remote_speaker_id == user_id,
}
})
.collect();
window.set_voice_members(Rc::new(VecModel::from(members)).into());
replace_model(window.get_voice_members(), members, |model| {
window.set_voice_members(model)
});
window.set_mic_enabled(state.mic_enabled);
window.set_deafened(state.deafen);
@ -227,7 +314,13 @@ pub fn sync_voice(window: &MainWindow, state: &UiState) {
if let Some(ch) = state.channels.iter().find(|c| &c.id == channel) {
window.set_voice_channel_name(SharedString::from(ch.name.as_str()));
}
} else {
window.set_voice_channel_name("".into());
}
window.set_voice_connected(matches!(
state.voice,
crate::ui::state::VoiceUiState::Active { .. }
));
let banner = match (state.local_speaking, state.remote_speaking) {
(true, true) => "you and others are talking".to_string(),
@ -250,8 +343,9 @@ pub fn sync_connect(window: &MainWindow, state: &UiState) {
address: SharedString::from(bm.address.as_str()),
})
.collect();
window.set_bookmarks(Rc::new(VecModel::from(bookmarks)).into());
window.set_address_input(SharedString::from(state.connect_input.as_str()));
replace_model(window.get_bookmarks(), bookmarks, |model| {
window.set_bookmarks(model)
});
window.set_status_text(SharedString::from(
state.status_msg.as_deref().unwrap_or(""),
));
@ -262,34 +356,25 @@ pub fn sync_connect(window: &MainWindow, state: &UiState) {
window.set_auto_reconnect(state.auto_reconnect);
if let ConnState::Connected { ref node_name, .. } = state.conn {
window.set_node_name(SharedString::from(node_name.as_str()));
} else {
} else if matches!(state.conn, ConnState::Disconnected) {
window.set_node_name(SharedString::from(""));
}
}
pub fn sync_settings(window: &MainWindow, state: &UiState, own_pubkey: &str) {
window.set_identity_pubkey(SharedString::from(own_pubkey));
window.set_vault_enabled(false);
window.set_voice_input_device(SharedString::from(""));
window.set_voice_output_device(SharedString::from(""));
window.set_vault_enabled(state.vault_enabled);
window.set_voice_bitrate(state.voice_bitrate);
window.set_vad_mode(state.vad_mode as i32);
window.set_volume(state.output_volume);
window.set_active_tab(SharedString::from("audio"));
window.set_settings_open(state.settings_open);
window.set_input_device_index(state.mic_device as i32);
window.set_output_device_index(state.output_device as i32);
window.set_passphrase_modal_open(state.settings_passphrase_open);
window.set_passphrase_input(SharedString::from(state.settings_passphrase_input.as_str()));
window.set_passphrase_confirm(SharedString::from(
state.settings_passphrase_confirm.as_str(),
));
window.set_passphrase_error(SharedString::from(
state.settings_passphrase_error.as_deref().unwrap_or(""),
));
window.set_export_modal_open(state.settings_export_open);
window.set_export_pass_input(SharedString::from(state.settings_export_pass.as_str()));
window.set_export_pass_confirm(SharedString::from(state.settings_export_confirm.as_str()));
window.set_export_output(SharedString::from(
state.settings_export_output.as_deref().unwrap_or(""),
));
@ -297,9 +382,6 @@ pub fn sync_settings(window: &MainWindow, state: &UiState, own_pubkey: &str) {
state.settings_export_error.as_deref().unwrap_or(""),
));
window.set_import_modal_open(state.settings_import_open);
window.set_import_keyfile_input(SharedString::from(state.settings_import_input.as_str()));
window.set_import_pass_input(SharedString::from(state.settings_import_pass.as_str()));
window.set_import_error(SharedString::from(
state.settings_import_error.as_deref().unwrap_or(""),
));