Introduce Kind in core::clipboard

This commit is contained in:
Héctor Ramón Jiménez 2024-02-13 03:14:08 +01:00
parent 4155edab8d
commit 508b3fe1f1
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
9 changed files with 97 additions and 130 deletions

View file

@ -1,5 +1,6 @@
//! Access the clipboard.
use crate::command::{self, Command};
use crate::core::clipboard::Kind;
use crate::futures::MaybeSend;
use std::fmt;
@ -9,10 +10,10 @@ use std::fmt;
/// [`Command`]: crate::Command
pub enum Action<T> {
/// Read the clipboard and produce `T` with the result.
Read(Box<dyn Fn(Option<String>) -> T>),
Read(Box<dyn Fn(Option<String>) -> T>, Kind),
/// Write the given contents to the clipboard.
Write(String),
Write(String, Kind),
}
impl<T> Action<T> {
@ -25,8 +26,10 @@ impl<T> Action<T> {
T: 'static,
{
match self {
Self::Read(o) => Action::Read(Box::new(move |s| f(o(s)))),
Self::Write(content) => Action::Write(content),
Self::Read(o, target) => {
Action::Read(Box::new(move |s| f(o(s))), target)
}
Self::Write(content, target) => Action::Write(content, target),
}
}
}
@ -34,8 +37,8 @@ impl<T> Action<T> {
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"),
Self::Read(_, target) => write!(f, "Action::Read{target:?}"),
Self::Write(_, target) => write!(f, "Action::Write({target:?})"),
}
}
}
@ -44,24 +47,34 @@ impl<T> fmt::Debug for Action<T> {
pub fn read<Message>(
f: impl Fn(Option<String>) -> Message + 'static,
) -> Command<Message> {
Command::single(command::Action::Clipboard(Action::Read(Box::new(f))))
Command::single(command::Action::Clipboard(Action::Read(
Box::new(f),
Kind::Standard,
)))
}
/// Read the current contents of the primary clipboard.
pub fn read_primary<Message>(
f: impl Fn(Option<String>) -> Message + 'static,
) -> Command<Message> {
Command::single(command::Action::Clipboard(Action::Read(
Box::new(f),
Kind::Primary,
)))
}
/// Write the given contents to the clipboard.
pub fn write<Message>(contents: String) -> Command<Message> {
Command::single(command::Action::Clipboard(Action::Write(contents)))
Command::single(command::Action::Clipboard(Action::Write(
contents,
Kind::Standard,
)))
}
/// Read the current contents of primary.
pub fn read_primary<Message>(
f: impl Fn(Option<String>) -> Message + 'static,
) -> Command<Message> {
Command::single(command::Action::ClipboardPrimary(Action::Read(Box::new(
f,
))))
}
/// Write the given contents to primary.
/// Write the given contents to the primary clipboard.
pub fn write_primary<Message>(contents: String) -> Command<Message> {
Command::single(command::Action::ClipboardPrimary(Action::Write(contents)))
Command::single(command::Action::Clipboard(Action::Write(
contents,
Kind::Primary,
)))
}

View file

@ -26,9 +26,6 @@ pub enum Action<T> {
/// Run a clipboard action.
Clipboard(clipboard::Action<T>),
/// Run a clipboard action on primary.
ClipboardPrimary(clipboard::Action<T>),
/// Run a window action.
Window(window::Action<T>),
@ -69,9 +66,6 @@ impl<T> Action<T> {
Self::Future(future) => Action::Future(Box::pin(future.map(f))),
Self::Stream(stream) => Action::Stream(Box::pin(stream.map(f))),
Self::Clipboard(action) => Action::Clipboard(action.map(f)),
Self::ClipboardPrimary(action) => {
Action::ClipboardPrimary(action.map(f))
}
Self::Window(window) => Action::Window(window.map(f)),
Self::System(system) => Action::System(system.map(f)),
Self::Widget(operation) => {
@ -94,9 +88,6 @@ impl<T> fmt::Debug for Action<T> {
Self::Clipboard(action) => {
write!(f, "Action::Clipboard({action:?})")
}
Self::ClipboardPrimary(action) => {
write!(f, "Action::ClipboardPrimary({action:?})")
}
Self::Window(action) => {
write!(f, "Action::Window({action:?})")
}