Skip to content

Commit

Permalink
Remove unnecessary Tauri Runtime Generic
Browse files Browse the repository at this point in the history
  • Loading branch information
HuakunShen committed Jun 24, 2024
1 parent d6d9eb6 commit 658cb52
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 52 deletions.
48 changes: 24 additions & 24 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use tauri::{command, AppHandle, Manager, Runtime, State, Window};
#[command]
pub fn has_text<R: Runtime>(
_app: AppHandle<R>,
clipboard: State<'_, Clipboard<R>>,
clipboard: State<'_, Clipboard>,
) -> Result<bool, String> {
clipboard.has_text()
}
Expand All @@ -14,7 +14,7 @@ pub fn has_text<R: Runtime>(
pub fn has_image<R: Runtime>(
_app: AppHandle<R>,
_window: Window<R>,
clipboard: State<'_, Clipboard<R>>,
clipboard: State<'_, Clipboard>,
) -> Result<bool, String> {
clipboard.has_image()
}
Expand All @@ -23,7 +23,7 @@ pub fn has_image<R: Runtime>(
pub fn has_html<R: Runtime>(
_app: AppHandle<R>,
_window: Window<R>,
clipboard: State<'_, Clipboard<R>>,
clipboard: State<'_, Clipboard>,
) -> Result<bool, String> {
clipboard.has_html()
}
Expand All @@ -32,7 +32,7 @@ pub fn has_html<R: Runtime>(
pub fn has_rtf<R: Runtime>(
_app: AppHandle<R>,
_window: Window<R>,
clipboard: State<'_, Clipboard<R>>,
clipboard: State<'_, Clipboard>,
) -> Result<bool, String> {
clipboard.has_rtf()
}
Expand All @@ -41,7 +41,7 @@ pub fn has_rtf<R: Runtime>(
pub fn has_files<R: Runtime>(
_app: AppHandle<R>,
_window: Window<R>,
clipboard: State<'_, Clipboard<R>>,
clipboard: State<'_, Clipboard>,
) -> Result<bool, String> {
clipboard.has_files()
}
Expand All @@ -50,7 +50,7 @@ pub fn has_files<R: Runtime>(
pub fn read_text<R: Runtime>(
_app: AppHandle<R>,
_window: Window<R>,
clipboard: State<'_, Clipboard<R>>,
clipboard: State<'_, Clipboard>,
) -> Result<String, String> {
clipboard.read_text()
}
Expand All @@ -59,7 +59,7 @@ pub fn read_text<R: Runtime>(
pub fn read_html<R: Runtime>(
_app: AppHandle<R>,
_window: Window<R>,
clipboard: State<'_, Clipboard<R>>,
clipboard: State<'_, Clipboard>,
) -> Result<String, String> {
clipboard.read_html()
}
Expand All @@ -68,7 +68,7 @@ pub fn read_html<R: Runtime>(
pub fn read_rtf<R: Runtime>(
_app: AppHandle<R>,
_window: Window<R>,
clipboard: State<'_, Clipboard<R>>,
clipboard: State<'_, Clipboard>,
) -> Result<String, String> {
clipboard.read_rtf()
}
Expand All @@ -77,7 +77,7 @@ pub fn read_rtf<R: Runtime>(
pub fn read_files<R: Runtime>(
_app: AppHandle<R>,
_window: Window<R>,
clipboard: State<'_, Clipboard<R>>,
clipboard: State<'_, Clipboard>,
) -> Result<Vec<String>, String> {
clipboard.read_files()
}
Expand All @@ -86,7 +86,7 @@ pub fn read_files<R: Runtime>(
pub fn read_files_uris<R: Runtime>(
_app: AppHandle<R>,
_window: Window<R>,
clipboard: State<'_, Clipboard<R>>,
clipboard: State<'_, Clipboard>,
) -> Result<Vec<String>, String> {
clipboard.read_files_uris()
}
Expand All @@ -95,7 +95,7 @@ pub fn read_files_uris<R: Runtime>(
pub fn write_files_uris<R: Runtime>(
_app: AppHandle<R>,
_window: Window<R>,
clipboard: State<'_, Clipboard<R>>,
clipboard: State<'_, Clipboard>,
files_uris: Vec<String>,
) -> Result<(), String> {
clipboard.write_files_uris(files_uris)
Expand All @@ -106,7 +106,7 @@ pub fn write_files_uris<R: Runtime>(
pub fn write_files<R: Runtime>(
_app: AppHandle<R>,
_window: Window<R>,
clipboard: State<'_, Clipboard<R>>,
clipboard: State<'_, Clipboard>,
files_paths: Vec<String>,
) -> Result<(), String> {
for file in &files_paths {
Expand Down Expand Up @@ -140,7 +140,7 @@ pub fn write_files<R: Runtime>(
pub fn write_text<R: Runtime>(
_app: AppHandle<R>,
_window: Window<R>,
clipboard: State<'_, Clipboard<R>>,
clipboard: State<'_, Clipboard>,
text: String,
) -> Result<(), String> {
clipboard.write_text(text)
Expand All @@ -150,7 +150,7 @@ pub fn write_text<R: Runtime>(
pub fn write_html<R: Runtime>(
_app: AppHandle<R>,
_window: Window<R>,
clipboard: State<'_, Clipboard<R>>,
clipboard: State<'_, Clipboard>,
html: String,
) -> Result<(), String> {
clipboard.write_html(html)
Expand All @@ -160,7 +160,7 @@ pub fn write_html<R: Runtime>(
pub fn write_html_and_text<R: Runtime>(
_app: AppHandle<R>,
_window: Window<R>,
clipboard: State<'_, Clipboard<R>>,
clipboard: State<'_, Clipboard>,
html: String,
text: String,
) -> Result<(), String> {
Expand All @@ -171,7 +171,7 @@ pub fn write_html_and_text<R: Runtime>(
pub fn write_rtf<R: Runtime>(
_app: AppHandle<R>,
_window: Window<R>,
clipboard: State<'_, Clipboard<R>>,
clipboard: State<'_, Clipboard>,
rtf: String,
) -> Result<(), String> {
clipboard.write_rtf(rtf)
Expand All @@ -182,7 +182,7 @@ pub fn write_rtf<R: Runtime>(
pub async fn read_image_base64<R: Runtime>(
_app: AppHandle<R>,
_window: Window<R>,
clipboard: State<'_, Clipboard<R>>,
clipboard: State<'_, Clipboard>,
) -> Result<String, String> {
clipboard.read_image_base64()
}
Expand All @@ -191,7 +191,7 @@ pub async fn read_image_base64<R: Runtime>(
pub async fn read_image_binary<R: Runtime>(
_app: AppHandle<R>,
_window: Window<R>,
clipboard: State<'_, Clipboard<R>>,
clipboard: State<'_, Clipboard>,
) -> Result<Vec<u8>, String> {
clipboard.read_image_binary()
}
Expand All @@ -201,7 +201,7 @@ pub async fn read_image_binary<R: Runtime>(
pub async fn write_image_base64<R: Runtime>(
_app: AppHandle<R>,
_window: Window<R>,
clipboard: State<'_, Clipboard<R>>,
clipboard: State<'_, Clipboard>,
base64_image: String,
) -> Result<(), String> {
clipboard.write_image_base64(base64_image)
Expand All @@ -211,7 +211,7 @@ pub async fn write_image_base64<R: Runtime>(
pub async fn write_image_binary<R: Runtime>(
_app: AppHandle<R>,
_window: Window<R>,
clipboard: State<'_, Clipboard<R>>,
clipboard: State<'_, Clipboard>,
bytes: Vec<u8>,
) -> Result<(), String> {
clipboard.write_image_binary(bytes)
Expand All @@ -221,15 +221,15 @@ pub async fn write_image_binary<R: Runtime>(
pub fn clear<R: Runtime>(
_app: AppHandle<R>,
_window: Window<R>,
clipboard: State<'_, Clipboard<R>>,
clipboard: State<'_, Clipboard>,
) -> Result<(), String> {
clipboard.clear()
}

#[command]
pub async fn start_monitor<R: Runtime>(
app: tauri::AppHandle<R>,
state: tauri::State<'_, Clipboard<R>>,
state: tauri::State<'_, Clipboard>,
) -> Result<(), String> {
let _ = app.emit("plugin:clipboard://clipboard-monitor/status", true);
let clipboard = ClipboardMonitor::new(app);
Expand All @@ -250,7 +250,7 @@ pub async fn start_monitor<R: Runtime>(
#[command]
pub async fn stop_monitor<R: Runtime>(
app: tauri::AppHandle<R>,
state: tauri::State<'_, Clipboard<R>>,
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();
Expand All @@ -264,7 +264,7 @@ pub async fn stop_monitor<R: Runtime>(
#[command]
pub fn is_monitor_running<R: Runtime>(
_app: tauri::AppHandle<R>,
state: tauri::State<'_, Clipboard<R>>,
state: tauri::State<'_, Clipboard>,
) -> bool {
(*state.watcher_shutdown.lock().unwrap()).is_some()
}
11 changes: 3 additions & 8 deletions src/desktop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<R: Runtime, C: DeserializeOwned>(
app: &AppHandle<R>,
_api: PluginApi<R, C>,
) -> crate::Result<Clipboard<R>> {
pub fn init<R: Runtime, C: DeserializeOwned>(_api: PluginApi<R, C>) -> crate::Result<Clipboard> {
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<R: Runtime> {
app: AppHandle<R>,
pub struct Clipboard {
pub clipboard: Arc<Mutex<ClipboardRsContext>>,
pub watcher_shutdown: Arc<Mutex<Option<WatcherShutdown>>>,
}
impl<R: Runtime> Clipboard<R> {
impl Clipboard {
pub fn has(&self, format: ContentFormat) -> Result<bool, String> {
Ok(self
.clipboard
Expand Down
23 changes: 3 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -19,20 +19,6 @@ pub use desktop::Clipboard;
#[cfg(mobile)]
pub use mobile::Clipboard;

// #[derive(Default)]
// struct MyState(Mutex<HashMap<String, String>>);

/// Extensions to [`tauri::App`], [`tauri::AppHandle`] and [`tauri::Window`] to access the clipboard APIs.
pub trait ClipboardExt<R: Runtime> {
fn clipboard(&self) -> &Clipboard<R>;
}

impl<R: Runtime, T: Manager<R>> crate::ClipboardExt<R> for T {
fn clipboard(&self) -> &Clipboard<R> {
self.state::<Clipboard<R>>().inner()
}
}

/// Initializes the plugin.
pub fn init<R: Runtime>() -> TauriPlugin<R> {
Builder::new("clipboard")
Expand Down Expand Up @@ -66,11 +52,8 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
#[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()
Expand Down

0 comments on commit 658cb52

Please sign in to comment.