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 ReadPrimary {
33 channel: oneshot::Sender<Result<String, Error>>,
35 },
36
37 WritePrimary {
39 text: String,
41
42 channel: oneshot::Sender<Result<(), Error>>,
44 },
45}
46
47pub fn read_primary() -> Task<Result<Arc<String>, Error>> {
57 task::oneshot(|channel| crate::Action::Clipboard(Action::ReadPrimary { channel }))
58 .map(|result| result.map(Arc::new))
59}
60
61pub fn write_primary(text: impl Into<String>) -> Task<Result<(), Error>> {
66 let text = text.into();
67
68 task::oneshot(|channel| crate::Action::Clipboard(Action::WritePrimary { text, channel }))
69}
70
71pub fn read(kind: Kind) -> Task<Result<Arc<Content>, Error>> {
73 task::oneshot(|channel| crate::Action::Clipboard(Action::Read { kind, channel }))
74 .map(|result| result.map(Arc::new))
75}
76
77pub fn read_text() -> Task<Result<Arc<String>, Error>> {
79 task::oneshot(|channel| {
80 crate::Action::Clipboard(Action::Read {
81 kind: Kind::Text,
82 channel,
83 })
84 })
85 .map(|result| {
86 let Ok(Content::Text(text)) = result else {
87 return Err(Error::ContentNotAvailable);
88 };
89
90 Ok(Arc::new(text))
91 })
92}
93
94pub fn read_html() -> Task<Result<Arc<String>, Error>> {
96 task::oneshot(|channel| {
97 crate::Action::Clipboard(Action::Read {
98 kind: Kind::Html,
99 channel,
100 })
101 })
102 .map(|result| {
103 let Ok(Content::Html(html)) = result else {
104 return Err(Error::ContentNotAvailable);
105 };
106
107 Ok(Arc::new(html))
108 })
109}
110
111pub fn read_files() -> Task<Result<Arc<[PathBuf]>, Error>> {
113 task::oneshot(|channel| {
114 crate::Action::Clipboard(Action::Read {
115 kind: Kind::Files,
116 channel,
117 })
118 })
119 .map(|result| {
120 let Ok(Content::Files(files)) = result else {
121 return Err(Error::ContentNotAvailable);
122 };
123
124 Ok(Arc::from(files))
125 })
126}
127
128#[cfg(feature = "image")]
130pub fn read_image() -> Task<Result<crate::core::clipboard::Image, Error>> {
131 task::oneshot(|channel| {
132 crate::Action::Clipboard(Action::Read {
133 kind: Kind::Image,
134 channel,
135 })
136 })
137 .map(|result| {
138 let Ok(Content::Image(image)) = result else {
139 return Err(Error::ContentNotAvailable);
140 };
141
142 Ok(image)
143 })
144}
145
146pub fn write(content: impl Into<Content>) -> Task<Result<(), Error>> {
148 let content = content.into();
149
150 task::oneshot(|channel| crate::Action::Clipboard(Action::Write { content, channel }))
151}