use std::{fmt, path::PathBuf};
#[derive(Clone, PartialEq, Eq)]
pub struct ClipboardImage {
width: u32,
height: u32,
rgba: Vec<u8>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ClipboardImageError {
EmptySize { width: u32, height: u32 },
WrongLength { expected: usize, actual: usize },
}
impl fmt::Display for ClipboardImageError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::EmptySize { width, height } => write!(f, "a clipboard image needs a size; got {width}x{height}"),
Self::WrongLength { expected, actual } => {
write!(f, "a clipboard image needs {expected} RGBA bytes; got {actual}")
}
}
}
}
impl std::error::Error for ClipboardImageError {}
impl ClipboardImage {
pub fn from_rgba(width: u32, height: u32, rgba: Vec<u8>) -> Result<Self, ClipboardImageError> {
if width == 0 || height == 0 {
return Err(ClipboardImageError::EmptySize { width, height });
}
let expected = (width as usize) * (height as usize) * 4;
if rgba.len() != expected {
return Err(ClipboardImageError::WrongLength {
expected,
actual: rgba.len(),
});
}
Ok(Self { width, height, rgba })
}
pub fn width(&self) -> u32 {
self.width
}
pub fn height(&self) -> u32 {
self.height
}
pub fn rgba(&self) -> &[u8] {
&self.rgba
}
pub fn into_rgba(self) -> Vec<u8> {
self.rgba
}
#[cfg(feature = "clipboard")]
pub fn to_png(&self) -> Result<Vec<u8>, png::EncodingError> {
let mut out = Vec::new();
{
let mut encoder = png::Encoder::new(&mut out, self.width, self.height);
encoder.set_color(png::ColorType::Rgba);
encoder.set_depth(png::BitDepth::Eight);
let mut writer = encoder.write_header()?;
writer.write_image_data(&self.rgba)?;
}
Ok(out)
}
}
impl fmt::Debug for ClipboardImage {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ClipboardImage")
.field("width", &self.width)
.field("height", &self.height)
.field("bytes", &self.rgba.len())
.finish()
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct ClipboardKinds {
pub text: bool,
pub image: bool,
pub files: bool,
}
impl ClipboardKinds {
pub fn any(self) -> bool {
self.text || self.image || self.files
}
}
#[derive(Clone, Default, PartialEq)]
pub struct ClipboardContent {
text: Option<String>,
markdown: Option<String>,
image: Option<ClipboardImage>,
files: Vec<PathBuf>,
}
impl ClipboardContent {
pub fn empty() -> Self {
Self::default()
}
pub fn text(text: impl Into<String>) -> Self {
Self::empty().with_text(text)
}
pub fn image(image: ClipboardImage) -> Self {
Self::empty().with_image(image)
}
pub fn files(files: impl IntoIterator<Item = PathBuf>) -> Self {
Self::empty().with_files(files)
}
pub fn with_text(mut self, text: impl Into<String>) -> Self {
self.text = Some(text.into());
self
}
pub fn with_markdown(mut self, markdown: impl Into<String>) -> Self {
self.markdown = Some(markdown.into());
self
}
pub fn with_image(mut self, image: ClipboardImage) -> Self {
self.image = Some(image);
self
}
pub fn with_files(mut self, files: impl IntoIterator<Item = PathBuf>) -> Self {
self.files = files.into_iter().collect();
self
}
pub fn text_content(&self) -> Option<&str> {
self.text.as_deref().filter(|text| !text.is_empty())
}
pub fn markdown_content(&self) -> Option<&str> {
self.markdown.as_deref().filter(|markdown| !markdown.is_empty())
}
pub fn image_content(&self) -> Option<&ClipboardImage> {
self.image.as_ref()
}
pub fn file_paths(&self) -> &[PathBuf] {
&self.files
}
pub fn kinds(&self) -> ClipboardKinds {
ClipboardKinds {
text: self.text_content().is_some(),
image: self.image.is_some(),
files: !self.files.is_empty(),
}
}
pub fn is_empty(&self) -> bool {
!self.kinds().any()
}
pub fn into_parts(self) -> (Option<String>, Option<ClipboardImage>, Vec<PathBuf>) {
(self.text.filter(|text| !text.is_empty()), self.image, self.files)
}
}
impl fmt::Debug for ClipboardContent {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ClipboardContent")
.field("text_chars", &self.text_content().map(|text| text.chars().count()))
.field("image", &self.image)
.field("files", &self.files.len())
.finish()
}
}