#[cfg(target_arch = "wasm32")]
pub(crate) mod web;
#[cfg(windows)]
mod windows;
use std::path::PathBuf;
pub use crate::app::events::{ClipboardContent, ClipboardImage, ClipboardImageError, ClipboardKinds};
#[cfg(windows)]
pub fn copy_to_clipboard(text: impl AsRef<str>) -> bool {
windows::copy(text.as_ref())
}
#[cfg(target_arch = "wasm32")]
pub fn copy_to_clipboard(text: impl AsRef<str>) -> bool {
web::copy(text.as_ref())
}
#[cfg(not(any(windows, target_arch = "wasm32")))]
pub fn copy_to_clipboard(text: impl AsRef<str>) -> bool {
let Ok(mut clipboard) = arboard::Clipboard::new() else {
return false;
};
clipboard.set_text(text.as_ref().to_owned()).is_ok()
}
pub fn set_clipboard_text(text: impl AsRef<str>) -> bool {
copy_to_clipboard(text)
}
#[cfg(not(target_arch = "wasm32"))]
pub fn read_from_clipboard() -> Option<String> {
let mut clipboard = arboard::Clipboard::new().ok()?;
clipboard.get_text().ok()
}
#[cfg(target_arch = "wasm32")]
pub fn read_from_clipboard() -> Option<String> {
web::copied()
}
pub fn clipboard_text() -> Option<String> {
read_from_clipboard()
}
pub fn clear_clipboard() -> bool {
copy_to_clipboard("")
}
#[cfg(not(target_arch = "wasm32"))]
pub fn read_clipboard() -> Option<ClipboardContent> {
let mut clipboard = arboard::Clipboard::new().ok()?;
let mut content = ClipboardContent::empty();
if let Ok(text) = clipboard.get_text() {
content = content.with_text(text);
}
if let Ok(image) = clipboard.get_image()
&& let Ok(image) = clipboard_image(image)
{
content = content.with_image(image);
}
if let Ok(files) = clipboard.get().file_list() {
content = content.with_files(files);
}
(!content.is_empty()).then_some(content)
}
#[cfg(target_arch = "wasm32")]
pub fn read_clipboard() -> Option<ClipboardContent> {
let content = ClipboardContent::empty().with_text(web::copied()?);
(!content.is_empty()).then_some(content)
}
#[cfg(not(target_arch = "wasm32"))]
pub fn copy_image_to_clipboard(width: u32, height: u32, rgba: &[u8]) -> bool {
let Ok(image) = ClipboardImage::from_rgba(width, height, rgba.to_vec()) else {
return false;
};
let Ok(mut clipboard) = arboard::Clipboard::new() else {
return false;
};
clipboard
.set_image(arboard::ImageData {
width: image.width() as usize,
height: image.height() as usize,
bytes: image.into_rgba().into(),
})
.is_ok()
}
#[cfg(target_arch = "wasm32")]
pub fn copy_image_to_clipboard(width: u32, height: u32, rgba: &[u8]) -> bool {
web::copy_image(width, height, rgba)
}
#[cfg(not(target_arch = "wasm32"))]
pub fn copy_files_to_clipboard(paths: &[PathBuf]) -> bool {
let Ok(mut clipboard) = arboard::Clipboard::new() else {
return false;
};
clipboard.set().file_list(paths).is_ok()
}
#[cfg(target_arch = "wasm32")]
pub fn copy_files_to_clipboard(_paths: &[PathBuf]) -> bool {
false
}
#[cfg(not(target_arch = "wasm32"))]
fn clipboard_image(image: arboard::ImageData<'_>) -> Result<ClipboardImage, ClipboardImageError> {
let width = u32::try_from(image.width).unwrap_or(0);
let height = u32::try_from(image.height).unwrap_or(0);
ClipboardImage::from_rgba(width, height, image.bytes.into_owned())
}