From 658cb520a0eac02ff4b0d6ea883f02c2d96a5bd6 Mon Sep 17 00:00:00 2001 From: Huakun Shen Date: Mon, 24 Jun 2024 05:54:47 -0400 Subject: [PATCH] Remove unnecessary Tauri Runtime Generic --- src/commands.rs | 48 ++++++++++++++++++++++++------------------------ src/desktop.rs | 11 +++-------- src/lib.rs | 23 +++-------------------- 3 files changed, 30 insertions(+), 52 deletions(-) diff --git a/src/commands.rs b/src/commands.rs index a366b96..3d00627 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -5,7 +5,7 @@ use tauri::{command, AppHandle, Manager, Runtime, State, Window}; #[command] pub fn has_text( _app: AppHandle, - clipboard: State<'_, Clipboard>, + clipboard: State<'_, Clipboard>, ) -> Result { clipboard.has_text() } @@ -14,7 +14,7 @@ pub fn has_text( pub fn has_image( _app: AppHandle, _window: Window, - clipboard: State<'_, Clipboard>, + clipboard: State<'_, Clipboard>, ) -> Result { clipboard.has_image() } @@ -23,7 +23,7 @@ pub fn has_image( pub fn has_html( _app: AppHandle, _window: Window, - clipboard: State<'_, Clipboard>, + clipboard: State<'_, Clipboard>, ) -> Result { clipboard.has_html() } @@ -32,7 +32,7 @@ pub fn has_html( pub fn has_rtf( _app: AppHandle, _window: Window, - clipboard: State<'_, Clipboard>, + clipboard: State<'_, Clipboard>, ) -> Result { clipboard.has_rtf() } @@ -41,7 +41,7 @@ pub fn has_rtf( pub fn has_files( _app: AppHandle, _window: Window, - clipboard: State<'_, Clipboard>, + clipboard: State<'_, Clipboard>, ) -> Result { clipboard.has_files() } @@ -50,7 +50,7 @@ pub fn has_files( pub fn read_text( _app: AppHandle, _window: Window, - clipboard: State<'_, Clipboard>, + clipboard: State<'_, Clipboard>, ) -> Result { clipboard.read_text() } @@ -59,7 +59,7 @@ pub fn read_text( pub fn read_html( _app: AppHandle, _window: Window, - clipboard: State<'_, Clipboard>, + clipboard: State<'_, Clipboard>, ) -> Result { clipboard.read_html() } @@ -68,7 +68,7 @@ pub fn read_html( pub fn read_rtf( _app: AppHandle, _window: Window, - clipboard: State<'_, Clipboard>, + clipboard: State<'_, Clipboard>, ) -> Result { clipboard.read_rtf() } @@ -77,7 +77,7 @@ pub fn read_rtf( pub fn read_files( _app: AppHandle, _window: Window, - clipboard: State<'_, Clipboard>, + clipboard: State<'_, Clipboard>, ) -> Result, String> { clipboard.read_files() } @@ -86,7 +86,7 @@ pub fn read_files( pub fn read_files_uris( _app: AppHandle, _window: Window, - clipboard: State<'_, Clipboard>, + clipboard: State<'_, Clipboard>, ) -> Result, String> { clipboard.read_files_uris() } @@ -95,7 +95,7 @@ pub fn read_files_uris( pub fn write_files_uris( _app: AppHandle, _window: Window, - clipboard: State<'_, Clipboard>, + clipboard: State<'_, Clipboard>, files_uris: Vec, ) -> Result<(), String> { clipboard.write_files_uris(files_uris) @@ -106,7 +106,7 @@ pub fn write_files_uris( pub fn write_files( _app: AppHandle, _window: Window, - clipboard: State<'_, Clipboard>, + clipboard: State<'_, Clipboard>, files_paths: Vec, ) -> Result<(), String> { for file in &files_paths { @@ -140,7 +140,7 @@ pub fn write_files( pub fn write_text( _app: AppHandle, _window: Window, - clipboard: State<'_, Clipboard>, + clipboard: State<'_, Clipboard>, text: String, ) -> Result<(), String> { clipboard.write_text(text) @@ -150,7 +150,7 @@ pub fn write_text( pub fn write_html( _app: AppHandle, _window: Window, - clipboard: State<'_, Clipboard>, + clipboard: State<'_, Clipboard>, html: String, ) -> Result<(), String> { clipboard.write_html(html) @@ -160,7 +160,7 @@ pub fn write_html( pub fn write_html_and_text( _app: AppHandle, _window: Window, - clipboard: State<'_, Clipboard>, + clipboard: State<'_, Clipboard>, html: String, text: String, ) -> Result<(), String> { @@ -171,7 +171,7 @@ pub fn write_html_and_text( pub fn write_rtf( _app: AppHandle, _window: Window, - clipboard: State<'_, Clipboard>, + clipboard: State<'_, Clipboard>, rtf: String, ) -> Result<(), String> { clipboard.write_rtf(rtf) @@ -182,7 +182,7 @@ pub fn write_rtf( pub async fn read_image_base64( _app: AppHandle, _window: Window, - clipboard: State<'_, Clipboard>, + clipboard: State<'_, Clipboard>, ) -> Result { clipboard.read_image_base64() } @@ -191,7 +191,7 @@ pub async fn read_image_base64( pub async fn read_image_binary( _app: AppHandle, _window: Window, - clipboard: State<'_, Clipboard>, + clipboard: State<'_, Clipboard>, ) -> Result, String> { clipboard.read_image_binary() } @@ -201,7 +201,7 @@ pub async fn read_image_binary( pub async fn write_image_base64( _app: AppHandle, _window: Window, - clipboard: State<'_, Clipboard>, + clipboard: State<'_, Clipboard>, base64_image: String, ) -> Result<(), String> { clipboard.write_image_base64(base64_image) @@ -211,7 +211,7 @@ pub async fn write_image_base64( pub async fn write_image_binary( _app: AppHandle, _window: Window, - clipboard: State<'_, Clipboard>, + clipboard: State<'_, Clipboard>, bytes: Vec, ) -> Result<(), String> { clipboard.write_image_binary(bytes) @@ -221,7 +221,7 @@ pub async fn write_image_binary( pub fn clear( _app: AppHandle, _window: Window, - clipboard: State<'_, Clipboard>, + clipboard: State<'_, Clipboard>, ) -> Result<(), String> { clipboard.clear() } @@ -229,7 +229,7 @@ pub fn clear( #[command] pub async fn start_monitor( app: tauri::AppHandle, - state: tauri::State<'_, Clipboard>, + state: tauri::State<'_, Clipboard>, ) -> Result<(), String> { let _ = app.emit("plugin:clipboard://clipboard-monitor/status", true); let clipboard = ClipboardMonitor::new(app); @@ -250,7 +250,7 @@ pub async fn start_monitor( #[command] pub async fn stop_monitor( app: tauri::AppHandle, - state: tauri::State<'_, Clipboard>, + state: tauri::State<'_, Clipboard>, ) -> Result<(), String> { let _ = app.emit("plugin:clipboard://clipboard-monitor/status", false); let mut watcher_shutdown_state = state.watcher_shutdown.lock().unwrap(); @@ -264,7 +264,7 @@ pub async fn stop_monitor( #[command] pub fn is_monitor_running( _app: tauri::AppHandle, - state: tauri::State<'_, Clipboard>, + state: tauri::State<'_, Clipboard>, ) -> bool { (*state.watcher_shutdown.lock().unwrap()).is_some() } diff --git a/src/desktop.rs b/src/desktop.rs index b3e1e13..e48c7f1 100644 --- a/src/desktop.rs +++ b/src/desktop.rs @@ -8,24 +8,19 @@ use image::EncodableLayout; use serde::de::DeserializeOwned; use std::sync::{Arc, Mutex}; use tauri::{plugin::PluginApi, AppHandle, Manager, Runtime}; -pub fn init( - app: &AppHandle, - _api: PluginApi, -) -> crate::Result> { +pub fn init(_api: PluginApi) -> crate::Result { Ok(Clipboard { - app: app.clone(), clipboard: Arc::new(Mutex::new(ClipboardRsContext::new().unwrap())), watcher_shutdown: Arc::default(), }) } /// Access to the clipboard APIs. -pub struct Clipboard { - app: AppHandle, +pub struct Clipboard { pub clipboard: Arc>, pub watcher_shutdown: Arc>>, } -impl Clipboard { +impl Clipboard { pub fn has(&self, format: ContentFormat) -> Result { Ok(self .clipboard diff --git a/src/lib.rs b/src/lib.rs index 5094143..8e743e8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,12 +4,12 @@ use tauri::{ Manager, Runtime, }; +mod commands; #[cfg(desktop)] mod desktop; +mod error; #[cfg(mobile)] mod mobile; -mod commands; -mod error; mod models; pub use error::{Error, Result}; @@ -19,20 +19,6 @@ pub use desktop::Clipboard; #[cfg(mobile)] pub use mobile::Clipboard; -// #[derive(Default)] -// struct MyState(Mutex>); - -/// Extensions to [`tauri::App`], [`tauri::AppHandle`] and [`tauri::Window`] to access the clipboard APIs. -pub trait ClipboardExt { - fn clipboard(&self) -> &Clipboard; -} - -impl> crate::ClipboardExt for T { - fn clipboard(&self) -> &Clipboard { - self.state::>().inner() - } -} - /// Initializes the plugin. pub fn init() -> TauriPlugin { Builder::new("clipboard") @@ -66,11 +52,8 @@ pub fn init() -> TauriPlugin { #[cfg(mobile)] let clipboard = mobile::init(app, api)?; #[cfg(desktop)] - let clipboard = desktop::init(app, api)?; + let clipboard = desktop::init(api)?; app.manage(clipboard); - - // manage state so it is accessible by the commands - // app.manage(MyState::default()); Ok(()) }) .build()