1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#![crate_name = "arboard"]
#![crate_type = "lib"]
#![crate_type = "dylib"]
#![crate_type = "rlib"]
#[cfg(windows)]
extern crate clipboard_win;
#[cfg(windows)]
extern crate image;
mod common;
pub use common::{Error, ImageData};
#[cfg(all(unix, not(any(target_os = "macos", target_os = "android", target_os = "emscripten")),))]
pub(crate) mod common_linux;
#[cfg(all(unix, not(any(target_os = "macos", target_os = "android", target_os = "emscripten")),))]
pub mod x11_clipboard;
#[cfg(all(
unix,
not(any(target_os = "macos", target_os = "android", target_os = "emscripten")),
feature = "wayland-data-control"
))]
pub mod wayland_data_control_clipboard;
#[cfg(windows)]
pub mod windows_clipboard;
#[cfg(target_os = "macos")]
pub mod osx_clipboard;
#[cfg(all(unix, not(any(target_os = "macos", target_os = "android", target_os = "emscripten")),))]
type PlatformClipboard = common_linux::LinuxClipboard;
#[cfg(windows)]
type PlatformClipboard = windows_clipboard::WindowsClipboardContext;
#[cfg(target_os = "macos")]
type PlatformClipboard = osx_clipboard::OSXClipboardContext;
pub struct Clipboard {
platform: PlatformClipboard,
}
impl Clipboard {
pub fn new() -> Result<Self, Error> {
Ok(Clipboard { platform: PlatformClipboard::new()? })
}
pub fn get_text(&mut self) -> Result<String, Error> {
self.platform.get_text()
}
pub fn set_text(&mut self, text: String) -> Result<(), Error> {
self.platform.set_text(text)
}
pub fn get_image(&mut self) -> Result<ImageData, Error> {
self.platform.get_image()
}
pub fn set_image(&mut self, image: ImageData) -> Result<(), Error> {
self.platform.set_image(image)
}
}
#[cfg(test)]
#[test]
fn all_tests() {
let _ = env_logger::builder().is_test(true).try_init();
{
let mut ctx = Clipboard::new().unwrap();
let text = "some string";
ctx.set_text(text.to_owned()).unwrap();
assert_eq!(ctx.get_text().unwrap(), text);
drop(ctx);
use std::time::Duration;
std::thread::sleep(Duration::from_millis(100));
let mut ctx = Clipboard::new().unwrap();
assert_eq!(ctx.get_text().unwrap(), text);
}
{
let mut ctx = Clipboard::new().unwrap();
let text = "Some utf8: 🤓 ∑φ(n)<ε 🐔";
ctx.set_text(text.to_owned()).unwrap();
assert_eq!(ctx.get_text().unwrap(), text);
}
{
let mut ctx = Clipboard::new().unwrap();
#[rustfmt::skip]
let bytes = [
255, 100, 100, 255,
100, 255, 100, 100,
100, 100, 255, 100,
0, 0, 0, 255,
];
let img_data = ImageData { width: 2, height: 2, bytes: bytes.as_ref().into() };
ctx.set_image(img_data.clone()).unwrap();
let got = ctx.get_image().unwrap();
assert_eq!(img_data.bytes, got.bytes);
}
}