Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add set files util for pasteboard binding #111

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ libc = "0.2"
os_info = "3.0.1"
url = "2.1.1"
uuid = { version = "1.1", features = ["v4"], optional = true }
percent-encoding = "2.3.0"

[dev-dependencies]
eval = "0.4"
Expand Down
83 changes: 82 additions & 1 deletion src/pasteboard/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use std::path::PathBuf;
use objc::rc::{Id, Shared};
use objc::runtime::Object;
use objc::{class, msg_send, msg_send_id, sel};
use percent_encoding::{percent_encode, AsciiSet};
use url::Url;

use crate::error::Error;
Expand All @@ -26,6 +27,8 @@ use crate::foundation::{id, nil, NSArray, NSString, NSURL};
mod types;
pub use types::{PasteboardName, PasteboardType};

const ENCODE_SET: AsciiSet = percent_encoding::CONTROLS.add(b' ').add(b'-').add(b'%');

/// Represents an `NSPasteboard`, enabling you to handle copy/paste/drag and drop.
#[derive(Debug)]
pub struct Pasteboard(pub Id<Object, Shared>);
Expand Down Expand Up @@ -106,9 +109,87 @@ impl Pasteboard {
}));
}

let urls = NSArray::retain(contents).iter().map(|url| NSURL::retain(url)).collect();
let urls = NSArray::retain(contents).iter().map(NSURL::retain).collect();

Ok(urls)
}
}

pub fn get_text(&self) -> Result<String, Box<dyn std::error::Error>> {
unsafe {
let pb_type: NSString = NSString::from(PasteboardType::String);
let opt_nsstr: id = msg_send![&*self.0, stringForType: &*pb_type];

if opt_nsstr == nil {
return Err(Box::new(Error {
code: 666,
domain: "com.cacao-rs.pasteboard".to_string(),
description: "Pasteboard server returned no data.".to_string()
}));
}

Ok(NSString::retain(opt_nsstr).to_string())
}
}

/// Write a list of path to the pasteboard
pub fn set_files(&self, mut paths: Vec<PathBuf>) -> Result<(), Box<dyn std::error::Error>> {
unsafe {
let url_arr: NSArray = paths
.iter_mut()
.map(|path| {
let path = path.display().to_string();
let encoded = percent_encode(path.as_bytes(), &ENCODE_SET).to_string();
let url_str = format!("file://{}", encoded);
let url = NSURL::with_str(&url_str);
url.objc.autorelease_return()
})
.collect::<Vec<id>>()
.into();

let _: id = msg_send![&*self.0, clearContents];
let succ: bool = msg_send![&*self.0, writeObjects: &*url_arr];

if succ {
Ok(())
} else {
Err(Box::new(Error {
code: 666,
domain: "com.cacao-rs.pasteboard".to_string(),
description: "Pasteboard server set urls fail.".to_string()
}))
}
}
}
}

#[cfg(test)]
mod pasteboard_test {
use std::path::PathBuf;

use super::Pasteboard;

#[test]
fn paste_text() {
let pb = Pasteboard::unique();
let txt = "hello, world!";

pb.copy_text(txt);
let txt_got = pb.get_text().unwrap();
assert_eq!(txt, txt_got);
}

#[test]
fn paste_files() {
let pb = Pasteboard::unique();
let paths: Vec<PathBuf> = vec!["/bin/ls", "/bin/cat", "/tmp/👋- 2023 10 29.txt"]
.into_iter()
.map(|s| s.parse().unwrap())
.collect();

pb.set_files(paths.clone()).unwrap();
let urls = pb.get_file_urls().unwrap();
let got: Vec<PathBuf> = urls.into_iter().map(|u| u.pathbuf()).collect();
assert_eq!(got, paths);
}
}