split the rust code

This commit is contained in:
pandadev 2024-07-05 01:50:56 +02:00
parent c66cbc673a
commit 374dc45674
No known key found for this signature in database
GPG key ID: C39629DACB8E762F
6 changed files with 231 additions and 151 deletions

View file

@ -1,6 +1,6 @@
<template> <template>
<div class="bg" @keydown.down.prevent="selectNext" @keydown.up.prevent="selectPrevious" <div class="bg" @keydown.down.prevent="selectNext" @keydown.up.prevent="selectPrevious"
@keydown.enter.prevent="pasteSelectedItem" tabindex="0"> @keydown.enter.prevent="pasteSelectedItem" @keydown.esc="hideApp" tabindex="0">
<input v-model="searchQuery" @input="searchHistory" autocorrect="off" autocapitalize="off" spellcheck="false" <input v-model="searchQuery" @input="searchHistory" autocorrect="off" autocapitalize="off" spellcheck="false"
class="search" type="text" placeholder="Type to filter entries..."> class="search" type="text" placeholder="Type to filter entries...">
<div class="bottom-bar"> <div class="bottom-bar">
@ -44,7 +44,7 @@
import { ref, computed, onMounted, watch, nextTick } from 'vue'; import { ref, computed, onMounted, watch, nextTick } from 'vue';
import Database from '@tauri-apps/plugin-sql'; import Database from '@tauri-apps/plugin-sql';
import { register, unregister, isRegistered } from '@tauri-apps/plugin-global-shortcut'; import { register, unregister, isRegistered } from '@tauri-apps/plugin-global-shortcut';
import { writeText, paste } from '@tauri-apps/plugin-clipboard-manager'; import { writeText } from '@tauri-apps/plugin-clipboard-manager';
import { OverlayScrollbarsComponent } from "overlayscrollbars-vue"; import { OverlayScrollbarsComponent } from "overlayscrollbars-vue";
import 'overlayscrollbars/overlayscrollbars.css'; import 'overlayscrollbars/overlayscrollbars.css';
import { app, window } from '@tauri-apps/api'; import { app, window } from '@tauri-apps/api';

View 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
View 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(())
}

View 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();
});
}

View file

@ -1,27 +1,20 @@
use rand::distributions::Alphanumeric; mod clipboard_listener;
use rand::{thread_rng, Rng}; mod database;
use rdev::{listen, EventType, Key}; mod global_shortcut;
use sqlx::{sqlite::SqlitePoolOptions, SqlitePool}; mod tray;
use std::fs;
use std::sync::mpsc; use tauri::Manager;
use tokio::runtime::Runtime;
use tauri_plugin_autostart::MacosLauncher; use tauri_plugin_autostart::MacosLauncher;
use tauri_plugin_clipboard_manager::ClipboardExt;
use tauri_plugin_window_state::{AppHandleExt, StateFlags, WindowExt}; 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)] #[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() { pub fn run() {
let (tx, rx) = mpsc::channel();
tauri::Builder::default() tauri::Builder::default()
.plugin(tauri_plugin_os::init()) .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_global_shortcut::Builder::new().build())
.plugin(tauri_plugin_clipboard_manager::init()) .plugin(tauri_plugin_clipboard_manager::init())
.plugin(tauri_plugin_window_state::Builder::default().build()) .plugin(tauri_plugin_window_state::Builder::default().build())
@ -29,144 +22,16 @@ pub fn run() {
.setup(|app| { .setup(|app| {
let app_handle = app.handle().clone(); let app_handle = app.handle().clone();
let window = app.get_window("main").unwrap(); global_shortcut::setup(app_handle.clone());
let window_clone_for_tray = window.clone(); tray::setup(app)?;
let window_clone_for_click = window.clone(); database::setup(app)?;
clipboard_listener::setup(app_handle);
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);
if let Some(window) = app.get_window("main") { if let Some(window) = app.get_window("main") {
let _ = window.restore_state(StateFlags::POSITION); let _ = window.restore_state(StateFlags::POSITION);
window.show().unwrap(); 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(()) Ok(())
}) })
.on_window_event(|app, event| match event { .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!()) .run(tauri::generate_context!())
.expect("error while running tauri application"); .expect("error while running tauri application");
} }

51
src-tauri/src/tray.rs Normal file
View 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(())
}