2021-03-10 01:59:02 +01:00
|
|
|
//! Access the clipboard.
|
2023-03-05 06:35:20 +01:00
|
|
|
use crate::command::{self, Command};
|
|
|
|
|
use crate::futures::MaybeSend;
|
2022-01-28 21:45:30 +07:00
|
|
|
|
2021-09-13 11:20:54 +07:00
|
|
|
use std::fmt;
|
2021-03-10 01:59:02 +01:00
|
|
|
|
2021-09-13 11:20:54 +07:00
|
|
|
/// A clipboard action to be performed by some [`Command`].
|
|
|
|
|
///
|
|
|
|
|
/// [`Command`]: crate::Command
|
2021-09-01 19:21:49 +07:00
|
|
|
pub enum Action<T> {
|
2021-09-13 11:20:54 +07:00
|
|
|
/// Read the clipboard and produce `T` with the result.
|
2021-09-01 19:21:49 +07:00
|
|
|
Read(Box<dyn Fn(Option<String>) -> T>),
|
2021-09-13 11:20:54 +07:00
|
|
|
|
|
|
|
|
/// Write the given contents to the clipboard.
|
2021-09-02 13:46:01 +07:00
|
|
|
Write(String),
|
2021-09-01 19:21:49 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> Action<T> {
|
2021-09-13 11:20:54 +07:00
|
|
|
/// Maps the output of a clipboard [`Action`] using the provided closure.
|
2022-01-28 21:45:30 +07:00
|
|
|
pub fn map<A>(
|
|
|
|
|
self,
|
|
|
|
|
f: impl Fn(T) -> A + 'static + MaybeSend + Sync,
|
|
|
|
|
) -> Action<A>
|
2021-09-01 19:21:49 +07:00
|
|
|
where
|
|
|
|
|
T: 'static,
|
|
|
|
|
{
|
|
|
|
|
match self {
|
|
|
|
|
Self::Read(o) => Action::Read(Box::new(move |s| f(o(s)))),
|
2021-09-02 13:46:01 +07:00
|
|
|
Self::Write(content) => Action::Write(content),
|
2021-09-01 19:21:49 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-09-13 11:20:54 +07:00
|
|
|
|
|
|
|
|
impl<T> fmt::Debug for Action<T> {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
match self {
|
|
|
|
|
Self::Read(_) => write!(f, "Action::Read"),
|
|
|
|
|
Self::Write(_) => write!(f, "Action::Write"),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-05 06:35:20 +01:00
|
|
|
|
|
|
|
|
/// Read the current contents of the clipboard.
|
|
|
|
|
pub fn read<Message>(
|
|
|
|
|
f: impl Fn(Option<String>) -> Message + 'static,
|
|
|
|
|
) -> Command<Message> {
|
|
|
|
|
Command::single(command::Action::Clipboard(Action::Read(Box::new(f))))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Write the given contents to the clipboard.
|
|
|
|
|
pub fn write<Message>(contents: String) -> Command<Message> {
|
|
|
|
|
Command::single(command::Action::Clipboard(Action::Write(contents)))
|
|
|
|
|
}
|