mirror of
https://github.com/0PandaDEV/Qopy.git
synced 2025-04-22 05:34:04 +02:00
fixed hotkey meta + v
This commit is contained in:
parent
fa7797cabf
commit
0ee166a3c0
2 changed files with 40 additions and 42 deletions
|
@ -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<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) {
|
||||
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<u8>) -> Result<(Vec<u8>, String), image::Imag
|
|||
let img = image::load_from_memory(&image_bytes)?;
|
||||
let mut png_bytes: Vec<u8> = 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::<String>());
|
||||
Ok((png_bytes, image_name))
|
||||
Ok(png_bytes)
|
||||
}
|
||||
|
||||
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(
|
||||
"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;
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue