#[cfg(not(any(target_os = "ios", target_os = "android")))]
use arboard::Clipboard;
use crate::types::bus::ClipboardResult;
use crate::types::ids::Ticket;
pub struct NativeClipboard {
#[cfg(not(any(target_os = "ios", target_os = "android")))]
inner: Option<Clipboard>,
}
impl NativeClipboard {
pub fn new() -> Self {
#[cfg(any(target_os = "ios", target_os = "android"))]
{
return Self {};
}
#[cfg(not(any(target_os = "ios", target_os = "android")))]
Self {
inner: Clipboard::new()
.map_err(|e| {
log::warn!("clipboard open failed: {e}");
e
})
.ok(),
}
}
pub fn write(&mut self, text: &str) {
#[cfg(any(target_os = "ios", target_os = "android"))]
{
let _ = text;
return;
}
#[cfg(not(any(target_os = "ios", target_os = "android")))]
{
let Some(cb) = self.inner.as_mut() else {
return;
};
if let Err(e) = cb.set_text(text.to_owned()) {
log::warn!("clipboard write failed: {e}");
}
}
}
pub fn write_image(&mut self, img: &crate::types::bus::ClipboardImage) {
#[cfg(any(target_os = "ios", target_os = "android"))]
{
let _ = img;
return;
}
#[cfg(not(any(target_os = "ios", target_os = "android")))]
{
let Some(cb) = self.inner.as_mut() else {
return;
};
let data = arboard::ImageData {
width: img.width as usize,
height: img.height as usize,
bytes: std::borrow::Cow::Borrowed(&img.rgba),
};
if let Err(e) = cb.set_image(data) {
log::warn!("clipboard image write failed: {e}");
}
}
}
pub fn read_image(&mut self) -> Option<crate::types::bus::ClipboardImage> {
#[cfg(any(target_os = "ios", target_os = "android"))]
{
return None;
}
#[cfg(not(any(target_os = "ios", target_os = "android")))]
{
let img = self.inner.as_mut()?.get_image().ok()?;
Some(crate::types::bus::ClipboardImage {
width: img.width as u32,
height: img.height as u32,
rgba: img.bytes.into_owned().into(),
})
}
}
pub fn read(&mut self, ticket: Ticket) -> ClipboardResult {
#[cfg(any(target_os = "ios", target_os = "android"))]
{
return ClipboardResult { ticket, text: None };
}
#[cfg(not(any(target_os = "ios", target_os = "android")))]
{
let text = self.inner.as_mut().and_then(|cb| match cb.get_text() {
Ok(t) if t.is_empty() => None,
Ok(t) => Some(t),
Err(e) => {
log::warn!("clipboard read failed: {e}");
None
}
});
ClipboardResult { ticket, text }
}
}
}
impl Default for NativeClipboard {
fn default() -> Self {
Self::new()
}
}