tauri-sys/src/clipboard.rs
2022-11-13 21:06:06 +01:00

46 lines
1.2 KiB
Rust

use crate::Error;
/// Gets the clipboard content as plain text.
///
/// # Example
///
/// ```rust,no_run
/// use tauri_api::clipboard::read_text;
///
/// let clipboard_text = read_text().await;
/// ```
#[inline(always)]
pub async fn read_text() -> crate::Result<String> {
let js_val = inner::readText().await.map_err(Error::Other)?;
Ok(serde_wasm_bindgen::from_value(js_val)?)
}
/// Writes plain text to the clipboard.
///
/// # Example
///
/// ```rust,no_run
/// use tauri_api::clipboard::{write_text, read_text};
///
/// write_text("Tauri is awesome!").await;
/// assert_eq!(read_text().await, "Tauri is awesome!");
/// ```
///
/// @returns A promise indicating the success or failure of the operation.
#[inline(always)]
pub async fn write_text(text: &str) -> crate::Result<()> {
inner::writeText(text).await.map_err(Error::Other)
}
mod inner {
use wasm_bindgen::{prelude::wasm_bindgen, JsValue};
#[wasm_bindgen(module = "/dist/clipboard.js")]
extern "C" {
#[wasm_bindgen(catch)]
pub async fn readText() -> Result<JsValue, JsValue>;
#[wasm_bindgen(catch)]
pub async fn writeText(text: &str) -> Result<(), JsValue>;
}
}