iced_runtime/
clipboard.rs

1//! Access the clipboard.
2use window_clipboard::mime::{AllowedMimeTypes, AsMimeTypes};
3
4use crate::core::clipboard::Kind;
5use crate::futures::futures::channel::oneshot;
6use crate::task::{self, Task};
7
8/// A clipboard action to be performed by some [`Task`].
9///
10/// [`Task`]: crate::Task
11pub enum Action {
12    /// Read the clipboard and produce `String` with the result.
13    Read {
14        /// The clipboard target.
15        target: Kind,
16        /// The channel to send the read contents.
17        channel: oneshot::Sender<Option<String>>,
18    },
19
20    /// Write the given contents to the clipboard.
21    Write {
22        /// The clipboard target.
23        target: Kind,
24        /// The contents to be written.
25        contents: String,
26    },
27
28    /// Write the given contents to the clipboard.
29    WriteData(Box<dyn AsMimeTypes + Send + Sync + 'static>, Kind),
30
31    #[allow(clippy::type_complexity)]
32    /// Read the clipboard and produce `T` with the result.
33    ReadData(
34        Vec<String>,
35        oneshot::Sender<Option<(Vec<u8>, String)>>,
36        // Box<dyn Fn(Option<(Vec<u8>, String)>) -> T + Send + 'static>,
37        Kind,
38    ),
39}
40
41impl std::fmt::Debug for Action {
42    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43        match self {
44            Self::Read { channel: _, target } => {
45                write!(f, "Action::Read{target:?}")
46            }
47            Self::Write {
48                contents: _,
49                target,
50            } => {
51                write!(f, "Action::Write({target:?})")
52            }
53            Self::WriteData(_, target) => {
54                write!(f, "Action::WriteData({target:?})")
55            }
56            Self::ReadData(_, _, target) => {
57                write!(f, "Action::ReadData({target:?})")
58            }
59        }
60    }
61}
62
63/// Read the current contents of the clipboard.
64pub fn read() -> Task<Option<String>> {
65    task::oneshot(|channel| {
66        crate::Action::Clipboard(Action::Read {
67            target: Kind::Standard,
68            channel,
69        })
70    })
71}
72
73/// Read the current contents of the primary clipboard.
74pub fn read_primary() -> Task<Option<String>> {
75    task::oneshot(|channel| {
76        crate::Action::Clipboard(Action::Read {
77            target: Kind::Primary,
78            channel,
79        })
80    })
81}
82
83/// Write the given contents to the clipboard.
84pub fn write<T>(contents: String) -> Task<T> {
85    task::effect(crate::Action::Clipboard(Action::Write {
86        target: Kind::Standard,
87        contents,
88    }))
89}
90
91/// Write the given contents to the primary clipboard.
92pub fn write_primary<Message>(contents: String) -> Task<Message> {
93    task::effect(crate::Action::Clipboard(Action::Write {
94        target: Kind::Primary,
95        contents,
96    }))
97}
98/// Read the current contents of the clipboard.
99pub fn read_data<T: AllowedMimeTypes>() -> Task<Option<T>> {
100    task::oneshot(|tx| {
101        crate::Action::Clipboard(Action::ReadData(
102            T::allowed().into(),
103            tx,
104            Kind::Standard,
105        ))
106    })
107    .map(|d| d.and_then(|d| T::try_from(d).ok()))
108}
109
110/// Write the given contents to the clipboard.
111pub fn write_data<Message>(
112    contents: impl AsMimeTypes + std::marker::Sync + std::marker::Send + 'static,
113) -> Task<Message> {
114    task::effect(crate::Action::Clipboard(Action::WriteData(
115        Box::new(contents),
116        Kind::Standard,
117    )))
118}
119
120/// Read from the primary clipboard
121pub fn read_primary_data<T: AllowedMimeTypes>() -> Task<Option<T>> {
122    task::oneshot(|tx| {
123        crate::Action::Clipboard(Action::ReadData(
124            T::allowed().into(),
125            tx,
126            // Box::new(move |d| f(d.and_then(|d| T::try_from(d).ok()))),
127            Kind::Primary,
128        ))
129    })
130    .map(|d| d.and_then(|d| T::try_from(d).ok()))
131}
132
133/// Write the given contents to the clipboard.
134pub fn write_primary_data<Message>(
135    contents: impl AsMimeTypes + std::marker::Sync + std::marker::Send + 'static,
136) -> Task<Message> {
137    task::effect(crate::Action::Clipboard(Action::WriteData(
138        Box::new(contents),
139        Kind::Primary,
140    )))
141}