Skip to content

Commit 6c60cb5

Browse files
wesbillmanBrain
andauthored
fix(desktop): reserve PTT shortcut only during active huddle (#1315)
Signed-off-by: Wes <[email protected]> Co-authored-by: Brain <21994759fc7a6fa6b965551d35cfd7897d262f2495467f2d78694ddcfa6a5c7e@sprout-oss.stage.blox.sqprod.co>
1 parent 8de7d8f commit 6c60cb5

4 files changed

Lines changed: 138 additions & 90 deletions

File tree

‎desktop/src-tauri/src/huddle/mod.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ pub async fn set_voice_input_mode(
111111
}
112112
}
113113

114+
// A mode switch changes whether the global shortcut should be reserved but
115+
// does not otherwise emit; emit here so the frontend sees the new mode and
116+
// the shortcut registration is re-synced.
117+
state.emit_huddle_state_changed();
118+
114119
Ok(())
115120
}
116121

‎desktop/src-tauri/src/huddle/state.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ impl HuddleState {
218218
pub fn emit_huddle_state(app: &tauri::AppHandle, state: &HuddleState) {
219219
use tauri::Emitter;
220220
let _ = app.emit("huddle-state-changed", state);
221+
// Reserve Ctrl+Space with the OS only while a huddle is live in PTT mode.
222+
crate::ptt_shortcut::sync_registration(app, state);
221223
}
222224

223225
// ── Response types ────────────────────────────────────────────────────────────

‎desktop/src-tauri/src/lib.rs‎

Lines changed: 85 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ mod model_tests;
1313
mod models;
1414
pub mod nostr_convert;
1515
mod prevent_sleep;
16+
mod ptt_shortcut;
1617
mod relay;
1718
mod secret_store;
1819
mod shutdown;
@@ -98,94 +99,98 @@ pub fn run() {
9899
)
99100
.plugin(tauri_plugin_websocket::init())
100101
.plugin(tauri_plugin_dialog::init())
101-
.plugin(tauri_plugin_process::init())
102-
.plugin({
103-
use tauri_plugin_global_shortcut::ShortcutState;
104-
105-
// Generation counter for the release delay task. Incremented on
106-
// every press — a delayed release only fires if the generation
107-
// hasn't changed (i.e. no new press happened during the delay).
108-
// This prevents press→release→press within 200 ms from having
109-
// the first release clobber the second press.
110-
let ptt_press_gen = Arc::new(std::sync::atomic::AtomicU64::new(0));
111-
112-
tauri_plugin_global_shortcut::Builder::new()
113-
.with_handler(move |app, _shortcut, event| {
114-
let state = match app.try_state::<AppState>() {
115-
Some(s) => s,
116-
None => return,
117-
};
118-
119-
// Only act if a huddle is active and mode is PTT.
120-
let (is_ptt_mode, is_active) = match state.huddle_state.lock() {
121-
Ok(hs) => (
122-
hs.voice_input_mode == huddle::VoiceInputMode::PushToTalk,
123-
matches!(
124-
hs.phase,
125-
huddle::HuddlePhase::Connected | huddle::HuddlePhase::Active
126-
),
127-
),
128-
Err(_) => return,
129-
};
102+
.plugin(tauri_plugin_process::init());
103+
104+
// The global-shortcut plugin is omitted from test builds: linking it into
105+
// the lib-test binary makes it fail to load on Windows
106+
// (STATUS_ENTRYPOINT_NOT_FOUND) before any test runs.
107+
#[cfg(not(test))]
108+
let builder = builder.plugin({
109+
use tauri_plugin_global_shortcut::ShortcutState;
110+
111+
// Generation counter for the release delay task. Incremented on
112+
// every press — a delayed release only fires if the generation
113+
// hasn't changed (i.e. no new press happened during the delay).
114+
// This prevents press→release→press within 200 ms from having
115+
// the first release clobber the second press.
116+
let ptt_press_gen = Arc::new(std::sync::atomic::AtomicU64::new(0));
117+
118+
tauri_plugin_global_shortcut::Builder::new()
119+
.with_handler(move |app, _shortcut, event| {
120+
let state = match app.try_state::<AppState>() {
121+
Some(s) => s,
122+
None => return,
123+
};
130124

131-
if !is_ptt_mode || !is_active {
132-
return;
133-
}
125+
// Only act if a huddle is active and mode is PTT.
126+
let (is_ptt_mode, is_active) = match state.huddle_state.lock() {
127+
Ok(hs) => (
128+
hs.voice_input_mode == huddle::VoiceInputMode::PushToTalk,
129+
matches!(
130+
hs.phase,
131+
huddle::HuddlePhase::Connected | huddle::HuddlePhase::Active
132+
),
133+
),
134+
Err(_) => return,
135+
};
134136

135-
match event.state {
136-
ShortcutState::Pressed => {
137-
// Bump generation — invalidates any pending release delay.
138-
ptt_press_gen.fetch_add(1, std::sync::atomic::Ordering::Release);
137+
if !is_ptt_mode || !is_active {
138+
return;
139+
}
139140

140-
if let Ok(hs) = state.huddle_state.lock() {
141-
hs.ptt_active
141+
match event.state {
142+
ShortcutState::Pressed => {
143+
// Bump generation — invalidates any pending release delay.
144+
ptt_press_gen.fetch_add(1, std::sync::atomic::Ordering::Release);
145+
146+
if let Ok(hs) = state.huddle_state.lock() {
147+
hs.ptt_active
148+
.store(true, std::sync::atomic::Ordering::Release);
149+
// Only cancel TTS if it's actually playing — avoids
150+
// a stale cancel flag that drops the next queued message.
151+
if hs.tts_active.load(std::sync::atomic::Ordering::Acquire) {
152+
hs.tts_cancel
142153
.store(true, std::sync::atomic::Ordering::Release);
143-
// Only cancel TTS if it's actually playing — avoids
144-
// a stale cancel flag that drops the next queued message.
145-
if hs.tts_active.load(std::sync::atomic::Ordering::Acquire) {
146-
hs.tts_cancel
147-
.store(true, std::sync::atomic::Ordering::Release);
148-
}
149154
}
150-
// Emit ptt-state=true to the frontend.
151-
// The React side plays the press audio cue on this event
152-
// (Web Audio API via HuddleContext). Rust-side rodio audio
153-
// was considered but rejected: the rodio OutputStream must
154-
// outlive the handler and sharing it across the shortcut
155-
// closure adds lifecycle complexity for marginal gain.
156-
// The React implementation is sufficient and simpler.
157-
let _ = app.emit("ptt-state", true);
158155
}
159-
ShortcutState::Released => {
160-
// Capture generation at release time.
161-
let gen_at_release =
162-
ptt_press_gen.load(std::sync::atomic::Ordering::Acquire);
163-
let gen_arc = Arc::clone(&ptt_press_gen);
164-
let app_handle = app.clone();
165-
// 200 ms release delay — captures the tail of the utterance.
166-
// Only applies if no new press happened during the delay.
167-
tauri::async_runtime::spawn(async move {
168-
tokio::time::sleep(std::time::Duration::from_millis(200)).await;
169-
// Check generation — if it changed, a new press arrived.
170-
if gen_arc.load(std::sync::atomic::Ordering::Acquire)
171-
!= gen_at_release
172-
{
173-
return; // Superseded by a new press.
174-
}
175-
if let Some(state) = app_handle.try_state::<AppState>() {
176-
if let Ok(hs) = state.huddle_state.lock() {
177-
hs.ptt_active
178-
.store(false, std::sync::atomic::Ordering::Release);
179-
}
156+
// Emit ptt-state=true to the frontend.
157+
// The React side plays the press audio cue on this event
158+
// (Web Audio API via HuddleContext). Rust-side rodio audio
159+
// was considered but rejected: the rodio OutputStream must
160+
// outlive the handler and sharing it across the shortcut
161+
// closure adds lifecycle complexity for marginal gain.
162+
// The React implementation is sufficient and simpler.
163+
let _ = app.emit("ptt-state", true);
164+
}
165+
ShortcutState::Released => {
166+
// Capture generation at release time.
167+
let gen_at_release =
168+
ptt_press_gen.load(std::sync::atomic::Ordering::Acquire);
169+
let gen_arc = Arc::clone(&ptt_press_gen);
170+
let app_handle = app.clone();
171+
// 200 ms release delay — captures the tail of the utterance.
172+
// Only applies if no new press happened during the delay.
173+
tauri::async_runtime::spawn(async move {
174+
tokio::time::sleep(std::time::Duration::from_millis(200)).await;
175+
// Check generation — if it changed, a new press arrived.
176+
if gen_arc.load(std::sync::atomic::Ordering::Acquire) != gen_at_release
177+
{
178+
return; // Superseded by a new press.
179+
}
180+
if let Some(state) = app_handle.try_state::<AppState>() {
181+
if let Ok(hs) = state.huddle_state.lock() {
182+
hs.ptt_active
183+
.store(false, std::sync::atomic::Ordering::Release);
180184
}
181-
// Emit ptt-state=false — React plays the release audio cue.
182-
let _ = app_handle.emit("ptt-state", false);
183-
});
184-
}
185+
}
186+
// Emit ptt-state=false — React plays the release audio cue.
187+
let _ = app_handle.emit("ptt-state", false);
188+
});
185189
}
186-
})
187-
.build()
188-
});
190+
}
191+
})
192+
.build()
193+
});
189194

190195
// Only register the updater in release builds that were compiled with a
191196
// real updater configuration. Local unsigned builds omit that config and
@@ -326,16 +331,6 @@ pub fn run() {
326331
mgr.start_tts_download(state.http_client.clone());
327332
}
328333

329-
// Non-fatal: huddle works without the shortcut (user can switch to VAD mode).
330-
#[cfg(desktop)]
331-
{
332-
use tauri_plugin_global_shortcut::{Code, GlobalShortcutExt, Modifiers, Shortcut};
333-
let shortcut = Shortcut::new(Some(Modifiers::CONTROL), Code::Space);
334-
if let Err(e) = app.handle().global_shortcut().register(shortcut) {
335-
eprintln!("buzz-desktop: failed to register PTT shortcut: {e}");
336-
}
337-
}
338-
339334
// Handle deep link URLs received while the app is running (macOS)
340335
// and on cold start. The single-instance plugin handles forwarding
341336
// from duplicate launches on Windows/Linux.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//! Push-to-talk global shortcut lifecycle.
2+
//!
3+
//! `Ctrl+Space` is only reserved with the OS while a huddle is connected and
4+
//! the voice input mode is push-to-talk. Reserving it for the whole app
5+
//! lifetime conflicts with IDEs and other apps, so registration is synced to
6+
//! huddle state instead.
7+
8+
use crate::huddle::HuddleState;
9+
#[cfg(not(test))]
10+
use crate::huddle::{HuddlePhase, VoiceInputMode};
11+
12+
/// Whether the PTT shortcut should currently be reserved with the OS.
13+
#[cfg(not(test))]
14+
fn should_register(hs: &HuddleState) -> bool {
15+
hs.voice_input_mode == VoiceInputMode::PushToTalk
16+
&& matches!(hs.phase, HuddlePhase::Connected | HuddlePhase::Active)
17+
}
18+
19+
/// Register or unregister the PTT shortcut to match the given huddle state.
20+
///
21+
/// Idempotent and best-effort: failures are logged, never fatal — the huddle
22+
/// still works in VAD mode without the shortcut.
23+
#[cfg(not(test))]
24+
pub fn sync_registration(app: &tauri::AppHandle, hs: &HuddleState) {
25+
use tauri_plugin_global_shortcut::{Code, GlobalShortcutExt, Modifiers, Shortcut};
26+
27+
let shortcut = Shortcut::new(Some(Modifiers::CONTROL), Code::Space);
28+
let manager = app.global_shortcut();
29+
let want = should_register(hs);
30+
let is_registered = manager.is_registered(shortcut);
31+
32+
if want && !is_registered {
33+
if let Err(e) = manager.register(shortcut) {
34+
eprintln!("buzz-desktop: failed to register PTT shortcut: {e}");
35+
}
36+
} else if !want && is_registered {
37+
if let Err(e) = manager.unregister(shortcut) {
38+
eprintln!("buzz-desktop: failed to unregister PTT shortcut: {e}");
39+
}
40+
}
41+
}
42+
43+
/// Test builds omit the global-shortcut plugin (see `run()`), so syncing is a
44+
/// no-op — calling the plugin would panic without it installed.
45+
#[cfg(test)]
46+
pub fn sync_registration(_app: &tauri::AppHandle, _hs: &HuddleState) {}

0 commit comments

Comments
 (0)