1
0
Fork 0

Merge pull request #139 from wtholliday/macos-clipboard

Macos clipboard
This commit is contained in:
Micah Johnston 2023-02-23 12:09:17 -06:00 committed by GitHub
commit c129b12ead
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 47 additions and 4 deletions

View file

@ -2,7 +2,9 @@ use std::time::Duration;
use rtrb::{Consumer, RingBuffer}; use rtrb::{Consumer, RingBuffer};
use baseview::{Event, EventStatus, Window, WindowHandler, WindowScalePolicy}; #[cfg(target_os = "macos")]
use baseview::copy_to_clipboard;
use baseview::{Event, EventStatus, MouseEvent, Window, WindowHandler, WindowScalePolicy};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
enum Message { enum Message {
@ -22,7 +24,17 @@ impl WindowHandler for OpenWindowExample {
fn on_event(&mut self, _window: &mut Window, event: Event) -> EventStatus { fn on_event(&mut self, _window: &mut Window, event: Event) -> EventStatus {
match event { 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!")
}
_ => (),
}
}
Event::Keyboard(e) => println!("Keyboard event: {:?}", e), Event::Keyboard(e) => println!("Keyboard event: {:?}", e),
Event::Window(e) => println!("Window event: {:?}", e), Event::Window(e) => println!("Window event: {:?}", e),
} }

10
src/clipboard.rs Normal file
View file

@ -0,0 +1,10 @@
#[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;
pub fn copy_to_clipboard(data: &str) {
platform::copy_to_clipboard(data)
}

View file

@ -5,6 +5,7 @@ mod win;
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
mod x11; mod x11;
mod clipboard;
mod event; mod event;
mod keyboard; mod keyboard;
mod mouse_cursor; mod mouse_cursor;
@ -15,6 +16,7 @@ mod window_open_options;
#[cfg(feature = "opengl")] #[cfg(feature = "opengl")]
pub mod gl; pub mod gl;
pub use clipboard::*;
pub use event::*; pub use event::*;
pub use mouse_cursor::MouseCursor; pub use mouse_cursor::MouseCursor;
pub use window::*; pub use window::*;

View file

@ -5,8 +5,8 @@ use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc; use std::sync::Arc;
use cocoa::appkit::{ use cocoa::appkit::{
NSApp, NSApplication, NSApplicationActivationPolicyRegular, NSBackingStoreBuffered, NSView, NSApp, NSApplication, NSApplicationActivationPolicyRegular, NSBackingStoreBuffered,
NSWindow, NSWindowStyleMask, NSPasteboard, NSView, NSWindow, NSWindowStyleMask,
}; };
use cocoa::base::{id, nil, NO, YES}; use cocoa::base::{id, nil, NO, YES};
use cocoa::foundation::{NSAutoreleasePool, NSPoint, NSRect, NSSize, NSString}; use cocoa::foundation::{NSAutoreleasePool, NSPoint, NSRect, NSSize, NSString};
@ -483,3 +483,14 @@ unsafe impl HasRawWindowHandle for Window {
RawWindowHandle::AppKit(handle) RawWindowHandle::AppKit(handle)
} }
} }
pub fn copy_to_clipboard(string: &str) {
unsafe {
let pb = NSPasteboard::generalPasteboard(nil);
let ns_str = NSString::alloc(nil).init_str(string);
pb.clearContents();
pb.setString_forType(ns_str, cocoa::appkit::NSPasteboardTypeString);
}
}

View file

@ -763,3 +763,7 @@ unsafe impl HasRawWindowHandle for Window<'_> {
RawWindowHandle::Win32(handle) RawWindowHandle::Win32(handle)
} }
} }
pub fn copy_to_clipboard(data: &str) {
todo!()
}

View file

@ -704,3 +704,7 @@ fn mouse_id(id: u8) -> MouseButton {
id => MouseButton::Other(id), id => MouseButton::Other(id),
} }
} }
pub fn copy_to_clipboard(data: &str) {
todo!()
}