From c370c9c2e1f53829657eaff96dcd06078f7e5c9f Mon Sep 17 00:00:00 2001 From: thewh1teagle <61390950+thewh1teagle@users.noreply.github.com> Date: Sun, 19 Nov 2023 07:27:17 +0200 Subject: [PATCH] use unbound sender for macos state main thread --- Cargo.lock | 12 ++++++ desktop/src-tauri/Cargo.toml | 5 ++- desktop/src-tauri/src/main.rs | 74 ++++++++++++++++++++--------------- 3 files changed, 58 insertions(+), 33 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 87265f5..baec935 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3009,6 +3009,18 @@ dependencies = [ "bytes", "num_cpus", "pin-project-lite", + "tokio-macros", +] + +[[package]] +name = "tokio-macros" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.39", ] [[package]] diff --git a/desktop/src-tauri/Cargo.toml b/desktop/src-tauri/Cargo.toml index 4c11b7a..a86c28d 100644 --- a/desktop/src-tauri/Cargo.toml +++ b/desktop/src-tauri/Cargo.toml @@ -15,9 +15,10 @@ tauri-build = { version = "1.4", features = [] } [dependencies] tauri = { version = "1.4", features = ["shell-open"] } enigo = { version = "0.1.3" } -tokio = "1.34.0" +tokio = { version = "1.34.0", features = ["macros"] } -[target.'cfg(windows)'.dependencies] + +[target.'cfg(any(windows, target_os = "macos"))'.dependencies] window-shadows = "0.2.2" [features] diff --git a/desktop/src-tauri/src/main.rs b/desktop/src-tauri/src/main.rs index 9ea4828..b57ee81 100644 --- a/desktop/src-tauri/src/main.rs +++ b/desktop/src-tauri/src/main.rs @@ -1,58 +1,70 @@ // Prevents additional console window on Windows in release, DO NOT REMOVE!! #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] -#[cfg(windows)] + +#[cfg(any(windows, target_os = "macos"))] use window_shadows::set_shadow; use tauri::Manager; use enigo::*; use tauri::State; -use tokio::sync::Mutex; - +use tokio::sync::mpsc::UnboundedSender; +use tokio::sync::mpsc; +use tokio; -struct Controller(Mutex); +struct Controller(UnboundedSender); #[tauri::command] -async fn press(controller: State<'_, Controller>, key: &str) -> Result<(), String> { - let mut controller = (&controller.0).lock().await; - match key { - "VOL_UP" => { - controller.key_down(Key::VolumeUp); - }, - "VOL_DN" => { - controller.key_down(Key::VolumeDown); - }, - "PG_UP" => { - controller.key_down(Key::PageUp); - }, - "PG_DN" => { - controller.key_down(Key::PageDown); - }, - "F5" => { - controller.key_down(Key::F5); - }, - "ESC" => { - controller.key_down(Key::Escape); - } - _ => {} - } +async fn press(controller: State<'_, Controller>, key: String) -> Result<(), String> { + controller.0.send(key.to_string()).unwrap_or_default(); Ok(()) } +#[tokio::main] +async fn main() { + let mut controller = Enigo::new(); + let (ch_tx, mut ch_rx) = mpsc::unbounded_channel::(); -fn main() { - let controller = Enigo::new(); + tokio::spawn(async move { + // application specific stuff... + // send commands to ch_tx + // ch_tx.send(EnigoCommand::KeyClick(Key::Layout('E'))); + + while let Some(key) = ch_rx.recv().await { + match key.as_str() { + "VOL_UP" => { + controller.key_down(Key::VolumeUp); + }, + "VOL_DN" => { + controller.key_down(Key::VolumeDown); + }, + "PG_UP" => { + controller.key_down(Key::PageUp); + }, + "PG_DN" => { + controller.key_down(Key::PageDown); + }, + "F5" => { + controller.key_down(Key::F5); + }, + "ESC" => { + controller.key_down(Key::Escape); + } + _ => {} + } + } + }); tauri::Builder::default() .setup(|app| { let window = app.get_window("main").unwrap(); - #[cfg(windows)] + #[cfg(any(windows, target_os = "macos"))] set_shadow(&window, true).unwrap(); Ok(()) }) - .manage(Controller(Mutex::new(controller))) + .manage(Controller(ch_tx)) .invoke_handler(tauri::generate_handler![press]) .run(tauri::generate_context!()) .expect("error while running tauri application");