fix(client): derive voice status from a single state machine

The status bar and voice page kept saying the room was being joined while
the sidebar already reported a connected voice. Clicking the room you were
already in set a free-text 'joining' message that nothing cleared, because
the server does not answer a repeated join, and the voice page showed its
joining label whenever the member list was empty.

Joining is now an explicit voice state. The sidebar, the voice page and the
status text all read one derived status; a repeated click only shows the
room, and a server error cancels a pending join.
This commit is contained in:
Loki 2026-10-25 09:50:11 +01:00
parent 69d108b090
commit 7d488faf59
Signed by: boba
GPG key ID: 253067914055423B
9 changed files with 153 additions and 14 deletions

View file

@ -275,17 +275,19 @@ fn wire_signals(window: &MainWindow, state: &Rc<RefCell<AppState>>) {
s.ui.channels s.ui.channels
.iter() .iter()
.any(|c| c.id == id && c.kind == "voice"); .any(|c| c.id == id && c.kind == "voice");
if is_voice { let send_join = if is_voice {
s.ui.showing_voice = true; s.ui.request_voice_join(&id)
s.ui.status_msg = Some("Joining voice channel…".into());
} else { } else {
s.ui.select_text_channel(id.clone()); s.ui.select_text_channel(id.clone());
} true
};
let net = s.net.clone(); let net = s.net.clone();
drop(s); drop(s);
tokio::spawn(async move { if send_join {
net.send(NetCommand::JoinChannel { channel_id: id }).await; tokio::spawn(async move {
}); net.send(NetCommand::JoinChannel { channel_id: id }).await;
});
}
}); });
} }

View file

@ -43,7 +43,7 @@ impl State {
if source != state.endpoint { if source != state.endpoint {
// A relay answering from another address looks exactly like silence. // A relay answering from another address looks exactly like silence.
rejected += 1; rejected += 1;
if rejected == 1 || rejected % 500 == 0 { if rejected == 1 || rejected.is_multiple_of(500) {
tracing::warn!( tracing::warn!(
%source, %source,
expected = %state.endpoint, expected = %state.endpoint,
@ -58,7 +58,7 @@ impl State {
Ok(decoded) => decoded, Ok(decoded) => decoded,
Err(error) => { Err(error) => {
rejected += 1; rejected += 1;
if rejected == 1 || rejected % 500 == 0 { if rejected == 1 || rejected.is_multiple_of(500) {
tracing::warn!(%error, rejected, "voice datagram rejected"); tracing::warn!(%error, rejected, "voice datagram rejected");
} }
continue; continue;

View file

@ -338,10 +338,9 @@ pub fn sync_voice(window: &MainWindow, state: &UiState) {
} else { } else {
window.set_voice_channel_name("".into()); window.set_voice_channel_name("".into());
} }
window.set_voice_connected(matches!( let status = state.voice_status();
state.voice, window.set_voice_connected(status == crate::ui::state::VoiceStatus::Connected);
crate::ui::state::VoiceUiState::Active { .. } window.set_voice_joining(status == crate::ui::state::VoiceStatus::Joining);
));
let banner = match (state.local_speaking, state.remote_speaking) { let banner = match (state.local_speaking, state.remote_speaking) {
(true, true) => crate::ui::i18n::text("You and others are talking"), (true, true) => crate::ui::i18n::text("You and others are talking"),

View file

@ -34,6 +34,7 @@ export component MainWindow inherits Window {
private property <string> pending-delete-id; private property <string> pending-delete-id;
in-out property <string> active-dm-id; in-out property <string> active-dm-id;
in-out property <bool> voice-connected; in-out property <bool> voice-connected;
in-out property <bool> voice-joining;
in-out property <bool> members-visible: true; in-out property <bool> members-visible: true;
in-out property <[string]> input-devices; in-out property <[string]> input-devices;
in-out property <[string]> output-devices; in-out property <[string]> output-devices;
@ -286,6 +287,7 @@ export component MainWindow inherits Window {
deafened: root.deafened; deafened: root.deafened;
channel-name: root.voice-channel-name; channel-name: root.voice-channel-name;
activity-banner: root.activity-banner; activity-banner: root.activity-banner;
joining: root.voice-joining;
push-to-talk: root.vad-mode == 0; push-to-talk: root.vad-mode == 0;
ptt-held(held) => { root.ptt-held(held); } ptt-held(held) => { root.ptt-held(held); }
toggle-mic => { root.toggle-mic(); } toggle-mic => { root.toggle-mic(); }

View file

@ -8,6 +8,7 @@ export component VoicePanel inherits Rectangle {
in-out property <bool> deafened; in-out property <bool> deafened;
in-out property <string> channel-name; in-out property <string> channel-name;
in-out property <string> activity-banner; in-out property <string> activity-banner;
in property <bool> joining;
in-out property <bool> push-to-talk; in-out property <bool> push-to-talk;
callback preview-screen(); callback preview-screen();
callback ptt-held(bool); callback ptt-held(bool);
@ -53,7 +54,7 @@ export component VoicePanel inherits Rectangle {
Rectangle { horizontal-stretch: 1; } 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; } Rectangle { vertical-stretch: 1; }
} }
} }

View file

@ -2,9 +2,11 @@ mod default;
mod models; mod models;
mod navigation; mod navigation;
mod types; mod types;
mod voice;
pub use models::{Channel, ChatMessage, DmConversation, FriendState, GuildState, NodeBookmark}; pub use models::{Channel, ChatMessage, DmConversation, FriendState, GuildState, NodeBookmark};
pub use types::{ConnState, TrustPrompt, TrustPromptKind, UiState, VoiceUiState}; pub use types::{ConnState, TrustPrompt, TrustPromptKind, UiState, VoiceUiState};
pub use voice::VoiceStatus;
use models::bookmark_label_from_address; use models::bookmark_label_from_address;

View file

@ -5,6 +5,10 @@ use std::time::Instant;
pub enum VoiceUiState { pub enum VoiceUiState {
#[default] #[default]
Off, Off,
/// A join was requested; audio is not running yet.
Joining {
channel: String,
},
Active { Active {
channel: String, channel: String,
}, },

126
src/ui/state/voice.rs Normal file
View file

@ -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);
}
}

View file

@ -612,6 +612,7 @@ fn chat_handle(ui: &mut UiState, net: &NetHandle, event: NetEvent) {
ui.scroll_bottom = true; ui.scroll_bottom = true;
} }
NetEvent::Error { message, .. } => { NetEvent::Error { message, .. } => {
ui.cancel_pending_voice_join();
if message.starts_with("media:") { if message.starts_with("media:") {
crate::ui::media::on_error(&message); 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(); let active_voice = state.ui.voice_target.clone();
match active_voice { match active_voice {
Some(id) => start_voice(state, &id), 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), None => stop_voice(state),
} }
} }