use std::path::{Path, PathBuf};
use std::sync::Arc;
use objc2::rc::Retained;
use objc2::runtime::{NSObject, NSObjectProtocol, ProtocolObject};
use objc2::{AllocAnyThread, DefinedClass, define_class, msg_send};
use objc2_app_kit::{
NSFilePromiseProvider, NSFilePromiseProviderDelegate, NSPasteboard, NSPasteboardItem, NSPasteboardTypeFileURL, NSPasteboardTypePNG,
NSPasteboardTypeString, NSPasteboardWriting,
};
use objc2_foundation::{NSArray, NSData, NSError, NSOperationQueue, NSString, NSURL};
pub struct Clipboard(Retained<NSPasteboard>, bool);
impl Clipboard {
pub fn general() -> Self {
Clipboard(NSPasteboard::generalPasteboard(), false)
}
pub fn private() -> Self {
Clipboard(NSPasteboard::pasteboardWithUniqueName(), true)
}
pub fn pasteboard(&self) -> &NSPasteboard {
&self.0
}
pub fn set_text(&self, text: &str) {
self.0.clearContents();
self.0.setString_forType(&NSString::from_str(text), unsafe { NSPasteboardTypeString });
}
pub fn text(&self) -> Option<String> {
self.0.stringForType(unsafe { NSPasteboardTypeString }).map(|s| s.to_string())
}
pub fn set_png(&self, png: &[u8]) {
self.0.clearContents();
self.0.setData_forType(Some(&NSData::with_bytes(png)), unsafe { NSPasteboardTypePNG });
}
pub fn png(&self) -> Option<Vec<u8>> {
self.0.dataForType(unsafe { NSPasteboardTypePNG }).map(|d| d.to_vec())
}
pub fn set_files(&self, paths: &[PathBuf]) {
self.0.clearContents();
let urls: Vec<Retained<ProtocolObject<dyn NSPasteboardWriting>>> = paths.iter().map(|p| ProtocolObject::from_retained(file_url(p))).collect();
self.0.writeObjects(&NSArray::from_retained_slice(&urls));
}
pub fn files(&self) -> Vec<PathBuf> {
files_on(&self.0)
}
pub fn clear(&self) {
self.0.clearContents();
}
pub fn set(&self, item: &DragItem) {
match item {
DragItem::Text(t) => self.set_text(t),
DragItem::Png(png) => self.set_png(png),
DragItem::File(p) => self.set_files(std::slice::from_ref(p)),
DragItem::Promise { name, write } => {
let dir = std::env::temp_dir().join(format!("ferrokit-{}", std::process::id()));
let _ = std::fs::create_dir_all(&dir);
let path = dir.join(name);
if write(&path).is_ok() {
self.set_files(&[path]);
}
}
}
}
}
impl Drop for Clipboard {
fn drop(&mut self) {
if self.1 {
let () = unsafe { msg_send![&*self.0, releaseGlobally] };
}
}
}
fn file_url(path: &Path) -> Retained<NSURL> {
NSURL::fileURLWithPath(&NSString::from_str(&path.to_string_lossy()))
}
pub(crate) fn files_on(pasteboard: &NSPasteboard) -> Vec<PathBuf> {
let Some(items) = pasteboard.pasteboardItems() else { return Vec::new() };
items
.iter()
.filter_map(|i| i.stringForType(unsafe { NSPasteboardTypeFileURL }))
.filter_map(|s| NSURL::URLWithString(&s)?.path())
.map(|p| PathBuf::from(p.to_string()))
.collect()
}
pub type WriteFile = Arc<dyn Fn(&Path) -> std::io::Result<()> + Send + Sync>;
#[derive(Clone)]
pub enum DragItem {
Text(String),
Png(Vec<u8>),
File(PathBuf),
Promise {
name: String,
write: WriteFile,
},
}
impl DragItem {
pub fn promise(name: &str, write: impl Fn(&Path) -> std::io::Result<()> + Send + Sync + 'static) -> Self {
DragItem::Promise { name: name.into(), write: Arc::new(write) }
}
pub(crate) fn writer(&self) -> Retained<ProtocolObject<dyn NSPasteboardWriting>> {
match self {
DragItem::Text(t) => ProtocolObject::from_retained(NSString::from_str(t)),
DragItem::File(p) => ProtocolObject::from_retained(file_url(p)),
DragItem::Png(png) => {
let item = NSPasteboardItem::new();
item.setData_forType(&NSData::with_bytes(png), unsafe { NSPasteboardTypePNG });
ProtocolObject::from_retained(item)
}
DragItem::Promise { name, write } => {
let delegate = Promiser::alloc().set_ivars(Promise { name: name.clone(), write: write.clone() });
let delegate: Retained<Promiser> = unsafe { msg_send![super(delegate), init] };
let kind = uti_for(name);
let provider = unsafe {
let p = NSFilePromiseProvider::initWithFileType_delegate(NSFilePromiseProvider::alloc(), &NSString::from_str(&kind), ProtocolObject::from_ref(&*delegate));
p.setUserInfo(Some(&delegate));
p
};
ProtocolObject::from_retained(provider)
}
}
}
}
fn uti_for(name: &str) -> String {
let ext = Path::new(name).extension().map(|e| e.to_string_lossy().into_owned()).unwrap_or_default();
objc2_uniform_type_identifiers::UTType::typeWithFilenameExtension(&NSString::from_str(&ext))
.map(|t| t.identifier().to_string())
.unwrap_or_else(|| "public.data".into())
}
pub(crate) struct Promise {
name: String,
write: WriteFile,
}
define_class!(
#[unsafe(super(NSObject))]
#[name = "FerrokitFilePromise"]
#[ivars = Promise]
pub(crate) struct Promiser;
unsafe impl NSObjectProtocol for Promiser {}
unsafe impl NSFilePromiseProviderDelegate for Promiser {
#[unsafe(method_id(filePromiseProvider:fileNameForType:))]
fn file_name(&self, _provider: &NSFilePromiseProvider, _kind: &NSString) -> Retained<NSString> {
NSString::from_str(&self.ivars().name)
}
#[unsafe(method(filePromiseProvider:writePromiseToURL:completionHandler:))]
fn write_promise(&self, _provider: &NSFilePromiseProvider, url: &NSURL, done: &block2::DynBlock<dyn Fn(*mut NSError)>) {
let path = url.path().map(|p| PathBuf::from(p.to_string()));
let result = path.ok_or_else(|| std::io::Error::other("not a file URL")).and_then(|p| (self.ivars().write)(&p));
match result {
Ok(()) => done.call((std::ptr::null_mut(),)),
Err(e) => {
let error = NSError::new(-1, &NSString::from_str(&format!("ferrokit: {e}")));
done.call((Retained::as_ptr(&error) as *mut NSError,));
}
}
}
#[unsafe(method_id(operationQueueForFilePromiseProvider:))]
fn queue(&self, _provider: &NSFilePromiseProvider) -> Retained<NSOperationQueue> {
NSOperationQueue::new()
}
}
);
pub(crate) fn fulfil(item: &DragItem, dir: &Path) -> std::io::Result<PathBuf> {
match item {
DragItem::Promise { name, write } => {
let path = dir.join(name);
write(&path)?;
Ok(path)
}
_ => Err(std::io::Error::other("not a promised file")),
}
}