reimpl and remove clipboard_macos

Signed-off-by: Ye Sijun <junnplus@gmail.com>
This commit is contained in:
Ye Sijun authored and Wez Furlong committed 2023-05-07 11:05:55 -07:00
1 parent 349ce3cf3c
commit c154eb2d8a
5 files changed
+54 -38

No files matched your search

Generated
+1 -32
View File
@@ -717,17 +717,6 @@ dependencies = [
"winapi",
]
[[package]]
name = "clipboard_macos"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "145a7f9e9b89453bc0a5e32d166456405d389cea5b578f57f1274b1397588a95"
dependencies = [
"objc",
"objc-foundation",
"objc_id",
]
[[package]]
name = "cocoa"
version = "0.20.2"
@@ -3453,17 +3442,6 @@ dependencies = [
"objc_exception",
]
[[package]]
name = "objc-foundation"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1add1b659e36c9607c7aab864a76c7a4c2760cd0cd2e120f3fb8b952c7e22bf9"
dependencies = [
"block",
"objc",
"objc_id",
]
[[package]]
name = "objc_exception"
version = "0.1.2"
@@ -3473,15 +3451,6 @@ dependencies = [
"cc",
]
[[package]]
name = "objc_id"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c92d4ddb4bd7b50d730c215ff871754d0da6b2178849f8a2a2ab69712d0c073b"
dependencies = [
"objc",
]
[[package]]
name = "object"
version = "0.30.3"
@@ -6407,7 +6376,6 @@ dependencies = [
"bytes",
"cgl",
"clipboard-win",
"clipboard_macos",
"cocoa",
"config",
"core-foundation 0.7.0",
@@ -6436,6 +6404,7 @@ dependencies = [
"resize",
"serde",
"shared_library",
"shlex",
"smithay-client-toolkit",
"thiserror",
"tiny-skia",
+1 -1
View File
@@ -87,8 +87,8 @@ futures-lite = "1.12"
[target.'cfg(target_os="macos")'.dependencies]
cocoa = "0.20"
objc = "0.2"
clipboard_macos = "0.1"
core-foundation = "0.7"
core-graphics = "0.19"
cgl = "0.3"
plist = "1.3"
shlex = "1.1"
+48
View File
@@ -0,0 +1,48 @@
use crate::macos::{nsstring, nsstring_to_str};
use cocoa::appkit::{NSFilenamesPboardType, NSPasteboard, NSStringPboardType};
use cocoa::base::*;
use cocoa::foundation::NSArray;
pub struct Clipboard {
pasteboard: id,
}
impl Clipboard {
pub fn new() -> Self {
let pasteboard = unsafe { NSPasteboard::generalPasteboard(nil) };
if pasteboard.is_null() {
panic!("NSPasteboard::generalPasteboard returned null");
}
Clipboard { pasteboard }
}
pub fn read(&self) -> anyhow::Result<String> {
unsafe {
let plist = self.pasteboard.propertyListForType(NSFilenamesPboardType);
if !plist.is_null() {
let mut filenames = vec![];
for i in 0..plist.count() {
filenames.push(shlex::quote(nsstring_to_str(plist.objectAtIndex(i))));
}
return Ok(filenames.join(" "));
}
let s = self.pasteboard.stringForType(NSStringPboardType);
if !s.is_null() {
let str = nsstring_to_str(s);
return Ok(str.to_string());
}
}
anyhow::bail!("pasteboard read returned empty");
}
pub fn write(&mut self, data: String) -> anyhow::Result<()> {
unsafe {
self.pasteboard.clearContents();
let success: BOOL = self
.pasteboard
.writeObjects(NSArray::arrayWithObject(nil, *nsstring(&data)));
anyhow::ensure!(success == YES, "pasteboard write returned false");
Ok(())
}
}
}
+1
View File
@@ -6,6 +6,7 @@ use objc::*;
mod app;
pub mod bitmap;
pub mod clipboard;
pub mod connection;
pub mod menu;
pub mod window;
+3 -5
View File
@@ -3,6 +3,7 @@
use super::keycodes::*;
use super::{nsstring, nsstring_to_str};
use crate::clipboard::Clipboard as ClipboardContext;
use crate::connection::ConnectionOps;
use crate::os::macos::menu::{MenuItem, RepresentedItem};
use crate::parameters::{Border, Parameters, TitleBar};
@@ -14,7 +15,6 @@ use crate::{
};
use anyhow::{anyhow, bail, ensure};
use async_trait::async_trait;
use clipboard_macos::Clipboard as ClipboardContext;
use cocoa::appkit::{
self, CGFloat, NSApplication, NSApplicationActivateIgnoringOtherApps,
NSApplicationPresentationOptions, NSBackingStoreBuffered, NSEvent, NSEventModifierFlags,
@@ -767,15 +767,13 @@ impl WindowOps for Window {
fn get_clipboard(&self, _clipboard: Clipboard) -> Future<String> {
Future::result(
ClipboardContext::new()
.and_then(|ctx| ctx.read())
.read()
.map_err(|e| anyhow!("Failed to get clipboard:{}", e)),
)
}
fn set_clipboard(&self, _clipboard: Clipboard, text: String) {
ClipboardContext::new()
.and_then(|mut ctx| ctx.write(text))
.ok();
ClipboardContext::new().write(text).ok();
}
fn toggle_fullscreen(&self) {