fixed hotkey meta + v

This commit is contained in:
pandadev 2024-07-17 00:30:06 +02:00
parent fa7797cabf
commit 0ee166a3c0
No known key found for this signature in database
GPG key ID: C39629DACB8E762F
2 changed files with 40 additions and 42 deletions

View file

@ -53,7 +53,7 @@ pub fn setup(app_handle: tauri::AppHandle) {
if content != last_text { if content != last_text {
last_text = content.clone(); last_text = content.clone();
rt.block_on(async { 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(); last_image = image_bytes.clone();
rt.block_on(async { rt.block_on(async {
match convert_to_png(image_bytes) { match convert_to_png(image_bytes) {
Ok((png_image, image_name)) => { Ok(png_image) => {
let base64_image = STANDARD.encode(&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) => { Err(e) => {
println!("Failed to convert image to PNG: {}", 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<u8>) -> Result<(Vec<u8>, String), image::ImageError> { fn convert_to_png(image_bytes: Vec<u8>) -> Result<Vec<u8>, image::ImageError> {
match image::guess_format(&image_bytes) { match image::guess_format(&image_bytes) {
Ok(format) => println!("Image format: {:?}", format), Ok(format) => println!("Image format: {:?}", format),
Err(e) => println!("Failed to guess image format: {}", e), Err(e) => println!("Failed to guess image format: {}", e),
@ -91,11 +91,10 @@ fn convert_to_png(image_bytes: Vec<u8>) -> Result<(Vec<u8>, String), image::Imag
let img = image::load_from_memory(&image_bytes)?; let img = image::load_from_memory(&image_bytes)?;
let mut png_bytes: Vec<u8> = Vec::new(); let mut png_bytes: Vec<u8> = Vec::new();
img.write_to(&mut Cursor::new(&mut png_bytes), ImageFormat::Png)?; 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::<String>()); Ok(png_bytes)
Ok((png_bytes, image_name))
} }
async fn insert_content_if_not_exists(pool: &SqlitePool, content_type: &str, content: String, name: Option<String>) { async fn insert_content_if_not_exists(pool: &SqlitePool, content_type: &str, content: String) {
let last_content: Option<String> = sqlx::query_scalar( let last_content: Option<String> = sqlx::query_scalar(
"SELECT content FROM history WHERE content_type = ? ORDER BY timestamp DESC LIMIT 1", "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 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(id)
.bind(content_type) .bind(content_type)
.bind(content) .bind(content)
.bind(favicon_base64) .bind(favicon_base64)
.bind(name)
.execute(pool) .execute(pool)
.await; .await;
} }

View file

@ -1,48 +1,48 @@
use rdev::{listen, EventType, Key}; use rdev::{listen, EventType, Key};
use std::sync::mpsc; use std::sync::{Arc, Mutex, mpsc};
use tauri::Manager; use tauri::Manager;
pub fn setup(app_handle: tauri::AppHandle) { pub fn setup(app_handle: tauri::AppHandle) {
let (tx, rx) = mpsc::channel(); let (tx, rx) = mpsc::channel();
let meta_pressed = Arc::new(Mutex::new(false));
std::thread::spawn(move || { std::thread::spawn({
let meta_pressed = Arc::clone(&meta_pressed);
move || {
listen(move |event| { 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(); 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(); .unwrap();
}
}); });
std::thread::spawn(move || { std::thread::spawn(move || {
let mut meta_pressed = false;
while let Ok(event) = rx.recv() { while let Ok(event) = rx.recv() {
match event.event_type { let meta = meta_pressed.lock().unwrap();
EventType::KeyPress(Key::MetaLeft) | EventType::KeyPress(Key::MetaRight) => { if *meta && matches!(event.event_type, EventType::KeyPress(Key::KeyV)) {
meta_pressed = true; println!("Meta and Key V pressed");
println!("Meta key pressed");
}
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 window = app_handle.get_webview_window("main").unwrap();
let is_visible = window.is_visible().unwrap(); let is_visible = window.is_visible().unwrap();
if is_visible { if is_visible {
println!("Hiding window");
window.hide().unwrap(); window.hide().unwrap();
} else { } else {
println!("Showing window");
window.show().unwrap(); window.show().unwrap();
window.set_focus().unwrap(); window.set_focus().unwrap();
} }
} }
} }
_ => {}
}
}
}); });
} }