feat(*): distinguish between error and unset clipboard

This commit is contained in:
Danyel Bayraktar 2018-11-28 21:17:48 +01:00
parent afe036cafd
commit 638ff7fd33
4 changed files with 9 additions and 8 deletions

View file

@ -39,5 +39,5 @@ pub trait ClipboardProvider: Sized {
/// Method to set the clipboard contents as a String
fn set_contents(&mut self, String) -> Result<(), Box<Error>>;
/// Method to get clipboard contents not necessarily string
fn get_binary_contents(&mut self) -> Result<ClipboardContent, Box<Error>>;
fn get_binary_contents(&mut self) -> Result<Option<ClipboardContent>, Box<Error>>;
}

View file

@ -35,6 +35,7 @@ extern crate objc_foundation;
mod common;
pub use common::ClipboardProvider;
pub use common::ClipboardContent;
#[cfg(all(unix, not(any(target_os="macos", target_os="android"))))]
pub mod x11_clipboard;

View file

@ -29,10 +29,10 @@ impl ClipboardProvider for NopClipboardContext {
implemented on this platform.");
Ok("".to_string())
}
fn get_binary_contents(&mut self) -> Result<ClipboardContent, Box<Error>> {
fn get_binary_contents(&mut self) -> Result<Option<ClipboardContent>, Box<Error>> {
println!("Attempting to get the contents of the clipboard, which hasn't yet been \
implemented on this platform.");
Ok(ClipboardContent::__Nonexhaustive)
Ok(None)
}
fn set_contents(&mut self, _: String) -> Result<(), Box<Error>> {
println!("Attempting to set the contents of the clipboard, which hasn't yet been \

View file

@ -61,7 +61,7 @@ impl ClipboardProvider for OSXClipboardContext {
Ok(string_array[0].as_str().to_owned())
}
}
fn get_binary_contents(&mut self) -> Result<ClipboardContent, Box<Error>> {
fn get_binary_contents(&mut self) -> Result<Option<ClipboardContent>, Box<Error>> {
let string_class: Id<NSObject> = {
let cls: Id<Class> = unsafe { Id::from_ptr(class("NSString")) };
unsafe { transmute(cls) }
@ -86,12 +86,12 @@ impl ClipboardProvider for OSXClipboardContext {
Id::from_ptr(obj)
};
if contents.count() == 0 {
Err(err("pasteboard#readObjectsForClasses:options: returned empty"))
Ok(None)
} else {
let obj = &contents[0];
if obj.is_kind_of(Class::get("NSString").unwrap()) {
let s: &NSString = unsafe { transmute(obj) };
Ok(ClipboardContent::Utf8(s.as_str().to_owned()))
Ok(Some(ClipboardContent::Utf8(s.as_str().to_owned())))
} else if obj.is_kind_of(Class::get("NSImage").unwrap()) {
let tiff: &NSArray<NSObject> = unsafe { msg_send![obj, TIFFRepresentation] };
let len: usize = unsafe { msg_send![tiff, length] };
@ -99,10 +99,10 @@ impl ClipboardProvider for OSXClipboardContext {
let vec = unsafe { std::slice::from_raw_parts(bytes, len) };
// Here we copy the entire &[u8] into a new owned `Vec`
// Is there another way that doesn't copy multiple megabytes?
Ok(ClipboardContent::Tiff(vec.into()))
Ok(Some(ClipboardContent::Tiff(vec.into())))
} else if obj.is_kind_of(Class::get("NSURL").unwrap()) {
let s: &NSString = unsafe { msg_send![obj, absoluteString] };
Ok(ClipboardContent::Utf8(s.as_str().to_owned()))
Ok(Some(ClipboardContent::Utf8(s.as_str().to_owned())))
} else {
// let cls: &Class = unsafe { msg_send![obj, class] };
// println!("{}", cls.name());