use window_clipboard::mime::{AllowedMimeTypes, AsMimeTypes};
use crate::core::clipboard::Kind;
use crate::futures::futures::channel::oneshot;
use crate::task::{self, Task};
pub enum Action {
Read {
target: Kind,
channel: oneshot::Sender<Option<String>>,
},
Write {
target: Kind,
contents: String,
},
WriteData(Box<dyn AsMimeTypes + Send + Sync + 'static>, Kind),
#[allow(clippy::type_complexity)]
ReadData(
Vec<String>,
oneshot::Sender<Option<(Vec<u8>, String)>>,
Kind,
),
}
impl std::fmt::Debug for Action {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Read { channel: _, target } => {
write!(f, "Action::Read{target:?}")
}
Self::Write {
contents: _,
target,
} => {
write!(f, "Action::Write({target:?})")
}
Self::WriteData(_, target) => {
write!(f, "Action::WriteData({target:?})")
}
Self::ReadData(_, _, target) => {
write!(f, "Action::ReadData({target:?})")
}
}
}
}
pub fn read() -> Task<Option<String>> {
task::oneshot(|channel| {
crate::Action::Clipboard(Action::Read {
target: Kind::Standard,
channel,
})
})
}
pub fn read_primary() -> Task<Option<String>> {
task::oneshot(|channel| {
crate::Action::Clipboard(Action::Read {
target: Kind::Primary,
channel,
})
})
}
pub fn write<T>(contents: String) -> Task<T> {
task::effect(crate::Action::Clipboard(Action::Write {
target: Kind::Standard,
contents,
}))
}
pub fn write_primary<Message>(contents: String) -> Task<Message> {
task::effect(crate::Action::Clipboard(Action::Write {
target: Kind::Primary,
contents,
}))
}
pub fn read_data<T: AllowedMimeTypes>() -> Task<Option<T>> {
task::oneshot(|tx| {
crate::Action::Clipboard(Action::ReadData(
T::allowed().into(),
tx,
Kind::Standard,
))
})
.map(|d| d.and_then(|d| T::try_from(d).ok()))
}
pub fn write_data<Message>(
contents: impl AsMimeTypes + std::marker::Sync + std::marker::Send + 'static,
) -> Task<Message> {
task::effect(crate::Action::Clipboard(Action::WriteData(
Box::new(contents),
Kind::Standard,
)))
}
pub fn read_primary_data<T: AllowedMimeTypes>() -> Task<Option<T>> {
task::oneshot(|tx| {
crate::Action::Clipboard(Action::ReadData(
T::allowed().into(),
tx,
Kind::Primary,
))
})
.map(|d| d.and_then(|d| T::try_from(d).ok()))
}
pub fn write_primary_data<Message>(
contents: impl AsMimeTypes + std::marker::Sync + std::marker::Send + 'static,
) -> Task<Message> {
task::effect(crate::Action::Clipboard(Action::WriteData(
Box::new(contents),
Kind::Primary,
)))
}