also need a receiver method

This commit is contained in:
Wez Furlong committed 2018-02-22 23:06:50 -08:00
1 parent 4878aef542
commit 1fdbb33010
3 files changed
+16 -15

No files matched your search

+2
View File
@@ -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<String>) -> Result<(), Error>;
fn get_clipboard(&self) -> Result<String, Error>;
fn receiver(&self) -> &Receiver<Paste>;
}
+11 -3
View File
@@ -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<Paste>,
sender: Sender<Paste>,
}
impl ClipboardImpl for NoClipboard {
fn new(_wakeup: Wakeup) -> Result<Self, Error> {
Ok(Self {})
let (sender, receiver) = channel();
Ok(Self { sender, receiver })
}
fn set_clipboard(&self, _text: Option<String>) -> Result<(), Error> {
@@ -18,4 +23,7 @@ impl ClipboardImpl for NoClipboard {
fn get_clipboard(&self) -> Result<String, Error> {
Ok("".into())
}
fn receiver(&self) -> &Receiver<Paste> {
&self.receiver
}
}
+3 -12
View File
@@ -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<String, Error> {
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<Paste> {
fn receiver(&self) -> &Receiver<Paste> {
&self.receiver
}
}