Allow metadata to be associated with text written to clipboard

Co-Authored-By: Max Brunsfeld <max@zed.dev>
This commit is contained in:
Antonio Scandurra
2021-04-13 19:03:56 +02:00
co-authored by Max Brunsfeld
parent f4c1ffc329
commit 13514aae6c
10 changed files with 204 additions and 36 deletions
+5 -5
View File
@@ -5,7 +5,7 @@ use crate::{
platform::{self, WindowOptions},
presenter::Presenter,
util::post_inc,
AssetCache, AssetSource, FontCache, TextLayoutCache,
AssetCache, AssetSource, ClipboardItem, FontCache, TextLayoutCache,
};
use anyhow::{anyhow, Result};
use async_std::sync::Condvar;
@@ -1212,12 +1212,12 @@ impl MutableAppContext {
}
}
pub fn copy(&self, text: &str) {
self.platform.copy(text);
pub fn write_to_clipboard(&self, item: ClipboardItem) {
self.platform.write_to_clipboard(item);
}
pub fn paste(&self) -> Option<String> {
self.platform.paste()
pub fn read_from_clipboard(&self) -> Option<ClipboardItem> {
self.platform.read_from_clipboard()
}
}
+42
View File
@@ -0,0 +1,42 @@
use seahash::SeaHasher;
use serde::{Deserialize, Serialize};
use std::hash::{Hash, Hasher};
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ClipboardItem {
pub(crate) text: String,
pub(crate) metadata: Option<String>,
}
impl ClipboardItem {
pub fn new(text: String) -> Self {
Self {
text,
metadata: None,
}
}
pub fn with_metadata<T: Serialize>(mut self, metadata: T) -> Self {
self.metadata = Some(serde_json::to_string(&metadata).unwrap());
self
}
pub fn text(&self) -> &String {
&self.text
}
pub fn metadata<T>(&self) -> Option<T>
where
T: for<'a> Deserialize<'a>,
{
self.metadata
.as_ref()
.and_then(|m| serde_json::from_str(m).ok())
}
pub(crate) fn text_hash(text: &str) -> u64 {
let mut hasher = SeaHasher::new();
text.hash(&mut hasher);
hasher.finish()
}
}
+2
View File
@@ -7,6 +7,8 @@ pub use assets::*;
pub mod elements;
pub mod font_cache;
pub use font_cache::FontCache;
mod clipboard;
pub use clipboard::ClipboardItem;
pub mod fonts;
pub mod geometry;
mod presenter;
+121 -16
View File
@@ -1,5 +1,5 @@
use super::{BoolExt as _, Dispatcher, FontSystem, Window};
use crate::{executor, keymap::Keystroke, platform, Event, Menu, MenuItem};
use crate::{executor, keymap::Keystroke, platform, ClipboardItem, Event, Menu, MenuItem};
use cocoa::{
appkit::{
NSApplication, NSApplicationActivationPolicy::NSApplicationActivationPolicyRegular,
@@ -21,12 +21,13 @@ use ptr::null_mut;
use std::{
any::Any,
cell::RefCell,
convert::TryInto,
ffi::{c_void, CStr},
os::raw::c_char,
path::PathBuf,
ptr,
rc::Rc,
slice,
slice, str,
sync::Arc,
};
@@ -78,6 +79,9 @@ pub struct MacPlatform {
fonts: Arc<FontSystem>,
callbacks: RefCell<Callbacks>,
menu_item_actions: RefCell<Vec<(String, Option<Box<dyn Any>>)>>,
pasteboard: id,
text_hash_pasteboard_type: id,
metadata_pasteboard_type: id,
}
#[derive(Default)]
@@ -97,6 +101,9 @@ impl MacPlatform {
fonts: Arc::new(FontSystem::new()),
callbacks: Default::default(),
menu_item_actions: Default::default(),
pasteboard: unsafe { NSPasteboard::generalPasteboard(nil) },
text_hash_pasteboard_type: unsafe { ns_string("zed-text-hash") },
metadata_pasteboard_type: unsafe { ns_string("zed-metadata") },
}
}
@@ -177,6 +184,18 @@ impl MacPlatform {
menu_bar
}
unsafe fn read_from_pasteboard(&self, kind: id) -> Option<&[u8]> {
let data = self.pasteboard.dataForType(kind);
if data == nil {
None
} else {
Some(slice::from_raw_parts(
data.bytes() as *mut u8,
data.length() as usize,
))
}
}
}
impl platform::Platform for MacPlatform {
@@ -287,28 +306,71 @@ impl platform::Platform for MacPlatform {
}
}
fn copy(&self, text: &str) {
fn write_to_clipboard(&self, item: ClipboardItem) {
unsafe {
let data = NSData::dataWithBytes_length_(
self.pasteboard.clearContents();
let text_bytes = NSData::dataWithBytes_length_(
nil,
text.as_ptr() as *const c_void,
text.len() as u64,
item.text.as_ptr() as *const c_void,
item.text.len() as u64,
);
let pasteboard = NSPasteboard::generalPasteboard(nil);
pasteboard.clearContents();
pasteboard.setData_forType(data, NSPasteboardTypeString);
self.pasteboard
.setData_forType(text_bytes, NSPasteboardTypeString);
if let Some(metadata) = item.metadata.as_ref() {
let hash_bytes = ClipboardItem::text_hash(&item.text).to_be_bytes();
let hash_bytes = NSData::dataWithBytes_length_(
nil,
hash_bytes.as_ptr() as *const c_void,
hash_bytes.len() as u64,
);
self.pasteboard
.setData_forType(hash_bytes, self.text_hash_pasteboard_type);
let metadata_bytes = NSData::dataWithBytes_length_(
nil,
metadata.as_ptr() as *const c_void,
metadata.len() as u64,
);
self.pasteboard
.setData_forType(metadata_bytes, self.metadata_pasteboard_type);
}
}
}
fn paste(&self) -> Option<String> {
fn read_from_clipboard(&self) -> Option<ClipboardItem> {
unsafe {
let pasteboard = NSPasteboard::generalPasteboard(nil);
let data = pasteboard.dataForType(NSPasteboardTypeString);
if data == nil {
None
if let Some(text_bytes) = self.read_from_pasteboard(NSPasteboardTypeString) {
let text = String::from_utf8_lossy(&text_bytes).to_string();
let hash_bytes = self
.read_from_pasteboard(self.text_hash_pasteboard_type)
.and_then(|bytes| bytes.try_into().ok())
.map(u64::from_be_bytes);
let metadata_bytes = self
.read_from_pasteboard(self.metadata_pasteboard_type)
.and_then(|bytes| String::from_utf8(bytes.to_vec()).ok());
if let Some((hash, metadata)) = hash_bytes.zip(metadata_bytes) {
if hash == ClipboardItem::text_hash(&text) {
Some(ClipboardItem {
text,
metadata: Some(metadata),
})
} else {
Some(ClipboardItem {
text,
metadata: None,
})
}
} else {
Some(ClipboardItem {
text,
metadata: None,
})
}
} else {
let bytes = slice::from_raw_parts(data.bytes() as *mut u8, data.length() as usize);
Some(String::from_utf8_unchecked(bytes.to_vec()))
None
}
}
}
@@ -406,3 +468,46 @@ extern "C" fn handle_menu_item(this: &mut Object, _: Sel, item: id) {
unsafe fn ns_string(string: &str) -> id {
NSString::alloc(nil).init_str(string).autorelease()
}
#[cfg(test)]
mod tests {
use crate::platform::Platform;
use super::*;
#[test]
fn test_clipboard() {
let platform = build_platform();
assert_eq!(platform.read_from_clipboard(), None);
let item = ClipboardItem::new("1".to_string());
platform.write_to_clipboard(item.clone());
assert_eq!(platform.read_from_clipboard(), Some(item));
let item = ClipboardItem::new("2".to_string()).with_metadata(vec![3, 4]);
platform.write_to_clipboard(item.clone());
assert_eq!(platform.read_from_clipboard(), Some(item));
let text_from_other_app = "text from other app";
unsafe {
let bytes = NSData::dataWithBytes_length_(
nil,
text_from_other_app.as_ptr() as *const c_void,
text_from_other_app.len() as u64,
);
platform
.pasteboard
.setData_forType(bytes, NSPasteboardTypeString);
}
assert_eq!(
platform.read_from_clipboard(),
Some(ClipboardItem::new(text_from_other_app.to_string()))
);
}
fn build_platform() -> MacPlatform {
let mut platform = MacPlatform::new();
platform.pasteboard = unsafe { NSPasteboard::pasteboardWithUniqueName(nil) };
platform
}
}
+3 -3
View File
@@ -15,7 +15,7 @@ use crate::{
vector::Vector2F,
},
text_layout::Line,
Menu, Scene,
ClipboardItem, Menu, Scene,
};
use async_task::Runnable;
pub use event::Event;
@@ -42,8 +42,8 @@ pub trait Platform {
fn key_window_id(&self) -> Option<usize>;
fn prompt_for_paths(&self, options: PathPromptOptions) -> Option<Vec<PathBuf>>;
fn quit(&self);
fn copy(&self, text: &str);
fn paste(&self) -> Option<String>;
fn write_to_clipboard(&self, item: ClipboardItem);
fn read_from_clipboard(&self) -> Option<ClipboardItem>;
fn set_menus(&self, menus: Vec<Menu>);
}
+4 -2
View File
@@ -2,6 +2,8 @@ use pathfinder_geometry::vector::Vector2F;
use std::sync::Arc;
use std::{any::Any, rc::Rc};
use crate::ClipboardItem;
struct Platform {
dispatcher: Arc<dyn super::Dispatcher>,
fonts: Arc<dyn super::FontSystem>,
@@ -72,9 +74,9 @@ impl super::Platform for Platform {
None
}
fn copy(&self, _: &str) {}
fn write_to_clipboard(&self, _: ClipboardItem) {}
fn paste(&self) -> Option<String> {
fn read_from_clipboard(&self) -> Option<ClipboardItem> {
None
}
}