From 0ee166a3c02396e0825ce6e8c057a61b1fcc2a9e Mon Sep 17 00:00:00 2001 From: pandadev <70103896+0PandaDEV@users.noreply.github.com> Date: Wed, 17 Jul 2024 00:30:06 +0200 Subject: [PATCH] fixed hotkey meta + v --- src-tauri/src/clipboard.rs | 16 ++++----- src-tauri/src/hotkeys.rs | 66 +++++++++++++++++++------------------- 2 files changed, 40 insertions(+), 42 deletions(-) diff --git a/src-tauri/src/clipboard.rs b/src-tauri/src/clipboard.rs index 4d7f59d..c765fcc 100644 --- a/src-tauri/src/clipboard.rs +++ b/src-tauri/src/clipboard.rs @@ -53,7 +53,7 @@ pub fn setup(app_handle: tauri::AppHandle) { if content != last_text { last_text = content.clone(); rt.block_on(async { - insert_content_if_not_exists(&pool, "text", content, None).await; + insert_content_if_not_exists(&pool, "text", content).await; }); } } @@ -64,9 +64,9 @@ pub fn setup(app_handle: tauri::AppHandle) { last_image = image_bytes.clone(); rt.block_on(async { match convert_to_png(image_bytes) { - Ok((png_image, image_name)) => { + Ok(png_image) => { let base64_image = STANDARD.encode(&png_image); - insert_content_if_not_exists(&pool, "image", base64_image, Some(image_name)).await; + insert_content_if_not_exists(&pool, "image", base64_image).await; } Err(e) => { println!("Failed to convert image to PNG: {}", e); @@ -83,7 +83,7 @@ pub fn setup(app_handle: tauri::AppHandle) { }); } -fn convert_to_png(image_bytes: Vec) -> Result<(Vec, String), image::ImageError> { +fn convert_to_png(image_bytes: Vec) -> Result, image::ImageError> { match image::guess_format(&image_bytes) { Ok(format) => println!("Image format: {:?}", format), Err(e) => println!("Failed to guess image format: {}", e), @@ -91,11 +91,10 @@ fn convert_to_png(image_bytes: Vec) -> Result<(Vec, String), image::Imag let img = image::load_from_memory(&image_bytes)?; let mut png_bytes: Vec = Vec::new(); img.write_to(&mut Cursor::new(&mut png_bytes), ImageFormat::Png)?; - let image_name = format!("{}.png", thread_rng().sample_iter(&Alphanumeric).take(10).map(char::from).collect::()); - Ok((png_bytes, image_name)) + Ok(png_bytes) } -async fn insert_content_if_not_exists(pool: &SqlitePool, content_type: &str, content: String, name: Option) { +async fn insert_content_if_not_exists(pool: &SqlitePool, content_type: &str, content: String) { let last_content: Option = sqlx::query_scalar( "SELECT content FROM history WHERE content_type = ? ORDER BY timestamp DESC LIMIT 1", ) @@ -144,12 +143,11 @@ async fn insert_content_if_not_exists(pool: &SqlitePool, content_type: &str, con None }; - let _ = sqlx::query("INSERT INTO history (id, content_type, content, favicon, name) VALUES (?, ?, ?, ?, ?)") + let _ = sqlx::query("INSERT INTO history (id, content_type, content, favicon) VALUES (?, ?, ?, ?, ?)") .bind(id) .bind(content_type) .bind(content) .bind(favicon_base64) - .bind(name) .execute(pool) .await; } diff --git a/src-tauri/src/hotkeys.rs b/src-tauri/src/hotkeys.rs index 1e17f48..00d21d8 100644 --- a/src-tauri/src/hotkeys.rs +++ b/src-tauri/src/hotkeys.rs @@ -1,47 +1,47 @@ use rdev::{listen, EventType, Key}; -use std::sync::mpsc; +use std::sync::{Arc, Mutex, mpsc}; use tauri::Manager; pub fn setup(app_handle: tauri::AppHandle) { let (tx, rx) = mpsc::channel(); + let meta_pressed = Arc::new(Mutex::new(false)); - std::thread::spawn(move || { - listen(move |event| { - tx.send(event).unwrap(); - }) - .unwrap(); + std::thread::spawn({ + let meta_pressed = Arc::clone(&meta_pressed); + move || { + listen(move |event| { + match event.event_type { + EventType::KeyPress(Key::MetaLeft) | EventType::KeyPress(Key::MetaRight) => { + let mut meta = meta_pressed.lock().unwrap(); + *meta = true; + tx.send(event).unwrap(); + } + EventType::KeyRelease(Key::MetaLeft) | EventType::KeyRelease(Key::MetaRight) => { + let mut meta = meta_pressed.lock().unwrap(); + *meta = false; + } + _ => { + tx.send(event).unwrap(); + } + } + }) + .unwrap(); + } }); std::thread::spawn(move || { - let mut meta_pressed = false; - while let Ok(event) = rx.recv() { - match event.event_type { - EventType::KeyPress(Key::MetaLeft) | EventType::KeyPress(Key::MetaRight) => { - meta_pressed = true; - println!("Meta key pressed"); + let meta = meta_pressed.lock().unwrap(); + if *meta && matches!(event.event_type, EventType::KeyPress(Key::KeyV)) { + println!("Meta and Key V pressed"); + let window = app_handle.get_webview_window("main").unwrap(); + let is_visible = window.is_visible().unwrap(); + if is_visible { + window.hide().unwrap(); + } else { + window.show().unwrap(); + window.set_focus().unwrap(); } - EventType::KeyRelease(Key::MetaLeft) | EventType::KeyRelease(Key::MetaRight) => { - meta_pressed = false; - println!("Meta key released"); - } - EventType::KeyPress(Key::KeyV) => { - println!("V key pressed"); - if meta_pressed { - println!("Meta+V detected"); - let window = app_handle.get_webview_window("main").unwrap(); - let is_visible = window.is_visible().unwrap(); - if is_visible { - println!("Hiding window"); - window.hide().unwrap(); - } else { - println!("Showing window"); - window.show().unwrap(); - window.set_focus().unwrap(); - } - } - } - _ => {} } } });