From a25b1af45690bdd8e1cbb20ee3a5b1c4342de455 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 14 Jun 2024 01:47:39 +0200 Subject: Replace `Command` with a new `Task` API with chain support --- runtime/src/clipboard.rs | 98 ++++++++++++++++++++---------------------------- 1 file changed, 40 insertions(+), 58 deletions(-) (limited to 'runtime/src/clipboard.rs') diff --git a/runtime/src/clipboard.rs b/runtime/src/clipboard.rs index dd47c47d..19950d01 100644 --- a/runtime/src/clipboard.rs +++ b/runtime/src/clipboard.rs @@ -1,80 +1,62 @@ //! Access the clipboard. -use crate::command::{self, Command}; use crate::core::clipboard::Kind; -use crate::futures::MaybeSend; +use crate::futures::futures::channel::oneshot; +use crate::Task; -use std::fmt; - -/// A clipboard action to be performed by some [`Command`]. +/// A clipboard action to be performed by some [`Task`]. /// -/// [`Command`]: crate::Command -pub enum Action { +/// [`Task`]: crate::Task +#[derive(Debug)] +pub enum Action { /// Read the clipboard and produce `T` with the result. - Read(Box) -> T>, Kind), + Read { + /// The clipboard target. + target: Kind, + /// The channel to send the read contents. + channel: oneshot::Sender>, + }, /// Write the given contents to the clipboard. - Write(String, Kind), -} - -impl Action { - /// Maps the output of a clipboard [`Action`] using the provided closure. - pub fn map( - self, - f: impl Fn(T) -> A + 'static + MaybeSend + Sync, - ) -> Action - where - T: 'static, - { - match self { - Self::Read(o, target) => { - Action::Read(Box::new(move |s| f(o(s))), target) - } - Self::Write(content, target) => Action::Write(content, target), - } - } -} - -impl fmt::Debug for Action { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Self::Read(_, target) => write!(f, "Action::Read{target:?}"), - Self::Write(_, target) => write!(f, "Action::Write({target:?})"), - } - } + Write { + /// The clipboard target. + target: Kind, + /// The contents to be written. + contents: String, + }, } /// Read the current contents of the clipboard. -pub fn read( - f: impl Fn(Option) -> Message + 'static, -) -> Command { - Command::single(command::Action::Clipboard(Action::Read( - Box::new(f), - Kind::Standard, - ))) +pub fn read() -> Task> { + Task::oneshot(|channel| { + crate::Action::Clipboard(Action::Read { + target: Kind::Standard, + channel, + }) + }) } /// Read the current contents of the primary clipboard. -pub fn read_primary( - f: impl Fn(Option) -> Message + 'static, -) -> Command { - Command::single(command::Action::Clipboard(Action::Read( - Box::new(f), - Kind::Primary, - ))) +pub fn read_primary() -> Task> { + Task::oneshot(|channel| { + crate::Action::Clipboard(Action::Read { + target: Kind::Primary, + channel, + }) + }) } /// Write the given contents to the clipboard. -pub fn write(contents: String) -> Command { - Command::single(command::Action::Clipboard(Action::Write( +pub fn write(contents: String) -> Task { + Task::effect(crate::Action::Clipboard(Action::Write { + target: Kind::Standard, contents, - Kind::Standard, - ))) + })) } /// Write the given contents to the primary clipboard. -pub fn write_primary(contents: String) -> Command { - Command::single(command::Action::Clipboard(Action::Write( +pub fn write_primary(contents: String) -> Task { + Task::effect(crate::Action::Clipboard(Action::Write { + target: Kind::Primary, contents, - Kind::Primary, - ))) + })) } -- cgit