From 3253357eda1753ad44741caa8304d5300b6ff2d8 Mon Sep 17 00:00:00 2001 From: faumaray Date: Wed, 28 Oct 2026 10:10:29 +0300 Subject: [PATCH] voelin-gpui: the release guard of Hold to talk The risk slice 2 recorded: a key or the button held while voice drops leaves transmit on. Two cases, two guards. Voice that drops: the voice card, with the button, goes with it and nothing is left to release, and the session keeps `transmitting` into the next connection; after each batch of engine events the root state tells the session SetTransmitting(false) once for every session that was connected before it and is not after it and still says it transmits (the engine's own report, so no flag of ours can go stale; it does not answer the engine's answer, nor a reconnect). The sidebar's own hold is cleared by its observer of the state, which sets its own field and sends nothing. A window that loses the focus while the button is held: the mouse button comes up elsewhere, so MainView's window-activation observer lets the button go and the session is told. A window that holds nothing sends nothing, since the global hotkey is meant for use while another window has the focus. Tests: both guards (and that each fails without its guard, which was checked by removing it), the hold that is not one, nothing sent for a window that held nothing. Co-Authored-By: Claude Sonnet 5.5 --- crates/voelin-gpui/src/app.rs | 29 +++++ crates/voelin-gpui/src/shell/sidebar.rs | 14 +++ crates/voelin-gpui/src/view.rs | 13 +++ crates/voelin-gpui/src/view/voice_tests.rs | 123 ++++++++++++++++++++- 4 files changed, 178 insertions(+), 1 deletion(-) diff --git a/crates/voelin-gpui/src/app.rs b/crates/voelin-gpui/src/app.rs index 8ff616b..7528c78 100644 --- a/crates/voelin-gpui/src/app.rs +++ b/crates/voelin-gpui/src/app.rs @@ -495,18 +495,47 @@ impl AppState { cx: &mut Context, ) { let mut refresh = Refresh::default(); + let connected = self.connected_sessions(); for event in events { self.events_handled += 1; self.counters.engine_events.incr(); let update = self.handle(event); refresh.merge(self.perform(update, cx)); } + self.release_lost_transmit(&connected); self.counters.connections.set(self.connections() as u64); // Each area once for the whole batch. self.signals.raise(&refresh, cx); cx.notify(); } + /// The sessions whose voice is connected now. + fn connected_sessions(&self) -> Vec { + self.vm + .sessions + .iter() + .filter(|(_, view)| view.state.voice == VoiceState::Connected) + .map(|(&id, _)| id) + .collect() + } + + /// The release guard of Hold to talk, for voice that dropped: the key or + /// the button that was held when the connection went has nothing to be + /// released on (the voice card, with the button, goes with the voice, and + /// the session keeps `transmitting` until the next press), so the session + /// is told to stop transmitting once, for each session that was connected + /// before a batch of events and is not after it, if it still says it + /// transmits. The engine answers with a state without it, which this does + /// not answer again. + fn release_lost_transmit(&self, were_connected: &[i64]) { + for &id in were_connected { + let Some(view) = self.vm.sessions.get(&id) else { continue }; + if view.state.voice != VoiceState::Connected && view.state.transmitting { + self.send(Command::SetTransmitting { session: id as u64, on: false }); + } + } + } + /// One engine event ([`AppState::handle_events`]). pub fn handle_event(&mut self, event: Event, cx: &mut Context) { self.handle_events([event], cx); diff --git a/crates/voelin-gpui/src/shell/sidebar.rs b/crates/voelin-gpui/src/shell/sidebar.rs index 4cff26b..4978f95 100644 --- a/crates/voelin-gpui/src/shell/sidebar.rs +++ b/crates/voelin-gpui/src/shell/sidebar.rs @@ -110,6 +110,13 @@ impl Sidebar { let subscriptions = signals.watch(app, &[Area::Servers, Area::Toolbar], cx, |this: &mut Self, app, cx| { let view = SidebarView::of(app.read(cx)); + // The button goes with the voice card when voice leaves `Connected`, + // and the release has no element to land on: a hold that was + // started is over (the session is told by the root state, not by + // this observer, which only sets its own fields). + if !view.voice_connected { + this.holding = false; + } if view != this.view { this.view = view; cx.notify(); @@ -123,6 +130,13 @@ impl Sidebar { &self.view } + /// Let go of the hold-to-talk button if it is held (the window lost the + /// focus: the release will not come to it): whether it was held. The caller + /// tells the session. + pub fn release(&mut self) -> bool { + std::mem::take(&mut self.holding) + } + /// The hold-to-talk button was released, if it was held. fn let_go(&mut self, _: &MouseUpEvent, window: &mut Window, cx: &mut Context) { if std::mem::take(&mut self.holding) { diff --git a/crates/voelin-gpui/src/view.rs b/crates/voelin-gpui/src/view.rs index fc55ea7..ad516d3 100644 --- a/crates/voelin-gpui/src/view.rs +++ b/crates/voelin-gpui/src/view.rs @@ -143,6 +143,19 @@ impl MainView { cx.notify(); } }), + // The release guard of Hold to talk for a window that loses the focus + // while the button is held: the mouse button comes up somewhere else, + // and the release never reaches the button. Only the window's own + // button: the global hotkey is for use while another window has the + // focus. + cx.observe_window_activation(window, |this, window, cx| { + if window.is_window_active() { + return; + } + if this.sidebar.update(cx, |sidebar, _| sidebar.release()) { + this.app.update(cx, |app, cx| app.push_to_talk(false, cx)); + } + }), cx.observe_window_bounds(window, |this, window, cx| { report(&this.app, window, cx); // A cached pane renders only when told: it is told when the window diff --git a/crates/voelin-gpui/src/view/voice_tests.rs b/crates/voelin-gpui/src/view/voice_tests.rs index 8fd2925..b13eaf5 100644 --- a/crates/voelin-gpui/src/view/voice_tests.rs +++ b/crates/voelin-gpui/src/view/voice_tests.rs @@ -8,7 +8,7 @@ use std::sync::Arc; use gpui::{TestAppContext, VisualTestContext}; -use gpui_kit::{px, size}; +use gpui_kit::{MouseButton, px, size}; use voelin_core::{Command, Event, SessionState, VoiceState}; use voelin_ui_vm::bridge::MemberItem; use voelin_ui_vm::dev::logic as sample; @@ -359,3 +359,124 @@ fn the_server_card_shows_the_banner(cx: &mut TestAppContext) { shell.app.update(cx, |app, cx| app.shutdown(cx)); assert!(!folder.is_some_and(|folder| folder.exists()), "the close removes the sample pictures"); } + +/// What the engine reports for the sample session, in our channel. +fn reported(voice: VoiceState, transmitting: bool) -> Event { + Event::State { + session: sample::DEMO as u64, + state: SessionState { + voice, + own_channel: Some(2), + own_client: Some(1), + transmitting, + ..Default::default() + }, + } +} + +/// What a test that sent no command compares with. +const NOTHING: [bool; 0] = []; + +/// What the commands sent since the last call say about our own session's +/// transmitting, in order (the connection's own commands, such as the channel +/// chat it opens, are not what these tests are about). +fn transmits(shell: &Shell, cx: &mut VisualTestContext) -> Vec { + let id = sample::DEMO as u64; + shell + .app + .read_with(cx, |app, _| app.take_sent()) + .into_iter() + .filter_map(|command| match command { + Command::SetTransmitting { session, on } if session == id => Some(on), + _ => None, + }) + .collect() +} + +/// Hold to talk is released when voice drops under it: the button goes with +/// the voice card, nothing is left to release it, and the session would go on +/// transmitting into the next connection. The guard tells the session once, +/// when the batch that took the voice away says it still transmits, and not +/// again for the engine's answer or for a reconnect. +#[gpui::test] +fn hold_to_talk_is_released_when_voice_drops(cx: &mut TestAppContext) { + let (shell, cx) = tall(cx, Switches::default()); + let button = at(cx, "hold-to-talk").expect("push-to-talk is the default transmit mode"); + transmits(&shell, cx); + cx.simulate_mouse_down(button.center(), MouseButton::Left, Default::default()); + assert_eq!(transmits(&shell, cx), [true], "the button was pressed"); + + // The engine confirms it; still connected: nothing to guard. + shell.events.send(reported(VoiceState::Connected, true)).unwrap(); + frame(cx); + assert_eq!(transmits(&shell, cx), NOTHING, "a connected session transmitting is a hold"); + + // The voice drops with the button still down: the session says it transmits. + shell.events.send(reported(VoiceState::Disconnected, true)).unwrap(); + frame(cx); + assert_eq!(transmits(&shell, cx), [false], "told once"); + assert!(at(cx, "hold-to-talk").is_none(), "the button went with the voice card"); + + // The engine's answer and the reconnect are not answered. + shell.events.send(reported(VoiceState::Disconnected, false)).unwrap(); + shell.events.send(reported(VoiceState::Connecting, false)).unwrap(); + shell.events.send(reported(VoiceState::Connected, false)).unwrap(); + frame(cx); + assert_eq!(transmits(&shell, cx), NOTHING, "nothing more"); + + // The mouse button comes up where the button was: nothing was held any more. + cx.simulate_mouse_up(button.center(), MouseButton::Left, Default::default()); + assert_eq!(transmits(&shell, cx), NOTHING); +} + +/// A session that drops while it does not transmit is left alone. +#[gpui::test] +fn a_voice_that_drops_without_a_hold_is_not_told_anything(cx: &mut TestAppContext) { + let (shell, cx) = tall(cx, Switches::default()); + transmits(&shell, cx); + shell.events.send(reported(VoiceState::Disconnected, false)).unwrap(); + frame(cx); + assert_eq!(transmits(&shell, cx), NOTHING); +} + +/// Hold to talk is released when the window loses the focus while the button is +/// held: the mouse button comes up elsewhere, and the release never reaches the +/// button. +#[gpui::test] +fn hold_to_talk_is_released_when_the_window_loses_the_focus(cx: &mut TestAppContext) { + let (shell, cx) = tall(cx, Switches::default()); + cx.update(|window, _| window.activate_window()); + cx.run_until_parked(); + cx.update(|window, _| assert!(window.is_window_active(), "the window has the focus")); + transmits(&shell, cx); + let button = at(cx, "hold-to-talk").expect("the button"); + cx.simulate_mouse_down(button.center(), MouseButton::Left, Default::default()); + assert_eq!(transmits(&shell, cx), [true]); + cx.deactivate_window(); + frame(cx); + cx.update(|window, _| assert!(!window.is_window_active(), "the window lost the focus")); + assert_eq!(transmits(&shell, cx), [false], "released with the focus"); + cx.simulate_mouse_up(button.center(), MouseButton::Left, Default::default()); + assert_eq!( + transmits(&shell, cx), + NOTHING, + "and the release that comes later has nothing to do" + ); +} + +/// A window that held nothing sends nothing when it loses the focus: what +/// transmits then is not the window's button (the global hotkey is for use +/// while another window has the focus). +#[gpui::test] +fn a_window_that_holds_nothing_tells_the_session_nothing_when_it_loses_the_focus( + cx: &mut TestAppContext, +) { + let (shell, cx) = tall(cx, Switches::default()); + cx.update(|window, _| window.activate_window()); + cx.run_until_parked(); + transmits(&shell, cx); + cx.deactivate_window(); + frame(cx); + cx.update(|window, _| assert!(!window.is_window_active(), "the window lost the focus")); + assert_eq!(transmits(&shell, cx), NOTHING); +}