iced_runtime/
clipboard.rs

1//! Access the clipboard.
2use crate::core::clipboard::Kind;
3use crate::futures::futures::channel::oneshot;
4use crate::task::{self, Task};
5
6/// A clipboard action to be performed by some [`Task`].
7///
8/// [`Task`]: crate::Task
9#[derive(Debug)]
10pub enum Action {
11    /// Read the clipboard and produce `T` with the result.
12    Read {
13        /// The clipboard target.
14        target: Kind,
15        /// The channel to send the read contents.
16        channel: oneshot::Sender<Option<String>>,
17    },
18
19    /// Write the given contents to the clipboard.
20    Write {
21        /// The clipboard target.
22        target: Kind,
23        /// The contents to be written.
24        contents: String,
25    },
26}
27
28/// Read the current contents of the clipboard.
29pub fn read() -> Task<Option<String>> {
30    task::oneshot(|channel| {
31        crate::Action::Clipboard(Action::Read {
32            target: Kind::Standard,
33            channel,
34        })
35    })
36}
37
38/// Read the current contents of the primary clipboard.
39pub fn read_primary() -> Task<Option<String>> {
40    task::oneshot(|channel| {
41        crate::Action::Clipboard(Action::Read {
42            target: Kind::Primary,
43            channel,
44        })
45    })
46}
47
48/// Write the given contents to the clipboard.
49pub fn write<T>(contents: String) -> Task<T> {
50    task::effect(crate::Action::Clipboard(Action::Write {
51        target: Kind::Standard,
52        contents,
53    }))
54}
55
56/// Write the given contents to the primary clipboard.
57pub fn write_primary<Message>(contents: String) -> Task<Message> {
58    task::effect(crate::Action::Clipboard(Action::Write {
59        target: Kind::Primary,
60        contents,
61    }))
62}