iced_runtime/
clipboard.rs1use crate::core::clipboard::{Content, Error, Kind};
3use crate::futures::futures::channel::oneshot;
4use crate::task::{self, Task};
5
6use std::path::PathBuf;
7use std::sync::Arc;
8
9#[derive(Debug)]
13pub enum Action {
14 Read {
16 kind: Kind,
18 channel: oneshot::Sender<Result<Content, Error>>,
20 },
21
22 Write {
24 content: Content,
26
27 channel: oneshot::Sender<Result<(), Error>>,
29 },
30}
31
32pub fn read(kind: Kind) -> Task<Result<Arc<Content>, Error>> {
34 task::oneshot(|channel| crate::Action::Clipboard(Action::Read { kind, channel }))
35 .map(|result| result.map(Arc::new))
36}
37
38pub fn read_text() -> Task<Result<Arc<String>, Error>> {
40 task::oneshot(|channel| {
41 crate::Action::Clipboard(Action::Read {
42 kind: Kind::Text,
43 channel,
44 })
45 })
46 .map(|result| {
47 let Ok(Content::Text(text)) = result else {
48 return Err(Error::ContentNotAvailable);
49 };
50
51 Ok(Arc::new(text))
52 })
53}
54
55pub fn read_html() -> Task<Result<Arc<String>, Error>> {
57 task::oneshot(|channel| {
58 crate::Action::Clipboard(Action::Read {
59 kind: Kind::Html,
60 channel,
61 })
62 })
63 .map(|result| {
64 let Ok(Content::Html(html)) = result else {
65 return Err(Error::ContentNotAvailable);
66 };
67
68 Ok(Arc::new(html))
69 })
70}
71
72pub fn read_files() -> Task<Result<Arc<[PathBuf]>, Error>> {
74 task::oneshot(|channel| {
75 crate::Action::Clipboard(Action::Read {
76 kind: Kind::Files,
77 channel,
78 })
79 })
80 .map(|result| {
81 let Ok(Content::Files(files)) = result else {
82 return Err(Error::ContentNotAvailable);
83 };
84
85 Ok(Arc::from(files))
86 })
87}
88
89#[cfg(feature = "image")]
91pub fn read_image() -> Task<Result<crate::core::clipboard::Image, Error>> {
92 task::oneshot(|channel| {
93 crate::Action::Clipboard(Action::Read {
94 kind: Kind::Image,
95 channel,
96 })
97 })
98 .map(|result| {
99 let Ok(Content::Image(image)) = result else {
100 return Err(Error::ContentNotAvailable);
101 };
102
103 Ok(image)
104 })
105}
106
107pub fn write(content: impl Into<Content>) -> Task<Result<(), Error>> {
109 let content = content.into();
110
111 task::oneshot(|channel| crate::Action::Clipboard(Action::Write { content, channel }))
112}