diff --git a/src/main.rs b/src/main.rs index b2e4051..6f91cf8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -275,17 +275,19 @@ fn wire_signals(window: &MainWindow, state: &Rc>) { s.ui.channels .iter() .any(|c| c.id == id && c.kind == "voice"); - if is_voice { - s.ui.showing_voice = true; - s.ui.status_msg = Some("Joining voice channel…".into()); + let send_join = if is_voice { + s.ui.request_voice_join(&id) } else { s.ui.select_text_channel(id.clone()); - } + true + }; let net = s.net.clone(); drop(s); - tokio::spawn(async move { - net.send(NetCommand::JoinChannel { channel_id: id }).await; - }); + if send_join { + tokio::spawn(async move { + net.send(NetCommand::JoinChannel { channel_id: id }).await; + }); + } }); } diff --git a/src/net/session/handlers/voice.rs b/src/net/session/handlers/voice.rs index 9be37cc..a68bf2d 100644 --- a/src/net/session/handlers/voice.rs +++ b/src/net/session/handlers/voice.rs @@ -43,7 +43,7 @@ impl State { if source != state.endpoint { // A relay answering from another address looks exactly like silence. rejected += 1; - if rejected == 1 || rejected % 500 == 0 { + if rejected == 1 || rejected.is_multiple_of(500) { tracing::warn!( %source, expected = %state.endpoint, @@ -58,7 +58,7 @@ impl State { Ok(decoded) => decoded, Err(error) => { rejected += 1; - if rejected == 1 || rejected % 500 == 0 { + if rejected == 1 || rejected.is_multiple_of(500) { tracing::warn!(%error, rejected, "voice datagram rejected"); } continue; diff --git a/src/ui/bridge.rs b/src/ui/bridge.rs index e942eed..d0dc29c 100644 --- a/src/ui/bridge.rs +++ b/src/ui/bridge.rs @@ -338,10 +338,9 @@ pub fn sync_voice(window: &MainWindow, state: &UiState) { } else { window.set_voice_channel_name("".into()); } - window.set_voice_connected(matches!( - state.voice, - crate::ui::state::VoiceUiState::Active { .. } - )); + let status = state.voice_status(); + window.set_voice_connected(status == crate::ui::state::VoiceStatus::Connected); + window.set_voice_joining(status == crate::ui::state::VoiceStatus::Joining); let banner = match (state.local_speaking, state.remote_speaking) { (true, true) => crate::ui::i18n::text("You and others are talking"), diff --git a/src/ui/slint/main.slint b/src/ui/slint/main.slint index 7602e09..2878e2f 100644 --- a/src/ui/slint/main.slint +++ b/src/ui/slint/main.slint @@ -34,6 +34,7 @@ export component MainWindow inherits Window { private property pending-delete-id; in-out property active-dm-id; in-out property voice-connected; + in-out property voice-joining; in-out property members-visible: true; in-out property <[string]> input-devices; in-out property <[string]> output-devices; @@ -286,6 +287,7 @@ export component MainWindow inherits Window { deafened: root.deafened; channel-name: root.voice-channel-name; activity-banner: root.activity-banner; + joining: root.voice-joining; push-to-talk: root.vad-mode == 0; ptt-held(held) => { root.ptt-held(held); } toggle-mic => { root.toggle-mic(); } diff --git a/src/ui/slint/voice.slint b/src/ui/slint/voice.slint index 5ab5729..e830adf 100644 --- a/src/ui/slint/voice.slint +++ b/src/ui/slint/voice.slint @@ -8,6 +8,7 @@ export component VoicePanel inherits Rectangle { in-out property deafened; in-out property channel-name; in-out property activity-banner; + in property joining; in-out property push-to-talk; callback preview-screen(); callback ptt-held(bool); @@ -53,7 +54,7 @@ export component VoicePanel inherits Rectangle { Rectangle { horizontal-stretch: 1; } } } - if root.members.length == 0 : Text { text: @tr("Joining voice…"); color: Theme.text-muted; font-size: Math.max(11px, Theme.font-size * 0.875); } + if root.joining : Text { text: @tr("Joining voice…"); color: Theme.text-muted; font-size: Math.max(11px, Theme.font-size * 0.875); } Rectangle { vertical-stretch: 1; } } } diff --git a/src/ui/state/mod.rs b/src/ui/state/mod.rs index 86f317d..82ab5ca 100644 --- a/src/ui/state/mod.rs +++ b/src/ui/state/mod.rs @@ -2,9 +2,11 @@ mod default; mod models; mod navigation; mod types; +mod voice; pub use models::{Channel, ChatMessage, DmConversation, FriendState, GuildState, NodeBookmark}; pub use types::{ConnState, TrustPrompt, TrustPromptKind, UiState, VoiceUiState}; +pub use voice::VoiceStatus; use models::bookmark_label_from_address; diff --git a/src/ui/state/types.rs b/src/ui/state/types.rs index bdef68d..8e1c2c1 100644 --- a/src/ui/state/types.rs +++ b/src/ui/state/types.rs @@ -5,6 +5,10 @@ use std::time::Instant; pub enum VoiceUiState { #[default] Off, + /// A join was requested; audio is not running yet. + Joining { + channel: String, + }, Active { channel: String, }, diff --git a/src/ui/state/voice.rs b/src/ui/state/voice.rs new file mode 100644 index 0000000..a3404d5 --- /dev/null +++ b/src/ui/state/voice.rs @@ -0,0 +1,126 @@ +//! Voice connection status: one state machine feeds the status bar, the sidebar and the +//! voice page, so they can never disagree. +use super::{ConnState, UiState, VoiceUiState}; + +/// What the interface shows about the voice connection. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum VoiceStatus { + Off, + /// A join was requested and audio is not running yet. + Joining, + /// Audio is running in the voice room. + Connected, + Failed(String), +} + +impl UiState { + /// Derives the displayed voice status from the connection and voice state. + pub fn voice_status(&self) -> VoiceStatus { + if !matches!(self.conn, ConnState::Connected { .. }) { + return VoiceStatus::Off; + } + match &self.voice { + VoiceUiState::Off => VoiceStatus::Off, + VoiceUiState::Joining { .. } => VoiceStatus::Joining, + VoiceUiState::Active { .. } => VoiceStatus::Connected, + VoiceUiState::Error(message) => VoiceStatus::Failed(message.clone()), + } + } + + /// Handles a click on a voice channel. Returns true when a join request must be sent to + /// the server. Clicking the room you are already in only shows it: the server does not + /// answer a repeated join, so waiting for a reply would leave "joining" on screen forever. + pub fn request_voice_join(&mut self, channel_id: &str) -> bool { + self.showing_voice = true; + let current = match &self.voice { + VoiceUiState::Joining { channel } | VoiceUiState::Active { channel } => { + Some(channel.as_str()) + } + _ => None, + }; + if current == Some(channel_id) { + return false; + } + self.voice = VoiceUiState::Joining { + channel: channel_id.to_string(), + }; + true + } + + /// The server refused or lost a pending join: stop showing "joining". + pub fn cancel_pending_voice_join(&mut self) { + if matches!(self.voice, VoiceUiState::Joining { .. }) { + self.voice = VoiceUiState::Off; + self.voice_target = None; + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn connected() -> UiState { + UiState { + conn: ConnState::Connected { + session_id: "s".into(), + node_name: "n".into(), + }, + ..UiState::default() + } + } + + #[test] + fn joining_becomes_connected_only_when_audio_is_active() { + let mut ui = connected(); + assert_eq!(ui.voice_status(), VoiceStatus::Off); + assert!(ui.request_voice_join("lounge")); + assert_eq!(ui.voice_status(), VoiceStatus::Joining); + ui.voice = VoiceUiState::Active { + channel: "lounge".into(), + }; + assert_eq!(ui.voice_status(), VoiceStatus::Connected); + } + + #[test] + fn clicking_the_current_room_never_restarts_joining() { + let mut ui = connected(); + ui.voice = VoiceUiState::Active { + channel: "lounge".into(), + }; + ui.showing_voice = false; + assert!(!ui.request_voice_join("lounge")); + assert!(ui.showing_voice); + assert_eq!(ui.voice_status(), VoiceStatus::Connected); + // A different room is a real join. + assert!(ui.request_voice_join("other")); + assert_eq!(ui.voice_status(), VoiceStatus::Joining); + } + + #[test] + fn retry_after_failure_and_cancel_of_a_refused_join() { + let mut ui = connected(); + ui.voice = VoiceUiState::Error("no mic".into()); + assert_eq!(ui.voice_status(), VoiceStatus::Failed("no mic".into())); + assert!(ui.request_voice_join("lounge")); + ui.cancel_pending_voice_join(); + assert_eq!(ui.voice_status(), VoiceStatus::Off); + // Cancelling does not touch an established connection. + ui.voice = VoiceUiState::Active { + channel: "lounge".into(), + }; + ui.cancel_pending_voice_join(); + assert_eq!(ui.voice_status(), VoiceStatus::Connected); + } + + #[test] + fn status_is_off_while_the_control_connection_is_down() { + let ui = UiState { + voice: VoiceUiState::Active { + channel: "lounge".into(), + }, + ..UiState::default() + }; + assert_eq!(ui.voice_status(), VoiceStatus::Off); + } +} diff --git a/src/ui/update.rs b/src/ui/update.rs index 3e0f1ac..0fc96ff 100644 --- a/src/ui/update.rs +++ b/src/ui/update.rs @@ -612,6 +612,7 @@ fn chat_handle(ui: &mut UiState, net: &NetHandle, event: NetEvent) { ui.scroll_bottom = true; } NetEvent::Error { message, .. } => { + ui.cancel_pending_voice_join(); if message.starts_with("media:") { crate::ui::media::on_error(&message); } @@ -931,6 +932,8 @@ pub fn sync_voice_audio(state: &mut AppState) { let active_voice = state.ui.voice_target.clone(); match active_voice { Some(id) => start_voice(state, &id), + // A pending join has no target until the server acknowledges it. + None if matches!(state.ui.voice, VoiceUiState::Joining { .. }) => {} None => stop_voice(state), } }