Simplify and use copy_to_clipboard.
This commit is contained in:
parent
0675273801
commit
775d15df38
|
@ -2,7 +2,8 @@ use std::time::Duration;
|
||||||
|
|
||||||
use rtrb::{Consumer, RingBuffer};
|
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)]
|
#[derive(Debug, Clone)]
|
||||||
enum Message {
|
enum Message {
|
||||||
|
@ -22,7 +23,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!".into())
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
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),
|
||||||
}
|
}
|
||||||
|
|
11
src/clipboard.rs
Normal file
11
src/clipboard.rs
Normal file
|
@ -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)
|
||||||
|
}
|
|
@ -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::*;
|
||||||
|
|
|
@ -9,7 +9,7 @@ use cocoa::appkit::{
|
||||||
NSPasteboard, NSView, NSWindow, NSWindowStyleMask,
|
NSPasteboard, NSView, NSWindow, NSWindowStyleMask,
|
||||||
};
|
};
|
||||||
use cocoa::base::{id, nil, NO, YES};
|
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::{
|
use core_foundation::runloop::{
|
||||||
CFRunLoop, CFRunLoopTimer, CFRunLoopTimerContext, __CFRunLoopTimer, kCFRunLoopDefaultMode,
|
CFRunLoop, CFRunLoopTimer, CFRunLoopTimerContext, __CFRunLoopTimer, kCFRunLoopDefaultMode,
|
||||||
};
|
};
|
||||||
|
@ -484,21 +484,13 @@ unsafe impl HasRawWindowHandle for Window {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum ClipboardDataType {
|
pub fn copy_to_clipboard(string: String) {
|
||||||
String,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn copy_to_clipboard(data: String, data_type: ClipboardDataType) {
|
|
||||||
unsafe {
|
unsafe {
|
||||||
let pb = NSPasteboard::generalPasteboard(nil);
|
let pb = NSPasteboard::generalPasteboard(nil);
|
||||||
|
|
||||||
let data =
|
let ns_str = NSString::alloc(nil).init_str(&string);
|
||||||
NSData::dataWithBytes_length_(nil, data.as_ptr() as *const c_void, data.len() as u64);
|
|
||||||
|
|
||||||
let pb_type = match data_type {
|
pb.clearContents();
|
||||||
ClipboardDataType::String => cocoa::appkit::NSPasteboardTypeString,
|
pb.setString_forType(ns_str, cocoa::appkit::NSPasteboardTypeString);
|
||||||
};
|
|
||||||
|
|
||||||
pb.setData_forType(data, pb_type);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue