From 1fdbb33010919b93a700a07e03d94e7280d5e22d Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Thu, 22 Feb 2018 23:05:16 -0800 Subject: [PATCH] also need a receiver method --- src/clipboard/mod.rs | 2 ++ src/clipboard/none.rs | 14 +++++++++++--- src/clipboard/x11.rs | 15 +++------------ 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/clipboard/mod.rs b/src/clipboard/mod.rs index 9b2ba6a14..2479aa4bf 100644 --- a/src/clipboard/mod.rs +++ b/src/clipboard/mod.rs @@ -1,4 +1,5 @@ use failure::Error; +use std::sync::mpsc::Receiver; mod none; #[cfg(target_os = "macos")] @@ -32,4 +33,5 @@ pub trait ClipboardImpl { Self: Sized; fn set_clipboard(&self, text: Option) -> Result<(), Error>; fn get_clipboard(&self) -> Result; + fn receiver(&self) -> &Receiver; } diff --git a/src/clipboard/none.rs b/src/clipboard/none.rs index a0de6bf9e..96a252e3e 100644 --- a/src/clipboard/none.rs +++ b/src/clipboard/none.rs @@ -1,14 +1,19 @@ -use clipboard::ClipboardImpl; +use clipboard::{ClipboardImpl, Paste}; use failure::Error; +use std::sync::mpsc::{channel, Receiver, Sender}; use wakeup::Wakeup; /// A no-op clipboard implementation #[allow(dead_code)] -pub struct NoClipboard {} +pub struct NoClipboard { + receiver: Receiver, + sender: Sender, +} impl ClipboardImpl for NoClipboard { fn new(_wakeup: Wakeup) -> Result { - Ok(Self {}) + let (sender, receiver) = channel(); + Ok(Self { sender, receiver }) } fn set_clipboard(&self, _text: Option) -> Result<(), Error> { @@ -18,4 +23,7 @@ impl ClipboardImpl for NoClipboard { fn get_clipboard(&self) -> Result { Ok("".into()) } + fn receiver(&self) -> &Receiver { + &self.receiver + } } diff --git a/src/clipboard/x11.rs b/src/clipboard/x11.rs index 6907d2bc7..e4680be90 100644 --- a/src/clipboard/x11.rs +++ b/src/clipboard/x11.rs @@ -353,27 +353,18 @@ impl ClipboardImpl for Clipboard { self.sender.send(ClipRequest::SetClipboard(text))?; Ok(()) } + /// Blocks until the clipboard contents have been retrieved fn get_clipboard(&self) -> Result { - self.request_clipboard()?; + self.sender.send(ClipRequest::RequestClipboard)?; match self.receiver().recv_timeout(Duration::from_secs(10)) { Ok(Paste::All(result)) => return Ok(result), Ok(Paste::Cleared) => return Ok("".into()), other @ _ => bail!("unexpected result while waiting for paste: {:?}", other), } } -} -impl Clipboard { - /// Ask the selection owner for the clipboard contents. - /// The contents will be delivered asynchronously via - /// the receiver. - pub fn request_clipboard(&self) -> Result<(), Error> { - self.sender.send(ClipRequest::RequestClipboard)?; - Ok(()) - } - - pub fn receiver(&self) -> &Receiver { + fn receiver(&self) -> &Receiver { &self.receiver } }