Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 24 additions & 17 deletions src-tauri/src/shortcut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -842,25 +842,32 @@ pub fn register_shortcut(app: &AppHandle, binding: ShortcutBinding) -> Result<()
action.stop(ah, &binding_id_for_closure, &shortcut_string);
}
} else {
// Toggle mode: toggle on press only
if event.state == ShortcutState::Pressed {
let toggle_state_manager = ah.state::<ManagedToggleState>();

let mut states = toggle_state_manager.lock().expect("Failed to lock toggle state manager");

let is_currently_active = states.active_toggles
.entry(binding_id_for_closure.clone())
.or_insert(false);

if *is_currently_active {
action.stop(
ah,
&binding_id_for_closure,
&shortcut_string,
);
*is_currently_active = false; // Update state to inactive
} else {
// Determine action and update state while holding the lock,
// but RELEASE the lock before calling the action to avoid deadlocks.
// (Actions may need to acquire the lock themselves, e.g., cancel_current_operation)
let should_start: bool;
{
let toggle_state_manager = ah.state::<ManagedToggleState>();
let mut states = toggle_state_manager
.lock()
.expect("Failed to lock toggle state manager");

let is_currently_active = states
.active_toggles
.entry(binding_id_for_closure.clone())
.or_insert(false);

should_start = !*is_currently_active;
*is_currently_active = should_start;
} // Lock released here

// Now call the action without holding the lock
if should_start {
action.start(ah, &binding_id_for_closure, &shortcut_string);
*is_currently_active = true; // Update state to active
} else {
action.stop(ah, &binding_id_for_closure, &shortcut_string);
}
}
}
Expand Down
56 changes: 32 additions & 24 deletions src-tauri/src/signal_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,40 @@ pub fn setup_signal_handler(app_handle: AppHandle, mut signals: Signals) {
let shortcut_string = "SIGUSR2";

if let Some(action) = ACTION_MAP.get(binding_id) {
let toggle_state_manager =
app_handle_for_signal.state::<ManagedToggleState>();

let mut states = match toggle_state_manager.lock() {
Ok(s) => s,
Err(e) => {
warn!("Failed to lock toggle state manager: {e}");
continue;
}
};

let is_currently_active = states
.active_toggles
.entry(binding_id.to_string())
.or_insert(false);

if *is_currently_active {
debug!("SIGUSR2: Stopping transcription (currently active)");
action.stop(&app_handle_for_signal, binding_id, shortcut_string);
*is_currently_active = false; // Update state to inactive
debug!("SIGUSR2: Transcription stopped");
} else {
debug!("SIGUSR2: Starting transcription (currently inactive)");
// Determine action and update state while holding the lock,
// but RELEASE the lock before calling the action to avoid deadlocks.
// (Actions may need to acquire the lock themselves, e.g., cancel_current_operation)
let should_start: bool;
{
let toggle_state_manager =
app_handle_for_signal.state::<ManagedToggleState>();

let mut states = match toggle_state_manager.lock() {
Ok(s) => s,
Err(e) => {
warn!("Failed to lock toggle state manager: {e}");
continue;
}
};

let is_currently_active = states
.active_toggles
.entry(binding_id.to_string())
.or_insert(false);

should_start = !*is_currently_active;
*is_currently_active = should_start;
} // Lock released here

// Now call the action without holding the lock
if should_start {
debug!("SIGUSR2: Starting transcription (was inactive)");
action.start(&app_handle_for_signal, binding_id, shortcut_string);
*is_currently_active = true; // Update state to active
info!("SIGUSR2: Transcription started");
} else {
debug!("SIGUSR2: Stopping transcription (was active)");
action.stop(&app_handle_for_signal, binding_id, shortcut_string);
debug!("SIGUSR2: Transcription stopped");
}
} else {
warn!("No action defined in ACTION_MAP for binding ID '{binding_id}'");
Expand Down