From ffdc278e97e7b84340c1726f74eb679d8752f396 Mon Sep 17 00:00:00 2001 From: Taylor Holliday Date: Tue, 21 Feb 2023 15:22:52 -0800 Subject: [PATCH 1/6] Add copy_to_clipboard. --- src/macos/window.rs | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/macos/window.rs b/src/macos/window.rs index fd67de7..73d1c20 100644 --- a/src/macos/window.rs +++ b/src/macos/window.rs @@ -5,11 +5,11 @@ use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; use cocoa::appkit::{ - NSApp, NSApplication, NSApplicationActivationPolicyRegular, NSBackingStoreBuffered, NSView, - NSWindow, NSWindowStyleMask, + NSApp, NSApplication, NSApplicationActivationPolicyRegular, NSBackingStoreBuffered, + NSPasteboard, NSView, NSWindow, NSWindowStyleMask, }; use cocoa::base::{id, nil, NO, YES}; -use cocoa::foundation::{NSAutoreleasePool, NSPoint, NSRect, NSSize, NSString}; +use cocoa::foundation::{NSAutoreleasePool, NSData, NSPoint, NSRect, NSSize, NSString}; use core_foundation::runloop::{ CFRunLoop, CFRunLoopTimer, CFRunLoopTimerContext, __CFRunLoopTimer, kCFRunLoopDefaultMode, }; @@ -483,3 +483,22 @@ unsafe impl HasRawWindowHandle for Window { RawWindowHandle::AppKit(handle) } } + +pub enum ClipboardDataType { + String, +} + +pub fn copy_to_clipboard(data: String, dataType: ClipboardDataType) { + unsafe { + let pb = NSPasteboard::generalPasteboard(nil); + + let data = + NSData::dataWithBytes_length_(nil, data.as_ptr() as *const c_void, data.len() as u64); + + let pb_type = match dataType { + ClipboardDataType::String => cocoa::appkit::NSPasteboardTypeString, + }; + + pb.setData_forType(data, pb_type); + } +} From 06752738015e65e4d475684a9fa5b85aa87b4ba3 Mon Sep 17 00:00:00 2001 From: Taylor Holliday Date: Tue, 21 Feb 2023 15:26:22 -0800 Subject: [PATCH 2/6] Formatting --- src/macos/window.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/macos/window.rs b/src/macos/window.rs index 73d1c20..bcbe67c 100644 --- a/src/macos/window.rs +++ b/src/macos/window.rs @@ -488,14 +488,14 @@ pub enum ClipboardDataType { String, } -pub fn copy_to_clipboard(data: String, dataType: ClipboardDataType) { +pub fn copy_to_clipboard(data: String, data_type: ClipboardDataType) { unsafe { let pb = NSPasteboard::generalPasteboard(nil); let data = NSData::dataWithBytes_length_(nil, data.as_ptr() as *const c_void, data.len() as u64); - let pb_type = match dataType { + let pb_type = match data_type { ClipboardDataType::String => cocoa::appkit::NSPasteboardTypeString, }; From 775d15df3814409f94146ad22935a47c67936c57 Mon Sep 17 00:00:00 2001 From: Taylor Holliday Date: Tue, 21 Feb 2023 15:45:11 -0800 Subject: [PATCH 3/6] Simplify and use copy_to_clipboard. --- examples/open_window.rs | 15 +++++++++++++-- src/clipboard.rs | 11 +++++++++++ src/lib.rs | 2 ++ src/macos/window.rs | 18 +++++------------- 4 files changed, 31 insertions(+), 15 deletions(-) create mode 100644 src/clipboard.rs diff --git a/examples/open_window.rs b/examples/open_window.rs index 95141b9..58c0713 100644 --- a/examples/open_window.rs +++ b/examples/open_window.rs @@ -2,7 +2,8 @@ use std::time::Duration; use rtrb::{Consumer, RingBuffer}; -use baseview::{Event, EventStatus, Window, WindowHandler, WindowScalePolicy}; +use baseview::copy_to_clipboard; +use baseview::{Event, EventStatus, MouseEvent, Window, WindowHandler, WindowScalePolicy}; #[derive(Debug, Clone)] enum Message { @@ -22,7 +23,17 @@ impl WindowHandler for OpenWindowExample { fn on_event(&mut self, _window: &mut Window, event: Event) -> EventStatus { match event { - Event::Mouse(e) => println!("Mouse event: {:?}", e), + Event::Mouse(e) => { + println!("Mouse event: {:?}", e); + + #[cfg(target_os = "macos")] + match e { + MouseEvent::ButtonPressed { button, modifiers } => { + copy_to_clipboard("This is a test!".into()) + } + _ => (), + } + } Event::Keyboard(e) => println!("Keyboard event: {:?}", e), Event::Window(e) => println!("Window event: {:?}", e), } diff --git a/src/clipboard.rs b/src/clipboard.rs new file mode 100644 index 0000000..baad5f0 --- /dev/null +++ b/src/clipboard.rs @@ -0,0 +1,11 @@ +#[cfg(target_os = "macos")] +use crate::macos as platform; +#[cfg(target_os = "windows")] +use crate::win as platform; +#[cfg(target_os = "linux")] +use crate::x11 as platform; + +#[cfg(target_os = "macos")] +pub fn copy_to_clipboard(data: String) { + platform::copy_to_clipboard(data) +} diff --git a/src/lib.rs b/src/lib.rs index 46a3695..54d57dd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,7 @@ mod win; #[cfg(target_os = "linux")] mod x11; +mod clipboard; mod event; mod keyboard; mod mouse_cursor; @@ -15,6 +16,7 @@ mod window_open_options; #[cfg(feature = "opengl")] pub mod gl; +pub use clipboard::*; pub use event::*; pub use mouse_cursor::MouseCursor; pub use window::*; diff --git a/src/macos/window.rs b/src/macos/window.rs index bcbe67c..731e45e 100644 --- a/src/macos/window.rs +++ b/src/macos/window.rs @@ -9,7 +9,7 @@ use cocoa::appkit::{ NSPasteboard, NSView, NSWindow, NSWindowStyleMask, }; use cocoa::base::{id, nil, NO, YES}; -use cocoa::foundation::{NSAutoreleasePool, NSData, NSPoint, NSRect, NSSize, NSString}; +use cocoa::foundation::{NSAutoreleasePool, NSPoint, NSRect, NSSize, NSString}; use core_foundation::runloop::{ CFRunLoop, CFRunLoopTimer, CFRunLoopTimerContext, __CFRunLoopTimer, kCFRunLoopDefaultMode, }; @@ -484,21 +484,13 @@ unsafe impl HasRawWindowHandle for Window { } } -pub enum ClipboardDataType { - String, -} - -pub fn copy_to_clipboard(data: String, data_type: ClipboardDataType) { +pub fn copy_to_clipboard(string: String) { unsafe { let pb = NSPasteboard::generalPasteboard(nil); - let data = - NSData::dataWithBytes_length_(nil, data.as_ptr() as *const c_void, data.len() as u64); + let ns_str = NSString::alloc(nil).init_str(&string); - let pb_type = match data_type { - ClipboardDataType::String => cocoa::appkit::NSPasteboardTypeString, - }; - - pb.setData_forType(data, pb_type); + pb.clearContents(); + pb.setString_forType(ns_str, cocoa::appkit::NSPasteboardTypeString); } } From 9b1c3785c8eed2e7a4132b9ce5a7e04c3f4941b5 Mon Sep 17 00:00:00 2001 From: Taylor Holliday Date: Tue, 21 Feb 2023 16:29:38 -0800 Subject: [PATCH 4/6] Linux build --- examples/open_window.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/open_window.rs b/examples/open_window.rs index 58c0713..ae5a421 100644 --- a/examples/open_window.rs +++ b/examples/open_window.rs @@ -2,6 +2,7 @@ use std::time::Duration; use rtrb::{Consumer, RingBuffer}; +#[cfg(target_os = "macos")] use baseview::copy_to_clipboard; use baseview::{Event, EventStatus, MouseEvent, Window, WindowHandler, WindowScalePolicy}; From 2b70a08e1403c8e2c8e2cce2996e3c9f9f556238 Mon Sep 17 00:00:00 2001 From: Taylor Holliday Date: Tue, 21 Feb 2023 17:09:17 -0800 Subject: [PATCH 5/6] Add stubs for copy_to_clipboard. --- src/clipboard.rs | 1 - src/win/window.rs | 4 ++++ src/x11/window.rs | 4 ++++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/clipboard.rs b/src/clipboard.rs index baad5f0..05ed9e2 100644 --- a/src/clipboard.rs +++ b/src/clipboard.rs @@ -5,7 +5,6 @@ use crate::win as platform; #[cfg(target_os = "linux")] use crate::x11 as platform; -#[cfg(target_os = "macos")] pub fn copy_to_clipboard(data: String) { platform::copy_to_clipboard(data) } diff --git a/src/win/window.rs b/src/win/window.rs index 83ff6d2..f725d58 100644 --- a/src/win/window.rs +++ b/src/win/window.rs @@ -763,3 +763,7 @@ unsafe impl HasRawWindowHandle for Window<'_> { RawWindowHandle::Win32(handle) } } + +pub fn copy_to_clipboard(data: String) { + todo!() +} diff --git a/src/x11/window.rs b/src/x11/window.rs index 8056f90..d45cbbf 100644 --- a/src/x11/window.rs +++ b/src/x11/window.rs @@ -704,3 +704,7 @@ fn mouse_id(id: u8) -> MouseButton { id => MouseButton::Other(id), } } + +pub fn copy_to_clipboard(data: String) { + todo!() +} From dbda356826f96ed1d6fdb5d56efecad52cacb206 Mon Sep 17 00:00:00 2001 From: Taylor Holliday Date: Wed, 22 Feb 2023 10:14:54 -0800 Subject: [PATCH 6/6] Use &str instead of String. --- examples/open_window.rs | 2 +- src/clipboard.rs | 2 +- src/macos/window.rs | 4 ++-- src/win/window.rs | 2 +- src/x11/window.rs | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/open_window.rs b/examples/open_window.rs index ae5a421..0d285ae 100644 --- a/examples/open_window.rs +++ b/examples/open_window.rs @@ -30,7 +30,7 @@ impl WindowHandler for OpenWindowExample { #[cfg(target_os = "macos")] match e { MouseEvent::ButtonPressed { button, modifiers } => { - copy_to_clipboard("This is a test!".into()) + copy_to_clipboard(&"This is a test!") } _ => (), } diff --git a/src/clipboard.rs b/src/clipboard.rs index 05ed9e2..c4f7bc4 100644 --- a/src/clipboard.rs +++ b/src/clipboard.rs @@ -5,6 +5,6 @@ use crate::win as platform; #[cfg(target_os = "linux")] use crate::x11 as platform; -pub fn copy_to_clipboard(data: String) { +pub fn copy_to_clipboard(data: &str) { platform::copy_to_clipboard(data) } diff --git a/src/macos/window.rs b/src/macos/window.rs index 731e45e..54046dd 100644 --- a/src/macos/window.rs +++ b/src/macos/window.rs @@ -484,11 +484,11 @@ unsafe impl HasRawWindowHandle for Window { } } -pub fn copy_to_clipboard(string: String) { +pub fn copy_to_clipboard(string: &str) { unsafe { let pb = NSPasteboard::generalPasteboard(nil); - let ns_str = NSString::alloc(nil).init_str(&string); + let ns_str = NSString::alloc(nil).init_str(string); pb.clearContents(); pb.setString_forType(ns_str, cocoa::appkit::NSPasteboardTypeString); diff --git a/src/win/window.rs b/src/win/window.rs index f725d58..2af1abe 100644 --- a/src/win/window.rs +++ b/src/win/window.rs @@ -764,6 +764,6 @@ unsafe impl HasRawWindowHandle for Window<'_> { } } -pub fn copy_to_clipboard(data: String) { +pub fn copy_to_clipboard(data: &str) { todo!() } diff --git a/src/x11/window.rs b/src/x11/window.rs index d45cbbf..fcd9083 100644 --- a/src/x11/window.rs +++ b/src/x11/window.rs @@ -705,6 +705,6 @@ fn mouse_id(id: u8) -> MouseButton { } } -pub fn copy_to_clipboard(data: String) { +pub fn copy_to_clipboard(data: &str) { todo!() }