Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(clipboard): support readImage & writeImage #845

Merged
merged 18 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/clipboard-manager-image.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"clipboard-manager": "minor"
"clipboard-manager-js": "minor"
---

Add support for `read_image` and `write_image` to the clipboard plugin.
129 changes: 126 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions plugins/clipboard-manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
amrbashir marked this conversation as resolved.
Show resolved Hide resolved
lucasfernog marked this conversation as resolved.
Show resolved Hide resolved
52 changes: 50 additions & 2 deletions plugins/clipboard-manager/guest-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { invoke } from "@tauri-apps/api/core";

type ClipResponse = Record<"plainText", { text: string }>;

type ClipImageResponse = Record<"image", { buffer: number[] }>;

/**
* Writes plain text to the clipboard.
* @example
Expand Down Expand Up @@ -49,8 +51,54 @@ async function writeText(
* @since 2.0.0
*/
async function readText(): Promise<string> {
const kind: ClipResponse = await invoke("plugin:clipboard|read");
const kind: ClipResponse = await invoke("plugin:clipboard|read_text");
return kind.plainText.text;
}

export { writeText, readText };
/**
* 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.buffer], { type: 'image' })
* const url = URL.createObjectURL(blob)
* ```
* @since 2.0.0
*/
async function readImage(): Promise<Uint8Array> {
const kind: ClipImageResponse = await invoke("plugin:clipboard|read_image");
return Uint8Array.from(kind.image.buffer);
}

/**
* 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(buffer: Uint8Array | Array<number>): Promise<void> {
return invoke("plugin:clipboard|write", {
data: {
image: {
buffer: Array.isArray(buffer) ? buffer : Array.from(buffer),
},
},
});
}

export { writeText, readText, readImage, writeImage };
2 changes: 1 addition & 1 deletion plugins/clipboard-manager/src/api-iife.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion plugins/clipboard-manager/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,17 @@ pub(crate) async fn write<R: Runtime>(
}

#[command]
pub(crate) async fn read<R: Runtime>(
pub(crate) async fn read_text<R: Runtime>(
cijiugechu marked this conversation as resolved.
Show resolved Hide resolved
_app: AppHandle<R>,
clipboard: State<'_, Clipboard<R>>,
) -> Result<ClipboardContents> {
clipboard.read()
}

#[command]
pub(crate) async fn read_image<R: Runtime>(
_app: AppHandle<R>,
clipboard: State<'_, Clipboard<R>>,
) -> Result<ClipboardContents> {
clipboard.read_image()
}
Loading
Loading