mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-07-28 17:38:53 +02:00
refactor(clipboard): refactor clipboard function arguments for better clarity about the needed type (#1218)
This commit is contained in:
@@ -2,37 +2,54 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
use tauri::{command, AppHandle, Manager, ResourceId, Runtime, State, Webview};
|
||||
use tauri::{command, image::JsImage, AppHandle, Manager, ResourceId, Runtime, State, Webview};
|
||||
|
||||
use crate::{ClipKind, Clipboard, ClipboardContents, Result};
|
||||
use crate::{Clipboard, Result};
|
||||
|
||||
#[command]
|
||||
#[cfg(desktop)]
|
||||
pub(crate) async fn write_text<R: Runtime>(
|
||||
_app: AppHandle<R>,
|
||||
clipboard: State<'_, Clipboard<R>>,
|
||||
data: ClipKind,
|
||||
text: &str,
|
||||
#[allow(unused)] label: Option<String>,
|
||||
) -> Result<()> {
|
||||
clipboard.write_text(data)
|
||||
clipboard.write_text(text)
|
||||
}
|
||||
|
||||
#[command]
|
||||
pub(crate) async fn write_image<R: Runtime>(
|
||||
webview: Webview<R>,
|
||||
#[cfg(not(desktop))]
|
||||
pub(crate) async fn write_text<R: Runtime>(
|
||||
_app: AppHandle<R>,
|
||||
clipboard: State<'_, Clipboard<R>>,
|
||||
data: ClipKind,
|
||||
text: &str,
|
||||
#[allow(unused)] label: Option<&str>,
|
||||
) -> Result<()> {
|
||||
let resources_table = webview.resources_table();
|
||||
clipboard.write_image_inner(data, &resources_table)
|
||||
match label {
|
||||
Some(label) => clipboard.write_text_with_label(text, label),
|
||||
None => clipboard.write_text(text),
|
||||
}
|
||||
}
|
||||
|
||||
#[command]
|
||||
pub(crate) async fn read_text<R: Runtime>(
|
||||
_app: AppHandle<R>,
|
||||
clipboard: State<'_, Clipboard<R>>,
|
||||
) -> Result<ClipboardContents> {
|
||||
) -> Result<String> {
|
||||
clipboard.read_text()
|
||||
}
|
||||
|
||||
#[command]
|
||||
pub(crate) async fn write_image<R: Runtime>(
|
||||
webview: Webview<R>,
|
||||
clipboard: State<'_, Clipboard<R>>,
|
||||
image: JsImage,
|
||||
) -> Result<()> {
|
||||
let resources_table = webview.resources_table();
|
||||
let image = image.into_img(&resources_table)?;
|
||||
clipboard.write_image(&image)
|
||||
}
|
||||
|
||||
#[command]
|
||||
pub(crate) async fn read_image<R: Runtime>(
|
||||
webview: Webview<R>,
|
||||
@@ -48,9 +65,10 @@ pub(crate) async fn read_image<R: Runtime>(
|
||||
pub(crate) async fn write_html<R: Runtime>(
|
||||
_app: AppHandle<R>,
|
||||
clipboard: State<'_, Clipboard<R>>,
|
||||
data: ClipKind,
|
||||
html: &str,
|
||||
alt_text: Option<&str>,
|
||||
) -> Result<()> {
|
||||
clipboard.write_html(data)
|
||||
clipboard.write_html(html, alt_text)
|
||||
}
|
||||
|
||||
#[command]
|
||||
|
||||
@@ -5,9 +5,7 @@
|
||||
use arboard::ImageData;
|
||||
use image::ImageEncoder;
|
||||
use serde::de::DeserializeOwned;
|
||||
use tauri::{image::Image, plugin::PluginApi, AppHandle, Manager, ResourceTable, Runtime};
|
||||
|
||||
use crate::models::*;
|
||||
use tauri::{image::Image, plugin::PluginApi, AppHandle, Runtime};
|
||||
|
||||
use std::{borrow::Cow, sync::Mutex};
|
||||
|
||||
@@ -29,67 +27,50 @@ pub struct Clipboard<R: Runtime> {
|
||||
}
|
||||
|
||||
impl<R: Runtime> Clipboard<R> {
|
||||
pub fn write_text(&self, kind: ClipKind) -> crate::Result<()> {
|
||||
match kind {
|
||||
ClipKind::PlainText { text, .. } => match &self.clipboard {
|
||||
Ok(clipboard) => clipboard.lock().unwrap().set_text(text).map_err(Into::into),
|
||||
Err(e) => Err(crate::Error::Clipboard(e.to_string())),
|
||||
},
|
||||
_ => Err(crate::Error::Clipboard("Invalid clip kind".to_string())),
|
||||
pub fn write_text<'a, T: Into<Cow<'a, str>>>(&self, text: T) -> crate::Result<()> {
|
||||
match &self.clipboard {
|
||||
Ok(clipboard) => clipboard.lock().unwrap().set_text(text).map_err(Into::into),
|
||||
Err(e) => Err(crate::Error::Clipboard(e.to_string())),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn write_image_inner(
|
||||
&self,
|
||||
kind: ClipKind,
|
||||
resources_table: &ResourceTable,
|
||||
) -> crate::Result<()> {
|
||||
match kind {
|
||||
ClipKind::Image { image, .. } => match &self.clipboard {
|
||||
Ok(clipboard) => {
|
||||
let image = image.into_img(resources_table)?;
|
||||
clipboard
|
||||
.lock()
|
||||
.unwrap()
|
||||
.set_image(ImageData {
|
||||
bytes: Cow::Borrowed(image.rgba()),
|
||||
width: image.width() as usize,
|
||||
height: image.height() as usize,
|
||||
})
|
||||
.map_err(Into::into)
|
||||
}
|
||||
Err(e) => Err(crate::Error::Clipboard(e.to_string())),
|
||||
},
|
||||
_ => Err(crate::Error::Clipboard("Invalid clip kind".to_string())),
|
||||
pub fn write_image(&self, image: &Image<'_>) -> crate::Result<()> {
|
||||
match &self.clipboard {
|
||||
Ok(clipboard) => clipboard
|
||||
.lock()
|
||||
.unwrap()
|
||||
.set_image(ImageData {
|
||||
bytes: Cow::Borrowed(image.rgba()),
|
||||
width: image.width() as usize,
|
||||
height: image.height() as usize,
|
||||
})
|
||||
.map_err(Into::into),
|
||||
Err(e) => Err(crate::Error::Clipboard(e.to_string())),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn write_image(&self, kind: ClipKind) -> crate::Result<()> {
|
||||
let resources_table = self.app.resources_table();
|
||||
self.write_image_inner(kind, &resources_table)
|
||||
}
|
||||
|
||||
pub fn read_text(&self) -> crate::Result<ClipboardContents> {
|
||||
pub fn read_text(&self) -> crate::Result<String> {
|
||||
match &self.clipboard {
|
||||
Ok(clipboard) => {
|
||||
let text = clipboard.lock().unwrap().get_text()?;
|
||||
Ok(ClipboardContents::PlainText { text })
|
||||
Ok(text)
|
||||
}
|
||||
Err(e) => Err(crate::Error::Clipboard(e.to_string())),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn write_html(&self, kind: ClipKind) -> crate::Result<()> {
|
||||
match kind {
|
||||
ClipKind::Html { html, alt_html, .. } => match &self.clipboard {
|
||||
Ok(clipboard) => clipboard
|
||||
.lock()
|
||||
.unwrap()
|
||||
.set_html(html, alt_html)
|
||||
.map_err(Into::into),
|
||||
Err(e) => Err(crate::Error::Clipboard(e.to_string())),
|
||||
},
|
||||
_ => Err(crate::Error::Clipboard("Invalid clip kind!".to_string())),
|
||||
pub fn write_html<'a, T: Into<Cow<'a, str>>>(
|
||||
&self,
|
||||
html: T,
|
||||
alt_text: Option<T>,
|
||||
) -> crate::Result<()> {
|
||||
match &self.clipboard {
|
||||
Ok(clipboard) => clipboard
|
||||
.lock()
|
||||
.unwrap()
|
||||
.set_html(html, alt_text)
|
||||
.map_err(Into::into),
|
||||
Err(e) => Err(crate::Error::Clipboard(e.to_string())),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,6 @@ pub enum Error {
|
||||
PluginInvoke(#[from] tauri::plugin::mobile::PluginInvokeError),
|
||||
#[error("{0}")]
|
||||
Clipboard(String),
|
||||
#[cfg(desktop)]
|
||||
#[error(transparent)]
|
||||
Tauri(#[from] tauri::Error),
|
||||
#[cfg(desktop)]
|
||||
|
||||
@@ -16,8 +16,6 @@ use tauri::{
|
||||
Manager, Runtime,
|
||||
};
|
||||
|
||||
pub use models::*;
|
||||
|
||||
#[cfg(desktop)]
|
||||
mod desktop;
|
||||
#[cfg(mobile)]
|
||||
@@ -25,7 +23,6 @@ mod mobile;
|
||||
|
||||
mod commands;
|
||||
mod error;
|
||||
mod models;
|
||||
|
||||
pub use error::{Error, Result};
|
||||
|
||||
|
||||
@@ -3,13 +3,14 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tauri::{
|
||||
image::Image,
|
||||
plugin::{PluginApi, PluginHandle},
|
||||
AppHandle, Runtime,
|
||||
};
|
||||
|
||||
use crate::models::*;
|
||||
use std::borrow::Cow;
|
||||
|
||||
#[cfg(target_os = "android")]
|
||||
const PLUGIN_IDENTIFIER: &str = "app.tauri.clipboard";
|
||||
@@ -33,28 +34,44 @@ pub fn init<R: Runtime, C: DeserializeOwned>(
|
||||
pub struct Clipboard<R: Runtime>(PluginHandle<R>);
|
||||
|
||||
impl<R: Runtime> Clipboard<R> {
|
||||
pub fn write_text(&self, kind: ClipKind) -> crate::Result<()> {
|
||||
self.0.run_mobile_plugin("write", kind).map_err(Into::into)
|
||||
pub fn write_text<'a, T: Into<Cow<'a, str>>>(&self, text: T) -> crate::Result<()> {
|
||||
let text = text.into().to_string();
|
||||
self.0
|
||||
.run_mobile_plugin("write", ClipKind::PlainText { text, label: None })
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
pub(crate) fn write_image_inner(
|
||||
pub fn write_text_with_label<'a, T: Into<Cow<'a, str>>>(
|
||||
&self,
|
||||
kind: ClipKind,
|
||||
resources_table: &tauri::ResourceTable,
|
||||
text: T,
|
||||
label: T,
|
||||
) -> crate::Result<()> {
|
||||
let text = text.into().to_string();
|
||||
let label = label.into().to_string();
|
||||
self.0
|
||||
.run_mobile_plugin(
|
||||
"write",
|
||||
ClipKind::PlainText {
|
||||
text,
|
||||
label: Some(label),
|
||||
},
|
||||
)
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
pub fn write_image(&self, _image: &Image<'_>) -> crate::Result<()> {
|
||||
Err(crate::Error::Clipboard(
|
||||
"Unsupported on this platform".to_string(),
|
||||
))
|
||||
}
|
||||
|
||||
pub fn write_image(&self, kind: ClipKind) -> crate::Result<()> {
|
||||
Err(crate::Error::Clipboard(
|
||||
"Unsupported on this platform".to_string(),
|
||||
))
|
||||
}
|
||||
|
||||
pub fn read_text(&self) -> crate::Result<ClipboardContents> {
|
||||
self.0.run_mobile_plugin("read", ()).map_err(Into::into)
|
||||
pub fn read_text(&self) -> crate::Result<String> {
|
||||
self.0
|
||||
.run_mobile_plugin("read", ())
|
||||
.map(|c| match c {
|
||||
ClipboardContents::PlainText { text } => text,
|
||||
})
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
pub fn read_image(&self) -> crate::Result<Image<'_>> {
|
||||
@@ -64,7 +81,11 @@ impl<R: Runtime> Clipboard<R> {
|
||||
}
|
||||
|
||||
// Treat HTML as unsupported on mobile until tested
|
||||
pub fn write_html(&self, _kind: ClipKind) -> crate::Result<()> {
|
||||
pub fn write_html<'a, T: Into<Cow<'a, str>>>(
|
||||
&self,
|
||||
_html: T,
|
||||
_alt_text: Option<T>,
|
||||
) -> crate::Result<()> {
|
||||
Err(crate::Error::Clipboard(
|
||||
"Unsupported on this platform".to_string(),
|
||||
))
|
||||
@@ -76,3 +97,15 @@ impl<R: Runtime> Clipboard<R> {
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
enum ClipKind {
|
||||
PlainText { label: Option<String>, text: String },
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
enum ClipboardContents {
|
||||
PlainText { text: String },
|
||||
}
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[cfg_attr(mobile, derive(Serialize))]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum ClipKind {
|
||||
PlainText {
|
||||
label: Option<String>,
|
||||
text: String,
|
||||
},
|
||||
#[cfg(desktop)]
|
||||
Image {
|
||||
image: tauri::image::JsImage,
|
||||
},
|
||||
Html {
|
||||
html: String,
|
||||
alt_html: Option<String>,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum ClipboardContents {
|
||||
PlainText {
|
||||
text: String,
|
||||
},
|
||||
Image {
|
||||
bytes: Vec<u8>,
|
||||
width: usize,
|
||||
height: usize,
|
||||
},
|
||||
}
|
||||
Reference in New Issue
Block a user