mirror of
https://github.com/0PandaDEV/Qopy.git
synced 2025-04-21 13:14:04 +02:00
split the rust code
This commit is contained in:
parent
c66cbc673a
commit
374dc45674
6 changed files with 231 additions and 151 deletions
72
src-tauri/src/clipboard_listener.rs
Normal file
72
src-tauri/src/clipboard_listener.rs
Normal file
|
@ -0,0 +1,72 @@
|
|||
use rdev::{listen, simulate, EventType, Key};
|
||||
use std::sync::mpsc;
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
use tauri::Manager;
|
||||
use tauri_plugin_clipboard_manager::ClipboardExt;
|
||||
use sqlx::SqlitePool;
|
||||
use tokio::runtime::Runtime;
|
||||
use rand::{thread_rng, Rng};
|
||||
use rand::distributions::Alphanumeric;
|
||||
|
||||
#[tauri::command]
|
||||
pub fn simulate_paste() {
|
||||
let mut events = vec![
|
||||
EventType::KeyPress(Key::MetaLeft),
|
||||
EventType::KeyPress(Key::KeyV),
|
||||
EventType::KeyRelease(Key::KeyV),
|
||||
EventType::KeyRelease(Key::MetaLeft),
|
||||
];
|
||||
|
||||
thread::sleep(Duration::from_millis(100));
|
||||
|
||||
for event in events.drain(..) {
|
||||
simulate(&event).unwrap();
|
||||
thread::sleep(Duration::from_millis(20));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn setup(app_handle: tauri::AppHandle) {
|
||||
let (tx, rx) = mpsc::channel();
|
||||
|
||||
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() {
|
||||
match app_handle.clipboard().read_text() {
|
||||
Ok(content) => {
|
||||
let pool = app_handle.state::<SqlitePool>();
|
||||
let rt = app_handle.state::<Runtime>();
|
||||
rt.block_on(async {
|
||||
let exists = sqlx::query_scalar::<_, bool>("SELECT EXISTS(SELECT 1 FROM history WHERE content = ?)")
|
||||
.bind(&content)
|
||||
.fetch_one(&*pool)
|
||||
.await
|
||||
.unwrap_or(false);
|
||||
|
||||
if !exists {
|
||||
let id: String = thread_rng()
|
||||
.sample_iter(&Alphanumeric)
|
||||
.take(16)
|
||||
.map(char::from)
|
||||
.collect();
|
||||
let _ = sqlx::query("INSERT INTO history (id, content) VALUES (?, ?)")
|
||||
.bind(id)
|
||||
.bind(content)
|
||||
.execute(&*pool)
|
||||
.await;
|
||||
}
|
||||
});
|
||||
},
|
||||
Err(e) => eprintln!("Error reading clipboard: {:?}", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
})
|
||||
.unwrap();
|
||||
});
|
||||
}
|
60
src-tauri/src/database.rs
Normal file
60
src-tauri/src/database.rs
Normal file
|
@ -0,0 +1,60 @@
|
|||
use sqlx::sqlite::SqlitePoolOptions;
|
||||
use std::fs;
|
||||
use tokio::runtime::Runtime;
|
||||
use tauri::Manager;
|
||||
use rand::{thread_rng, Rng};
|
||||
use rand::distributions::Alphanumeric;
|
||||
|
||||
pub fn setup(app: &mut tauri::App) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let rt = Runtime::new().expect("Failed to create Tokio runtime");
|
||||
|
||||
let app_data_dir = app.path().app_data_dir().unwrap();
|
||||
fs::create_dir_all(&app_data_dir).expect("Failed to create app data directory");
|
||||
|
||||
let db_path = app_data_dir.join("data.db");
|
||||
let is_new_db = !db_path.exists();
|
||||
if is_new_db {
|
||||
fs::File::create(&db_path).expect("Failed to create database file");
|
||||
}
|
||||
|
||||
let db_url = format!("sqlite:{}", db_path.to_str().unwrap());
|
||||
let pool = rt.block_on(async {
|
||||
SqlitePoolOptions::new()
|
||||
.max_connections(5)
|
||||
.connect(&db_url)
|
||||
.await
|
||||
.expect("Failed to create pool")
|
||||
});
|
||||
|
||||
rt.block_on(async {
|
||||
sqlx::query(
|
||||
"CREATE TABLE IF NOT EXISTS history (
|
||||
id TEXT PRIMARY KEY,
|
||||
content TEXT NOT NULL,
|
||||
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
)"
|
||||
)
|
||||
.execute(&pool)
|
||||
.await
|
||||
.expect("Failed to create table");
|
||||
|
||||
if is_new_db {
|
||||
let id: String = thread_rng()
|
||||
.sample_iter(&Alphanumeric)
|
||||
.take(16)
|
||||
.map(char::from)
|
||||
.collect();
|
||||
sqlx::query("INSERT INTO history (id, content) VALUES (?, ?)")
|
||||
.bind(id)
|
||||
.bind("Welcome to your clipboard history!")
|
||||
.execute(&pool)
|
||||
.await
|
||||
.expect("Failed to insert welcome message");
|
||||
}
|
||||
});
|
||||
|
||||
app.manage(pool);
|
||||
app.manage(rt);
|
||||
|
||||
Ok(())
|
||||
}
|
29
src-tauri/src/global_shortcut.rs
Normal file
29
src-tauri/src/global_shortcut.rs
Normal file
|
@ -0,0 +1,29 @@
|
|||
use rdev::{listen, EventType, Key};
|
||||
use std::sync::mpsc;
|
||||
use tauri::Manager;
|
||||
|
||||
pub fn setup(app_handle: tauri::AppHandle) {
|
||||
let (tx, rx) = mpsc::channel();
|
||||
|
||||
std::thread::spawn(move || {
|
||||
listen(move |event| match event.event_type {
|
||||
EventType::KeyPress(Key::MetaLeft | Key::MetaRight) => {
|
||||
let _ = tx.send(true);
|
||||
}
|
||||
EventType::KeyRelease(Key::KeyV) => {
|
||||
if rx.try_recv().is_ok() {
|
||||
let window = app_handle.get_window("main").unwrap();
|
||||
let is_visible = window.is_visible().unwrap();
|
||||
if is_visible {
|
||||
window.hide().unwrap();
|
||||
} else {
|
||||
window.show().unwrap();
|
||||
window.set_focus().unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
})
|
||||
.unwrap();
|
||||
});
|
||||
}
|
|
@ -1,172 +1,37 @@
|
|||
use rand::distributions::Alphanumeric;
|
||||
use rand::{thread_rng, Rng};
|
||||
use rdev::{listen, EventType, Key};
|
||||
use sqlx::{sqlite::SqlitePoolOptions, SqlitePool};
|
||||
use std::fs;
|
||||
use std::sync::mpsc;
|
||||
use tokio::runtime::Runtime;
|
||||
mod clipboard_listener;
|
||||
mod database;
|
||||
mod global_shortcut;
|
||||
mod tray;
|
||||
|
||||
use tauri::Manager;
|
||||
use tauri_plugin_autostart::MacosLauncher;
|
||||
use tauri_plugin_clipboard_manager::ClipboardExt;
|
||||
use tauri_plugin_window_state::{AppHandleExt, StateFlags, WindowExt};
|
||||
use tauri::{
|
||||
menu::{MenuBuilder, MenuItemBuilder},
|
||||
tray::{MouseButton, TrayIconBuilder, TrayIconEvent},
|
||||
Manager
|
||||
};
|
||||
use tauri::image::Image;
|
||||
|
||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||
pub fn run() {
|
||||
let (tx, rx) = mpsc::channel();
|
||||
|
||||
tauri::Builder::default()
|
||||
.plugin(tauri_plugin_os::init())
|
||||
.plugin(tauri_plugin_autostart::init(MacosLauncher::LaunchAgent, Some(vec![])))
|
||||
.plugin(tauri_plugin_autostart::init(
|
||||
MacosLauncher::LaunchAgent,
|
||||
Some(vec![]),
|
||||
))
|
||||
.plugin(tauri_plugin_global_shortcut::Builder::new().build())
|
||||
.plugin(tauri_plugin_clipboard_manager::init())
|
||||
.plugin(tauri_plugin_window_state::Builder::default().build())
|
||||
.plugin(tauri_plugin_sql::Builder::default().build())
|
||||
.setup(|app| {
|
||||
let app_handle = app.handle().clone();
|
||||
|
||||
let window = app.get_window("main").unwrap();
|
||||
let window_clone_for_tray = window.clone();
|
||||
let window_clone_for_click = window.clone();
|
||||
|
||||
let _tray = TrayIconBuilder::new()
|
||||
.menu(
|
||||
&MenuBuilder::new(app)
|
||||
.items(&[&MenuItemBuilder::with_id("show", "Show/Hide").build(app)?])
|
||||
.items(&[&MenuItemBuilder::with_id("quit", "Quit").build(app)?])
|
||||
.build()?,
|
||||
)
|
||||
.on_menu_event(move |_app, event| match event.id().as_ref() {
|
||||
"quit" => {
|
||||
std::process::exit(0);
|
||||
}
|
||||
"show" => {
|
||||
let is_visible = window_clone_for_tray.is_visible().unwrap();
|
||||
if is_visible {
|
||||
window_clone_for_tray.hide().unwrap();
|
||||
} else {
|
||||
window_clone_for_tray.show().unwrap();
|
||||
window_clone_for_tray.set_focus().unwrap();
|
||||
}
|
||||
}
|
||||
_ => (),
|
||||
})
|
||||
.on_tray_icon_event(move |_tray, event| {
|
||||
if let TrayIconEvent::Click { button, .. } = event {
|
||||
if button == MouseButton::Left {
|
||||
let is_visible = window_clone_for_click.is_visible().unwrap();
|
||||
if is_visible {
|
||||
window_clone_for_click.hide().unwrap();
|
||||
} else {
|
||||
window_clone_for_click.show().unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
.icon(Image::from_path("icons/Square71x71Logo.png").unwrap())
|
||||
.build(app)?;
|
||||
|
||||
//////////////////////////////////////
|
||||
|
||||
let rt = Runtime::new().expect("Failed to create Tokio runtime");
|
||||
|
||||
let app_data_dir = app.path().app_data_dir().unwrap();
|
||||
fs::create_dir_all(&app_data_dir).expect("Failed to create app data directory");
|
||||
|
||||
let db_path = app_data_dir.join("data.db");
|
||||
let is_new_db = !db_path.exists();
|
||||
if is_new_db {
|
||||
fs::File::create(&db_path).expect("Failed to create database file");
|
||||
}
|
||||
|
||||
let db_url = format!("sqlite:{}", db_path.to_str().unwrap());
|
||||
let pool = rt.block_on(async {
|
||||
SqlitePoolOptions::new()
|
||||
.max_connections(5)
|
||||
.connect(&db_url)
|
||||
.await
|
||||
.expect("Failed to create pool")
|
||||
});
|
||||
|
||||
rt.block_on(async {
|
||||
sqlx::query(
|
||||
"CREATE TABLE IF NOT EXISTS history (
|
||||
id TEXT PRIMARY KEY,
|
||||
content TEXT NOT NULL,
|
||||
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
)"
|
||||
)
|
||||
.execute(&pool)
|
||||
.await
|
||||
.expect("Failed to create table");
|
||||
|
||||
if is_new_db {
|
||||
let id: String = thread_rng()
|
||||
.sample_iter(&Alphanumeric)
|
||||
.take(16)
|
||||
.map(char::from)
|
||||
.collect();
|
||||
sqlx::query("INSERT INTO history (id, content) VALUES (?, ?)")
|
||||
.bind(id)
|
||||
.bind("Welcome to your clipboard history!")
|
||||
.execute(&pool)
|
||||
.await
|
||||
.expect("Failed to insert welcome message");
|
||||
}
|
||||
});
|
||||
|
||||
app.manage(pool);
|
||||
app.manage(rt);
|
||||
global_shortcut::setup(app_handle.clone());
|
||||
tray::setup(app)?;
|
||||
database::setup(app)?;
|
||||
clipboard_listener::setup(app_handle);
|
||||
|
||||
if let Some(window) = app.get_window("main") {
|
||||
let _ = window.restore_state(StateFlags::POSITION);
|
||||
window.show().unwrap();
|
||||
}
|
||||
|
||||
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() {
|
||||
match app_handle.clipboard().read_text() {
|
||||
Ok(content) => {
|
||||
let pool = app_handle.state::<SqlitePool>();
|
||||
let rt = app_handle.state::<Runtime>();
|
||||
rt.block_on(async {
|
||||
let exists = sqlx::query_scalar::<_, bool>("SELECT EXISTS(SELECT 1 FROM history WHERE content = ?)")
|
||||
.bind(&content)
|
||||
.fetch_one(&*pool)
|
||||
.await
|
||||
.unwrap_or(false);
|
||||
|
||||
if !exists {
|
||||
let id: String = thread_rng()
|
||||
.sample_iter(&Alphanumeric)
|
||||
.take(16)
|
||||
.map(char::from)
|
||||
.collect();
|
||||
let _ = sqlx::query("INSERT INTO history (id, content) VALUES (?, ?)")
|
||||
.bind(id)
|
||||
.bind(content)
|
||||
.execute(&*pool)
|
||||
.await;
|
||||
}
|
||||
});
|
||||
},
|
||||
Err(e) => eprintln!("Error reading clipboard: {:?}", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
})
|
||||
.unwrap();
|
||||
});
|
||||
Ok(())
|
||||
})
|
||||
.on_window_event(|app, event| match event {
|
||||
|
@ -177,6 +42,9 @@ pub fn run() {
|
|||
}
|
||||
_ => {}
|
||||
})
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
clipboard_listener::simulate_paste
|
||||
])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
}
|
||||
|
|
51
src-tauri/src/tray.rs
Normal file
51
src-tauri/src/tray.rs
Normal file
|
@ -0,0 +1,51 @@
|
|||
use tauri::{
|
||||
Manager,
|
||||
menu::{MenuBuilder, MenuItemBuilder},
|
||||
tray::{MouseButton, TrayIconBuilder, TrayIconEvent},
|
||||
image::Image,
|
||||
};
|
||||
|
||||
pub fn setup(app: &mut tauri::App) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let window = app.get_window("main").unwrap();
|
||||
let window_clone_for_tray = window.clone();
|
||||
let window_clone_for_click = window.clone();
|
||||
|
||||
let _tray = TrayIconBuilder::new()
|
||||
.menu(
|
||||
&MenuBuilder::new(app)
|
||||
.items(&[&MenuItemBuilder::with_id("show", "Show/Hide").build(app)?])
|
||||
.items(&[&MenuItemBuilder::with_id("quit", "Quit").build(app)?])
|
||||
.build()?,
|
||||
)
|
||||
.on_menu_event(move |_app, event| match event.id().as_ref() {
|
||||
"quit" => {
|
||||
std::process::exit(0);
|
||||
}
|
||||
"show" => {
|
||||
let is_visible = window_clone_for_tray.is_visible().unwrap();
|
||||
if is_visible {
|
||||
window_clone_for_tray.hide().unwrap();
|
||||
} else {
|
||||
window_clone_for_tray.show().unwrap();
|
||||
window_clone_for_tray.set_focus().unwrap();
|
||||
}
|
||||
}
|
||||
_ => (),
|
||||
})
|
||||
.on_tray_icon_event(move |_tray, event| {
|
||||
if let TrayIconEvent::Click { button, .. } = event {
|
||||
if button == MouseButton::Left {
|
||||
let is_visible = window_clone_for_click.is_visible().unwrap();
|
||||
if is_visible {
|
||||
window_clone_for_click.hide().unwrap();
|
||||
} else {
|
||||
window_clone_for_click.show().unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
.icon(Image::from_path("icons/Square71x71Logo.png").unwrap())
|
||||
.build(app)?;
|
||||
|
||||
Ok(())
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue