2021-03-10 01:59:02 +01:00
|
|
|
//! Access the clipboard.
|
2024-02-13 03:14:08 +01:00
|
|
|
use crate::core::clipboard::Kind;
|
2024-06-14 01:47:39 +02:00
|
|
|
use crate::futures::futures::channel::oneshot;
|
2024-07-05 01:13:28 +02:00
|
|
|
use crate::task::{self, Task};
|
2022-01-28 21:45:30 +07:00
|
|
|
|
2024-06-14 01:47:39 +02:00
|
|
|
/// A clipboard action to be performed by some [`Task`].
|
2021-09-13 11:20:54 +07:00
|
|
|
///
|
2024-06-14 01:47:39 +02:00
|
|
|
/// [`Task`]: crate::Task
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub enum Action {
|
2021-09-13 11:20:54 +07:00
|
|
|
/// Read the clipboard and produce `T` with the result.
|
2024-06-14 01:47:39 +02:00
|
|
|
Read {
|
|
|
|
|
/// The clipboard target.
|
|
|
|
|
target: Kind,
|
|
|
|
|
/// The channel to send the read contents.
|
|
|
|
|
channel: oneshot::Sender<Option<String>>,
|
|
|
|
|
},
|
2021-09-13 11:20:54 +07:00
|
|
|
|
|
|
|
|
/// Write the given contents to the clipboard.
|
2024-06-14 01:47:39 +02:00
|
|
|
Write {
|
|
|
|
|
/// The clipboard target.
|
|
|
|
|
target: Kind,
|
|
|
|
|
/// The contents to be written.
|
|
|
|
|
contents: String,
|
|
|
|
|
},
|
2021-09-13 11:20:54 +07:00
|
|
|
}
|
2023-03-05 06:35:20 +01:00
|
|
|
|
|
|
|
|
/// Read the current contents of the clipboard.
|
2024-06-14 01:47:39 +02:00
|
|
|
pub fn read() -> Task<Option<String>> {
|
2024-07-05 01:13:28 +02:00
|
|
|
task::oneshot(|channel| {
|
2024-06-14 01:47:39 +02:00
|
|
|
crate::Action::Clipboard(Action::Read {
|
|
|
|
|
target: Kind::Standard,
|
|
|
|
|
channel,
|
|
|
|
|
})
|
|
|
|
|
})
|
2023-03-05 06:35:20 +01:00
|
|
|
}
|
|
|
|
|
|
2024-02-13 03:14:08 +01:00
|
|
|
/// Read the current contents of the primary clipboard.
|
2024-06-14 01:47:39 +02:00
|
|
|
pub fn read_primary() -> Task<Option<String>> {
|
2024-07-05 01:13:28 +02:00
|
|
|
task::oneshot(|channel| {
|
2024-06-14 01:47:39 +02:00
|
|
|
crate::Action::Clipboard(Action::Read {
|
|
|
|
|
target: Kind::Primary,
|
|
|
|
|
channel,
|
|
|
|
|
})
|
|
|
|
|
})
|
2024-02-13 03:14:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Write the given contents to the clipboard.
|
2024-06-14 01:47:39 +02:00
|
|
|
pub fn write<T>(contents: String) -> Task<T> {
|
2024-07-05 01:13:28 +02:00
|
|
|
task::effect(crate::Action::Clipboard(Action::Write {
|
2024-06-14 01:47:39 +02:00
|
|
|
target: Kind::Standard,
|
2024-02-13 03:14:08 +01:00
|
|
|
contents,
|
2024-06-14 01:47:39 +02:00
|
|
|
}))
|
2024-02-07 17:25:40 +01:00
|
|
|
}
|
|
|
|
|
|
2024-02-13 03:14:08 +01:00
|
|
|
/// Write the given contents to the primary clipboard.
|
2024-06-14 01:47:39 +02:00
|
|
|
pub fn write_primary<Message>(contents: String) -> Task<Message> {
|
2024-07-05 01:13:28 +02:00
|
|
|
task::effect(crate::Action::Clipboard(Action::Write {
|
2024-06-14 01:47:39 +02:00
|
|
|
target: Kind::Primary,
|
2024-02-13 03:14:08 +01:00
|
|
|
contents,
|
2024-06-14 01:47:39 +02:00
|
|
|
}))
|
2024-02-07 17:25:40 +01:00
|
|
|
}
|