Skip to content

Commit 5044a98

Browse files
authored
fix: app hotkey hanlder invoked twice (key pressed and released) (#531)
1 parent 7216581 commit 5044a98

1 file changed

Lines changed: 25 additions & 26 deletions

File tree

src-tauri/src/local/application/with_feature.rs

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ use std::path::PathBuf;
3030
use tauri::{async_runtime, AppHandle, Manager, Runtime};
3131
use tauri_plugin_fs_pro::{icon, metadata, name, IconOptions};
3232
use tauri_plugin_global_shortcut::GlobalShortcutExt;
33+
use tauri_plugin_global_shortcut::Shortcut;
34+
use tauri_plugin_global_shortcut::ShortcutEvent;
35+
use tauri_plugin_global_shortcut::ShortcutState;
3336
use tauri_plugin_store::StoreExt;
3437
use tokio::sync::oneshot::Sender as OneshotSender;
3538

@@ -717,6 +720,26 @@ fn get_app_alias<R: Runtime>(tauri_app_handle: &AppHandle<R>, app_path: &str) ->
717720
Some(string)
718721
}
719722

723+
/// The handler that will be invoked when an application hotkey is pressed.
724+
///
725+
/// The `app_path` argument is for logging-only.
726+
fn app_hotkey_handler<R: Runtime>(
727+
app_path: String,
728+
) -> impl Fn(&AppHandle<R>, &Shortcut, ShortcutEvent) + Send + Sync + 'static {
729+
move |tauri_app_handle, _hot_key, event| {
730+
if event.state() == ShortcutState::Pressed {
731+
let app_path_clone = app_path.clone();
732+
let tauri_app_handle_clone = tauri_app_handle.clone();
733+
// This closure will be executed on the main thread, so we spawn to reduce the potential UI lag.
734+
async_runtime::spawn(async move {
735+
if let Err(e) = open(tauri_app_handle_clone, app_path_clone).await {
736+
warn!("failed to open app due to [{}]", e);
737+
}
738+
});
739+
}
740+
}
741+
}
742+
720743
fn register_app_hotkey_upon_start<R: Runtime>(
721744
tauri_app_handle: AppHandle<R>,
722745
) -> Result<(), String> {
@@ -732,19 +755,7 @@ fn register_app_hotkey_upon_start<R: Runtime>(
732755

733756
tauri_app_handle
734757
.global_shortcut()
735-
.on_shortcut(
736-
hotkey.as_str(),
737-
move |tauri_app_handle, _hot_key, _event| {
738-
let app_path_clone = app_path.clone();
739-
let tauri_app_handle_clone = tauri_app_handle.clone();
740-
// This closure will be executed on the main thread, so we spawn to reduce the potential UI lag.
741-
async_runtime::spawn(async move {
742-
if let Err(e) = open(tauri_app_handle_clone, app_path_clone).await {
743-
warn!("failed to open app due to [{}]", e);
744-
}
745-
});
746-
},
747-
)
758+
.on_shortcut(hotkey.as_str(), app_hotkey_handler(app_path))
748759
.map_err(|e| e.to_string())?;
749760
}
750761

@@ -765,19 +776,7 @@ pub async fn register_app_hotkey<R: Runtime>(
765776

766777
tauri_app_handle
767778
.global_shortcut()
768-
.on_shortcut(
769-
hotkey.as_str(),
770-
move |tauri_app_handle, _hot_key, _event| {
771-
let app_path_clone = app_path.clone();
772-
let tauri_app_handle_clone = tauri_app_handle.clone();
773-
// This closure will be executed on the main thread, so we spawn to reduce the potential UI lag.
774-
async_runtime::spawn(async move {
775-
if let Err(e) = open(tauri_app_handle_clone, app_path_clone).await {
776-
warn!("failed to open app due to [{}]", e);
777-
}
778-
});
779-
},
780-
)
779+
.on_shortcut(hotkey.as_str(), app_hotkey_handler(app_path))
781780
.map_err(|e| e.to_string())?;
782781

783782
Ok(())

0 commit comments

Comments
 (0)