From 75583ac6ceb48c616f68be35e5ca3eda0a5af7dd Mon Sep 17 00:00:00 2001 From: PandaDEV <70103896+0PandaDEV@users.noreply.github.com> Date: Sun, 25 Aug 2024 17:40:52 +1000 Subject: [PATCH] back to normal setup methods --- src-tauri/src/api/clipboard.rs | 51 ++++++++++++---------------------- src-tauri/src/api/database.rs | 16 ++--------- src-tauri/src/api/hotkeys.rs | 17 +++--------- src-tauri/src/api/tray.rs | 21 ++++---------- src-tauri/src/api/updater.rs | 14 ++-------- src-tauri/src/main.rs | 43 +++++++++++++++------------- 6 files changed, 56 insertions(+), 106 deletions(-) diff --git a/src-tauri/src/api/clipboard.rs b/src-tauri/src/api/clipboard.rs index 385bd04..5bf29bc 100644 --- a/src-tauri/src/api/clipboard.rs +++ b/src-tauri/src/api/clipboard.rs @@ -1,6 +1,6 @@ use base64::Engine; use base64::engine::general_purpose::STANDARD; -use tauri::{AppHandle, Manager, Emitter, Listener}; +use tauri::{AppHandle, Manager, Runtime, Emitter, Listener}; use tauri_plugin_clipboard::Clipboard; use tokio::runtime::Runtime as TokioRuntime; use regex::Regex; @@ -16,20 +16,14 @@ use sha2::{Sha256, Digest}; use rdev::{simulate, Key, EventType}; use lazy_static::lazy_static; use image::ImageFormat; -use tauri::plugin::TauriPlugin; -pub fn init() -> TauriPlugin { - tauri::plugin::Builder::new("clipboard") - .invoke_handler(tauri::generate_handler![ - read_image, - simulate_paste, - get_image_path - ]) - .setup(|app_handle, _api| { - setup(app_handle.clone()); - Ok(()) - }) - .build() +lazy_static! { + static ref APP_DATA_DIR: Mutex> = Mutex::new(None); +} + +pub fn set_app_data_dir(path: std::path::PathBuf) { + let mut dir = APP_DATA_DIR.lock().unwrap(); + *dir = Some(path); } #[tauri::command] @@ -64,15 +58,7 @@ pub fn get_image_path(app_handle: tauri::AppHandle, filename: String) -> String image_path.to_str().unwrap_or("").to_string() } -#[tauri::command] -pub fn start_monitor(app_handle: tauri::AppHandle) -> Result<(), String> { - let clipboard = app_handle.state::(); - clipboard.start_monitor(app_handle.clone()).map_err(|e| e.to_string())?; - app_handle.emit("plugin:clipboard://clipboard-monitor/status", true).map_err(|e| e.to_string())?; - Ok(()) -} - -fn setup(app: AppHandle) { +pub fn setup(app: &AppHandle) { let app = app.clone(); let runtime = TokioRuntime::new().expect("Failed to create Tokio runtime"); @@ -124,14 +110,14 @@ fn setup(app: AppHandle) { }); } -async fn get_pool(app_handle: &tauri::AppHandle) -> Result> { +async fn get_pool(app_handle: &AppHandle) -> Result> { let app_data_dir = app_handle.path().app_data_dir().expect("Failed to get app data directory"); let db_path = app_data_dir.join("data.db"); let database_url = format!("sqlite:{}", db_path.to_str().unwrap()); SqlitePool::connect(&database_url).await.map_err(|e| Box::new(e) as Box) } -async fn insert_content_if_not_exists(app_handle: tauri::AppHandle, pool: SqlitePool, content_type: &str, content: String) { +async fn insert_content_if_not_exists(app_handle: AppHandle, pool: SqlitePool, content_type: &str, content: String) { let last_content: Option = sqlx::query_scalar( "SELECT content FROM history WHERE content_type = ? ORDER BY timestamp DESC LIMIT 1", ) @@ -191,7 +177,7 @@ async fn insert_content_if_not_exists(app_handle: tauri::AppH } } -async fn save_image(app_handle: &tauri::AppHandle, base64_image: &str) -> Result> { +async fn save_image(app_handle: &AppHandle, base64_image: &str) -> Result> { let image_data = STANDARD.decode(base64_image)?; let mut hasher = Sha256::new(); hasher.update(&image_data); @@ -226,11 +212,10 @@ async fn fetch_favicon_as_base64(url: url::Url) -> Result, Box> = Mutex::new(None); -} - -pub fn set_app_data_dir(path: std::path::PathBuf) { - let mut dir = APP_DATA_DIR.lock().unwrap(); - *dir = Some(path); +#[tauri::command] +pub fn start_monitor(app_handle: AppHandle) -> Result<(), String> { + let clipboard = app_handle.state::(); + clipboard.start_monitor(app_handle.clone()).map_err(|e| e.to_string())?; + app_handle.emit("plugin:clipboard://clipboard-monitor/status", true).map_err(|e| e.to_string())?; + Ok(()) } \ No newline at end of file diff --git a/src-tauri/src/api/database.rs b/src-tauri/src/api/database.rs index d848d54..cb6f88d 100644 --- a/src-tauri/src/api/database.rs +++ b/src-tauri/src/api/database.rs @@ -1,21 +1,11 @@ -use tauri::plugin::TauriPlugin; -use tauri::{AppHandle, Manager, Emitter}; 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 init() -> TauriPlugin { - tauri::plugin::Builder::new("database") - .setup(|app_handle: &AppHandle, _api| { - setup(app_handle)?; - Ok(()) - }) - .build() -} - -fn setup(app: &AppHandle) -> Result<(), Box> { +pub fn setup(app: &mut tauri::App) -> Result<(), Box> { let rt = Runtime::new().expect("Failed to create Tokio runtime"); let app_data_dir = app.path().app_data_dir().unwrap(); @@ -76,7 +66,5 @@ fn setup(app: &AppHandle) -> Result<(), Box> { app.manage(pool); app.manage(rt); - app.emit("database_initialized", ()).expect("Failed to emit database_initialized event"); - Ok(()) } \ No newline at end of file diff --git a/src-tauri/src/api/hotkeys.rs b/src-tauri/src/api/hotkeys.rs index 4ec097d..eaa23e6 100644 --- a/src-tauri/src/api/hotkeys.rs +++ b/src-tauri/src/api/hotkeys.rs @@ -1,18 +1,9 @@ -use tauri::plugin::TauriPlugin; -use tauri::Manager; use rdev::{listen, EventType, Key}; -use crate::utils::commands; +use tauri::Manager; -pub fn init() -> TauriPlugin { - tauri::plugin::Builder::new("hotkeys") - .setup(|app, _| { - setup(app.app_handle().clone()); - Ok(()) - }) - .build() -} +use crate::utils::commands::center_window_on_current_monitor; -fn setup(app_handle: tauri::AppHandle) { +pub fn setup(app_handle: tauri::AppHandle) { std::thread::spawn(move || { let mut meta_pressed = false; listen(move |event| { @@ -29,7 +20,7 @@ fn setup(app_handle: tauri::AppHandle) { let window = app_handle.get_webview_window("main").unwrap(); window.show().unwrap(); window.set_focus().unwrap(); - commands::center_window_on_current_monitor(&window); + center_window_on_current_monitor(&window); } } _ => {} diff --git a/src-tauri/src/api/tray.rs b/src-tauri/src/api/tray.rs index f80f031..d279774 100644 --- a/src-tauri/src/api/tray.rs +++ b/src-tauri/src/api/tray.rs @@ -1,19 +1,10 @@ -use tauri::AppHandle; -use tauri::plugin::TauriPlugin; -use tauri::Manager; -use tauri::menu::{MenuBuilder, MenuItemBuilder}; -use tauri::tray::{MouseButton, TrayIconBuilder, TrayIconEvent}; +use tauri::{ + Manager, + menu::{MenuBuilder, MenuItemBuilder}, + tray::{MouseButton, TrayIconBuilder, TrayIconEvent}, +}; -pub fn init() -> TauriPlugin { - tauri::plugin::Builder::new("tray") - .setup(|app, _api| { - setup(app)?; - Ok(()) - }) - .build() -} - -fn setup(app: &AppHandle) -> Result<(), Box> { +pub fn setup(app: &mut tauri::App) -> Result<(), Box> { let window = app.get_webview_window("main").unwrap(); let window_clone_for_tray = window.clone(); let window_clone_for_click = window.clone(); diff --git a/src-tauri/src/api/updater.rs b/src-tauri/src/api/updater.rs index 567d028..6c474c2 100644 --- a/src-tauri/src/api/updater.rs +++ b/src-tauri/src/api/updater.rs @@ -1,16 +1,8 @@ -use tauri::plugin::TauriPlugin; -use tauri::AppHandle; +use tauri::{AppHandle, async_runtime}; use tauri_plugin_dialog::{DialogExt, MessageDialogKind}; use tauri_plugin_updater::UpdaterExt; -use tokio; -pub fn init() -> TauriPlugin { - tauri::plugin::Builder::new("updater") - .invoke_handler(tauri::generate_handler![check_for_updates]) - .build() -} -#[tauri::command] pub async fn check_for_updates(app: AppHandle) { println!("Checking for updates..."); @@ -36,7 +28,7 @@ pub async fn check_for_updates(app: AppHandle) { if !response { return; } - tokio::spawn(async move { + async_runtime::spawn(async move { if let Err(e) = update.download_and_install(|_, _| {}, || {}).await { println!("Error installing new update: {:?}", e); app.dialog().message( @@ -51,4 +43,4 @@ pub async fn check_for_updates(app: AppHandle) { println!("Failed to check for updates: {:?}", e); } } -} \ No newline at end of file +} diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 1827fa6..ef8fb34 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -6,11 +6,12 @@ mod api; mod utils; -use tauri::{Manager, Listener}; +use tauri::Manager; use tauri_plugin_autostart::MacosLauncher; use tauri_plugin_prevent_default::Flags; fn main() { + #[allow(unused_variables)] tauri::Builder::default() .plugin(tauri_plugin_clipboard::init()) .plugin(tauri_plugin_os::init()) @@ -21,11 +22,6 @@ fn main() { MacosLauncher::LaunchAgent, Some(vec![]), )) - .plugin(api::updater::init()) - .plugin(api::database::init()) - .plugin(api::tray::init()) - .plugin(api::hotkeys::init()) - .plugin(api::clipboard::init()) .plugin( tauri_plugin_prevent_default::Builder::new() .with_flags(Flags::all().difference(Flags::CONTEXT_MENU)) @@ -33,26 +29,33 @@ fn main() { ) .setup(|app| { let app_handle = app.handle().clone(); - - let app_data_dir = app - .path() - .app_data_dir() - .expect("Failed to get app data directory"); - api::clipboard::set_app_data_dir(app_data_dir); + + api::hotkeys::setup(app_handle.clone()); + api::tray::setup(app)?; + api::database::setup(app)?; + api::clipboard::setup(app.handle()); + let _ = api::clipboard::start_monitor(app_handle.clone()); if let Some(window) = app.get_webview_window("main") { utils::commands::center_window_on_current_monitor(&window); window.hide().unwrap(); } - let update_handle = app_handle.clone(); - tauri::async_runtime::spawn(async move { - api::updater::check_for_updates(update_handle).await; - }); + // #[cfg(dev)] + // { + // let window = app.get_webview_window("main").unwrap(); + // window.open_devtools(); + // window.close_devtools(); + // } - let monitor_handle = app_handle.clone(); - app_handle.listen("database_initialized", move |_| { - let _ = api::clipboard::start_monitor(monitor_handle.clone()); + let app_data_dir = app + .path() + .app_data_dir() + .expect("Failed to get app data directory"); + api::clipboard::set_app_data_dir(app_data_dir); + + tauri::async_runtime::spawn(async move { + api::updater::check_for_updates(app_handle).await; }); Ok(()) @@ -72,4 +75,4 @@ fn main() { ]) .run(tauri::generate_context!()) .expect("error while running tauri application"); -} \ No newline at end of file +}