window_clipboard/
lib.rs

1pub use mime;
2
3#[cfg(all(
4    unix,
5    not(any(
6        target_os = "macos",
7        target_os = "ios",
8        target_os = "android",
9        target_os = "emscripten",
10        target_os = "redox"
11    ))
12))]
13#[path = "platform/linux.rs"]
14mod platform;
15
16#[cfg(target_os = "windows")]
17#[path = "platform/windows.rs"]
18mod platform;
19
20#[cfg(target_os = "macos")]
21#[path = "platform/macos.rs"]
22mod platform;
23
24#[cfg(target_os = "ios")]
25#[path = "platform/ios.rs"]
26mod platform;
27
28#[cfg(target_os = "android")]
29#[path = "platform/android.rs"]
30mod platform;
31
32#[cfg(not(any(
33    all(
34        unix,
35        not(any(
36            target_os = "macos",
37            target_os = "ios",
38            target_os = "android",
39            target_os = "emscripten",
40            target_os = "redox"
41        ))
42    ),
43    target_os = "windows",
44    target_os = "macos",
45    target_os = "ios",
46    target_os = "android"
47)))]
48#[path = "platform/dummy.rs"]
49mod platform;
50
51pub mod dnd;
52
53use mime::ClipboardStoreData;
54use raw_window_handle::HasDisplayHandle;
55use std::error::Error;
56
57pub type Clipboard = PlatformClipboard<platform::Clipboard>;
58
59pub struct PlatformClipboard<C> {
60    raw: C,
61}
62
63impl PlatformClipboard<platform::Clipboard> {
64    /// Safety: the display handle must be valid for the lifetime of `Clipboard`
65    pub unsafe fn connect<W: HasDisplayHandle + ?Sized>(
66        window: &W,
67    ) -> Result<Self, Box<dyn Error>> {
68        Ok(PlatformClipboard {
69            raw: platform::connect(window)?,
70        })
71    }
72
73    pub fn read(&self) -> Result<String, Box<dyn Error>> {
74        self.raw.read()
75    }
76
77    pub fn write(&mut self, contents: String) -> Result<(), Box<dyn Error>> {
78        self.raw.write(contents)
79    }
80}
81
82impl<C: ClipboardProvider> PlatformClipboard<C> {
83    pub fn read_primary(&self) -> Option<Result<String, Box<dyn Error>>> {
84        self.raw.read_primary()
85    }
86
87    pub fn write_primary(
88        &mut self,
89        contents: String,
90    ) -> Option<Result<(), Box<dyn Error>>> {
91        self.raw.write_primary(contents)
92    }
93
94    pub fn read_data<T: 'static>(&self) -> Option<Result<T, Box<dyn Error>>>
95    where
96        T: mime::AllowedMimeTypes,
97    {
98        self.raw.read_data()
99    }
100
101    pub fn write_data<T: Send + Sync + 'static>(
102        &mut self,
103        contents: ClipboardStoreData<T>,
104    ) -> Option<Result<(), Box<dyn Error>>>
105    where
106        T: mime::AsMimeTypes,
107    {
108        self.raw.write_data(contents)
109    }
110
111    pub fn read_primary_data<T: 'static>(
112        &self,
113    ) -> Option<Result<T, Box<dyn Error>>>
114    where
115        T: mime::AllowedMimeTypes,
116    {
117        self.raw.read_primary_data()
118    }
119
120    pub fn read_primary_raw(
121        &self,
122        allowed: Vec<String>,
123    ) -> Option<Result<(Vec<u8>, String), Box<dyn Error>>> {
124        self.raw.read_primary_raw(allowed)
125    }
126
127    pub fn read_raw(
128        &self,
129        allowed: Vec<String>,
130    ) -> Option<Result<(Vec<u8>, String), Box<dyn Error>>> {
131        self.raw.read_raw(allowed)
132    }
133
134    pub fn write_primary_data<T: Send + Sync + 'static>(
135        &mut self,
136        contents: ClipboardStoreData<T>,
137    ) -> Option<Result<(), Box<dyn Error>>>
138    where
139        T: mime::AsMimeTypes,
140    {
141        self.raw.write_primary_data(contents)
142    }
143}
144
145pub trait ClipboardProvider {
146    fn read(&self) -> Result<String, Box<dyn Error>>;
147
148    fn write(&mut self, contents: String) -> Result<(), Box<dyn Error>>;
149
150    fn read_primary(&self) -> Option<Result<String, Box<dyn Error>>> {
151        None
152    }
153
154    fn write_primary(
155        &mut self,
156        _contents: String,
157    ) -> Option<Result<(), Box<dyn Error>>> {
158        None
159    }
160
161    fn read_data<T: 'static>(&self) -> Option<Result<T, Box<dyn Error>>>
162    where
163        T: mime::AllowedMimeTypes,
164    {
165        None
166    }
167
168    fn write_data<T: Send + Sync + 'static>(
169        &mut self,
170        _contents: ClipboardStoreData<T>,
171    ) -> Option<Result<(), Box<dyn Error>>>
172    where
173        T: mime::AsMimeTypes,
174    {
175        None
176    }
177
178    fn read_primary_data<T: 'static>(&self) -> Option<Result<T, Box<dyn Error>>>
179    where
180        T: mime::AllowedMimeTypes,
181    {
182        None
183    }
184
185    fn read_primary_raw(
186        &self,
187        _allowed: Vec<String>,
188    ) -> Option<Result<(Vec<u8>, String), Box<dyn Error>>> {
189        None
190    }
191
192    fn read_raw(
193        &self,
194        _allowed: Vec<String>,
195    ) -> Option<Result<(Vec<u8>, String), Box<dyn Error>>> {
196        None
197    }
198
199    fn write_primary_data<T: Send + Sync + 'static>(
200        &mut self,
201        _contents: ClipboardStoreData<T>,
202    ) -> Option<Result<(), Box<dyn Error>>>
203    where
204        T: mime::AsMimeTypes,
205    {
206        None
207    }
208}