Start on copy-paste

This commit is contained in:
Antonio Scandurra
2021-04-13 14:58:10 +02:00
parent 7469240a2e
commit f4c1ffc329
6 changed files with 87 additions and 9 deletions
+4
View File
@@ -1215,6 +1215,10 @@ impl MutableAppContext {
pub fn copy(&self, text: &str) {
self.platform.copy(text);
}
pub fn paste(&self) -> Option<String> {
self.platform.paste()
}
}
impl ReadModel for MutableAppContext {
+14
View File
@@ -26,6 +26,7 @@ use std::{
path::PathBuf,
ptr,
rc::Rc,
slice,
sync::Arc,
};
@@ -299,6 +300,19 @@ impl platform::Platform for MacPlatform {
}
}
fn paste(&self) -> Option<String> {
unsafe {
let pasteboard = NSPasteboard::generalPasteboard(nil);
let data = pasteboard.dataForType(NSPasteboardTypeString);
if data == nil {
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()))
}
}
}
fn set_menus(&self, menus: Vec<Menu>) {
unsafe {
let app: id = msg_send![APP_CLASS, sharedApplication];
+1
View File
@@ -43,6 +43,7 @@ pub trait Platform {
fn prompt_for_paths(&self, options: PathPromptOptions) -> Option<Vec<PathBuf>>;
fn quit(&self);
fn copy(&self, text: &str);
fn paste(&self) -> Option<String>;
fn set_menus(&self, menus: Vec<Menu>);
}
+4
View File
@@ -73,6 +73,10 @@ impl super::Platform for Platform {
}
fn copy(&self, _: &str) {}
fn paste(&self) -> Option<String> {
None
}
}
impl Window {