Skip to main content

iced_core/
clipboard.rs

1//! Access the clipboard.
2use std::path::PathBuf;
3use std::sync::Arc;
4
5/// A set of clipboard requests.
6#[derive(Debug, Clone)]
7pub struct Clipboard {
8    /// The read requests the runtime must fulfill.
9    pub reads: Vec<Kind>,
10    /// The content that must be written to the clipboard by the runtime,
11    /// if any.
12    pub write: Option<Content>,
13}
14
15impl Clipboard {
16    /// Creates a new empty set of [`Clipboard`] requests.
17    pub fn new() -> Self {
18        Self {
19            reads: Vec::new(),
20            write: None,
21        }
22    }
23
24    /// Merges the current [`Clipboard`] requests with others.
25    pub fn merge(&mut self, other: &mut Self) {
26        self.reads.append(&mut other.reads);
27        self.write = other.write.take().or(self.write.take());
28    }
29}
30
31impl Default for Clipboard {
32    fn default() -> Self {
33        Self::new()
34    }
35}
36
37/// A clipboard event.
38#[derive(Debug, Clone, PartialEq)]
39pub enum Event {
40    /// The clipboard was read.
41    Read(Result<Arc<Content>, Error>),
42
43    /// The clipboard was written.
44    Written(Result<(), Error>),
45}
46
47/// Some clipboard content.
48#[derive(Debug, Clone, PartialEq)]
49#[allow(missing_docs)]
50#[non_exhaustive]
51pub enum Content {
52    Text(String),
53    Html(String),
54    #[cfg(feature = "image")]
55    Image(Image),
56    Files(Vec<PathBuf>),
57}
58
59impl From<String> for Content {
60    fn from(text: String) -> Self {
61        Self::Text(text)
62    }
63}
64
65#[cfg(feature = "image")]
66impl From<Image> for Content {
67    fn from(image: Image) -> Self {
68        Self::Image(image)
69    }
70}
71
72impl From<Vec<PathBuf>> for Content {
73    fn from(files: Vec<PathBuf>) -> Self {
74        Self::Files(files)
75    }
76}
77
78/// The kind of some clipboard [`Content`].
79#[derive(Debug, Clone, Copy, PartialEq, Eq)]
80#[allow(missing_docs)]
81#[non_exhaustive]
82pub enum Kind {
83    Text,
84    Html,
85    #[cfg(feature = "image")]
86    Image,
87    Files,
88}
89
90/// A clipboard image.
91#[cfg(feature = "image")]
92#[derive(Debug, Clone, PartialEq)]
93pub struct Image {
94    /// The pixels of the image in RGBA format.
95    pub rgba: crate::Bytes,
96
97    /// The physical [`Size`](crate::Size) of the image.
98    pub size: crate::Size<u32>,
99}
100
101/// A clipboard error.
102#[derive(Debug, Clone, PartialEq)]
103pub enum Error {
104    /// The clipboard in the current environment is either not present or could not be accessed.
105    ClipboardUnavailable,
106
107    /// The native clipboard is not accessible due to being held by another party.
108    ClipboardOccupied,
109
110    /// The clipboard contents were not available in the requested format.
111    /// This could either be due to the clipboard being empty or the clipboard contents having
112    /// an incompatible format to the requested one
113    ContentNotAvailable,
114
115    /// The image or the text that was about the be transferred to/from the clipboard could not be
116    /// converted to the appropriate format.
117    ConversionFailure,
118
119    /// Any error that doesn't fit the other error types.
120    Unknown {
121        /// A description only meant to help the developer that should not be relied on as a
122        /// means to identify an error case during runtime.
123        description: Arc<String>,
124    },
125}