Skip to content

Commit

Permalink
use unbound sender for macos state main thread
Browse files Browse the repository at this point in the history
  • Loading branch information
thewh1teagle committed Nov 19, 2023
1 parent ec92b09 commit c370c9c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 33 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions desktop/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
74 changes: 43 additions & 31 deletions desktop/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
@@ -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<Enigo>);
struct Controller(UnboundedSender<String>);

#[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::<String>();

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");
Expand Down

0 comments on commit c370c9c

Please sign in to comment.