feat(clipboard): support readImage & writeImage (#845)

* feat(clipboard): support `read_image` & `write_image`

* fix plugin name

* platform specific bahavior

* remove unnecessary BufWriter

* improvement

* update example

* update example

* format

* header, fix change file

* use image from tauri

* fix ci

* update tauri, fix read

* image crate only on desktop [skip ci]

* Update plugins/authenticator/src/u2f_crate/protocol.rs [skip ci]

Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com>

* Update plugins/authenticator/src/u2f_crate/protocol.rs [skip ci]

Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com>

* update deps, address code review

* fix mobile [skip ci]

---------

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
Co-authored-by: Lucas Nogueira <lucas@tauri.app>
Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com>
This commit is contained in:
阿良仔
2024-03-19 20:35:37 +08:00
committed by GitHub
parent 7b9fa6607b
commit 9dec9605ed
91 changed files with 1331 additions and 795 deletions
+1 -1
View File
@@ -24,6 +24,6 @@
"LICENSE"
],
"dependencies": {
"@tauri-apps/api": "2.0.0-beta.4"
"@tauri-apps/api": "2.0.0-beta.6"
}
}
@@ -139,14 +139,10 @@
},
"platforms": {
"description": "Target platforms this permission applies. By default all platforms are affected by this permission.",
"default": [
"linux",
"macOS",
"windows",
"android",
"iOS"
"type": [
"array",
"null"
],
"type": "array",
"items": {
"$ref": "#/definitions/Target"
}
@@ -95,7 +95,8 @@ impl U2f {
challenge: Challenge,
response: RegisterResponse,
) -> Result<Registration> {
if expiration(challenge.timestamp) > Duration::seconds(300) {
// Safe to unwrap since 300 is within the constraints of Duration::try_seconds
if expiration(challenge.timestamp) > Duration::try_seconds(300).unwrap() {
return Err(U2fError::ChallengeExpired);
}
@@ -152,7 +153,8 @@ impl U2f {
sign_resp: SignResponse,
counter: u32,
) -> Result<u32> {
if expiration(challenge.timestamp) > Duration::seconds(300) {
// Safe to unwrap since 300 is within the constraints of Duration::try_seconds
if expiration(challenge.timestamp) > Duration::try_seconds(300).unwrap() {
return Err(U2fError::ChallengeExpired);
}
+1 -1
View File
@@ -23,6 +23,6 @@
"LICENSE"
],
"dependencies": {
"@tauri-apps/api": "2.0.0-beta.4"
"@tauri-apps/api": "2.0.0-beta.6"
}
}
@@ -139,14 +139,10 @@
},
"platforms": {
"description": "Target platforms this permission applies. By default all platforms are affected by this permission.",
"default": [
"linux",
"macOS",
"windows",
"android",
"iOS"
"type": [
"array",
"null"
],
"type": "array",
"items": {
"$ref": "#/definitions/Target"
}
+1 -1
View File
@@ -24,6 +24,6 @@
"LICENSE"
],
"dependencies": {
"@tauri-apps/api": "2.0.0-beta.4"
"@tauri-apps/api": "2.0.0-beta.6"
}
}
@@ -139,14 +139,10 @@
},
"platforms": {
"description": "Target platforms this permission applies. By default all platforms are affected by this permission.",
"default": [
"linux",
"macOS",
"windows",
"android",
"iOS"
"type": [
"array",
"null"
],
"type": "array",
"items": {
"$ref": "#/definitions/Target"
}
+1 -1
View File
@@ -27,6 +27,6 @@
"tslib": "2.6.0"
},
"dependencies": {
"@tauri-apps/api": "2.0.0-beta.4"
"@tauri-apps/api": "2.0.0-beta.6"
}
}
@@ -139,14 +139,10 @@
},
"platforms": {
"description": "Target platforms this permission applies. By default all platforms are affected by this permission.",
"default": [
"linux",
"macOS",
"windows",
"android",
"iOS"
"type": [
"array",
"null"
],
"type": "array",
"items": {
"$ref": "#/definitions/Target"
}
+1 -1
View File
@@ -23,6 +23,6 @@
"LICENSE"
],
"dependencies": {
"@tauri-apps/api": "2.0.0-beta.4"
"@tauri-apps/api": "2.0.0-beta.6"
}
}
+3 -7
View File
@@ -139,14 +139,10 @@
},
"platforms": {
"description": "Target platforms this permission applies. By default all platforms are affected by this permission.",
"default": [
"linux",
"macOS",
"windows",
"android",
"iOS"
"type": [
"array",
"null"
],
"type": "array",
"items": {
"$ref": "#/definitions/Target"
}
+1
View File
@@ -25,3 +25,4 @@ thiserror = { workspace = true }
[target."cfg(any(target_os = \"macos\", windows, target_os = \"linux\", target_os = \"dragonfly\", target_os = \"freebsd\", target_os = \"openbsd\", target_os = \"netbsd\"))".dependencies]
arboard = "3"
image = "0.24"
@@ -87,7 +87,7 @@ class ClipboardPlugin(private val activity: Activity) : Plugin(activity) {
@Command
@Suppress("MoveVariableDeclarationIntoWhen")
fun write(invoke: Invoke) {
fun writeText(invoke: Invoke) {
val args = invoke.parseArgs(WriteOptions::class.java)
val clipData = when (args) {
@@ -106,7 +106,7 @@ class ClipboardPlugin(private val activity: Activity) : Plugin(activity) {
}
@Command
fun read(invoke: Invoke) {
fun readText(invoke: Invoke) {
val data = if (manager.hasPrimaryClip()) {
if (manager.primaryClipDescription?.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN) == true) {
val item: ClipData.Item = manager.primaryClip!!.getItemAt(0)
+8 -1
View File
@@ -2,7 +2,14 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
const COMMANDS: &[&str] = &["write", "read"];
const COMMANDS: &[&str] = &[
"write_text",
"read_text",
"write_image",
"read_image",
"write_html",
"clear",
];
fn main() {
if let Err(error) = tauri_plugin::Builder::new(COMMANDS)
+53 -4
View File
@@ -9,6 +9,7 @@
*/
import { invoke } from "@tauri-apps/api/core";
import { Image, transformImage } from "@tauri-apps/api/image";
type ClipResponse = Record<"plainText", { text: string }>;
@@ -29,7 +30,7 @@ async function writeText(
text: string,
opts?: { label?: string },
): Promise<void> {
return invoke("plugin:clipboard-manager|write", {
return invoke("plugin:clipboard-manager|write_text", {
data: {
plainText: {
label: opts?.label,
@@ -49,12 +50,60 @@ async function writeText(
* @since 2.0.0
*/
async function readText(): Promise<string> {
const kind: ClipResponse = await invoke("plugin:clipboard-manager|read");
const kind: ClipResponse = await invoke("plugin:clipboard-manager|read_text");
return kind.plainText.text;
}
/**
* Writes HTML or fallbacks to write provided plain text to the clipboard.
* Gets the clipboard content as Uint8Array image.
* @example
* ```typescript
* import { readImage } from '@tauri-apps/plugin-clipboard-manager';
*
* const clipboardImage = await readImage();
* const blob = new Blob([clipboardImage.bytes], { type: 'image' })
* const url = URL.createObjectURL(blob)
* ```
* @since 2.0.0
*/
async function readImage(): Promise<Image> {
return await invoke<number>("plugin:clipboard-manager|read_image").then(
(rid) => new Image(rid),
);
}
/**
* Writes image buffer to the clipboard.
* @example
* ```typescript
* import { writeImage } from '@tauri-apps/plugin-clipboard-manager';
* const buffer = [
* // A red pixel
* 255, 0, 0, 255,
*
* // A green pixel
* 0, 255, 0, 255,
* ];
* await writeImage(buffer);
*
* @returns A promise indicating the success or failure of the operation.
*
* @since 2.0.0
*/
async function writeImage(
image: string | Image | Uint8Array | ArrayBuffer | number[],
): Promise<void> {
return invoke("plugin:clipboard-manager|write_image", {
data: {
image: {
image: transformImage(image),
},
},
});
}
/**
* * Writes HTML or fallbacks to write provided plain text to the clipboard.
* @example
* ```typescript
* import { writeHtml, readHtml } from '@tauri-apps/plugin-clipboard-manager';
@@ -92,4 +141,4 @@ async function clear(): Promise<void> {
return;
}
export { writeText, readText, writeHtml, clear };
export { writeText, readText, writeHtml, clear, readImage, writeImage };
@@ -16,7 +16,7 @@ enum ReadClipData: Codable {
}
class ClipboardPlugin: Plugin {
@objc public func write(_ invoke: Invoke) throws {
@objc public func writeText(_ invoke: Invoke) throws {
let options = try invoke.parseArgs(WriteOptions.self)
let clipboard = UIPasteboard.general
switch options {
@@ -30,7 +30,7 @@ class ClipboardPlugin: Plugin {
}
@objc public func read(_ invoke: Invoke) throws {
@objc public func readText(_ invoke: Invoke) throws {
let clipboard = UIPasteboard.general
if let text = clipboard.string {
invoke.resolve(ReadClipData.plainText(text: text))
+1 -1
View File
@@ -23,6 +23,6 @@
"LICENSE"
],
"dependencies": {
"@tauri-apps/api": "2.0.0-beta.4"
"@tauri-apps/api": "2.0.0-beta.6"
}
}
@@ -0,0 +1,13 @@
# Automatically generated - DO NOT EDIT!
"$schema" = "../../schemas/schema.json"
[[permission]]
identifier = "allow-clear"
description = "Enables the clear command without any pre-configured scope."
commands.allow = ["clear"]
[[permission]]
identifier = "deny-clear"
description = "Denies the clear command without any pre-configured scope."
commands.deny = ["clear"]
@@ -1,13 +0,0 @@
# Automatically generated - DO NOT EDIT!
"$schema" = "../../schemas/schema.json"
[[permission]]
identifier = "allow-read"
description = "Enables the read command without any pre-configured scope."
commands.allow = ["read"]
[[permission]]
identifier = "deny-read"
description = "Denies the read command without any pre-configured scope."
commands.deny = ["read"]
@@ -0,0 +1,13 @@
# Automatically generated - DO NOT EDIT!
"$schema" = "../../schemas/schema.json"
[[permission]]
identifier = "allow-read-image"
description = "Enables the read_image command without any pre-configured scope."
commands.allow = ["read_image"]
[[permission]]
identifier = "deny-read-image"
description = "Denies the read_image command without any pre-configured scope."
commands.deny = ["read_image"]
@@ -0,0 +1,13 @@
# Automatically generated - DO NOT EDIT!
"$schema" = "../../schemas/schema.json"
[[permission]]
identifier = "allow-read-text"
description = "Enables the read_text command without any pre-configured scope."
commands.allow = ["read_text"]
[[permission]]
identifier = "deny-read-text"
description = "Denies the read_text command without any pre-configured scope."
commands.deny = ["read_text"]
@@ -1,13 +0,0 @@
# Automatically generated - DO NOT EDIT!
"$schema" = "../../schemas/schema.json"
[[permission]]
identifier = "allow-write"
description = "Enables the write command without any pre-configured scope."
commands.allow = ["write"]
[[permission]]
identifier = "deny-write"
description = "Denies the write command without any pre-configured scope."
commands.deny = ["write"]
@@ -0,0 +1,13 @@
# Automatically generated - DO NOT EDIT!
"$schema" = "../../schemas/schema.json"
[[permission]]
identifier = "allow-write-html"
description = "Enables the write_html command without any pre-configured scope."
commands.allow = ["write_html"]
[[permission]]
identifier = "deny-write-html"
description = "Denies the write_html command without any pre-configured scope."
commands.deny = ["write_html"]
@@ -0,0 +1,13 @@
# Automatically generated - DO NOT EDIT!
"$schema" = "../../schemas/schema.json"
[[permission]]
identifier = "allow-write-image"
description = "Enables the write_image command without any pre-configured scope."
commands.allow = ["write_image"]
[[permission]]
identifier = "deny-write-image"
description = "Denies the write_image command without any pre-configured scope."
commands.deny = ["write_image"]
@@ -0,0 +1,13 @@
# Automatically generated - DO NOT EDIT!
"$schema" = "../../schemas/schema.json"
[[permission]]
identifier = "allow-write-text"
description = "Enables the write_text command without any pre-configured scope."
commands.allow = ["write_text"]
[[permission]]
identifier = "deny-write-text"
description = "Denies the write_text command without any pre-configured scope."
commands.deny = ["write_text"]
@@ -1,6 +1,14 @@
| Permission | Description |
|------|-----|
|`allow-read`|Enables the read command without any pre-configured scope.|
|`deny-read`|Denies the read command without any pre-configured scope.|
|`allow-write`|Enables the write command without any pre-configured scope.|
|`deny-write`|Denies the write command without any pre-configured scope.|
|`allow-clear`|Enables the clear command without any pre-configured scope.|
|`deny-clear`|Denies the clear command without any pre-configured scope.|
|`allow-read-image`|Enables the read_image command without any pre-configured scope.|
|`deny-read-image`|Denies the read_image command without any pre-configured scope.|
|`allow-read-text`|Enables the read_text command without any pre-configured scope.|
|`deny-read-text`|Denies the read_text command without any pre-configured scope.|
|`allow-write-html`|Enables the write_html command without any pre-configured scope.|
|`deny-write-html`|Denies the write_html command without any pre-configured scope.|
|`allow-write-image`|Enables the write_image command without any pre-configured scope.|
|`deny-write-image`|Denies the write_image command without any pre-configured scope.|
|`allow-write-text`|Enables the write_text command without any pre-configured scope.|
|`deny-write-text`|Denies the write_text command without any pre-configured scope.|
@@ -139,14 +139,10 @@
},
"platforms": {
"description": "Target platforms this permission applies. By default all platforms are affected by this permission.",
"default": [
"linux",
"macOS",
"windows",
"android",
"iOS"
"type": [
"array",
"null"
],
"type": "array",
"items": {
"$ref": "#/definitions/Target"
}
@@ -299,31 +295,87 @@
"type": "string",
"oneOf": [
{
"description": "allow-read -> Enables the read command without any pre-configured scope.",
"description": "allow-clear -> Enables the clear command without any pre-configured scope.",
"type": "string",
"enum": [
"allow-read"
"allow-clear"
]
},
{
"description": "deny-read -> Denies the read command without any pre-configured scope.",
"description": "deny-clear -> Denies the clear command without any pre-configured scope.",
"type": "string",
"enum": [
"deny-read"
"deny-clear"
]
},
{
"description": "allow-write -> Enables the write command without any pre-configured scope.",
"description": "allow-read-image -> Enables the read_image command without any pre-configured scope.",
"type": "string",
"enum": [
"allow-write"
"allow-read-image"
]
},
{
"description": "deny-write -> Denies the write command without any pre-configured scope.",
"description": "deny-read-image -> Denies the read_image command without any pre-configured scope.",
"type": "string",
"enum": [
"deny-write"
"deny-read-image"
]
},
{
"description": "allow-read-text -> Enables the read_text command without any pre-configured scope.",
"type": "string",
"enum": [
"allow-read-text"
]
},
{
"description": "deny-read-text -> Denies the read_text command without any pre-configured scope.",
"type": "string",
"enum": [
"deny-read-text"
]
},
{
"description": "allow-write-html -> Enables the write_html command without any pre-configured scope.",
"type": "string",
"enum": [
"allow-write-html"
]
},
{
"description": "deny-write-html -> Denies the write_html command without any pre-configured scope.",
"type": "string",
"enum": [
"deny-write-html"
]
},
{
"description": "allow-write-image -> Enables the write_image command without any pre-configured scope.",
"type": "string",
"enum": [
"allow-write-image"
]
},
{
"description": "deny-write-image -> Denies the write_image command without any pre-configured scope.",
"type": "string",
"enum": [
"deny-write-image"
]
},
{
"description": "allow-write-text -> Enables the write_text command without any pre-configured scope.",
"type": "string",
"enum": [
"allow-write-text"
]
},
{
"description": "deny-write-text -> Denies the write_text command without any pre-configured scope.",
"type": "string",
"enum": [
"deny-write-text"
]
}
]
+1 -1
View File
@@ -1 +1 @@
if("__TAURI__"in window){var __TAURI_PLUGIN_CLIPBOARDMANAGER__=function(n){"use strict";async function r(n,r={},a){return window.__TAURI_INTERNALS__.invoke(n,r,a)}return"function"==typeof SuppressedError&&SuppressedError,n.clear=async function(){await r("plugin:clipboard-manager|clear")},n.readText=async function(){return(await r("plugin:clipboard-manager|read")).plainText.text},n.writeHtml=async function(n,a){return r("plugin:clipboard-manager|write_html",{data:{html:{html:n,altHtml:a}}})},n.writeText=async function(n,a){return r("plugin:clipboard-manager|write",{data:{plainText:{label:a?.label,text:n}}})},n}({});Object.defineProperty(window.__TAURI__,"clipboardManager",{value:__TAURI_PLUGIN_CLIPBOARDMANAGER__})}
if("__TAURI__"in window){var __TAURI_PLUGIN_CLIPBOARDMANAGER__=function(e){"use strict";var r;async function t(e,r={},t){return window.__TAURI_INTERNALS__.invoke(e,r,t)}"function"==typeof SuppressedError&&SuppressedError;class n{get rid(){return function(e,r,t,n){if("a"===t&&!n)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof r?e!==r||!n:!r.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===t?n:"a"===t?n.call(e):n?n.value:r.get(e)}(this,r,"f")}constructor(e){r.set(this,void 0),function(e,r,t,n,a){if("m"===n)throw new TypeError("Private method is not writable");if("a"===n&&!a)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof r?e!==r||!a:!r.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");"a"===n?a.call(e,t):a?a.value=t:r.set(e,t)}(this,r,e,"f")}async close(){return t("plugin:resources|close",{rid:this.rid})}}r=new WeakMap;class a extends n{constructor(e){super(e)}static async new(e,r,n){return t("plugin:image|new",{rgba:i(e),width:r,height:n}).then((e=>new a(e)))}static async fromBytes(e){return t("plugin:image|from_bytes",{bytes:i(e)}).then((e=>new a(e)))}static async fromPath(e){return t("plugin:image|from_path",{path:e}).then((e=>new a(e)))}async rgba(){return t("plugin:image|rgba",{rid:this.rid}).then((e=>new Uint8Array(e)))}async size(){return t("plugin:image|size",{rid:this.rid})}}function i(e){return null==e?null:"string"==typeof e?e:e instanceof Uint8Array?Array.from(e):e instanceof ArrayBuffer?Array.from(new Uint8Array(e)):e instanceof a?e.rid:e}return e.clear=async function(){await t("plugin:clipboard-manager|clear")},e.readImage=async function(){return await t("plugin:clipboard-manager|read_image").then((e=>new a(e)))},e.readText=async function(){return(await t("plugin:clipboard-manager|read_text")).plainText.text},e.writeHtml=async function(e,r){return t("plugin:clipboard-manager|write_html",{data:{html:{html:e,altHtml:r}}})},e.writeImage=async function(e){return t("plugin:clipboard-manager|write_image",{data:{image:{image:i(e)}}})},e.writeText=async function(e,r){return t("plugin:clipboard-manager|write_text",{data:{plainText:{label:r?.label,text:e}}})},e}({});Object.defineProperty(window.__TAURI__,"clipboardManager",{value:__TAURI_PLUGIN_CLIPBOARDMANAGER__})}
+25 -5
View File
@@ -2,25 +2,45 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
use tauri::{command, AppHandle, Runtime, State};
use tauri::{command, AppHandle, Manager, ResourceId, Runtime, State};
use crate::{ClipKind, Clipboard, ClipboardContents, Result};
#[command]
pub(crate) async fn write<R: Runtime>(
pub(crate) async fn write_text<R: Runtime>(
_app: AppHandle<R>,
clipboard: State<'_, Clipboard<R>>,
data: ClipKind,
) -> Result<()> {
clipboard.write(data)
clipboard.write_text(data)
}
#[command]
pub(crate) async fn read<R: Runtime>(
pub(crate) async fn write_image<R: Runtime>(
_app: AppHandle<R>,
clipboard: State<'_, Clipboard<R>>,
data: ClipKind,
) -> Result<()> {
clipboard.write_image(data)
}
#[command]
pub(crate) async fn read_text<R: Runtime>(
_app: AppHandle<R>,
clipboard: State<'_, Clipboard<R>>,
) -> Result<ClipboardContents> {
clipboard.read()
clipboard.read_text()
}
#[command]
pub(crate) async fn read_image<R: Runtime>(
app: AppHandle<R>,
clipboard: State<'_, Clipboard<R>>,
) -> Result<ResourceId> {
let image = clipboard.read_image()?.to_owned();
let mut resources_table = app.resources_table();
let rid = resources_table.add(image);
Ok(rid)
}
#[command]
+48 -5
View File
@@ -2,12 +2,14 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
use arboard::ImageData;
use image::ImageEncoder;
use serde::de::DeserializeOwned;
use tauri::{plugin::PluginApi, AppHandle, Runtime};
use tauri::{image::Image, plugin::PluginApi, AppHandle, Runtime};
use crate::models::*;
use std::sync::Mutex;
use std::{borrow::Cow, sync::Mutex};
pub fn init<R: Runtime, C: DeserializeOwned>(
app: &AppHandle<R>,
@@ -27,17 +29,38 @@ pub struct Clipboard<R: Runtime> {
}
impl<R: Runtime> Clipboard<R> {
pub fn write(&self, kind: ClipKind) -> crate::Result<()> {
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())),
_ => Err(crate::Error::Clipboard("Invalid clip kind".to_string())),
}
}
pub fn read(&self) -> crate::Result<ClipboardContents> {
pub fn write_image(&self, kind: ClipKind) -> crate::Result<()> {
match kind {
ClipKind::Image { image, .. } => match &self.clipboard {
Ok(clipboard) => {
let image = image.into_img(&self.app)?;
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 read_text(&self) -> crate::Result<ClipboardContents> {
match &self.clipboard {
Ok(clipboard) => {
let text = clipboard.lock().unwrap().get_text()?;
@@ -67,4 +90,24 @@ impl<R: Runtime> Clipboard<R> {
Err(e) => Err(crate::Error::Clipboard(e.to_string())),
}
}
pub fn read_image(&self) -> crate::Result<Image<'_>> {
match &self.clipboard {
Ok(clipboard) => {
let image = clipboard.lock().unwrap().get_image()?;
let mut buffer: Vec<u8> = Vec::new();
image::codecs::png::PngEncoder::new(&mut buffer).write_image(
&image.bytes,
image.width as u32,
image.height as u32,
image::ColorType::Rgba8,
)?;
let image = Image::new_owned(buffer, image.width as u32, image.height as u32);
Ok(image)
}
Err(e) => Err(crate::Error::Clipboard(e.to_string())),
}
}
}
+6
View File
@@ -13,6 +13,12 @@ pub enum Error {
PluginInvoke(#[from] tauri::plugin::mobile::PluginInvokeError),
#[error("{0}")]
Clipboard(String),
#[cfg(desktop)]
#[error(transparent)]
Tauri(#[from] tauri::Error),
#[cfg(desktop)]
#[error("invalid image: {0}")]
Image(#[from] image::ImageError),
}
impl Serialize for Error {
+4 -4
View File
@@ -50,11 +50,11 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
Builder::new("clipboard-manager")
.js_init_script(include_str!("api-iife.js").to_string())
.invoke_handler(tauri::generate_handler![
commands::write,
commands::read,
#[cfg(desktop)]
commands::write_text,
commands::read_text,
commands::read_image,
commands::write_image,
commands::write_html,
#[cfg(desktop)]
commands::clear
])
.setup(|app, api| {
+15 -2
View File
@@ -4,6 +4,7 @@
use serde::de::DeserializeOwned;
use tauri::{
image::Image,
plugin::{PluginApi, PluginHandle},
AppHandle, Runtime,
};
@@ -32,14 +33,26 @@ pub fn init<R: Runtime, C: DeserializeOwned>(
pub struct Clipboard<R: Runtime>(PluginHandle<R>);
impl<R: Runtime> Clipboard<R> {
pub fn write(&self, kind: ClipKind) -> crate::Result<()> {
pub fn write_text(&self, kind: ClipKind) -> crate::Result<()> {
self.0.run_mobile_plugin("write", kind).map_err(Into::into)
}
pub fn read(&self) -> crate::Result<ClipboardContents> {
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_image(&self) -> crate::Result<Image<'_>> {
Err(crate::Error::Clipboard(
"Unsupported on this platform".to_string(),
))
}
// Treat HTML as unsupported on mobile until tested
pub fn write_html(&self, _kind: ClipKind) -> crate::Result<()> {
Err(crate::Error::Clipboard(
+14 -2
View File
@@ -4,13 +4,18 @@
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
#[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>,
@@ -20,5 +25,12 @@ pub enum ClipKind {
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum ClipboardContents {
PlainText { text: String },
PlainText {
text: String,
},
Image {
bytes: Vec<u8>,
width: usize,
height: usize,
},
}
+2 -2
View File
@@ -10,11 +10,11 @@
"tauri": "tauri"
},
"dependencies": {
"@tauri-apps/api": "2.0.0-beta.4",
"@tauri-apps/api": "2.0.0-beta.6",
"@tauri-apps/plugin-deep-link": "2.0.0-beta.2"
},
"devDependencies": {
"@tauri-apps/cli": "2.0.0-beta.7",
"@tauri-apps/cli": "2.0.0-beta.9",
"internal-ip": "^8.0.0",
"typescript": "^5.2.2",
"vite": "^5.0.12"
+1 -1
View File
@@ -24,6 +24,6 @@
"LICENSE"
],
"dependencies": {
"@tauri-apps/api": "2.0.0-beta.4"
"@tauri-apps/api": "2.0.0-beta.6"
}
}
@@ -139,14 +139,10 @@
},
"platforms": {
"description": "Target platforms this permission applies. By default all platforms are affected by this permission.",
"default": [
"linux",
"macOS",
"windows",
"android",
"iOS"
"type": [
"array",
"null"
],
"type": "array",
"items": {
"$ref": "#/definitions/Target"
}
+1 -1
View File
@@ -23,6 +23,6 @@
"LICENSE"
],
"dependencies": {
"@tauri-apps/api": "2.0.0-beta.4"
"@tauri-apps/api": "2.0.0-beta.6"
}
}
@@ -139,14 +139,10 @@
},
"platforms": {
"description": "Target platforms this permission applies. By default all platforms are affected by this permission.",
"default": [
"linux",
"macOS",
"windows",
"android",
"iOS"
"type": [
"array",
"null"
],
"type": "array",
"items": {
"$ref": "#/definitions/Target"
}
+1 -1
View File
@@ -24,6 +24,6 @@
"LICENSE"
],
"dependencies": {
"@tauri-apps/api": "2.0.0-beta.4"
"@tauri-apps/api": "2.0.0-beta.6"
}
}
+3 -7
View File
@@ -139,14 +139,10 @@
},
"platforms": {
"description": "Target platforms this permission applies. By default all platforms are affected by this permission.",
"default": [
"linux",
"macOS",
"windows",
"android",
"iOS"
"type": [
"array",
"null"
],
"type": "array",
"items": {
"$ref": "#/definitions/Target"
}
+1 -1
View File
@@ -23,6 +23,6 @@
"LICENSE"
],
"dependencies": {
"@tauri-apps/api": "2.0.0-beta.4"
"@tauri-apps/api": "2.0.0-beta.6"
}
}
@@ -139,14 +139,10 @@
},
"platforms": {
"description": "Target platforms this permission applies. By default all platforms are affected by this permission.",
"default": [
"linux",
"macOS",
"windows",
"android",
"iOS"
"type": [
"array",
"null"
],
"type": "array",
"items": {
"$ref": "#/definitions/Target"
}
+1 -1
View File
@@ -23,6 +23,6 @@
"LICENSE"
],
"dependencies": {
"@tauri-apps/api": "2.0.0-beta.4"
"@tauri-apps/api": "2.0.0-beta.6"
}
}
+3 -7
View File
@@ -139,14 +139,10 @@
},
"platforms": {
"description": "Target platforms this permission applies. By default all platforms are affected by this permission.",
"default": [
"linux",
"macOS",
"windows",
"android",
"iOS"
"type": [
"array",
"null"
],
"type": "array",
"items": {
"$ref": "#/definitions/Target"
}
+1 -1
View File
@@ -24,6 +24,6 @@
"LICENSE"
],
"dependencies": {
"@tauri-apps/api": "2.0.0-beta.4"
"@tauri-apps/api": "2.0.0-beta.6"
}
}
+3 -7
View File
@@ -139,14 +139,10 @@
},
"platforms": {
"description": "Target platforms this permission applies. By default all platforms are affected by this permission.",
"default": [
"linux",
"macOS",
"windows",
"android",
"iOS"
"type": [
"array",
"null"
],
"type": "array",
"items": {
"$ref": "#/definitions/Target"
}
+1 -1
View File
@@ -27,6 +27,6 @@
"tslib": "2.6.0"
},
"dependencies": {
"@tauri-apps/api": "2.0.0-beta.4"
"@tauri-apps/api": "2.0.0-beta.6"
}
}
+3 -7
View File
@@ -139,14 +139,10 @@
},
"platforms": {
"description": "Target platforms this permission applies. By default all platforms are affected by this permission.",
"default": [
"linux",
"macOS",
"windows",
"android",
"iOS"
"type": [
"array",
"null"
],
"type": "array",
"items": {
"$ref": "#/definitions/Target"
}
-1
View File
@@ -33,7 +33,6 @@ windows-version = { version = "0.1", optional = true }
[target."cfg(all(unix, not(target_os = \"macos\")))".dependencies]
lazy_static = { version = "1", optional = true }
image = { version = "0.24", optional = true }
zbus = { version = "4", optional = true }
log = "0.4"
env_logger = { version = "0.10", optional = true }
+1 -1
View File
@@ -23,6 +23,6 @@
"LICENSE"
],
"dependencies": {
"@tauri-apps/api": "2.0.0-beta.4"
"@tauri-apps/api": "2.0.0-beta.6"
}
}
@@ -139,14 +139,10 @@
},
"platforms": {
"description": "Target platforms this permission applies. By default all platforms are affected by this permission.",
"default": [
"linux",
"macOS",
"windows",
"android",
"iOS"
"type": [
"array",
"null"
],
"type": "array",
"items": {
"$ref": "#/definitions/Target"
}
+1 -1
View File
@@ -23,6 +23,6 @@
"LICENSE"
],
"dependencies": {
"@tauri-apps/api": "2.0.0-beta.4"
"@tauri-apps/api": "2.0.0-beta.6"
}
}
+3 -7
View File
@@ -139,14 +139,10 @@
},
"platforms": {
"description": "Target platforms this permission applies. By default all platforms are affected by this permission.",
"default": [
"linux",
"macOS",
"windows",
"android",
"iOS"
"type": [
"array",
"null"
],
"type": "array",
"items": {
"$ref": "#/definitions/Target"
}
+1 -1
View File
@@ -24,6 +24,6 @@
"LICENSE"
],
"dependencies": {
"@tauri-apps/api": "2.0.0-beta.4"
"@tauri-apps/api": "2.0.0-beta.6"
}
}
@@ -139,14 +139,10 @@
},
"platforms": {
"description": "Target platforms this permission applies. By default all platforms are affected by this permission.",
"default": [
"linux",
"macOS",
"windows",
"android",
"iOS"
"type": [
"array",
"null"
],
"type": "array",
"items": {
"$ref": "#/definitions/Target"
}
+1 -1
View File
@@ -23,6 +23,6 @@
"LICENSE"
],
"dependencies": {
"@tauri-apps/api": "2.0.0-beta.4"
"@tauri-apps/api": "2.0.0-beta.6"
}
}
@@ -139,14 +139,10 @@
},
"platforms": {
"description": "Target platforms this permission applies. By default all platforms are affected by this permission.",
"default": [
"linux",
"macOS",
"windows",
"android",
"iOS"
"type": [
"array",
"null"
],
"type": "array",
"items": {
"$ref": "#/definitions/Target"
}
+1 -1
View File
@@ -23,6 +23,6 @@
"LICENSE"
],
"dependencies": {
"@tauri-apps/api": "2.0.0-beta.4"
"@tauri-apps/api": "2.0.0-beta.6"
}
}
@@ -139,14 +139,10 @@
},
"platforms": {
"description": "Target platforms this permission applies. By default all platforms are affected by this permission.",
"default": [
"linux",
"macOS",
"windows",
"android",
"iOS"
"type": [
"array",
"null"
],
"type": "array",
"items": {
"$ref": "#/definitions/Target"
}
@@ -9,6 +9,6 @@
"author": "",
"license": "MIT",
"devDependencies": {
"@tauri-apps/cli": "2.0.0-beta.7"
"@tauri-apps/cli": "2.0.0-beta.9"
}
}
+1 -1
View File
@@ -24,6 +24,6 @@
"LICENSE"
],
"dependencies": {
"@tauri-apps/api": "2.0.0-beta.4"
"@tauri-apps/api": "2.0.0-beta.6"
}
}
+3 -7
View File
@@ -139,14 +139,10 @@
},
"platforms": {
"description": "Target platforms this permission applies. By default all platforms are affected by this permission.",
"default": [
"linux",
"macOS",
"windows",
"android",
"iOS"
"type": [
"array",
"null"
],
"type": "array",
"items": {
"$ref": "#/definitions/Target"
}
+1 -1
View File
@@ -24,6 +24,6 @@
"LICENSE"
],
"dependencies": {
"@tauri-apps/api": "2.0.0-beta.4"
"@tauri-apps/api": "2.0.0-beta.6"
}
}
@@ -139,14 +139,10 @@
},
"platforms": {
"description": "Target platforms this permission applies. By default all platforms are affected by this permission.",
"default": [
"linux",
"macOS",
"windows",
"android",
"iOS"
"type": [
"array",
"null"
],
"type": "array",
"items": {
"$ref": "#/definitions/Target"
}
+1 -1
View File
@@ -24,6 +24,6 @@
"LICENSE"
],
"dependencies": {
"@tauri-apps/api": "2.0.0-beta.4"
"@tauri-apps/api": "2.0.0-beta.6"
}
}
@@ -139,14 +139,10 @@
},
"platforms": {
"description": "Target platforms this permission applies. By default all platforms are affected by this permission.",
"default": [
"linux",
"macOS",
"windows",
"android",
"iOS"
"type": [
"array",
"null"
],
"type": "array",
"items": {
"$ref": "#/definitions/Target"
}
+1 -1
View File
@@ -23,6 +23,6 @@
"LICENSE"
],
"dependencies": {
"@tauri-apps/api": "2.0.0-beta.4"
"@tauri-apps/api": "2.0.0-beta.6"
}
}
@@ -139,14 +139,10 @@
},
"platforms": {
"description": "Target platforms this permission applies. By default all platforms are affected by this permission.",
"default": [
"linux",
"macOS",
"windows",
"android",
"iOS"
"type": [
"array",
"null"
],
"type": "array",
"items": {
"$ref": "#/definitions/Target"
}
+1 -1
View File
@@ -24,6 +24,6 @@
"LICENSE"
],
"dependencies": {
"@tauri-apps/api": "2.0.0-beta.4"
"@tauri-apps/api": "2.0.0-beta.6"
}
}
@@ -139,14 +139,10 @@
},
"platforms": {
"description": "Target platforms this permission applies. By default all platforms are affected by this permission.",
"default": [
"linux",
"macOS",
"windows",
"android",
"iOS"
"type": [
"array",
"null"
],
"type": "array",
"items": {
"$ref": "#/definitions/Target"
}
@@ -9,7 +9,7 @@
"preview": "vite preview"
},
"devDependencies": {
"@tauri-apps/cli": "2.0.0-beta.7",
"@tauri-apps/cli": "2.0.0-beta.9",
"typescript": "^5.3.3",
"vite": "^5.0.12"
},
+1 -1
View File
@@ -23,6 +23,6 @@
"LICENSE"
],
"dependencies": {
"@tauri-apps/api": "2.0.0-beta.4"
"@tauri-apps/api": "2.0.0-beta.6"
}
}
@@ -139,14 +139,10 @@
},
"platforms": {
"description": "Target platforms this permission applies. By default all platforms are affected by this permission.",
"default": [
"linux",
"macOS",
"windows",
"android",
"iOS"
"type": [
"array",
"null"
],
"type": "array",
"items": {
"$ref": "#/definitions/Target"
}
+1 -1
View File
@@ -24,6 +24,6 @@
"LICENSE"
],
"dependencies": {
"@tauri-apps/api": "2.0.0-beta.4"
"@tauri-apps/api": "2.0.0-beta.6"
}
}
@@ -139,14 +139,10 @@
},
"platforms": {
"description": "Target platforms this permission applies. By default all platforms are affected by this permission.",
"default": [
"linux",
"macOS",
"windows",
"android",
"iOS"
"type": [
"array",
"null"
],
"type": "array",
"items": {
"$ref": "#/definitions/Target"
}
File diff suppressed because one or more lines are too long