finally image support

This commit is contained in:
pandadev 2024-07-16 23:08:42 +02:00
parent bae359f1cf
commit c67ed49c3d
No known key found for this signature in database
GPG key ID: C39629DACB8E762F
9 changed files with 98 additions and 100 deletions

View file

@ -13,6 +13,9 @@ use url::Url;
use reqwest::Client;
use arboard::Clipboard;
use regex::Regex;
use image::ImageFormat;
use image::DynamicImage;
use std::io::Cursor;
#[tauri::command]
pub fn simulate_paste() {
@ -33,45 +36,58 @@ pub fn simulate_paste() {
pub fn setup(app_handle: tauri::AppHandle) {
let (tx, rx) = mpsc::channel();
let mut is_processing = false;
let is_processing = std::sync::Arc::new(std::sync::Mutex::new(false));
std::thread::spawn(move || {
listen(move |event| match event.event_type {
EventType::KeyPress(Key::ControlLeft | Key::ControlRight) => {
let _ = tx.send(true);
}
EventType::KeyRelease(Key::KeyC) => {
if rx.try_recv().is_ok() && !is_processing {
is_processing = true;
let pool = app_handle.state::<SqlitePool>();
let rt = app_handle.state::<Runtime>();
let mut clipboard = Clipboard::new().unwrap();
if let Ok(content) = clipboard.get_text() {
rt.block_on(async {
insert_content_if_not_exists(&pool, "text", content).await;
});
}
if let Ok(image) = clipboard.get_image() {
rt.block_on(async {
let base64_image = STANDARD.encode(&image.bytes);
insert_content_if_not_exists(&pool, "image", base64_image).await;
});
}
is_processing = false;
std::thread::spawn({
let is_processing = std::sync::Arc::clone(&is_processing);
move || {
listen(move |event| match event.event_type {
EventType::KeyPress(Key::ControlLeft | Key::ControlRight) => {
let _ = tx.send(true);
}
}
EventType::KeyRelease(Key::ControlLeft | Key::ControlRight) => {
is_processing = false;
}
_ => {}
})
.unwrap();
EventType::KeyRelease(Key::KeyC) => {
let mut is_processing = is_processing.lock().unwrap();
if rx.try_recv().is_ok() && !*is_processing {
*is_processing = true;
let pool = app_handle.state::<SqlitePool>();
let rt = app_handle.state::<Runtime>();
let mut clipboard = Clipboard::new().unwrap();
if let Ok(content) = clipboard.get_text() {
rt.block_on(async {
insert_content_if_not_exists(&pool, "text", content).await;
});
}
if let Ok(image) = clipboard.get_image() {
rt.block_on(async {
let png_image = convert_to_png(image.bytes.to_vec());
let base64_image = STANDARD.encode(&png_image);
insert_content_if_not_exists(&pool, "image", base64_image).await;
});
}
*is_processing = false;
}
}
EventType::KeyRelease(Key::ControlLeft | Key::ControlRight) => {
let mut is_processing = is_processing.lock().unwrap();
*is_processing = false;
}
_ => {}
})
.unwrap();
}
});
}
fn convert_to_png(image_bytes: Vec<u8>) -> Vec<u8> {
let img = image::load_from_memory(&image_bytes).unwrap();
let mut png_bytes: Vec<u8> = Vec::new();
img.write_to(&mut Cursor::new(&mut png_bytes), ImageFormat::Png).unwrap();
png_bytes
}
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",

View file

@ -40,6 +40,13 @@ pub fn setup(app: &mut tauri::App) -> Result<(), Box<dyn std::error::Error>> {
.await
.expect("Failed to create table");
sqlx::query(
"CREATE INDEX IF NOT EXISTS idx_timestamp ON history (timestamp)"
)
.execute(&pool)
.await
.expect("Failed to create index");
if is_new_db {
let id: String = thread_rng()
.sample_iter(&Alphanumeric)

View file

@ -29,7 +29,7 @@ pub fn setup(app_handle: tauri::AppHandle) {
println!("V key pressed");
if meta_pressed {
println!("Meta+V detected");
let window = app_handle.get_window("main").unwrap();
let window = app_handle.get_webview_window("main").unwrap();
let is_visible = window.is_visible().unwrap();
if is_visible {
println!("Hiding window");

View file

@ -12,7 +12,7 @@ use tauri::Manager;
use tauri::PhysicalPosition;
use tauri_plugin_autostart::MacosLauncher;
fn center_window_on_current_monitor(window: &tauri::Window) {
fn center_window_on_current_monitor(window: &tauri::WebviewWindow) {
if let Some(monitor) = window.current_monitor().unwrap() {
let monitor_size = monitor.size();
let window_size = window.outer_size().unwrap();
@ -43,9 +43,9 @@ fn main() {
hotkeys::setup(app_handle.clone());
tray::setup(app)?;
database::setup(app)?;
clipboard::setup(app_handle);
clipboard::setup(app_handle.clone());
if let Some(window) = app.get_window("main") {
if let Some(window) = app.get_webview_window("main") {
center_window_on_current_monitor(&window);
window.hide().unwrap();
}
@ -60,15 +60,12 @@ fn main() {
Ok(())
})
.on_window_event(|app, event| match event {
#[cfg(not(dev))]
tauri::WindowEvent::Focused(false) => {
println!("Window lost focus");
if let Some(window) = app.get_window("main") {
if let Some(window) = app.get_webview_window("main") {
window.hide().unwrap();
}
}
tauri::WindowEvent::Focused(true) => {
println!("Window gained focus");
}
_ => {}
})
.invoke_handler(tauri::generate_handler![clipboard::simulate_paste])

View file

@ -5,7 +5,7 @@ use tauri::{
};
pub fn setup(app: &mut tauri::App) -> Result<(), Box<dyn std::error::Error>> {
let window = app.get_window("main").unwrap();
let window = app.get_webview_window("main").unwrap();
let window_clone_for_tray = window.clone();
let window_clone_for_click = window.clone();