1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
use crate::common::*;
use crate::Result;
use objc::runtime::{Class, Object};
use objc_foundation::{INSArray, INSObject, INSString};
use objc_foundation::{NSArray, NSDictionary, NSObject, NSString};
use objc_id::{Id, Owned};
use std::mem::transmute;
pub struct MacOSClipboardContext {
pasteboard: Id<Object>,
}
#[link(name = "AppKit", kind = "framework")]
extern "C" {}
impl ClipboardProvider for MacOSClipboardContext {
fn new() -> Result<MacOSClipboardContext> {
let cls = Class::get("NSPasteboard").ok_or_else(|| "Class::get(\"NSPasteboard\")")?;
let pasteboard: *mut Object = unsafe { msg_send![cls, generalPasteboard] };
if pasteboard.is_null() {
return Err("NSPasteboard#generalPasteboard returned null".into());
}
let pasteboard: Id<Object> = unsafe { Id::from_ptr(pasteboard) };
Ok(MacOSClipboardContext { pasteboard })
}
fn get_contents(&mut self) -> Result<String> {
let string_class: Id<NSObject> = {
let cls: Id<Class> = unsafe { Id::from_ptr(class("NSString")) };
unsafe { transmute(cls) }
};
let classes: Id<NSArray<NSObject, Owned>> = NSArray::from_vec(vec![string_class]);
let options: Id<NSDictionary<NSObject, NSObject>> = NSDictionary::new();
let string_array: Id<NSArray<NSString>> = unsafe {
let obj: *mut NSArray<NSString> =
msg_send![self.pasteboard, readObjectsForClasses:&*classes options:&*options];
if obj.is_null() {
return Err("pasteboard#readObjectsForClasses:options: returned null".into());
}
Id::from_ptr(obj)
};
if string_array.count() == 0 {
Err("pasteboard#readObjectsForClasses:options: returned empty".into())
} else {
Ok(string_array[0].as_str().to_owned())
}
}
fn set_contents(&mut self, data: String) -> Result<()> {
let string_array = NSArray::from_vec(vec![NSString::from_str(&data)]);
let _: usize = unsafe { msg_send![self.pasteboard, clearContents] };
let success: bool = unsafe { msg_send![self.pasteboard, writeObjects: string_array] };
if success {
Ok(())
} else {
Err("NSPasteboard#writeObjects: returned false".into())
}
}
fn clear(&mut self) -> Result<()> {
let _: usize = unsafe { msg_send![self.pasteboard, clearContents] };
Ok(())
}
}
#[inline]
pub fn class(name: &str) -> *mut Class {
unsafe { transmute(Class::get(name)) }
}