@@ -13,6 +13,7 @@ mod model_tests;
1313mod models;
1414pub mod nostr_convert;
1515mod prevent_sleep;
16+ mod ptt_shortcut;
1617mod relay;
1718mod secret_store;
1819mod 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.
0 commit comments